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.

379 lines
9.2KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2015 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 "NanoWidgets.hpp"
  19. #include "StandaloneWindow.hpp"
  20. #include "extra/ScopedPointer.hpp"
  21. #include "extra/String.hpp"
  22. #include "src/nanovg/nanovg.h"
  23. #include "src/oui-blendish/blendish.h"
  24. // ------------------------------------------------------
  25. // use namespace
  26. USE_NAMESPACE_DISTRHO;
  27. USE_NAMESPACE_DGL;
  28. // ------------------------------------------------------
  29. // Test
  30. class BlendishCommon : public NanoWidget
  31. {
  32. public:
  33. enum State {
  34. kStateDefault = 0, // not interacting
  35. kStateHover = 1, // the mouse is hovering over the control
  36. kStateActive = 2 // the widget is activated (pressed) or in an active state (toggled)
  37. };
  38. BlendishCommon(NanoWidget* groupWidget)
  39. : NanoWidget(groupWidget),
  40. fState(kStateDefault)
  41. {
  42. /*
  43. NVGcontext* const context(getContext());
  44. if (nvgFindFont(context, "__dpf_blendish__") < 0)
  45. {
  46. bndSetFont(nvgCreateFont(context, "__dpf_blendish__", "./blendish_res/DejaVuSans.ttf"));
  47. bndSetIconImage(nvgCreateImage(context, "./blendish_res/blender_icons16.png", 0));
  48. }*/
  49. setSize(250, BND_WIDGET_HEIGHT);
  50. }
  51. State getCurrentState() const noexcept
  52. {
  53. return fState;
  54. }
  55. protected:
  56. bool onMouse(const MouseEvent& e)
  57. {
  58. if (! e.press)
  59. return false;
  60. if (! contains(e.pos))
  61. return false;
  62. if (fState == kStateActive)
  63. fState = kStateHover;
  64. else
  65. fState = kStateActive;
  66. repaint();
  67. return true;
  68. }
  69. bool onMotion(const MotionEvent& e)
  70. {
  71. if (! contains(e.pos))
  72. {
  73. if (fState == kStateHover)
  74. {
  75. fState = kStateDefault;
  76. repaint();
  77. return true;
  78. }
  79. return false;
  80. }
  81. if (fState == kStateDefault)
  82. {
  83. fState = kStateHover;
  84. repaint();
  85. }
  86. return true;
  87. }
  88. private:
  89. State fState;
  90. };
  91. // ------------------------------------------------------
  92. // Test
  93. class BlendishLabel : public BlendishCommon
  94. {
  95. public:
  96. BlendishLabel(NanoWidget* groupWidget)
  97. : BlendishCommon(groupWidget),
  98. fIconId(-1),
  99. fText()
  100. {
  101. setText("this is a label");
  102. //_updateBounds();
  103. }
  104. int getIconId() const noexcept
  105. {
  106. return fIconId;
  107. }
  108. void setIconId(int iconId) noexcept
  109. {
  110. if (fIconId == iconId)
  111. return;
  112. fIconId = iconId;
  113. _updateBounds();
  114. repaint();
  115. }
  116. const char* getText() const noexcept
  117. {
  118. return fText;
  119. }
  120. void setText(const char* text) noexcept
  121. {
  122. if (fText == text)
  123. return;
  124. fText = text;
  125. _updateBounds();
  126. repaint();
  127. }
  128. protected:
  129. void onNanoDisplay() override
  130. {
  131. bndLabel(getContext(),
  132. getAbsoluteX(), getAbsoluteY(), getWidth(), getHeight(),
  133. fIconId, fText);
  134. }
  135. private:
  136. int fIconId;
  137. String fText;
  138. void _updateBounds()
  139. {
  140. const float width = bndLabelWidth (getContext(), fIconId, fText);
  141. const float height = bndLabelHeight(getContext(), fIconId, fText, width);
  142. setSize(width, height);
  143. }
  144. };
  145. class BlendishToolButton : public BlendishCommon
  146. {
  147. public:
  148. BlendishToolButton(NanoWidget* groupWidget)
  149. : BlendishCommon(groupWidget) {}
  150. protected:
  151. void onNanoDisplay() override
  152. {
  153. bndToolButton(getContext(),
  154. getAbsoluteX(), getAbsoluteY(), getWidth(), getHeight(),
  155. 0, static_cast<BNDwidgetState>(getCurrentState()), BND_ICON_SOUND,
  156. "this is a tool-button [sound]");
  157. }
  158. };
  159. class BlendishPushButton: public BlendishCommon
  160. {
  161. public:
  162. BlendishPushButton(NanoWidget* groupWidget)
  163. : BlendishCommon(groupWidget) {}
  164. protected:
  165. void onNanoDisplay() override
  166. {
  167. bndRadioButton(getContext(),
  168. getAbsoluteX(), getAbsoluteY(), getWidth(), getHeight(),
  169. 0, static_cast<BNDwidgetState>(getCurrentState()), BND_ICON_NEW,
  170. "this is a push-button [file->new]");
  171. }
  172. };
  173. // text field here
  174. class BlendishCheckbox : public BlendishCommon
  175. {
  176. public:
  177. BlendishCheckbox(NanoWidget* groupWidget)
  178. : BlendishCommon(groupWidget) {}
  179. protected:
  180. void onNanoDisplay() override
  181. {
  182. bndOptionButton(getContext(),
  183. getAbsoluteX(), getAbsoluteY(), getWidth(), getHeight(),
  184. static_cast<BNDwidgetState>(getCurrentState()),
  185. "this is a checkbox");
  186. }
  187. };
  188. class BlendishComboBox : public BlendishCommon
  189. {
  190. public:
  191. BlendishComboBox(NanoWidget* groupWidget)
  192. : BlendishCommon(groupWidget) {}
  193. protected:
  194. void onNanoDisplay() override
  195. {
  196. bndChoiceButton(getContext(),
  197. getAbsoluteX(), getAbsoluteY(), getWidth(), getHeight(),
  198. 0, static_cast<BNDwidgetState>(getCurrentState()), BND_ICON_NEW,
  199. "this is a combobox");
  200. // bndMenuBackground
  201. // bndMenuLabel
  202. // bndMenuItem
  203. }
  204. };
  205. class BlendishColorButton : public BlendishCommon
  206. {
  207. public:
  208. BlendishColorButton(NanoWidget* groupWidget)
  209. : BlendishCommon(groupWidget) {}
  210. protected:
  211. void onNanoDisplay() override
  212. {
  213. bndColorButton(getContext(),
  214. getAbsoluteX(), getAbsoluteY(), getWidth(), getHeight(),
  215. BND_DEFAULT,
  216. Color::fromHTML("#132487"));
  217. }
  218. };
  219. // bndNumberField
  220. class BlendishSlider : public BlendishCommon
  221. {
  222. public:
  223. BlendishSlider(NanoWidget* groupWidget)
  224. : BlendishCommon(groupWidget) {}
  225. protected:
  226. void onNanoDisplay() override
  227. {
  228. bndSlider(getContext(),
  229. getAbsoluteX(), getAbsoluteY(), getWidth(), getHeight(),
  230. 0, static_cast<BNDwidgetState>(getCurrentState()),
  231. 0.25f, "this is a slider", "and value");
  232. }
  233. };
  234. class BlendishScrollBar : public BlendishCommon
  235. {
  236. public:
  237. BlendishScrollBar(NanoWidget* groupWidget, bool horizontal)
  238. : BlendishCommon(groupWidget)
  239. {
  240. if (horizontal)
  241. setSize(250, BND_SCROLLBAR_HEIGHT);
  242. else
  243. setSize(BND_SCROLLBAR_WIDTH, 200);
  244. }
  245. protected:
  246. void onNanoDisplay() override
  247. {
  248. bndScrollBar(getContext(),
  249. getAbsoluteX(), getAbsoluteY(), getWidth(), getHeight(),
  250. static_cast<BNDwidgetState>(getCurrentState()),
  251. 0.25f, 0.5f);
  252. }
  253. };
  254. // bndTooltipBackground
  255. // bndNodePort
  256. // bndNodeWire
  257. // bndColoredNodeWire
  258. // bndNodeBackground
  259. // bndSplitterWidgets
  260. // bndJoinAreaOverlay
  261. // ------------------------------------------------------
  262. // Test
  263. class TestWidget : public NanoWidget
  264. {
  265. public:
  266. TestWidget(Window& parent)
  267. : NanoWidget(parent, NanoVG::CREATE_ANTIALIAS|NanoVG::CREATE_STENCIL_STROKES),
  268. w1_R(this, "this is a real button", BND_ICON_ALIGN),
  269. w0(this),
  270. w2(this),
  271. w3(this),
  272. w4(this),
  273. w5(this),
  274. w6(this),
  275. w7(this, true),
  276. w7b(this, false)
  277. {
  278. w1_R.setAbsolutePos(10, 10+25*1);
  279. w0.setAbsolutePos(10, 10+25*0);
  280. w2.setAbsolutePos(10, 10+25*2);
  281. w3.setAbsolutePos(10, 10+25*3);
  282. w4.setAbsolutePos(10, 10+25*4);
  283. w5.setAbsolutePos(10, 10+25*5);
  284. w6.setAbsolutePos(10, 10+25*6);
  285. w7.setAbsolutePos(10, 10+25*7);
  286. //w8.setAbsolutePos(10, 10+25*8);
  287. //w9.setAbsolutePos(10, 10+25*9);
  288. w7b.setAbsolutePos(470, 10);
  289. }
  290. protected:
  291. void onNanoDisplay() override
  292. {
  293. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  294. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  295. }
  296. private:
  297. BlendishButton w1_R;
  298. BlendishLabel w0;
  299. BlendishPushButton w2;
  300. BlendishCheckbox w3;
  301. BlendishComboBox w4;
  302. BlendishColorButton w5;
  303. BlendishSlider w6;
  304. BlendishScrollBar w7;
  305. BlendishScrollBar w7b;
  306. };
  307. // ------------------------------------------------------
  308. // main entry point
  309. int main()
  310. {
  311. StandaloneWindow win;
  312. TestWidget w(win);
  313. win.setSize(500, 500);
  314. win.setTitle("Blendish");
  315. win.exec();
  316. return 0;
  317. }
  318. // ------------------------------------------------------