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.

404 lines
9.4KB

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