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.

288 lines
8.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. class ExampleUIParameters : public UI
  20. {
  21. public:
  22. /**
  23. For simplicity this UI will be of constant size.
  24. */
  25. static const int kUIWidth = 512;
  26. static const int kUIHeight = 512;
  27. /**
  28. Get key name from an index.
  29. */
  30. static const char* getStateKeyFromIndex(const uint32_t index) noexcept
  31. {
  32. switch (index)
  33. {
  34. case 0: return "top-left";
  35. case 1: return "top-center";
  36. case 2: return "top-right";
  37. case 3: return "middle-left";
  38. case 4: return "middle-center";
  39. case 5: return "middle-right";
  40. case 6: return "bottom-left";
  41. case 7: return "bottom-center";
  42. case 8: return "bottom-right";
  43. }
  44. return "unknown";
  45. }
  46. /* constructor */
  47. ExampleUIParameters()
  48. : UI()
  49. {
  50. /**
  51. Initialize the grid to all off per default.
  52. */
  53. std::memset(fParamGrid, 0, sizeof(bool)*9);
  54. setSize(kUIWidth, kUIHeight);
  55. }
  56. protected:
  57. /* --------------------------------------------------------------------------------------------------------
  58. * DSP/Plugin Callbacks */
  59. /**
  60. This plugin has no parameters, so we can safely ignore this.
  61. */
  62. void parameterChanged(uint32_t, float) override {}
  63. /**
  64. A program has been loaded on the plugin side.
  65. This is called by the host to inform the UI about program changes.
  66. */
  67. void programLoaded(uint32_t index) override
  68. {
  69. d_stdout("UI programLoaded %i", index);
  70. switch (index)
  71. {
  72. case 0:
  73. fParamGrid[0] = false;
  74. fParamGrid[1] = false;
  75. fParamGrid[2] = false;
  76. fParamGrid[3] = false;
  77. fParamGrid[4] = false;
  78. fParamGrid[5] = false;
  79. fParamGrid[6] = false;
  80. fParamGrid[7] = false;
  81. fParamGrid[8] = false;
  82. break;
  83. case 1:
  84. fParamGrid[0] = true;
  85. fParamGrid[1] = true;
  86. fParamGrid[2] = false;
  87. fParamGrid[3] = false;
  88. fParamGrid[4] = true;
  89. fParamGrid[5] = true;
  90. fParamGrid[6] = true;
  91. fParamGrid[7] = false;
  92. fParamGrid[8] = true;
  93. break;
  94. }
  95. repaint();
  96. }
  97. /**
  98. A state has changed on the plugin side.
  99. This is called by the host to inform the UI about state changes.
  100. */
  101. void stateChanged(const char* key, const char* value) override
  102. {
  103. const bool valueOnOff = (std::strcmp(value, "true") == 0);
  104. // check which block changed
  105. /**/ if (std::strcmp(key, "top-left") == 0)
  106. fParamGrid[0] = valueOnOff;
  107. else if (std::strcmp(key, "top-center") == 0)
  108. fParamGrid[1] = valueOnOff;
  109. else if (std::strcmp(key, "top-right") == 0)
  110. fParamGrid[2] = valueOnOff;
  111. else if (std::strcmp(key, "middle-left") == 0)
  112. fParamGrid[3] = valueOnOff;
  113. else if (std::strcmp(key, "middle-center") == 0)
  114. fParamGrid[4] = valueOnOff;
  115. else if (std::strcmp(key, "middle-right") == 0)
  116. fParamGrid[5] = valueOnOff;
  117. else if (std::strcmp(key, "bottom-left") == 0)
  118. fParamGrid[6] = valueOnOff;
  119. else if (std::strcmp(key, "bottom-center") == 0)
  120. fParamGrid[7] = valueOnOff;
  121. else if (std::strcmp(key, "bottom-right") == 0)
  122. fParamGrid[8] = valueOnOff;
  123. // trigger repaint
  124. repaint();
  125. }
  126. /* --------------------------------------------------------------------------------------------------------
  127. * Widget Callbacks */
  128. /**
  129. The OpenGL drawing function.
  130. This UI will draw a 3x3 grid, with on/off states according to plugin state.
  131. */
  132. void onDisplay() override
  133. {
  134. Rectangle<int> r;
  135. r.setWidth(kUIWidth/3 - 6);
  136. r.setHeight(kUIHeight/3 - 6);
  137. // draw left, center and right columns
  138. for (int i=0; i<3; ++i)
  139. {
  140. r.setX(3 + i*kUIWidth/3);
  141. // top
  142. r.setY(3);
  143. if (fParamGrid[0+i])
  144. glColor3f(0.8f, 0.5f, 0.3f);
  145. else
  146. glColor3f(0.3f, 0.5f, 0.8f);
  147. r.draw();
  148. // middle
  149. r.setY(3 + kUIHeight/3);
  150. if (fParamGrid[3+i])
  151. glColor3f(0.8f, 0.5f, 0.3f);
  152. else
  153. glColor3f(0.3f, 0.5f, 0.8f);
  154. r.draw();
  155. // bottom
  156. r.setY(3 + kUIHeight*2/3);
  157. if (fParamGrid[6+i])
  158. glColor3f(0.8f, 0.5f, 0.3f);
  159. else
  160. glColor3f(0.3f, 0.5f, 0.8f);
  161. r.draw();
  162. }
  163. }
  164. /**
  165. Mouse press event.
  166. This UI will de/activate blocks when you click them and reports it as a state change to the plugin.
  167. */
  168. bool onMouse(const MouseEvent& ev) override
  169. {
  170. // Test for left-clicked + pressed first.
  171. if (ev.button != 1 || ! ev.press)
  172. return false;
  173. Rectangle<int> r;
  174. r.setWidth(kUIWidth/3 - 6);
  175. r.setHeight(kUIHeight/3 - 6);
  176. // handle left, center and right columns
  177. for (int i=0; i<3; ++i)
  178. {
  179. r.setX(3 + i*kUIWidth/3);
  180. // top
  181. r.setY(3);
  182. if (r.contains(ev.pos))
  183. {
  184. // index that this block applies to
  185. const uint32_t index = 0+i;
  186. // invert block state
  187. fParamGrid[index] = !fParamGrid[index];
  188. // report change to host (and thus plugin)
  189. setState(getStateKeyFromIndex(index), fParamGrid[index] ? "true" : "false");
  190. // trigger repaint
  191. repaint();
  192. break;
  193. }
  194. // middle
  195. r.setY(3 + kUIHeight/3);
  196. if (r.contains(ev.pos))
  197. {
  198. // same as before
  199. const uint32_t index = 3+i;
  200. fParamGrid[index] = !fParamGrid[index];
  201. setState(getStateKeyFromIndex(index), fParamGrid[index] ? "true" : "false");
  202. repaint();
  203. break;
  204. }
  205. // bottom
  206. r.setY(3 + kUIHeight*2/3);
  207. if (r.contains(ev.pos))
  208. {
  209. // same as before
  210. const uint32_t index = 6+i;
  211. fParamGrid[index] = !fParamGrid[index];
  212. setState(getStateKeyFromIndex(index), fParamGrid[index] ? "true" : "false");
  213. repaint();
  214. break;
  215. }
  216. }
  217. return true;
  218. }
  219. // -------------------------------------------------------------------------------------------------------
  220. private:
  221. /**
  222. Our states used to display the grid.
  223. The host does not know about these.
  224. */
  225. bool fParamGrid[9];
  226. /**
  227. Set our UI class as non-copyable and add a leak detector just in case.
  228. */
  229. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExampleUIParameters)
  230. };
  231. /* ------------------------------------------------------------------------------------------------------------
  232. * UI entry point, called by DPF to create a new UI instance. */
  233. UI* createUI()
  234. {
  235. return new ExampleUIParameters();
  236. }
  237. // -----------------------------------------------------------------------------------------------------------
  238. END_NAMESPACE_DISTRHO