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.

319 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. setSize (400, 300);
  118. if (auto* ui = createProcessorEditor (*node->getProcessor(), type))
  119. {
  120. setContentOwned (ui, true);
  121. setResizable (ui->isResizable(), false);
  122. }
  123. #if JUCE_IOS || JUCE_ANDROID
  124. auto screenBounds = Desktop::getInstance().getDisplays().getTotalBounds (true).toFloat();
  125. auto scaleFactor = jmin ((screenBounds.getWidth() - 50) / getWidth(), (screenBounds.getHeight() - 50) / getHeight());
  126. if (scaleFactor < 1.0f)
  127. setSize ((int) (getWidth() * scaleFactor), (int) (getHeight() * scaleFactor));
  128. setTopLeftPosition (20, 20);
  129. #else
  130. setTopLeftPosition (node->properties.getWithDefault (getLastXProp (type), Random::getSystemRandom().nextInt (500)),
  131. node->properties.getWithDefault (getLastYProp (type), Random::getSystemRandom().nextInt (500)));
  132. #endif
  133. node->properties.set (getOpenProp (type), true);
  134. setVisible (true);
  135. }
  136. ~PluginWindow() override
  137. {
  138. clearContentComponent();
  139. }
  140. void moved() override
  141. {
  142. node->properties.set (getLastXProp (type), getX());
  143. node->properties.set (getLastYProp (type), getY());
  144. }
  145. void closeButtonPressed() override
  146. {
  147. node->properties.set (getOpenProp (type), false);
  148. activeWindowList.removeObject (this);
  149. }
  150. static String getLastXProp (Type type) { return "uiLastX_" + getTypeName (type); }
  151. static String getLastYProp (Type type) { return "uiLastY_" + getTypeName (type); }
  152. static String getOpenProp (Type type) { return "uiopen_" + getTypeName (type); }
  153. OwnedArray<PluginWindow>& activeWindowList;
  154. const AudioProcessorGraph::Node::Ptr node;
  155. const Type type;
  156. BorderSize<int> getBorderThickness() override
  157. {
  158. #if JUCE_IOS || JUCE_ANDROID
  159. const int border = 10;
  160. return { border, border, border, border };
  161. #else
  162. return DocumentWindow::getBorderThickness();
  163. #endif
  164. }
  165. private:
  166. float getDesktopScaleFactor() const override { return 1.0f; }
  167. static AudioProcessorEditor* createProcessorEditor (AudioProcessor& processor,
  168. PluginWindow::Type type)
  169. {
  170. if (type == PluginWindow::Type::normal)
  171. {
  172. if (processor.hasEditor())
  173. if (auto* ui = processor.createEditorIfNeeded())
  174. return ui;
  175. type = PluginWindow::Type::generic;
  176. }
  177. if (type == PluginWindow::Type::generic) return new GenericAudioProcessorEditor (processor);
  178. if (type == PluginWindow::Type::programs) return new ProgramAudioProcessorEditor (processor);
  179. if (type == PluginWindow::Type::audioIO) return new IOConfigurationWindow (processor);
  180. if (type == PluginWindow::Type::debug) return new PluginDebugWindow (processor);
  181. jassertfalse;
  182. return {};
  183. }
  184. static String getTypeName (Type type)
  185. {
  186. switch (type)
  187. {
  188. case Type::normal: return "Normal";
  189. case Type::generic: return "Generic";
  190. case Type::programs: return "Programs";
  191. case Type::audioIO: return "IO";
  192. case Type::debug: return "Debug";
  193. case Type::numTypes:
  194. default: return {};
  195. }
  196. }
  197. //==============================================================================
  198. struct ProgramAudioProcessorEditor : public AudioProcessorEditor
  199. {
  200. ProgramAudioProcessorEditor (AudioProcessor& p) : AudioProcessorEditor (p)
  201. {
  202. setOpaque (true);
  203. addAndMakeVisible (panel);
  204. Array<PropertyComponent*> programs;
  205. auto numPrograms = p.getNumPrograms();
  206. int totalHeight = 0;
  207. for (int i = 0; i < numPrograms; ++i)
  208. {
  209. auto name = p.getProgramName (i).trim();
  210. if (name.isEmpty())
  211. name = "Unnamed";
  212. auto pc = new PropertyComp (name, p);
  213. programs.add (pc);
  214. totalHeight += pc->getPreferredHeight();
  215. }
  216. panel.addProperties (programs);
  217. setSize (400, jlimit (25, 400, totalHeight));
  218. }
  219. void paint (Graphics& g) override
  220. {
  221. g.fillAll (Colours::grey);
  222. }
  223. void resized() override
  224. {
  225. panel.setBounds (getLocalBounds());
  226. }
  227. private:
  228. struct PropertyComp : public PropertyComponent,
  229. private AudioProcessorListener
  230. {
  231. PropertyComp (const String& name, AudioProcessor& p) : PropertyComponent (name), owner (p)
  232. {
  233. owner.addListener (this);
  234. }
  235. ~PropertyComp() override
  236. {
  237. owner.removeListener (this);
  238. }
  239. void refresh() override {}
  240. void audioProcessorChanged (AudioProcessor*, const ChangeDetails&) override {}
  241. void audioProcessorParameterChanged (AudioProcessor*, int, float) override {}
  242. AudioProcessor& owner;
  243. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertyComp)
  244. };
  245. PropertyPanel panel;
  246. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProgramAudioProcessorEditor)
  247. };
  248. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PluginWindow)
  249. };