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.

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