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.

250 lines
7.0KB

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