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.

289 lines
6.9KB

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