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.

287 lines
8.8KB

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