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.

322 lines
9.4KB

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