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.

383 lines
10KB

  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_POINT_CPP
  21. #include "dgl/src/pugl.cpp"
  22. #include "dgl/src/Application.cpp"
  23. #include "dgl/src/ApplicationPrivateData.cpp"
  24. #include "dgl/src/Geometry.cpp"
  25. #include "dgl/src/OpenGL.cpp"
  26. #include "dgl/src/SubWidget.cpp"
  27. #include "dgl/src/SubWidgetPrivateData.cpp"
  28. #include "dgl/src/TopLevelWidget.cpp"
  29. #include "dgl/src/TopLevelWidgetPrivateData.cpp"
  30. #include "dgl/src/Widget.cpp"
  31. #include "dgl/src/WidgetPrivateData.cpp"
  32. #include "dgl/src/Window.cpp"
  33. #include "dgl/src/WindowPrivateData.cpp"
  34. #include "dgl/StandaloneWindow.hpp"
  35. #include "widgets/ExampleColorWidget.hpp"
  36. #include "widgets/ExampleRectanglesWidget.hpp"
  37. #include "widgets/ExampleShapesWidget.hpp"
  38. START_NAMESPACE_DGL
  39. // --------------------------------------------------------------------------------------------------------------------
  40. // Left side tab-like widget
  41. class LeftSideWidget : public SubWidget
  42. {
  43. public:
  44. static const int kPageCount = 5;
  45. class Callback
  46. {
  47. public:
  48. virtual ~Callback() {}
  49. virtual void curPageChanged(int curPage) = 0;
  50. };
  51. LeftSideWidget(Widget* parent, Callback* const cb)
  52. : SubWidget(parent),
  53. callback(cb),
  54. curPage(0),
  55. curHover(-1)
  56. {
  57. #if 0
  58. // for text
  59. font = nvg.createFontFromFile("sans", "./nanovg_res/Roboto-Regular.ttf");
  60. using namespace DemoArtwork;
  61. img1.loadFromMemory(ico1Data, ico1Width, ico1Height, GL_BGR);
  62. img2.loadFromMemory(ico2Data, ico2Width, ico2Height, GL_BGR);
  63. img3.loadFromMemory(ico3Data, ico3Width, ico2Height, GL_BGR);
  64. img4.loadFromMemory(ico4Data, ico4Width, ico4Height, GL_BGR);
  65. img5.loadFromMemory(ico5Data, ico5Width, ico5Height, GL_BGR);
  66. #endif
  67. }
  68. protected:
  69. void onDisplay() override
  70. {
  71. const int iconSize = bgIcon.getWidth();
  72. glColor3f(0.027f, 0.027f, 0.027f);
  73. Rectangle<uint>(0, 0, getSize()).draw();
  74. bgIcon.setY(curPage*iconSize + curPage*3);
  75. glColor3f(0.129f, 0.129f, 0.129f);
  76. bgIcon.draw();
  77. glColor3f(0.184f, 0.184f, 0.184f);
  78. bgIcon.drawOutline();
  79. if (curHover != curPage && curHover != -1)
  80. {
  81. Rectangle<int> rHover(1, curHover*iconSize + curHover*3, iconSize-2, iconSize-2);
  82. glColor3f(0.071f, 0.071f, 0.071f);
  83. rHover.draw();
  84. glColor3f(0.102f, 0.102f, 0.102f);
  85. rHover.drawOutline();
  86. }
  87. glLineWidth(2.0f);
  88. glColor3f(0.184f, 0.184f, 0.184f);
  89. lineSep.draw();
  90. // reset color
  91. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  92. #if 0
  93. const int pad = iconSize/2 - DemoArtwork::ico1Width/2;
  94. img1.drawAt(pad, pad);
  95. img2.drawAt(pad, pad + 3 + iconSize);
  96. img3.drawAt(pad, pad + 6 + iconSize*2);
  97. img4.drawAt(pad, pad + 9 + iconSize*3);
  98. img5.drawAt(pad, pad + 12 + iconSize*4);
  99. // draw some text
  100. nvg.beginFrame(this);
  101. nvg.fontSize(23.0f);
  102. nvg.textAlign(NanoVG::ALIGN_LEFT|NanoVG::ALIGN_TOP);
  103. //nvg.textLineHeight(20.0f);
  104. nvg.fillColor(220,220,220,220);
  105. nvg.textBox(10, 420, iconSize, "Haha,", nullptr);
  106. nvg.textBox(15, 440, iconSize, "Look!", nullptr);
  107. nvg.endFrame();
  108. #endif
  109. }
  110. bool onMouse(const MouseEvent& ev) override
  111. {
  112. if (ev.button != 1 || ! ev.press)
  113. return false;
  114. if (! contains(ev.pos))
  115. return false;
  116. const int iconSize = bgIcon.getWidth();
  117. for (int i=0; i<kPageCount; ++i)
  118. {
  119. bgIcon.setY(i*iconSize + i*3);
  120. if (bgIcon.contains(ev.pos))
  121. {
  122. curPage = i;
  123. callback->curPageChanged(i);
  124. repaint();
  125. break;
  126. }
  127. }
  128. return true;
  129. }
  130. bool onMotion(const MotionEvent& ev) override
  131. {
  132. if (contains(ev.pos))
  133. {
  134. const int iconSize = bgIcon.getWidth();
  135. for (int i=0; i<kPageCount; ++i)
  136. {
  137. bgIcon.setY(i*iconSize + i*3);
  138. if (bgIcon.contains(ev.pos))
  139. {
  140. if (curHover == i)
  141. return true;
  142. curHover = i;
  143. repaint();
  144. return true;
  145. }
  146. }
  147. if (curHover == -1)
  148. return true;
  149. curHover = -1;
  150. repaint();
  151. return true;
  152. }
  153. else
  154. {
  155. if (curHover == -1)
  156. return false;
  157. curHover = -1;
  158. repaint();
  159. return true;
  160. }
  161. }
  162. void onResize(const ResizeEvent& ev) override
  163. {
  164. const uint width = ev.size.getWidth();
  165. const uint height = ev.size.getHeight();
  166. bgIcon.setWidth(width-4);
  167. bgIcon.setHeight(width-4);
  168. lineSep.setStartPos(width, 0);
  169. lineSep.setEndPos(width, height);
  170. }
  171. private:
  172. Callback* const callback;
  173. int curPage, curHover;
  174. Rectangle<double> bgIcon;
  175. Line<int> lineSep;
  176. #if 0
  177. Image img1, img2, img3, img4, img5;
  178. // for text
  179. NanoVG nvg;D
  180. NanoVG::FontId font;
  181. #endif
  182. };
  183. // --------------------------------------------------------------------------------------------------------------------
  184. // Main Demo Window, having a left-side tab-like widget and main area for current widget
  185. class DemoWindow : public StandaloneWindow,
  186. public LeftSideWidget::Callback
  187. {
  188. static const int kSidebarWidth = 81;
  189. public:
  190. static constexpr const char* const kExampleWidgetName = "Demo";
  191. DemoWindow(Application& app)
  192. : StandaloneWindow(app),
  193. wColor(this),
  194. wRects(this),
  195. wShapes(this),
  196. wLeft(this, this),
  197. curWidget(nullptr)
  198. {
  199. wColor.hide();
  200. // wImages.hide();
  201. wRects.hide();
  202. wShapes.hide();
  203. // wText.hide();
  204. // //wPerf.hide();
  205. wColor.setAbsoluteX(kSidebarWidth);
  206. // wImages.setAbsoluteX(kSidebarWidth);
  207. wRects.setAbsoluteX(kSidebarWidth);
  208. wShapes.setAbsoluteX(kSidebarWidth);
  209. // wText.setAbsoluteX(kSidebarWidth);
  210. wLeft.setAbsolutePos(2, 2);
  211. // wPerf.setAbsoluteY(5);
  212. setSize(600, 500);
  213. setTitle("DGL Demo");
  214. curPageChanged(0);
  215. }
  216. protected:
  217. void curPageChanged(int curPage) override
  218. {
  219. if (curWidget != nullptr)
  220. curWidget->hide();
  221. switch (curPage)
  222. {
  223. case 0:
  224. curWidget = &wColor;
  225. break;
  226. // case 1:
  227. // curWidget = &wImages;
  228. // break;
  229. case 2:
  230. curWidget = &wRects;
  231. break;
  232. case 3:
  233. curWidget = &wShapes;
  234. break;
  235. // case 4:
  236. // curWidget = &wText;
  237. // break;
  238. default:
  239. curWidget = nullptr;
  240. break;
  241. }
  242. if (curWidget != nullptr)
  243. curWidget->show();
  244. }
  245. void onDisplay() override
  246. {
  247. }
  248. void onReshape(uint width, uint height) override
  249. {
  250. StandaloneWindow::onReshape(width, height);
  251. if (width < kSidebarWidth)
  252. return;
  253. Size<uint> size(width-kSidebarWidth, height);
  254. wColor.setSize(size);
  255. // wImages.setSize(size);
  256. wRects.setSize(size);
  257. wShapes.setSize(size);
  258. // wText.setSize(size);
  259. wLeft.setSize(kSidebarWidth-4, height-4);
  260. //wRezHandle.setAbsoluteX(width-wRezHandle.getWidth());
  261. //wRezHandle.setAbsoluteY(height-wRezHandle.getHeight());
  262. // wPerf.setAbsoluteX(width-wPerf.getWidth()-5);
  263. }
  264. private:
  265. ExampleColorSubWidget wColor;
  266. // ExampleImagesWidget wImages;
  267. ExampleRectanglesSubWidget wRects;
  268. ExampleShapesSubWidget wShapes;
  269. // ExampleTextWidget wText;
  270. LeftSideWidget wLeft;
  271. //ResizeHandle wRezHandle;
  272. // NanoPerfWidget wPerf;
  273. Widget* curWidget;
  274. };
  275. // --------------------------------------------------------------------------------------------------------------------
  276. // Special handy function that runs a StandaloneWindow inside the function scope
  277. template <class ExampleWidgetStandaloneWindow>
  278. void createAndShowExampleWidgetStandaloneWindow(Application& app)
  279. {
  280. ExampleWidgetStandaloneWindow swin(app);
  281. swin.setSize(600, 500);
  282. swin.setTitle(ExampleWidgetStandaloneWindow::kExampleWidgetName);
  283. swin.show();
  284. app.exec();
  285. }
  286. // --------------------------------------------------------------------------------------------------------------------
  287. END_NAMESPACE_DGL
  288. int main(int argc, char* argv[])
  289. {
  290. USE_NAMESPACE_DGL;
  291. using DGL_NAMESPACE::Window;
  292. Application app;
  293. if (argc > 1)
  294. {
  295. // TODO images, text
  296. /**/ if (std::strcmp(argv[1], "color") == 0)
  297. createAndShowExampleWidgetStandaloneWindow<ExampleColorStandaloneWindow>(app);
  298. else if (std::strcmp(argv[1], "rectangles") == 0)
  299. createAndShowExampleWidgetStandaloneWindow<ExampleRectanglesStandaloneWindow>(app);
  300. else if (std::strcmp(argv[1], "shapes") == 0)
  301. createAndShowExampleWidgetStandaloneWindow<ExampleShapesStandaloneWindow>(app);
  302. else
  303. d_stderr2("Invalid demo mode, must be one of: color, rectangles, shapes");
  304. }
  305. else
  306. {
  307. createAndShowExampleWidgetStandaloneWindow<DemoWindow>(app);
  308. }
  309. return 0;
  310. }
  311. // --------------------------------------------------------------------------------------------------------------------