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.

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