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.

413 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_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. printf("LEFT SIDE WIDGET onDisplay %i\n", iconSize);
  73. glColor3f(0.027f, 0.027f, 0.027f);
  74. Rectangle<uint>(0, 0, getSize()).draw();
  75. bgIcon.setY(curPage*iconSize + curPage*3);
  76. glColor3f(0.129f, 0.129f, 0.129f);
  77. bgIcon.draw();
  78. glColor3f(0.184f, 0.184f, 0.184f);
  79. bgIcon.drawOutline();
  80. if (curHover != curPage && curHover != -1)
  81. {
  82. Rectangle<int> rHover(1, curHover*iconSize + curHover*3, iconSize-2, iconSize-2);
  83. glColor3f(0.071f, 0.071f, 0.071f);
  84. rHover.draw();
  85. glColor3f(0.102f, 0.102f, 0.102f);
  86. rHover.drawOutline();
  87. }
  88. glLineWidth(2.0f);
  89. glColor3f(0.184f, 0.184f, 0.184f);
  90. lineSep.draw();
  91. // reset color
  92. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  93. #if 0
  94. const int pad = iconSize/2 - DemoArtwork::ico1Width/2;
  95. img1.drawAt(pad, pad);
  96. img2.drawAt(pad, pad + 3 + iconSize);
  97. img3.drawAt(pad, pad + 6 + iconSize*2);
  98. img4.drawAt(pad, pad + 9 + iconSize*3);
  99. img5.drawAt(pad, pad + 12 + iconSize*4);
  100. // draw some text
  101. nvg.beginFrame(this);
  102. nvg.fontSize(23.0f);
  103. nvg.textAlign(NanoVG::ALIGN_LEFT|NanoVG::ALIGN_TOP);
  104. //nvg.textLineHeight(20.0f);
  105. nvg.fillColor(220,220,220,220);
  106. nvg.textBox(10, 420, iconSize, "Haha,", nullptr);
  107. nvg.textBox(15, 440, iconSize, "Look!", nullptr);
  108. nvg.endFrame();
  109. #endif
  110. }
  111. bool onMouse(const MouseEvent& ev) override
  112. {
  113. if (ev.button != 1 || ! ev.press)
  114. return false;
  115. if (! contains(ev.pos))
  116. return false;
  117. const int iconSize = bgIcon.getWidth();
  118. for (int i=0; i<kPageCount; ++i)
  119. {
  120. bgIcon.setY(i*iconSize + i*3);
  121. if (bgIcon.contains(ev.pos))
  122. {
  123. curPage = i;
  124. callback->curPageChanged(i);
  125. repaint();
  126. break;
  127. }
  128. }
  129. return true;
  130. }
  131. bool onMotion(const MotionEvent& ev) override
  132. {
  133. if (contains(ev.pos))
  134. {
  135. const int iconSize = bgIcon.getWidth();
  136. for (int i=0; i<kPageCount; ++i)
  137. {
  138. bgIcon.setY(i*iconSize + i*3);
  139. if (bgIcon.contains(ev.pos))
  140. {
  141. if (curHover == i)
  142. return true;
  143. curHover = i;
  144. repaint();
  145. return true;
  146. }
  147. }
  148. if (curHover == -1)
  149. return true;
  150. curHover = -1;
  151. repaint();
  152. return true;
  153. }
  154. else
  155. {
  156. if (curHover == -1)
  157. return false;
  158. curHover = -1;
  159. repaint();
  160. return true;
  161. }
  162. }
  163. void onResize(const ResizeEvent& ev) override
  164. {
  165. const int width = ev.size.getWidth();
  166. const int height = ev.size.getHeight();
  167. bgIcon.setWidth(width-4);
  168. bgIcon.setHeight(width-4);
  169. lineSep.setStartPos(width, 0);
  170. lineSep.setEndPos(width, height);
  171. }
  172. private:
  173. Callback* const callback;
  174. int curPage, curHover;
  175. Rectangle<double> bgIcon;
  176. Line<int> lineSep;
  177. #if 0
  178. Image img1, img2, img3, img4, img5;
  179. // for text
  180. NanoVG nvg;D
  181. NanoVG::FontId font;
  182. #endif
  183. };
  184. // --------------------------------------------------------------------------------------------------------------------
  185. // Main Demo Window, having a left-side tab-like widget and main area for current widget
  186. class DemoWindow : public StandaloneWindow,
  187. public LeftSideWidget::Callback
  188. {
  189. static const int kSidebarWidth = 81;
  190. public:
  191. static constexpr const char* const kExampleWidgetName = "Demo";
  192. DemoWindow(Application& app)
  193. : StandaloneWindow(app),
  194. wColor(this),
  195. wRects(this),
  196. wShapes(this),
  197. wLeft(this, this),
  198. curWidget(nullptr)
  199. {
  200. wColor.hide();
  201. // wImages.hide();
  202. wRects.hide();
  203. wShapes.hide();
  204. // wText.hide();
  205. // //wPerf.hide();
  206. wColor.setAbsoluteX(kSidebarWidth);
  207. // wImages.setAbsoluteX(kSidebarWidth);
  208. wRects.setAbsoluteX(kSidebarWidth);
  209. wShapes.setAbsoluteX(kSidebarWidth);
  210. // wText.setAbsoluteX(kSidebarWidth);
  211. wLeft.setAbsolutePos(2, 2);
  212. // wPerf.setAbsoluteY(5);
  213. setSize(600, 500);
  214. setTitle("DGL Demo");
  215. curPageChanged(0);
  216. }
  217. protected:
  218. void curPageChanged(int curPage) override
  219. {
  220. if (curWidget != nullptr)
  221. curWidget->hide();
  222. switch (curPage)
  223. {
  224. case 0:
  225. curWidget = &wColor;
  226. break;
  227. // case 1:
  228. // curWidget = &wImages;
  229. // break;
  230. case 2:
  231. curWidget = &wRects;
  232. break;
  233. case 3:
  234. curWidget = &wShapes;
  235. break;
  236. // case 4:
  237. // curWidget = &wText;
  238. // break;
  239. default:
  240. curWidget = nullptr;
  241. break;
  242. }
  243. if (curWidget != nullptr)
  244. curWidget->show();
  245. }
  246. void onDisplay() override
  247. {
  248. static int counter = 0;
  249. printf("print %i\n", ++counter);
  250. glColor3f(0.471f, 0.971f, 0.171f);
  251. Rectangle<uint>(0, 0, getSize()).draw();
  252. }
  253. void onReshape(uint width, uint height) override
  254. {
  255. StandaloneWindow::onReshape(width, height);
  256. if (width < kSidebarWidth)
  257. return;
  258. Size<uint> size(width-kSidebarWidth, height);
  259. wColor.setSize(size);
  260. // wImages.setSize(size);
  261. wRects.setSize(size);
  262. wShapes.setSize(size);
  263. // wText.setSize(size);
  264. wLeft.setSize(kSidebarWidth-4, height-4);
  265. //wRezHandle.setAbsoluteX(width-wRezHandle.getWidth());
  266. //wRezHandle.setAbsoluteY(height-wRezHandle.getHeight());
  267. // wPerf.setAbsoluteX(width-wPerf.getWidth()-5);
  268. }
  269. private:
  270. ExampleColorSubWidget wColor;
  271. // ExampleImagesWidget wImages;
  272. ExampleRectanglesSubWidget wRects;
  273. ExampleShapesSubWidget wShapes;
  274. // ExampleTextWidget wText;
  275. LeftSideWidget wLeft;
  276. //ResizeHandle wRezHandle;
  277. // NanoPerfWidget wPerf;
  278. Widget* curWidget;
  279. };
  280. // --------------------------------------------------------------------------------------------------------------------
  281. // Testing StandaloneWindow, for custom local widget drawing code
  282. class TestingWidgetStandaloneWindow : public StandaloneWindow
  283. {
  284. public:
  285. static constexpr const char* kExampleWidgetName = "Testing";
  286. TestingWidgetStandaloneWindow(Application& app)
  287. : StandaloneWindow(app)
  288. {
  289. }
  290. protected:
  291. void onDisplay() override
  292. {
  293. glColor3f(0.5f, 0.3f, 0.9f);
  294. Rectangle<uint>(0, 0, 500, 500).draw();
  295. // getWidth(), getHeight()
  296. }
  297. };
  298. // --------------------------------------------------------------------------------------------------------------------
  299. // Special handy function that runs a StandaloneWindow inside the function scope
  300. template <class ExampleWidgetStandaloneWindow>
  301. void createAndShowExampleWidgetStandaloneWindow(Application& app)
  302. {
  303. ExampleWidgetStandaloneWindow swin(app);
  304. swin.setSize(600, 500);
  305. swin.setTitle(ExampleWidgetStandaloneWindow::kExampleWidgetName);
  306. swin.show();
  307. app.exec();
  308. }
  309. // --------------------------------------------------------------------------------------------------------------------
  310. END_NAMESPACE_DGL
  311. int main(int argc, char* argv[])
  312. {
  313. USE_NAMESPACE_DGL;
  314. using DGL_NAMESPACE::Window;
  315. Application app;
  316. if (argc > 1)
  317. {
  318. // TODO images, text
  319. /**/ if (std::strcmp(argv[1], "color") == 0)
  320. createAndShowExampleWidgetStandaloneWindow<ExampleColorStandaloneWindow>(app);
  321. else if (std::strcmp(argv[1], "rectangles") == 0)
  322. createAndShowExampleWidgetStandaloneWindow<ExampleRectanglesStandaloneWindow>(app);
  323. else if (std::strcmp(argv[1], "shapes") == 0)
  324. createAndShowExampleWidgetStandaloneWindow<ExampleShapesStandaloneWindow>(app);
  325. else if (std::strcmp(argv[1], "testing") == 0)
  326. createAndShowExampleWidgetStandaloneWindow<TestingWidgetStandaloneWindow>(app);
  327. else
  328. d_stderr2("Invalid demo mode, must be one of: color, rectangles, shapes");
  329. }
  330. else
  331. {
  332. createAndShowExampleWidgetStandaloneWindow<DemoWindow>(app);
  333. }
  334. return 0;
  335. }
  336. // --------------------------------------------------------------------------------------------------------------------