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.

306 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 "../Plugins/IOConfigurationWindow.h"
  21. class PluginGraph;
  22. /**
  23. A window that shows a log of parameter change messages sent by the plugin.
  24. */
  25. class PluginDebugWindow : public AudioProcessorEditor,
  26. public AudioProcessorParameter::Listener,
  27. public ListBoxModel,
  28. public AsyncUpdater
  29. {
  30. public:
  31. PluginDebugWindow (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 const int maxLogSize = 300;
  88. constexpr static const 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 ((int) (getWidth() * scaleFactor), (int) (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,
  157. PluginWindow::Type type)
  158. {
  159. if (type == PluginWindow::Type::normal)
  160. {
  161. if (auto* ui = processor.createEditorIfNeeded())
  162. return ui;
  163. type = PluginWindow::Type::generic;
  164. }
  165. if (type == PluginWindow::Type::generic) return new GenericAudioProcessorEditor (processor);
  166. if (type == PluginWindow::Type::programs) return new ProgramAudioProcessorEditor (processor);
  167. if (type == PluginWindow::Type::audioIO) return new IOConfigurationWindow (processor);
  168. if (type == PluginWindow::Type::debug) return new PluginDebugWindow (processor);
  169. jassertfalse;
  170. return {};
  171. }
  172. static String getTypeName (Type type)
  173. {
  174. switch (type)
  175. {
  176. case Type::normal: return "Normal";
  177. case Type::generic: return "Generic";
  178. case Type::programs: return "Programs";
  179. case Type::audioIO: return "IO";
  180. case Type::debug: return "Debug";
  181. case Type::numTypes:
  182. default: return {};
  183. }
  184. }
  185. //==============================================================================
  186. struct ProgramAudioProcessorEditor : public AudioProcessorEditor
  187. {
  188. ProgramAudioProcessorEditor (AudioProcessor& p) : AudioProcessorEditor (p)
  189. {
  190. setOpaque (true);
  191. addAndMakeVisible (panel);
  192. Array<PropertyComponent*> programs;
  193. auto numPrograms = p.getNumPrograms();
  194. int totalHeight = 0;
  195. for (int i = 0; i < numPrograms; ++i)
  196. {
  197. auto name = p.getProgramName (i).trim();
  198. if (name.isEmpty())
  199. name = "Unnamed";
  200. auto pc = new PropertyComp (name, p);
  201. programs.add (pc);
  202. totalHeight += pc->getPreferredHeight();
  203. }
  204. panel.addProperties (programs);
  205. setSize (400, jlimit (25, 400, totalHeight));
  206. }
  207. void paint (Graphics& g) override
  208. {
  209. g.fillAll (Colours::grey);
  210. }
  211. void resized() override
  212. {
  213. panel.setBounds (getLocalBounds());
  214. }
  215. private:
  216. struct PropertyComp : public PropertyComponent,
  217. private AudioProcessorListener
  218. {
  219. PropertyComp (const String& name, AudioProcessor& p) : PropertyComponent (name), owner (p)
  220. {
  221. owner.addListener (this);
  222. }
  223. ~PropertyComp() override
  224. {
  225. owner.removeListener (this);
  226. }
  227. void refresh() override {}
  228. void audioProcessorChanged (AudioProcessor*) override {}
  229. void audioProcessorParameterChanged (AudioProcessor*, int, float) override {}
  230. AudioProcessor& owner;
  231. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertyComp)
  232. };
  233. PropertyPanel panel;
  234. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProgramAudioProcessorEditor)
  235. };
  236. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PluginWindow)
  237. };