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.

245 lines
6.8KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 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. /**
  27. For simplicity this UI will be of constant size.
  28. */
  29. static const int kUIWidth = 512;
  30. static const int kUIHeight = 512;
  31. /* constructor */
  32. ExampleUIParameters()
  33. : UI(kUIWidth, kUIHeight)
  34. {
  35. /**
  36. Initialize all our parameters to their defaults.
  37. In this example all default values are false, so we can simply zero them.
  38. */
  39. std::memset(fParamGrid, 0, sizeof(bool)*9);
  40. }
  41. protected:
  42. /* --------------------------------------------------------------------------------------------------------
  43. * DSP/Plugin Callbacks */
  44. /**
  45. A parameter has changed on the plugin side.
  46. This is called by the host to inform the UI about parameter changes.
  47. */
  48. void parameterChanged(uint32_t index, float value) override
  49. {
  50. // update our grid state to match the plugin side
  51. fParamGrid[index] = (value > 0.5f);
  52. // trigger repaint
  53. repaint();
  54. }
  55. /**
  56. A program has been loaded on the plugin side.
  57. This is called by the host to inform the UI about program changes.
  58. */
  59. void programLoaded(uint32_t index) override
  60. {
  61. switch (index)
  62. {
  63. case 0:
  64. fParamGrid[0] = false;
  65. fParamGrid[1] = false;
  66. fParamGrid[2] = false;
  67. fParamGrid[3] = false;
  68. fParamGrid[4] = false;
  69. fParamGrid[5] = false;
  70. fParamGrid[6] = false;
  71. fParamGrid[7] = false;
  72. fParamGrid[8] = false;
  73. break;
  74. case 1:
  75. fParamGrid[0] = true;
  76. fParamGrid[1] = true;
  77. fParamGrid[2] = false;
  78. fParamGrid[3] = false;
  79. fParamGrid[4] = true;
  80. fParamGrid[5] = true;
  81. fParamGrid[6] = true;
  82. fParamGrid[7] = false;
  83. fParamGrid[8] = true;
  84. break;
  85. }
  86. repaint();
  87. }
  88. /* --------------------------------------------------------------------------------------------------------
  89. * Widget Callbacks */
  90. /**
  91. The OpenGL drawing function.
  92. This UI will draw a 3x3 grid, with on/off states according to plugin parameters.
  93. */
  94. void onDisplay() override
  95. {
  96. Rectangle<int> r;
  97. r.setWidth(kUIWidth/3 - 6);
  98. r.setHeight(kUIHeight/3 - 6);
  99. // draw left, center and right columns
  100. for (int i=0; i<3; ++i)
  101. {
  102. r.setX(3 + i*kUIWidth/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 + kUIHeight/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 + kUIHeight*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. Rectangle<int> r;
  136. r.setWidth(kUIWidth/3 - 6);
  137. r.setHeight(kUIHeight/3 - 6);
  138. // handle left, center and right columns
  139. for (int i=0; i<3; ++i)
  140. {
  141. r.setX(3 + i*kUIWidth/3);
  142. // top
  143. r.setY(3);
  144. if (r.contains(ev.pos))
  145. {
  146. // parameter index that this block applies to
  147. const uint32_t index = 0+i;
  148. // invert block state
  149. fParamGrid[index] = !fParamGrid[index];
  150. // report change to host (and thus plugin)
  151. setParameterValue(index, fParamGrid[index] ? 1.0f : 0.0f);
  152. // trigger repaint
  153. repaint();
  154. break;
  155. }
  156. // middle
  157. r.setY(3 + kUIHeight/3);
  158. if (r.contains(ev.pos))
  159. {
  160. // same as before
  161. const uint32_t index = 3+i;
  162. fParamGrid[index] = !fParamGrid[index];
  163. setParameterValue(index, fParamGrid[index] ? 1.0f : 0.0f);
  164. repaint();
  165. break;
  166. }
  167. // bottom
  168. r.setY(3 + kUIHeight*2/3);
  169. if (r.contains(ev.pos))
  170. {
  171. // same as before
  172. const uint32_t index = 6+i;
  173. fParamGrid[index] = !fParamGrid[index];
  174. setParameterValue(index, fParamGrid[index] ? 1.0f : 0.0f);
  175. repaint();
  176. break;
  177. }
  178. }
  179. return true;
  180. }
  181. // -------------------------------------------------------------------------------------------------------
  182. private:
  183. /**
  184. Our parameters used to display the grid on/off states.
  185. They match the parameters on the plugin side, but here we define them as booleans.
  186. */
  187. bool fParamGrid[9];
  188. /**
  189. Set our UI class as non-copyable and add a leak detector just in case.
  190. */
  191. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExampleUIParameters)
  192. };
  193. /* ------------------------------------------------------------------------------------------------------------
  194. * UI entry point, called by DPF to create a new UI instance. */
  195. UI* createUI()
  196. {
  197. return new ExampleUIParameters();
  198. }
  199. // -----------------------------------------------------------------------------------------------------------
  200. END_NAMESPACE_DISTRHO