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.

241 lines
7.0KB

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