DISTRHO Plugin Framework
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.

285 lines
8.3KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2026 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. #include "DistrhoUI.hpp"
  17. #include "Color.hpp"
  18. START_NAMESPACE_DISTRHO
  19. /**
  20. We need a few classes from DGL.
  21. */
  22. using DGL_NAMESPACE::Color;
  23. using DGL_NAMESPACE::GraphicsContext;
  24. using DGL_NAMESPACE::Rectangle;
  25. // -----------------------------------------------------------------------------------------------------------
  26. class ExampleUIParameters : public UI
  27. {
  28. public:
  29. /* constructor */
  30. ExampleUIParameters()
  31. : UI()
  32. {
  33. /**
  34. Initialize all our parameters to their defaults.
  35. In this example all default values are false, so we can simply zero them.
  36. */
  37. std::memset(fParamGrid, 0, sizeof(bool)*9);
  38. // set minimum UI size
  39. const double scaleFactor = getScaleFactor();
  40. setGeometryConstraints(DISTRHO_UI_DEFAULT_WIDTH * scaleFactor, DISTRHO_UI_DEFAULT_HEIGHT * scaleFactor, true);
  41. }
  42. protected:
  43. /* --------------------------------------------------------------------------------------------------------
  44. * DSP/Plugin Callbacks */
  45. /**
  46. A parameter has changed on the plugin side.
  47. This is called by the host to inform the UI about parameter changes.
  48. */
  49. void parameterChanged(uint32_t index, float value) override
  50. {
  51. // update our grid state to match the plugin side
  52. fParamGrid[index] = (value > 0.5f);
  53. // trigger repaint
  54. repaint();
  55. }
  56. /**
  57. A program has been loaded on the plugin side.
  58. This is called by the host to inform the UI about program changes.
  59. */
  60. void programLoaded(uint32_t index) override
  61. {
  62. switch (index)
  63. {
  64. case 0:
  65. fParamGrid[0] = false;
  66. fParamGrid[1] = false;
  67. fParamGrid[2] = false;
  68. fParamGrid[3] = false;
  69. fParamGrid[4] = false;
  70. fParamGrid[5] = false;
  71. fParamGrid[6] = false;
  72. fParamGrid[7] = false;
  73. fParamGrid[8] = false;
  74. break;
  75. case 1:
  76. fParamGrid[0] = true;
  77. fParamGrid[1] = true;
  78. fParamGrid[2] = false;
  79. fParamGrid[3] = false;
  80. fParamGrid[4] = true;
  81. fParamGrid[5] = true;
  82. fParamGrid[6] = true;
  83. fParamGrid[7] = false;
  84. fParamGrid[8] = true;
  85. break;
  86. }
  87. repaint();
  88. }
  89. /* --------------------------------------------------------------------------------------------------------
  90. * Widget Callbacks */
  91. /**
  92. The OpenGL drawing function.
  93. This UI will draw a 3x3 grid, with on/off states according to plugin parameters.
  94. */
  95. void onDisplay() override
  96. {
  97. const GraphicsContext& context(getGraphicsContext());
  98. const uint width = getWidth();
  99. const uint height = getHeight();
  100. const uint minwh = std::min(width, height);
  101. const uint bgColor = getBackgroundColor();
  102. Rectangle<double> r;
  103. // if host doesn't respect aspect-ratio but supports ui background, draw out-of-bounds color from it
  104. if (width != height && bgColor != 0)
  105. {
  106. const int red = (bgColor >> 24) & 0xff;
  107. const int green = (bgColor >> 16) & 0xff;
  108. const int blue = (bgColor >> 8) & 0xff;
  109. Color(red, green, blue).setFor(context);
  110. if (width > height)
  111. {
  112. r.setPos(height, 0);
  113. r.setSize(width-height, height);
  114. }
  115. else
  116. {
  117. r.setPos(0, width);
  118. r.setSize(width, height-width);
  119. }
  120. r.draw(context);
  121. }
  122. r.setWidth(minwh/3 - 6);
  123. r.setHeight(minwh/3 - 6);
  124. // draw left, center and right columns
  125. for (int i=0; i<3; ++i)
  126. {
  127. r.setX(3 + i*minwh/3);
  128. // top
  129. r.setY(3);
  130. if (fParamGrid[0+i])
  131. Color(0.8f, 0.5f, 0.3f).setFor(context);
  132. else
  133. Color(0.3f, 0.5f, 0.8f).setFor(context);
  134. r.draw(context);
  135. // middle
  136. r.setY(3 + minwh/3);
  137. if (fParamGrid[3+i])
  138. Color(0.8f, 0.5f, 0.3f).setFor(context);
  139. else
  140. Color(0.3f, 0.5f, 0.8f).setFor(context);
  141. r.draw(context);
  142. // bottom
  143. r.setY(3 + minwh*2/3);
  144. if (fParamGrid[6+i])
  145. Color(0.8f, 0.5f, 0.3f).setFor(context);
  146. else
  147. Color(0.3f, 0.5f, 0.8f).setFor(context);
  148. r.draw(context);
  149. }
  150. }
  151. /**
  152. Mouse press event.
  153. This UI will de/activate blocks when you click them and reports it as a parameter change to the plugin.
  154. */
  155. bool onMouse(const MouseEvent& ev) override
  156. {
  157. // Test for left-clicked + pressed first.
  158. if (ev.button != 1 || ! ev.press)
  159. return false;
  160. const uint width = getWidth();
  161. const uint height = getHeight();
  162. const uint minwh = std::min(width, height);
  163. Rectangle<double> r;
  164. r.setWidth(minwh/3 - 6);
  165. r.setHeight(minwh/3 - 6);
  166. // handle left, center and right columns
  167. for (int i=0; i<3; ++i)
  168. {
  169. r.setX(3 + i*minwh/3);
  170. // top
  171. r.setY(3);
  172. if (r.contains(ev.pos))
  173. {
  174. // parameter index that this block applies to
  175. const uint32_t index = 0+i;
  176. // invert block state
  177. fParamGrid[index] = !fParamGrid[index];
  178. // report change to host (and thus plugin)
  179. editParameter(index, true);
  180. setParameterValue(index, fParamGrid[index] ? 1.0f : 0.0f);
  181. editParameter(index, false);
  182. // trigger repaint
  183. repaint();
  184. break;
  185. }
  186. // middle
  187. r.setY(3 + minwh/3);
  188. if (r.contains(ev.pos))
  189. {
  190. // same as before
  191. const uint32_t index = 3+i;
  192. fParamGrid[index] = !fParamGrid[index];
  193. editParameter(index, true);
  194. setParameterValue(index, fParamGrid[index] ? 1.0f : 0.0f);
  195. editParameter(index, false);
  196. repaint();
  197. break;
  198. }
  199. // bottom
  200. r.setY(3 + minwh*2/3);
  201. if (r.contains(ev.pos))
  202. {
  203. // same as before
  204. const uint32_t index = 6+i;
  205. fParamGrid[index] = !fParamGrid[index];
  206. editParameter(index, true);
  207. setParameterValue(index, fParamGrid[index] ? 1.0f : 0.0f);
  208. editParameter(index, false);
  209. repaint();
  210. break;
  211. }
  212. }
  213. return true;
  214. }
  215. // -------------------------------------------------------------------------------------------------------
  216. private:
  217. /**
  218. Our parameters used to display the grid on/off states.
  219. They match the parameters on the plugin side, but here we define them as booleans.
  220. */
  221. bool fParamGrid[9];
  222. /**
  223. Set our UI class as non-copyable and add a leak detector just in case.
  224. */
  225. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExampleUIParameters)
  226. };
  227. /* ------------------------------------------------------------------------------------------------------------
  228. * UI entry point, called by DPF to create a new UI instance. */
  229. UI* createUI()
  230. {
  231. return new ExampleUIParameters();
  232. }
  233. // -----------------------------------------------------------------------------------------------------------
  234. END_NAMESPACE_DISTRHO