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.

248 lines
6.9KB

  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::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);
  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. Rectangle<int> r;
  96. r.setWidth(width/3 - 6);
  97. r.setHeight(height/3 - 6);
  98. // draw left, center and right columns
  99. for (int i=0; i<3; ++i)
  100. {
  101. r.setX(3 + i*width/3);
  102. // top
  103. r.setY(3);
  104. if (fParamGrid[0+i])
  105. glColor3f(0.8f, 0.5f, 0.3f);
  106. else
  107. glColor3f(0.3f, 0.5f, 0.8f);
  108. r.draw();
  109. // middle
  110. r.setY(3 + height/3);
  111. if (fParamGrid[3+i])
  112. glColor3f(0.8f, 0.5f, 0.3f);
  113. else
  114. glColor3f(0.3f, 0.5f, 0.8f);
  115. r.draw();
  116. // bottom
  117. r.setY(3 + height*2/3);
  118. if (fParamGrid[6+i])
  119. glColor3f(0.8f, 0.5f, 0.3f);
  120. else
  121. glColor3f(0.3f, 0.5f, 0.8f);
  122. r.draw();
  123. }
  124. }
  125. /**
  126. Mouse press event.
  127. This UI will de/activate blocks when you click them and reports it as a parameter change to the plugin.
  128. */
  129. bool onMouse(const MouseEvent& ev) override
  130. {
  131. // Test for left-clicked + pressed first.
  132. if (ev.button != 1 || ! ev.press)
  133. return false;
  134. const uint width = getWidth();
  135. const uint height = getHeight();
  136. Rectangle<int> r;
  137. r.setWidth(width/3 - 6);
  138. r.setHeight(height/3 - 6);
  139. // handle left, center and right columns
  140. for (int i=0; i<3; ++i)
  141. {
  142. r.setX(3 + i*width/3);
  143. // top
  144. r.setY(3);
  145. if (r.contains(ev.pos))
  146. {
  147. // parameter index that this block applies to
  148. const uint32_t index = 0+i;
  149. // invert block state
  150. fParamGrid[index] = !fParamGrid[index];
  151. // report change to host (and thus plugin)
  152. setParameterValue(index, fParamGrid[index] ? 1.0f : 0.0f);
  153. // trigger repaint
  154. repaint();
  155. break;
  156. }
  157. // middle
  158. r.setY(3 + height/3);
  159. if (r.contains(ev.pos))
  160. {
  161. // same as before
  162. const uint32_t index = 3+i;
  163. fParamGrid[index] = !fParamGrid[index];
  164. setParameterValue(index, fParamGrid[index] ? 1.0f : 0.0f);
  165. repaint();
  166. break;
  167. }
  168. // bottom
  169. r.setY(3 + height*2/3);
  170. if (r.contains(ev.pos))
  171. {
  172. // same as before
  173. const uint32_t index = 6+i;
  174. fParamGrid[index] = !fParamGrid[index];
  175. setParameterValue(index, fParamGrid[index] ? 1.0f : 0.0f);
  176. repaint();
  177. break;
  178. }
  179. }
  180. return true;
  181. }
  182. // -------------------------------------------------------------------------------------------------------
  183. private:
  184. /**
  185. Our parameters used to display the grid on/off states.
  186. They match the parameters on the plugin side, but here we define them as booleans.
  187. */
  188. bool fParamGrid[9];
  189. /**
  190. Set our UI class as non-copyable and add a leak detector just in case.
  191. */
  192. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExampleUIParameters)
  193. };
  194. /* ------------------------------------------------------------------------------------------------------------
  195. * UI entry point, called by DPF to create a new UI instance. */
  196. UI* createUI()
  197. {
  198. return new ExampleUIParameters();
  199. }
  200. // -----------------------------------------------------------------------------------------------------------
  201. END_NAMESPACE_DISTRHO