DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

392 lines
11KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DGL_OPENGL
  17. #error OpenGL build required for Demo
  18. #endif
  19. #include "tests.hpp"
  20. #define DPF_TEST_DEMO
  21. #include "dgl/OpenGL.hpp"
  22. #include "dgl/src/pugl.cpp"
  23. #include "dgl/src/Application.cpp"
  24. #include "dgl/src/ApplicationPrivateData.cpp"
  25. #include "dgl/src/Color.cpp"
  26. #include "dgl/src/Geometry.cpp"
  27. #include "dgl/src/ImageBase.cpp"
  28. #include "dgl/src/NanoVG.cpp"
  29. #include "dgl/src/OpenGL.cpp"
  30. #include "dgl/src/Resources.cpp"
  31. #include "dgl/src/SubWidget.cpp"
  32. #include "dgl/src/SubWidgetPrivateData.cpp"
  33. #include "dgl/src/TopLevelWidget.cpp"
  34. #include "dgl/src/TopLevelWidgetPrivateData.cpp"
  35. #include "dgl/src/Widget.cpp"
  36. #include "dgl/src/WidgetPrivateData.cpp"
  37. #include "dgl/src/Window.cpp"
  38. #include "dgl/src/WindowPrivateData.cpp"
  39. #include "dgl/StandaloneWindow.hpp"
  40. #include "widgets/ExampleColorWidget.hpp"
  41. #include "widgets/ExampleImagesWidget.hpp"
  42. #include "widgets/ExampleRectanglesWidget.hpp"
  43. #include "widgets/ExampleTextWidget.hpp"
  44. #include "widgets/ExampleShapesWidget.hpp"
  45. #include "demo_res/DemoArtwork.cpp"
  46. #include "images_res/CatPics.cpp"
  47. START_NAMESPACE_DGL
  48. // --------------------------------------------------------------------------------------------------------------------
  49. // Left side tab-like widget
  50. class LeftSideWidget : public SubWidget
  51. {
  52. public:
  53. static const int kPageCount = 5;
  54. class Callback
  55. {
  56. public:
  57. virtual ~Callback() {}
  58. virtual void curPageChanged(int curPage) = 0;
  59. };
  60. LeftSideWidget(Widget* parent, Callback* const cb)
  61. : SubWidget(parent),
  62. callback(cb),
  63. curPage(0),
  64. curHover(-1)
  65. {
  66. // for text
  67. nvg.loadSharedResources();
  68. using namespace DemoArtwork;
  69. img1.loadFromMemory(ico1Data, ico1Width, ico1Height, GL_BGR);
  70. img2.loadFromMemory(ico2Data, ico2Width, ico2Height, GL_BGR);
  71. img3.loadFromMemory(ico3Data, ico3Width, ico2Height, GL_BGR);
  72. img4.loadFromMemory(ico4Data, ico4Width, ico4Height, GL_BGR);
  73. img5.loadFromMemory(ico5Data, ico5Width, ico5Height, GL_BGR);
  74. }
  75. protected:
  76. void onDisplay() override
  77. {
  78. const int iconSize = bgIcon.getWidth();
  79. glColor3f(0.027f, 0.027f, 0.027f);
  80. Rectangle<uint>(0, 0, getSize()).draw();
  81. bgIcon.setY(curPage*iconSize + curPage*3);
  82. glColor3f(0.129f, 0.129f, 0.129f);
  83. bgIcon.draw();
  84. glColor3f(0.184f, 0.184f, 0.184f);
  85. bgIcon.drawOutline();
  86. if (curHover != curPage && curHover != -1)
  87. {
  88. Rectangle<int> rHover(1, curHover*iconSize + curHover*3, iconSize-2, iconSize-2);
  89. glColor3f(0.071f, 0.071f, 0.071f);
  90. rHover.draw();
  91. glColor3f(0.102f, 0.102f, 0.102f);
  92. rHover.drawOutline();
  93. }
  94. glLineWidth(2.0f);
  95. glColor3f(0.184f, 0.184f, 0.184f);
  96. lineSep.draw();
  97. // reset color
  98. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  99. const int pad = iconSize/2 - DemoArtwork::ico1Width/2;
  100. img1.drawAt(pad, pad);
  101. img2.drawAt(pad, pad + 3 + iconSize);
  102. img3.drawAt(pad, pad + 6 + iconSize*2);
  103. img4.drawAt(pad, pad + 9 + iconSize*3);
  104. img5.drawAt(pad, pad + 12 + iconSize*4);
  105. // draw some text
  106. nvg.beginFrame(this);
  107. nvg.fontSize(23.0f);
  108. nvg.textAlign(NanoVG::ALIGN_LEFT|NanoVG::ALIGN_TOP);
  109. //nvg.textLineHeight(20.0f);
  110. nvg.fillColor(220,220,220,220);
  111. nvg.textBox(10, 420, iconSize, "Haha,", nullptr);
  112. nvg.textBox(15, 440, iconSize, "Look!", nullptr);
  113. nvg.endFrame();
  114. }
  115. bool onMouse(const MouseEvent& ev) override
  116. {
  117. if (ev.button != 1 || ! ev.press)
  118. return false;
  119. if (! contains(ev.pos))
  120. return false;
  121. const int iconSize = bgIcon.getWidth();
  122. for (int i=0; i<kPageCount; ++i)
  123. {
  124. bgIcon.setY(i*iconSize + i*3);
  125. if (bgIcon.contains(ev.pos))
  126. {
  127. curPage = i;
  128. callback->curPageChanged(i);
  129. repaint();
  130. break;
  131. }
  132. }
  133. return true;
  134. }
  135. bool onMotion(const MotionEvent& ev) override
  136. {
  137. if (contains(ev.pos))
  138. {
  139. const int iconSize = bgIcon.getWidth();
  140. for (int i=0; i<kPageCount; ++i)
  141. {
  142. bgIcon.setY(i*iconSize + i*3);
  143. if (bgIcon.contains(ev.pos))
  144. {
  145. if (curHover == i)
  146. return true;
  147. curHover = i;
  148. repaint();
  149. return true;
  150. }
  151. }
  152. if (curHover == -1)
  153. return true;
  154. curHover = -1;
  155. repaint();
  156. return true;
  157. }
  158. else
  159. {
  160. if (curHover == -1)
  161. return false;
  162. curHover = -1;
  163. repaint();
  164. return true;
  165. }
  166. }
  167. void onResize(const ResizeEvent& ev) override
  168. {
  169. const uint width = ev.size.getWidth();
  170. const uint height = ev.size.getHeight();
  171. bgIcon.setWidth(width-4);
  172. bgIcon.setHeight(width-4);
  173. lineSep.setStartPos(width, 0);
  174. lineSep.setEndPos(width, height);
  175. }
  176. private:
  177. Callback* const callback;
  178. int curPage, curHover;
  179. Rectangle<double> bgIcon;
  180. Line<int> lineSep;
  181. OpenGLImage img1, img2, img3, img4, img5;
  182. // for text
  183. NanoVG nvg;
  184. };
  185. // --------------------------------------------------------------------------------------------------------------------
  186. // Main Demo Window, having a left-side tab-like widget and main area for current widget
  187. class DemoWindow : public StandaloneWindow,
  188. public LeftSideWidget::Callback
  189. {
  190. static const int kSidebarWidth = 81;
  191. public:
  192. static constexpr const char* const kExampleWidgetName = "Demo";
  193. DemoWindow(Application& app)
  194. : StandaloneWindow(app),
  195. wColor(this),
  196. wImages(this),
  197. wRects(this),
  198. wShapes(this),
  199. wText(this),
  200. wLeft(this, this),
  201. curWidget(nullptr)
  202. {
  203. wColor.hide();
  204. wImages.hide();
  205. wRects.hide();
  206. wShapes.hide();
  207. wText.hide();
  208. // //wPerf.hide();
  209. wColor.setAbsoluteX(kSidebarWidth);
  210. wImages.setAbsoluteX(kSidebarWidth);
  211. wRects.setAbsoluteX(kSidebarWidth);
  212. wShapes.setAbsoluteX(kSidebarWidth);
  213. wText.setAbsoluteX(kSidebarWidth);
  214. wLeft.setAbsolutePos(2, 2);
  215. // wPerf.setAbsoluteY(5);
  216. setSize(600, 500);
  217. setTitle("DGL Demo");
  218. curPageChanged(0);
  219. }
  220. protected:
  221. void curPageChanged(int curPage) override
  222. {
  223. if (curWidget != nullptr)
  224. curWidget->hide();
  225. switch (curPage)
  226. {
  227. case 0:
  228. curWidget = &wColor;
  229. break;
  230. case 1:
  231. curWidget = &wImages;
  232. break;
  233. case 2:
  234. curWidget = &wRects;
  235. break;
  236. case 3:
  237. curWidget = &wShapes;
  238. break;
  239. case 4:
  240. curWidget = &wText;
  241. break;
  242. default:
  243. curWidget = nullptr;
  244. break;
  245. }
  246. if (curWidget != nullptr)
  247. curWidget->show();
  248. }
  249. void onDisplay() override
  250. {
  251. }
  252. void onReshape(uint width, uint height) override
  253. {
  254. StandaloneWindow::onReshape(width, height);
  255. if (width < kSidebarWidth)
  256. return;
  257. Size<uint> size(width-kSidebarWidth, height);
  258. wColor.setSize(size);
  259. wImages.setSize(size);
  260. wRects.setSize(size);
  261. wShapes.setSize(size);
  262. wText.setSize(size);
  263. wLeft.setSize(kSidebarWidth-4, height-4);
  264. //wRezHandle.setAbsoluteX(width-wRezHandle.getWidth());
  265. //wRezHandle.setAbsoluteY(height-wRezHandle.getHeight());
  266. // wPerf.setAbsoluteX(width-wPerf.getWidth()-5);
  267. }
  268. private:
  269. ExampleColorSubWidget wColor;
  270. ExampleImagesSubWidget wImages;
  271. ExampleRectanglesSubWidget wRects;
  272. ExampleShapesSubWidget wShapes;
  273. ExampleTextSubWidget wText;
  274. LeftSideWidget wLeft;
  275. //ResizeHandle wRezHandle;
  276. // NanoPerfWidget wPerf;
  277. Widget* curWidget;
  278. };
  279. // --------------------------------------------------------------------------------------------------------------------
  280. // Special handy function that runs a StandaloneWindow inside the function scope
  281. template <class ExampleWidgetStandaloneWindow>
  282. void createAndShowExampleWidgetStandaloneWindow(Application& app)
  283. {
  284. ExampleWidgetStandaloneWindow swin(app);
  285. swin.setSize(600, 500);
  286. swin.setTitle(ExampleWidgetStandaloneWindow::kExampleWidgetName);
  287. swin.show();
  288. app.exec();
  289. }
  290. // --------------------------------------------------------------------------------------------------------------------
  291. END_NAMESPACE_DGL
  292. int main(int argc, char* argv[])
  293. {
  294. USE_NAMESPACE_DGL;
  295. using DGL_NAMESPACE::Window;
  296. Application app;
  297. if (argc > 1)
  298. {
  299. // TODO images, text
  300. /**/ if (std::strcmp(argv[1], "color") == 0)
  301. createAndShowExampleWidgetStandaloneWindow<ExampleColorStandaloneWindow>(app);
  302. else if (std::strcmp(argv[1], "images") == 0)
  303. createAndShowExampleWidgetStandaloneWindow<ExampleImagesStandaloneWindow>(app);
  304. else if (std::strcmp(argv[1], "rectangles") == 0)
  305. createAndShowExampleWidgetStandaloneWindow<ExampleRectanglesStandaloneWindow>(app);
  306. else if (std::strcmp(argv[1], "shapes") == 0)
  307. createAndShowExampleWidgetStandaloneWindow<ExampleShapesStandaloneWindow>(app);
  308. else if (std::strcmp(argv[1], "text") == 0)
  309. createAndShowExampleWidgetStandaloneWindow<ExampleTextStandaloneWindow>(app);
  310. else
  311. d_stderr2("Invalid demo mode, must be one of: color, rectangles, shapes");
  312. }
  313. else
  314. {
  315. createAndShowExampleWidgetStandaloneWindow<DemoWindow>(app);
  316. }
  317. return 0;
  318. }
  319. // --------------------------------------------------------------------------------------------------------------------