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.

399 lines
9.2KB

  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(int button, bool press, int x, int y) override
  88. {
  89. if (button != 1 || ! press)
  90. return false;
  91. if (! 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. // Resize handle
  156. class ResizeHandle : public Widget
  157. {
  158. public:
  159. ResizeHandle(Window& parent)
  160. : Widget(parent),
  161. fMouseUnder(false),
  162. fLastX(0),
  163. fLastY(0)
  164. {
  165. fColor[0] = 1.0f;
  166. fColor[1] = 1.0f;
  167. fColor[2] = 1.0f;
  168. fColor[3] = 1.0f;
  169. setSize(16, 16);
  170. }
  171. void setColor(float color[4]) noexcept
  172. {
  173. std::memcpy(fColor, color, sizeof(float)*4);
  174. }
  175. protected:
  176. void onDisplay() override
  177. {
  178. glColor4f(fColor[0], fColor[1], fColor[2], fColor[3]);
  179. fLine1.draw();
  180. fLine2.draw();
  181. fLine3.draw();
  182. // reset color
  183. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  184. }
  185. void onReshape(int width, int height) override
  186. {
  187. fLine1.setStartPos(width/10, height*9/10);
  188. fLine1.setEndPos(width*9/10, width/10);
  189. fLine2.setStartPos(width*4/10, height*9/10);
  190. fLine2.setEndPos(width*9/10, height*4/10);
  191. fLine3.setStartPos(width*7/10, height*9/10);
  192. fLine3.setEndPos(width*9/10, height*7/10);
  193. }
  194. bool onMouse(int button, bool press, int x, int y) override
  195. {
  196. if (button != 1)
  197. return false;
  198. if (fMouseUnder)
  199. {
  200. if (! press)
  201. fMouseUnder = false;
  202. return true;
  203. }
  204. if (! contains(x, y))
  205. return false;
  206. if (! press)
  207. return false;
  208. fLastX = x+getX();
  209. fLastY = y+getY();
  210. fMouseUnder = true;
  211. return true;
  212. }
  213. bool onMotion(int x, int y) override
  214. {
  215. if (! fMouseUnder)
  216. return false;
  217. x += getX();
  218. y += getY();
  219. const int movedX = x - fLastX;
  220. const int movedY = y - fLastY;
  221. fLastX = x;
  222. fLastY = y;
  223. d_stdout("Moved %i, %i", movedX, movedY);
  224. Size<uint> size(getParentWindow().getSize());
  225. size += Size<uint>(movedX, movedY);
  226. getParentWindow().setSize(size);
  227. repaint();
  228. return true;
  229. }
  230. float fColor[4];
  231. bool fMouseUnder;
  232. int fLastX, fLastY;
  233. Line<float> fLine1, fLine2, fLine3;
  234. };
  235. // ------------------------------------------------------
  236. // Our Demo Window
  237. class DemoWindow : public Window,
  238. public LeftSideWidget::Callback
  239. {
  240. public:
  241. DemoWindow(App& app)
  242. : Window(app),
  243. wColor(*this),
  244. wImages(*this),
  245. wRects(*this),
  246. wShapes(*this),
  247. wLeft(*this, this),
  248. wRezHandle(*this),
  249. curWidget(nullptr)
  250. {
  251. wColor.hide();
  252. wImages.hide();
  253. wRects.hide();
  254. wShapes.hide();
  255. wColor.setX(80);
  256. wImages.setX(80);
  257. wRects.setX(80);
  258. wShapes.setX(80);
  259. wLeft.setPos(2, 2);
  260. setSize(600, 500);
  261. setTitle("DGL Demo");
  262. curPageChanged(0);
  263. }
  264. void onReshape(int width, int height) override
  265. {
  266. Size<int> size(width-80, height);
  267. wColor.setSize(size);
  268. wImages.setSize(size);
  269. wRects.setSize(size);
  270. wShapes.setSize(size);
  271. wLeft.setSize(73, height-4);
  272. wRezHandle.setX(width-wRezHandle.getWidth());
  273. wRezHandle.setY(height-wRezHandle.getHeight());
  274. Window::onReshape(width, height);
  275. }
  276. protected:
  277. void curPageChanged(int curPage) override
  278. {
  279. if (curWidget != nullptr)
  280. {
  281. curWidget->hide();
  282. curWidget = nullptr;
  283. }
  284. switch (curPage)
  285. {
  286. case 0:
  287. curWidget = &wColor;
  288. break;
  289. case 1:
  290. curWidget = &wImages;
  291. break;
  292. case 2:
  293. curWidget = &wRects;
  294. break;
  295. case 3:
  296. curWidget = &wShapes;
  297. break;
  298. }
  299. if (curWidget != nullptr)
  300. curWidget->show();
  301. }
  302. private:
  303. ExampleColorWidget wColor;
  304. ExampleImagesWidget wImages;
  305. ExampleRectanglesWidget wRects;
  306. ExampleShapesWidget wShapes;
  307. LeftSideWidget wLeft;
  308. ResizeHandle wRezHandle;
  309. Widget* curWidget;
  310. };
  311. // ------------------------------------------------------
  312. // main entry point
  313. int main()
  314. {
  315. App app;
  316. DemoWindow win(app);
  317. win.show();
  318. app.exec();
  319. return 0;
  320. }
  321. // ------------------------------------------------------