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.

261 lines
7.8KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2019 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 "DistrhoPluginInfo.h"
  17. #include "DistrhoUI.hpp"
  18. #include "Window.hpp"
  19. START_NAMESPACE_DISTRHO
  20. // -----------------------------------------------------------------------------------------------------------
  21. class InfoExampleUI : public UI
  22. {
  23. static const uint kInitialWidth = 405;
  24. static const uint kInitialHeight = 256;
  25. public:
  26. InfoExampleUI()
  27. : UI(kInitialWidth, kInitialHeight),
  28. fSampleRate(getSampleRate()),
  29. fScale(1.0f)
  30. {
  31. std::memset(fParameters, 0, sizeof(float)*kParameterCount);
  32. std::memset(fStrBuf, 0, sizeof(char)*(0xff+1));
  33. #ifdef DGL_NO_SHARED_RESOURCES
  34. createFontFromFile("sans", "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf");
  35. #else
  36. loadSharedResources();
  37. #endif
  38. setGeometryConstraints(kInitialWidth, kInitialHeight, true);
  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. fParameters[index] = value;
  50. repaint();
  51. }
  52. /* --------------------------------------------------------------------------------------------------------
  53. * DSP/Plugin Callbacks (optional) */
  54. /**
  55. Optional callback to inform the UI about a sample rate change on the plugin side.
  56. */
  57. void sampleRateChanged(double newSampleRate) override
  58. {
  59. fSampleRate = newSampleRate;
  60. repaint();
  61. }
  62. /* --------------------------------------------------------------------------------------------------------
  63. * Widget Callbacks */
  64. /**
  65. The NanoVG drawing function.
  66. */
  67. void onNanoDisplay() override
  68. {
  69. const float lineHeight = 20 * fScale;
  70. fontSize(15.0f * fScale);
  71. textLineHeight(lineHeight);
  72. float x = 0.0f * fScale;
  73. float y = 15.0f * fScale;
  74. // buffer size
  75. drawLeft(x, y, "Buffer Size:");
  76. drawRight(x, y, getTextBufInt(fParameters[kParameterBufferSize]));
  77. y+=lineHeight;
  78. // sample rate
  79. drawLeft(x, y, "Sample Rate:");
  80. drawRight(x, y, getTextBufFloat(fSampleRate));
  81. y+=lineHeight;
  82. // separator
  83. y+=lineHeight;
  84. // time stuff
  85. drawLeft(x, y, "Playing:");
  86. drawRight(x, y, (fParameters[kParameterTimePlaying] > 0.5f) ? "Yes" : "No");
  87. y+=lineHeight;
  88. drawLeft(x, y, "Frame:");
  89. drawRight(x, y, getTextBufInt(fParameters[kParameterTimeFrame]));
  90. y+=lineHeight;
  91. drawLeft(x, y, "Time:");
  92. drawRight(x, y, getTextBufTime(fParameters[kParameterTimeFrame]));
  93. y+=lineHeight;
  94. // separator
  95. y+=lineHeight;
  96. // param changes
  97. drawLeft(x, y, "Param Changes:", 20);
  98. drawRight(x, y, (fParameters[kParameterCanRequestParameterValueChanges] > 0.5f) ? "Yes" : "No", 40);
  99. y+=lineHeight;
  100. // BBT
  101. x = 200.0f * fScale;
  102. y = 15.0f * fScale;
  103. const bool validBBT(fParameters[kParameterTimeValidBBT] > 0.5f);
  104. drawLeft(x, y, "BBT Valid:");
  105. drawRight(x, y, validBBT ? "Yes" : "No");
  106. y+=lineHeight;
  107. if (! validBBT)
  108. return;
  109. drawLeft(x, y, "Bar:");
  110. drawRight(x, y, getTextBufInt(fParameters[kParameterTimeBar]));
  111. y+=lineHeight;
  112. drawLeft(x, y, "Beat:");
  113. drawRight(x, y, getTextBufInt(fParameters[kParameterTimeBeat]));
  114. y+=lineHeight;
  115. drawLeft(x, y, "Tick:");
  116. drawRight(x, y, getTextBufFloatExtra(fParameters[kParameterTimeTick]));
  117. y+=lineHeight;
  118. drawLeft(x, y, "Bar Start Tick:");
  119. drawRight(x, y, getTextBufFloat(fParameters[kParameterTimeBarStartTick]));
  120. y+=lineHeight;
  121. drawLeft(x, y, "Beats Per Bar:");
  122. drawRight(x, y, getTextBufFloat(fParameters[kParameterTimeBeatsPerBar]));
  123. y+=lineHeight;
  124. drawLeft(x, y, "Beat Type:");
  125. drawRight(x, y, getTextBufFloat(fParameters[kParameterTimeBeatType]));
  126. y+=lineHeight;
  127. drawLeft(x, y, "Ticks Per Beat:");
  128. drawRight(x, y, getTextBufFloat(fParameters[kParameterTimeTicksPerBeat]));
  129. y+=lineHeight;
  130. drawLeft(x, y, "BPM:");
  131. drawRight(x, y, getTextBufFloat(fParameters[kParameterTimeBeatsPerMinute]));
  132. y+=lineHeight;
  133. }
  134. void onResize(const ResizeEvent& ev) override
  135. {
  136. fScale = static_cast<float>(ev.size.getHeight())/static_cast<float>(kInitialHeight);
  137. UI::onResize(ev);
  138. }
  139. // -------------------------------------------------------------------------------------------------------
  140. private:
  141. // Parameters
  142. float fParameters[kParameterCount];
  143. double fSampleRate;
  144. // UI stuff
  145. float fScale;
  146. // temp buf for text
  147. char fStrBuf[0xff+1];
  148. // helpers for putting text into fStrBuf and returning it
  149. const char* getTextBufInt(const int value)
  150. {
  151. std::snprintf(fStrBuf, 0xff, "%i", value);
  152. return fStrBuf;
  153. }
  154. const char* getTextBufFloat(const float value)
  155. {
  156. std::snprintf(fStrBuf, 0xff, "%.1f", value);
  157. return fStrBuf;
  158. }
  159. const char* getTextBufFloatExtra(const float value)
  160. {
  161. std::snprintf(fStrBuf, 0xff, "%.2f", value + 0.001f);
  162. return fStrBuf;
  163. }
  164. const char* getTextBufTime(const uint64_t frame)
  165. {
  166. const uint32_t time = frame / uint64_t(fSampleRate);
  167. const uint32_t secs = time % 60;
  168. const uint32_t mins = (time / 60) % 60;
  169. const uint32_t hrs = (time / 3600) % 60;
  170. std::snprintf(fStrBuf, 0xff, "%02i:%02i:%02i", hrs, mins, secs);
  171. return fStrBuf;
  172. }
  173. // helpers for drawing text
  174. void drawLeft(float x, const float y, const char* const text, const int offset = 0)
  175. {
  176. const float width = (100.0f + offset) * fScale;
  177. x += offset * fScale;
  178. beginPath();
  179. fillColor(200, 200, 200);
  180. textAlign(ALIGN_RIGHT|ALIGN_TOP);
  181. textBox(x, y, width, text);
  182. closePath();
  183. }
  184. void drawRight(float x, const float y, const char* const text, const int offset = 0)
  185. {
  186. const float width = (100.0f + offset) * fScale;
  187. x += offset * fScale;
  188. beginPath();
  189. fillColor(255, 255, 255);
  190. textAlign(ALIGN_LEFT|ALIGN_TOP);
  191. textBox(x + (105 * fScale), y, width, text);
  192. closePath();
  193. }
  194. /**
  195. Set our UI class as non-copyable and add a leak detector just in case.
  196. */
  197. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(InfoExampleUI)
  198. };
  199. /* ------------------------------------------------------------------------------------------------------------
  200. * UI entry point, called by DPF to create a new UI instance. */
  201. UI* createUI()
  202. {
  203. return new InfoExampleUI();
  204. }
  205. // -----------------------------------------------------------------------------------------------------------
  206. END_NAMESPACE_DISTRHO