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.

298 lines
7.1KB

  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. // Our Demo Window
  36. class LeftSizeWidget : public Widget
  37. {
  38. public:
  39. class Callback
  40. {
  41. public:
  42. virtual ~Callback() {}
  43. virtual void curPageChanged(int curPage) = 0;
  44. };
  45. LeftSizeWidget(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 cx = getX();
  61. const int cy = getY();
  62. const int iconSize = getWidth();
  63. glColor3f(0.027f, 0.027f, 0.027f);
  64. bg.draw();
  65. bgIcon.setY(cy + curPage*iconSize + curPage + 1);
  66. glColor3f(0.129f, 0.129f, 0.129f);
  67. bgIcon.draw();
  68. glColor3f(0.184f, 0.184f, 0.184f);
  69. bgIcon.drawOutline();
  70. if (curHover != curPage && curHover != -1)
  71. {
  72. Rectangle<int> rHover(cx + 1, cy + curHover*iconSize + curHover + 1, iconSize-2, iconSize-2);
  73. glColor3f(0.071f, 0.071f, 0.071f);
  74. rHover.draw();
  75. glColor3f(0.102f, 0.102f, 0.102f);
  76. rHover.drawOutline();
  77. }
  78. glLineWidth(2.0f);
  79. glColor3f(0.184f, 0.184f, 0.184f);
  80. lineSep.draw();
  81. // reset color
  82. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  83. const int pad = iconSize/2 - DemoArtwork::ico1Width/2;
  84. img1.drawAt(cx + pad, cy + pad);
  85. img2.drawAt(cx + pad, cy + pad + 1 + iconSize);
  86. img3.drawAt(cx + pad, cy + pad + 2 + iconSize*2);
  87. img4.drawAt(cx + pad, cy + pad + 3 + iconSize*3);
  88. }
  89. bool onMouse(int button, bool press, int x, int y) override
  90. {
  91. if (button != 1 || ! press)
  92. return false;
  93. if (! bg.contains(x, y))
  94. return false;
  95. const int iconSize = getWidth();
  96. for (int i=0; i<4; ++i)
  97. {
  98. bgIcon.setY(i*iconSize + i + 1);
  99. if (bgIcon.contains(x, y))
  100. {
  101. curPage = i;
  102. callback->curPageChanged(i);
  103. repaint();
  104. break;
  105. }
  106. }
  107. return true;
  108. }
  109. bool onMotion(int x, int y) override
  110. {
  111. if (getArea().contains(x, y))
  112. {
  113. const int iconSize = getWidth();
  114. for (int i=0; i<4; ++i)
  115. {
  116. bgIcon.setY(i*iconSize + i + 1);
  117. if (bgIcon.contains(x, y))
  118. {
  119. if (curHover == i)
  120. return true;
  121. curHover = i;
  122. repaint();
  123. return true;
  124. }
  125. }
  126. if (curHover == -1)
  127. return true;
  128. curHover = -1;
  129. repaint();
  130. return true;
  131. }
  132. else
  133. {
  134. if (curHover == -1)
  135. return false;
  136. curHover = -1;
  137. repaint();
  138. return true;
  139. }
  140. }
  141. void onReshape(int, int) override
  142. {
  143. const int cx = getX();
  144. const int iconSize = getWidth();
  145. bg = getArea();
  146. bgIcon.setX(cx+1);
  147. bgIcon.setWidth(iconSize-2);
  148. bgIcon.setHeight(iconSize-2);
  149. lineSep.setStartPos(iconSize+4, getY());
  150. lineSep.setEndPos(iconSize+4, getHeight());
  151. }
  152. private:
  153. Callback* const callback;
  154. int curPage, curHover;
  155. Rectangle<int> bg, bgIcon;
  156. Line<int> lineSep;
  157. Image img1, img2, img3, img4;
  158. };
  159. // ------------------------------------------------------
  160. // Our Demo Window
  161. class DemoWindow : public Window,
  162. public LeftSizeWidget::Callback
  163. {
  164. public:
  165. DemoWindow(App& app)
  166. : Window(app),
  167. wColor(*this),
  168. wImages(*this),
  169. wRects(*this),
  170. wShapes(*this),
  171. wLeft(*this, this),
  172. curWidget(nullptr)
  173. {
  174. wColor.hide();
  175. wImages.hide();
  176. wRects.hide();
  177. wShapes.hide();
  178. wColor.setPos(80, 2);
  179. wImages.setPos(80, 2);
  180. wRects.setPos(80, 2);
  181. wShapes.setPos(80, 2);
  182. wLeft.setPos(2, 2);
  183. setSize(600, 500);
  184. setTitle("DGL Demo");
  185. curPageChanged(0);
  186. }
  187. void onReshape(int width, int height) override
  188. {
  189. Size<int> size(width-80, height);
  190. wColor.setSize(size);
  191. wImages.setSize(size);
  192. wRects.setSize(size);
  193. wShapes.setSize(size);
  194. wLeft.setSize(73, height);
  195. Window::onReshape(width, height);
  196. }
  197. protected:
  198. void curPageChanged(int curPage) override
  199. {
  200. if (curWidget != nullptr)
  201. {
  202. curWidget->hide();
  203. curWidget = nullptr;
  204. }
  205. switch (curPage)
  206. {
  207. case 0:
  208. curWidget = &wColor;
  209. break;
  210. case 1:
  211. curWidget = &wImages;
  212. break;
  213. case 2:
  214. curWidget = &wRects;
  215. break;
  216. case 3:
  217. curWidget = &wShapes;
  218. break;
  219. }
  220. if (curWidget != nullptr)
  221. curWidget->show();
  222. }
  223. private:
  224. ExampleColorWidget wColor;
  225. ExampleImagesWidget wImages;
  226. ExampleRectanglesWidget wRects;
  227. ExampleShapesWidget wShapes;
  228. LeftSizeWidget wLeft;
  229. Widget* curWidget;
  230. };
  231. // ------------------------------------------------------
  232. // main entry point
  233. int main()
  234. {
  235. App app;
  236. DemoWindow win(app);
  237. win.show();
  238. app.exec();
  239. return 0;
  240. }
  241. // ------------------------------------------------------