DPF OpenGL examples
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.

438 lines
11KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2018 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. // ------------------------------------------------------
  17. // DGL Stuff
  18. #include "StandaloneWindow.hpp"
  19. #include "widgets/ExampleColorWidget.hpp"
  20. #include "widgets/ExampleImagesWidget.hpp"
  21. #include "widgets/ExampleRectanglesWidget.hpp"
  22. #include "widgets/ExampleShapesWidget.hpp"
  23. #include "widgets/ExampleTextWidget.hpp"
  24. #include "widgets/NanoPerfWidget.hpp"
  25. // ------------------------------------------------------
  26. // Images
  27. #include "demo_res/DemoArtwork.cpp"
  28. #include "images_res/CatPics.cpp"
  29. // ------------------------------------------------------
  30. // Left side tab-like widget
  31. class LeftSideWidget : public Widget
  32. {
  33. public:
  34. static const int kPageCount = 5;
  35. class Callback
  36. {
  37. public:
  38. virtual ~Callback() {}
  39. virtual void curPageChanged(int curPage) = 0;
  40. };
  41. LeftSideWidget(Window& parent, Callback* const cb)
  42. : Widget(parent),
  43. callback(cb),
  44. curPage(0),
  45. curHover(-1)
  46. {
  47. // for text
  48. font = nvg.createFontFromFile("sans", "./nanovg_res/Roboto-Regular.ttf");
  49. using namespace DemoArtwork;
  50. img1.loadFromMemory(ico1Data, ico1Width, ico1Height, GL_BGR);
  51. img2.loadFromMemory(ico2Data, ico2Width, ico2Height, GL_BGR);
  52. img3.loadFromMemory(ico3Data, ico3Width, ico2Height, GL_BGR);
  53. img4.loadFromMemory(ico4Data, ico4Width, ico4Height, GL_BGR);
  54. img5.loadFromMemory(ico5Data, ico5Width, ico5Height, GL_BGR);
  55. }
  56. protected:
  57. void onDisplay() override
  58. {
  59. const int iconSize = bgIcon.getWidth();
  60. glColor3f(0.027f, 0.027f, 0.027f);
  61. Rectangle<uint>(0, 0, getSize()).draw();
  62. bgIcon.setY(curPage*iconSize + curPage*3);
  63. glColor3f(0.129f, 0.129f, 0.129f);
  64. bgIcon.draw();
  65. glColor3f(0.184f, 0.184f, 0.184f);
  66. bgIcon.drawOutline();
  67. if (curHover != curPage && curHover != -1)
  68. {
  69. Rectangle<int> rHover(1, curHover*iconSize + curHover*3, iconSize-2, iconSize-2);
  70. glColor3f(0.071f, 0.071f, 0.071f);
  71. rHover.draw();
  72. glColor3f(0.102f, 0.102f, 0.102f);
  73. rHover.drawOutline();
  74. }
  75. glLineWidth(2.0f);
  76. glColor3f(0.184f, 0.184f, 0.184f);
  77. lineSep.draw();
  78. // reset color
  79. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  80. const int pad = iconSize/2 - DemoArtwork::ico1Width/2;
  81. img1.drawAt(pad, pad);
  82. img2.drawAt(pad, pad + 3 + iconSize);
  83. img3.drawAt(pad, pad + 6 + iconSize*2);
  84. img4.drawAt(pad, pad + 9 + iconSize*3);
  85. img5.drawAt(pad, pad + 12 + iconSize*4);
  86. // draw some text
  87. nvg.beginFrame(this);
  88. nvg.fontSize(23.0f);
  89. nvg.textAlign(NanoVG::ALIGN_LEFT|NanoVG::ALIGN_TOP);
  90. //nvg.textLineHeight(20.0f);
  91. nvg.fillColor(220,220,220,220);
  92. nvg.textBox(10, 420, iconSize, "Haha,", nullptr);
  93. nvg.textBox(15, 440, iconSize, "Look!", nullptr);
  94. nvg.endFrame();
  95. }
  96. bool onMouse(const MouseEvent& ev) override
  97. {
  98. if (ev.button != 1 || ! ev.press)
  99. return false;
  100. if (! contains(ev.pos))
  101. return false;
  102. const int iconSize = bgIcon.getWidth();
  103. for (int i=0; i<kPageCount; ++i)
  104. {
  105. bgIcon.setY(i*iconSize + i*3);
  106. if (bgIcon.contains(ev.pos))
  107. {
  108. curPage = i;
  109. callback->curPageChanged(i);
  110. repaint();
  111. break;
  112. }
  113. }
  114. return true;
  115. }
  116. bool onMotion(const MotionEvent& ev) override
  117. {
  118. if (contains(ev.pos))
  119. {
  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. if (curHover == i)
  127. return true;
  128. curHover = i;
  129. repaint();
  130. return true;
  131. }
  132. }
  133. if (curHover == -1)
  134. return true;
  135. curHover = -1;
  136. repaint();
  137. return true;
  138. }
  139. else
  140. {
  141. if (curHover == -1)
  142. return false;
  143. curHover = -1;
  144. repaint();
  145. return true;
  146. }
  147. }
  148. void onResize(const ResizeEvent& ev) override
  149. {
  150. const int width = ev.size.getWidth();
  151. const int height = ev.size.getHeight();
  152. bgIcon.setWidth(width-4);
  153. bgIcon.setHeight(width-4);
  154. lineSep.setStartPos(width, 0);
  155. lineSep.setEndPos(width, height);
  156. }
  157. private:
  158. Callback* const callback;
  159. int curPage, curHover;
  160. Rectangle<int> bgIcon;
  161. Line<int> lineSep;
  162. Image img1, img2, img3, img4, img5;
  163. // for text
  164. NanoVG nvg;
  165. NanoVG::FontId font;
  166. };
  167. #if 0
  168. // ------------------------------------------------------
  169. // Resize handle
  170. class ResizeHandle : public Widget
  171. {
  172. public:
  173. ResizeHandle(Window& parent)
  174. : Widget(parent),
  175. fMouseUnder(false),
  176. fLastX(0),
  177. fLastY(0)
  178. {
  179. fColor[0] = 1.0f;
  180. fColor[1] = 1.0f;
  181. fColor[2] = 1.0f;
  182. fColor[3] = 1.0f;
  183. setSize(16, 16);
  184. }
  185. void setColor(float color[4]) noexcept
  186. {
  187. std::memcpy(fColor, color, sizeof(float)*4);
  188. }
  189. protected:
  190. void onDisplay() override
  191. {
  192. glColor4f(fColor[0], fColor[1], fColor[2], fColor[3]);
  193. fLine1.draw();
  194. fLine2.draw();
  195. fLine3.draw();
  196. // reset color
  197. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  198. }
  199. void onReshape(int width, int height) override
  200. {
  201. fLine1.setStartPos(width/10, height*9/10);
  202. fLine1.setEndPos(width*9/10, width/10);
  203. fLine2.setStartPos(width*4/10, height*9/10);
  204. fLine2.setEndPos(width*9/10, height*4/10);
  205. fLine3.setStartPos(width*7/10, height*9/10);
  206. fLine3.setEndPos(width*9/10, height*7/10);
  207. }
  208. bool onMouse(int button, bool press, int x, int y) override
  209. {
  210. if (button != 1)
  211. return false;
  212. if (fMouseUnder)
  213. {
  214. if (! press)
  215. fMouseUnder = false;
  216. return true;
  217. }
  218. if (! contains(x, y))
  219. return false;
  220. if (! press)
  221. return false;
  222. fLastX = x+getAbsoluteX();
  223. fLastY = y+getAbsoluteY();
  224. fMouseUnder = true;
  225. return true;
  226. }
  227. bool onMotion(int x, int y) override
  228. {
  229. if (! fMouseUnder)
  230. return false;
  231. x += getAbsoluteX();
  232. y += getAbsoluteY();
  233. const int movedX = x - fLastX;
  234. const int movedY = y - fLastY;
  235. fLastX = x;
  236. fLastY = y;
  237. d_stdout("Moved %i, %i", movedX, movedY);
  238. Size<uint> size(getParentWindow().getSize());
  239. size += Size<uint>(movedX, movedY);
  240. getParentWindow().setSize(size);
  241. repaint();
  242. return true;
  243. }
  244. float fColor[4];
  245. bool fMouseUnder;
  246. int fLastX, fLastY;
  247. Line<float> fLine1, fLine2, fLine3;
  248. };
  249. #endif
  250. // ------------------------------------------------------
  251. // Our Demo Window
  252. class DemoWindow : public Window,
  253. public LeftSideWidget::Callback
  254. {
  255. public:
  256. static const int kSidebarWidth = 81;
  257. DemoWindow(Application& app)
  258. : Window(app),
  259. wColor(*this),
  260. wImages(*this),
  261. wRects(*this),
  262. wShapes(*this),
  263. wText(*this),
  264. wLeft(*this, this),
  265. //wRezHandle(*this),
  266. wPerf(*this, NanoPerfWidget::RENDER_FPS, "TESTING!!"),
  267. curWidget(nullptr)
  268. {
  269. wColor.hide();
  270. wImages.hide();
  271. wRects.hide();
  272. wShapes.hide();
  273. wText.hide();
  274. //wPerf.hide();
  275. wColor.setAbsoluteX(kSidebarWidth);
  276. wImages.setAbsoluteX(kSidebarWidth);
  277. wRects.setAbsoluteX(kSidebarWidth);
  278. wShapes.setAbsoluteX(kSidebarWidth);
  279. wText.setAbsoluteX(kSidebarWidth);
  280. wLeft.setAbsolutePos(2, 2);
  281. wPerf.setAbsoluteY(5);
  282. setSize(600, 500);
  283. setTitle("DGL Demo");
  284. curPageChanged(0);
  285. }
  286. void onReshape(uint width, uint height) override
  287. {
  288. if (width < kSidebarWidth)
  289. return;
  290. Size<uint> size(width-kSidebarWidth, height);
  291. wColor.setSize(size);
  292. wImages.setSize(size);
  293. wRects.setSize(size);
  294. wShapes.setSize(size);
  295. wText.setSize(size);
  296. wLeft.setSize(kSidebarWidth-4, height-4);
  297. //wRezHandle.setAbsoluteX(width-wRezHandle.getWidth());
  298. //wRezHandle.setAbsoluteY(height-wRezHandle.getHeight());
  299. wPerf.setAbsoluteX(width-wPerf.getWidth()-5);
  300. Window::onReshape(width, height);
  301. }
  302. protected:
  303. void curPageChanged(int curPage) override
  304. {
  305. if (curWidget != nullptr)
  306. {
  307. curWidget->hide();
  308. curWidget = nullptr;
  309. }
  310. switch (curPage)
  311. {
  312. case 0:
  313. curWidget = &wColor;
  314. break;
  315. case 1:
  316. curWidget = &wImages;
  317. break;
  318. case 2:
  319. curWidget = &wRects;
  320. break;
  321. case 3:
  322. curWidget = &wShapes;
  323. break;
  324. case 4:
  325. curWidget = &wText;
  326. break;
  327. }
  328. if (curWidget != nullptr)
  329. curWidget->show();
  330. }
  331. private:
  332. ExampleColorWidget wColor;
  333. ExampleImagesWidget wImages;
  334. ExampleRectanglesWidget wRects;
  335. ExampleShapesWidget wShapes;
  336. ExampleTextWidget wText;
  337. LeftSideWidget wLeft;
  338. //ResizeHandle wRezHandle;
  339. NanoPerfWidget wPerf;
  340. Widget* curWidget;
  341. };
  342. // ------------------------------------------------------
  343. // main entry point
  344. int main()
  345. {
  346. Application app;
  347. DemoWindow win(app);
  348. win.show();
  349. app.exec();
  350. return 0;
  351. }
  352. // ------------------------------------------------------