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.

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