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.

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