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.

292 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::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 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(int button, bool press, int x, int y) override
  88. {
  89. if (button != 1 || ! press)
  90. return false;
  91. if (! bg.contains(x, y))
  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(x, y))
  98. {
  99. curPage = i;
  100. callback->curPageChanged(i);
  101. repaint();
  102. break;
  103. }
  104. }
  105. return true;
  106. }
  107. bool onMotion(int x, int y) override
  108. {
  109. if (contains(x, y))
  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(x, y))
  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 onReshape(int width, int height) override
  140. {
  141. bg.setSize(width, height);
  142. bgIcon.setWidth(width-2);
  143. bgIcon.setHeight(width-2);
  144. lineSep.setStartX(width+2);
  145. lineSep.setEndPos(width+2, height);
  146. }
  147. private:
  148. Callback* const callback;
  149. int curPage, curHover;
  150. Rectangle<int> bg, bgIcon;
  151. Line<int> lineSep;
  152. Image img1, img2, img3, img4;
  153. };
  154. // ------------------------------------------------------
  155. // Our Demo Window
  156. class DemoWindow : public Window,
  157. public LeftSizeWidget::Callback
  158. {
  159. public:
  160. DemoWindow(App& app)
  161. : Window(app),
  162. wColor(*this),
  163. wImages(*this),
  164. wRects(*this),
  165. wShapes(*this),
  166. wLeft(*this, this),
  167. curWidget(nullptr)
  168. {
  169. wColor.hide();
  170. wImages.hide();
  171. wRects.hide();
  172. wShapes.hide();
  173. wColor.setX(80);
  174. wImages.setX(80);
  175. wRects.setX(80);
  176. wShapes.setX(80);
  177. wLeft.setPos(2, 2);
  178. setSize(600, 500);
  179. setTitle("DGL Demo");
  180. curPageChanged(0);
  181. }
  182. void onReshape(int width, int height) override
  183. {
  184. Size<int> size(width-80, height);
  185. wColor.setSize(size);
  186. wImages.setSize(size);
  187. wRects.setSize(size);
  188. wShapes.setSize(size);
  189. wLeft.setSize(73, height-4);
  190. Window::onReshape(width, height);
  191. }
  192. protected:
  193. void curPageChanged(int curPage) override
  194. {
  195. if (curWidget != nullptr)
  196. {
  197. curWidget->hide();
  198. curWidget = nullptr;
  199. }
  200. switch (curPage)
  201. {
  202. case 0:
  203. curWidget = &wColor;
  204. break;
  205. case 1:
  206. curWidget = &wImages;
  207. break;
  208. case 2:
  209. curWidget = &wRects;
  210. break;
  211. case 3:
  212. curWidget = &wShapes;
  213. break;
  214. }
  215. if (curWidget != nullptr)
  216. curWidget->show();
  217. }
  218. private:
  219. ExampleColorWidget wColor;
  220. ExampleImagesWidget wImages;
  221. ExampleRectanglesWidget wRects;
  222. ExampleShapesWidget wShapes;
  223. LeftSizeWidget wLeft;
  224. Widget* curWidget;
  225. };
  226. // ------------------------------------------------------
  227. // main entry point
  228. int main()
  229. {
  230. App app;
  231. DemoWindow win(app);
  232. win.show();
  233. app.exec();
  234. return 0;
  235. }
  236. // ------------------------------------------------------