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.

422 lines
10KB

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