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.

282 lines
8.5KB

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