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.

305 lines
9.6KB

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