The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

311 lines
9.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. #include "../Filters/FilterIOConfiguration.h"
  21. class FilterGraph;
  22. /**
  23. A window that shows a log of parameter change messagse sent by the plugin.
  24. */
  25. class FilterDebugWindow : public AudioProcessorEditor,
  26. public AudioProcessorParameter::Listener,
  27. public ListBoxModel,
  28. public AsyncUpdater
  29. {
  30. public:
  31. FilterDebugWindow (AudioProcessor& proc)
  32. : AudioProcessorEditor (proc), audioProc (proc)
  33. {
  34. setSize (500, 200);
  35. addAndMakeVisible (list);
  36. for (auto* p : audioProc.getParameters())
  37. p->addListener (this);
  38. log.add ("Parameter debug log started");
  39. }
  40. void parameterValueChanged (int parameterIndex, float newValue) override
  41. {
  42. auto* param = audioProc.getParameters()[parameterIndex];
  43. auto value = param->getCurrentValueAsText().quoted() + " (" + String (newValue, 4) + ")";
  44. appendToLog ("parameter change", *param, value);
  45. }
  46. void parameterGestureChanged (int parameterIndex, bool gestureIsStarting) override
  47. {
  48. auto* param = audioProc.getParameters()[parameterIndex];
  49. appendToLog ("gesture", *param, gestureIsStarting ? "start" : "end");
  50. }
  51. private:
  52. void appendToLog (StringRef action, AudioProcessorParameter& param, StringRef value)
  53. {
  54. String entry (action + " " + param.getName (30).quoted() + " [" + String (param.getParameterIndex()) + "]: " + value);
  55. {
  56. ScopedLock lock (pendingLogLock);
  57. pendingLogEntries.add (entry);
  58. }
  59. triggerAsyncUpdate();
  60. }
  61. void resized() override
  62. {
  63. list.setBounds(getLocalBounds());
  64. }
  65. int getNumRows() override
  66. {
  67. return log.size();
  68. }
  69. void paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool) override
  70. {
  71. g.setColour (getLookAndFeel().findColour (TextEditor::textColourId));
  72. if (isPositiveAndBelow (rowNumber, log.size()))
  73. g.drawText (log[rowNumber], Rectangle<int> { 0, 0, width, height }, Justification::left, true);
  74. }
  75. void handleAsyncUpdate() override
  76. {
  77. if (log.size() > logSizeTrimThreshold)
  78. log.removeRange (0, log.size() - maxLogSize);
  79. {
  80. ScopedLock lock (pendingLogLock);
  81. log.addArray (pendingLogEntries);
  82. pendingLogEntries.clear();
  83. }
  84. list.updateContent();
  85. list.scrollToEnsureRowIsOnscreen (log.size() - 1);
  86. }
  87. constexpr static int maxLogSize = 300;
  88. constexpr static int logSizeTrimThreshold = 400;
  89. ListBox list { "Log", this };
  90. StringArray log;
  91. StringArray pendingLogEntries;
  92. CriticalSection pendingLogLock;
  93. AudioProcessor& audioProc;
  94. };
  95. //==============================================================================
  96. /**
  97. A desktop window containing a plugin's GUI.
  98. */
  99. class PluginWindow : public DocumentWindow
  100. {
  101. public:
  102. enum class Type
  103. {
  104. normal = 0,
  105. generic,
  106. programs,
  107. audioIO,
  108. debug,
  109. numTypes
  110. };
  111. PluginWindow (AudioProcessorGraph::Node* n, Type t, OwnedArray<PluginWindow>& windowList)
  112. : DocumentWindow (n->getProcessor()->getName(),
  113. LookAndFeel::getDefaultLookAndFeel().findColour (ResizableWindow::backgroundColourId),
  114. DocumentWindow::minimiseButton | DocumentWindow::closeButton),
  115. activeWindowList (windowList),
  116. node (n), type (t)
  117. {
  118. setSize (400, 300);
  119. if (auto* ui = createProcessorEditor (*node->getProcessor(), type))
  120. setContentOwned (ui, true);
  121. #if JUCE_IOS || JUCE_ANDROID
  122. auto screenBounds = Desktop::getInstance().getDisplays().getTotalBounds (true).toFloat();
  123. auto scaleFactor = jmin ((screenBounds.getWidth() - 50) / getWidth(), (screenBounds.getHeight() - 50) / getHeight());
  124. if (scaleFactor < 1.0f)
  125. setSize (getWidth() * scaleFactor, getHeight() * scaleFactor);
  126. setTopLeftPosition (20, 20);
  127. #else
  128. setTopLeftPosition (node->properties.getWithDefault (getLastXProp (type), Random::getSystemRandom().nextInt (500)),
  129. node->properties.getWithDefault (getLastYProp (type), Random::getSystemRandom().nextInt (500)));
  130. #endif
  131. node->properties.set (getOpenProp (type), true);
  132. setVisible (true);
  133. }
  134. ~PluginWindow() override
  135. {
  136. clearContentComponent();
  137. }
  138. void moved() override
  139. {
  140. node->properties.set (getLastXProp (type), getX());
  141. node->properties.set (getLastYProp (type), getY());
  142. }
  143. void closeButtonPressed() override
  144. {
  145. node->properties.set (getOpenProp (type), false);
  146. activeWindowList.removeObject (this);
  147. }
  148. static String getLastXProp (Type type) { return "uiLastX_" + getTypeName (type); }
  149. static String getLastYProp (Type type) { return "uiLastY_" + getTypeName (type); }
  150. static String getOpenProp (Type type) { return "uiopen_" + getTypeName (type); }
  151. OwnedArray<PluginWindow>& activeWindowList;
  152. const AudioProcessorGraph::Node::Ptr node;
  153. const Type type;
  154. private:
  155. float getDesktopScaleFactor() const override { return 1.0f; }
  156. static AudioProcessorEditor* createProcessorEditor (AudioProcessor& processor, PluginWindow::Type type)
  157. {
  158. if (type == PluginWindow::Type::normal)
  159. {
  160. if (auto* ui = processor.createEditorIfNeeded())
  161. return ui;
  162. type = PluginWindow::Type::generic;
  163. }
  164. if (type == PluginWindow::Type::generic)
  165. return new GenericAudioProcessorEditor (&processor);
  166. if (type == PluginWindow::Type::programs)
  167. return new ProgramAudioProcessorEditor (processor);
  168. if (type == PluginWindow::Type::audioIO)
  169. return new FilterIOConfigurationWindow (processor);
  170. if (type == PluginWindow::Type::debug)
  171. return new FilterDebugWindow (processor);
  172. jassertfalse;
  173. return {};
  174. }
  175. static String getTypeName (Type type)
  176. {
  177. switch (type)
  178. {
  179. case Type::normal: return "Normal";
  180. case Type::generic: return "Generic";
  181. case Type::programs: return "Programs";
  182. case Type::audioIO: return "IO";
  183. case Type::debug: return "Debug";
  184. default: return {};
  185. }
  186. }
  187. //==============================================================================
  188. struct ProgramAudioProcessorEditor : public AudioProcessorEditor
  189. {
  190. ProgramAudioProcessorEditor (AudioProcessor& p) : AudioProcessorEditor (p)
  191. {
  192. setOpaque (true);
  193. addAndMakeVisible (panel);
  194. Array<PropertyComponent*> programs;
  195. auto numPrograms = p.getNumPrograms();
  196. int totalHeight = 0;
  197. for (int i = 0; i < numPrograms; ++i)
  198. {
  199. auto name = p.getProgramName (i).trim();
  200. if (name.isEmpty())
  201. name = "Unnamed";
  202. auto pc = new PropertyComp (name, p);
  203. programs.add (pc);
  204. totalHeight += pc->getPreferredHeight();
  205. }
  206. panel.addProperties (programs);
  207. setSize (400, jlimit (25, 400, totalHeight));
  208. }
  209. void paint (Graphics& g) override
  210. {
  211. g.fillAll (Colours::grey);
  212. }
  213. void resized() override
  214. {
  215. panel.setBounds (getLocalBounds());
  216. }
  217. private:
  218. struct PropertyComp : public PropertyComponent,
  219. private AudioProcessorListener
  220. {
  221. PropertyComp (const String& name, AudioProcessor& p) : PropertyComponent (name), owner (p)
  222. {
  223. owner.addListener (this);
  224. }
  225. ~PropertyComp() override
  226. {
  227. owner.removeListener (this);
  228. }
  229. void refresh() override {}
  230. void audioProcessorChanged (AudioProcessor*) override {}
  231. void audioProcessorParameterChanged (AudioProcessor*, int, float) override {}
  232. AudioProcessor& owner;
  233. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertyComp)
  234. };
  235. PropertyPanel panel;
  236. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProgramAudioProcessorEditor)
  237. };
  238. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PluginWindow)
  239. };