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.

317 lines
10KB

  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. setResizable (true, false);
  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. BorderSize<int> getBorderThickness() override
  155. {
  156. #if JUCE_IOS || JUCE_ANDROID
  157. const int border = 10;
  158. return { border, border, border, border };
  159. #else
  160. return DocumentWindow::getBorderThickness();
  161. #endif
  162. }
  163. private:
  164. float getDesktopScaleFactor() const override { return 1.0f; }
  165. static AudioProcessorEditor* createProcessorEditor (AudioProcessor& processor,
  166. PluginWindow::Type type)
  167. {
  168. if (type == PluginWindow::Type::normal)
  169. {
  170. if (processor.hasEditor())
  171. if (auto* ui = processor.createEditorIfNeeded())
  172. return ui;
  173. type = PluginWindow::Type::generic;
  174. }
  175. if (type == PluginWindow::Type::generic) return new GenericAudioProcessorEditor (processor);
  176. if (type == PluginWindow::Type::programs) return new ProgramAudioProcessorEditor (processor);
  177. if (type == PluginWindow::Type::audioIO) return new IOConfigurationWindow (processor);
  178. if (type == PluginWindow::Type::debug) return new PluginDebugWindow (processor);
  179. jassertfalse;
  180. return {};
  181. }
  182. static String getTypeName (Type type)
  183. {
  184. switch (type)
  185. {
  186. case Type::normal: return "Normal";
  187. case Type::generic: return "Generic";
  188. case Type::programs: return "Programs";
  189. case Type::audioIO: return "IO";
  190. case Type::debug: return "Debug";
  191. case Type::numTypes:
  192. default: return {};
  193. }
  194. }
  195. //==============================================================================
  196. struct ProgramAudioProcessorEditor : public AudioProcessorEditor
  197. {
  198. ProgramAudioProcessorEditor (AudioProcessor& p) : AudioProcessorEditor (p)
  199. {
  200. setOpaque (true);
  201. addAndMakeVisible (panel);
  202. Array<PropertyComponent*> programs;
  203. auto numPrograms = p.getNumPrograms();
  204. int totalHeight = 0;
  205. for (int i = 0; i < numPrograms; ++i)
  206. {
  207. auto name = p.getProgramName (i).trim();
  208. if (name.isEmpty())
  209. name = "Unnamed";
  210. auto pc = new PropertyComp (name, p);
  211. programs.add (pc);
  212. totalHeight += pc->getPreferredHeight();
  213. }
  214. panel.addProperties (programs);
  215. setSize (400, jlimit (25, 400, totalHeight));
  216. }
  217. void paint (Graphics& g) override
  218. {
  219. g.fillAll (Colours::grey);
  220. }
  221. void resized() override
  222. {
  223. panel.setBounds (getLocalBounds());
  224. }
  225. private:
  226. struct PropertyComp : public PropertyComponent,
  227. private AudioProcessorListener
  228. {
  229. PropertyComp (const String& name, AudioProcessor& p) : PropertyComponent (name), owner (p)
  230. {
  231. owner.addListener (this);
  232. }
  233. ~PropertyComp() override
  234. {
  235. owner.removeListener (this);
  236. }
  237. void refresh() override {}
  238. void audioProcessorChanged (AudioProcessor*, const ChangeDetails&) override {}
  239. void audioProcessorParameterChanged (AudioProcessor*, int, float) override {}
  240. AudioProcessor& owner;
  241. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertyComp)
  242. };
  243. PropertyPanel panel;
  244. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProgramAudioProcessorEditor)
  245. };
  246. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PluginWindow)
  247. };