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.

442 lines
11KB

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