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.

324 lines
9.3KB

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