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.

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