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.

210 lines
5.9KB

  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(kUIWidth, kUIHeight)
  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. * DSP/Plugin Callbacks */
  43. /**
  44. A parameter has changed on the plugin side.
  45. This is called by the host to inform the UI about parameter changes.
  46. */
  47. void parameterChanged(uint32_t index, float value) override
  48. {
  49. // update our grid state to match the plugin side
  50. fParamGrid[index] = (value > 0.5f);
  51. // trigger repaint
  52. repaint();
  53. }
  54. /* --------------------------------------------------------------------------------------------------------
  55. * Widget Callbacks */
  56. /**
  57. The OpenGL drawing function.
  58. This UI will draw a 3x3 grid, with on/off states according to plugin parameters.
  59. */
  60. void onDisplay() override
  61. {
  62. Rectangle<int> r;
  63. r.setWidth(kUIWidth/3 - 6);
  64. r.setHeight(kUIHeight/3 - 6);
  65. // draw left, center and right columns
  66. for (int i=0; i<3; ++i)
  67. {
  68. r.setX(3 + i*kUIWidth/3);
  69. // top
  70. r.setY(3);
  71. if (fParamGrid[0+i])
  72. glColor3f(0.8f, 0.5f, 0.3f);
  73. else
  74. glColor3f(0.3f, 0.5f, 0.8f);
  75. r.draw();
  76. // middle
  77. r.setY(3 + kUIHeight/3);
  78. if (fParamGrid[3+i])
  79. glColor3f(0.8f, 0.5f, 0.3f);
  80. else
  81. glColor3f(0.3f, 0.5f, 0.8f);
  82. r.draw();
  83. // bottom
  84. r.setY(3 + kUIHeight*2/3);
  85. if (fParamGrid[6+i])
  86. glColor3f(0.8f, 0.5f, 0.3f);
  87. else
  88. glColor3f(0.3f, 0.5f, 0.8f);
  89. r.draw();
  90. }
  91. }
  92. /**
  93. Mouse press event.
  94. This UI will de/activate blocks when you click them and reports it as a parameter change to the plugin.
  95. */
  96. bool onMouse(const MouseEvent& ev) override
  97. {
  98. // Test for left-clicked + pressed first.
  99. if (ev.button != 1 || ! ev.press)
  100. return false;
  101. Rectangle<int> r;
  102. r.setWidth(kUIWidth/3 - 6);
  103. r.setHeight(kUIHeight/3 - 6);
  104. // handle left, center and right columns
  105. for (int i=0; i<3; ++i)
  106. {
  107. r.setX(3 + i*kUIWidth/3);
  108. // top
  109. r.setY(3);
  110. if (r.contains(ev.pos))
  111. {
  112. // parameter index that this block applies to
  113. const uint32_t index = 0+i;
  114. // invert block state
  115. fParamGrid[index] = !fParamGrid[index];
  116. // report change to host (and thus plugin)
  117. setParameterValue(index, fParamGrid[index] ? 1.0f : 0.0f);
  118. // trigger repaint
  119. repaint();
  120. break;
  121. }
  122. // middle
  123. r.setY(3 + kUIHeight/3);
  124. if (r.contains(ev.pos))
  125. {
  126. // same as before
  127. const uint32_t index = 3+i;
  128. fParamGrid[index] = !fParamGrid[index];
  129. setParameterValue(index, fParamGrid[index] ? 1.0f : 0.0f);
  130. repaint();
  131. break;
  132. }
  133. // bottom
  134. r.setY(3 + kUIHeight*2/3);
  135. if (r.contains(ev.pos))
  136. {
  137. // same as before
  138. const uint32_t index = 6+i;
  139. fParamGrid[index] = !fParamGrid[index];
  140. setParameterValue(index, fParamGrid[index] ? 1.0f : 0.0f);
  141. repaint();
  142. break;
  143. }
  144. }
  145. return true;
  146. }
  147. // -------------------------------------------------------------------------------------------------------
  148. private:
  149. /**
  150. Our parameters used to display the grid on/off states.
  151. They match the parameters on the plugin side, but here we define them as booleans.
  152. */
  153. bool fParamGrid[9];
  154. /**
  155. Set our UI class as non-copyable and add a leak detector just in case.
  156. */
  157. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExampleUIParameters)
  158. };
  159. /* ------------------------------------------------------------------------------------------------------------
  160. * UI entry point, called by DPF to create a new UI instance. */
  161. UI* createUI()
  162. {
  163. return new ExampleUIParameters();
  164. }
  165. // -----------------------------------------------------------------------------------------------------------
  166. END_NAMESPACE_DISTRHO