DPF Plugin examples
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.

223 lines
6.2KB

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