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.

338 lines
11KB

  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. inline String getFormatSuffix (const AudioProcessor* plugin)
  21. {
  22. const auto format = [plugin]()
  23. {
  24. if (auto* instance = dynamic_cast<const AudioPluginInstance*> (plugin))
  25. return instance->getPluginDescription().pluginFormatName;
  26. return String();
  27. }();
  28. return format.isNotEmpty() ? (" (" + format + ")") : format;
  29. }
  30. class PluginGraph;
  31. /**
  32. A window that shows a log of parameter change messages sent by the plugin.
  33. */
  34. class PluginDebugWindow : public AudioProcessorEditor,
  35. public AudioProcessorParameter::Listener,
  36. public ListBoxModel,
  37. public AsyncUpdater
  38. {
  39. public:
  40. PluginDebugWindow (AudioProcessor& proc)
  41. : AudioProcessorEditor (proc), audioProc (proc)
  42. {
  43. setSize (500, 200);
  44. addAndMakeVisible (list);
  45. for (auto* p : audioProc.getParameters())
  46. p->addListener (this);
  47. log.add ("Parameter debug log started");
  48. }
  49. ~PluginDebugWindow() override
  50. {
  51. for (auto* p : audioProc.getParameters())
  52. p->removeListener (this);
  53. }
  54. void parameterValueChanged (int parameterIndex, float newValue) override
  55. {
  56. auto* param = audioProc.getParameters()[parameterIndex];
  57. auto value = param->getCurrentValueAsText().quoted() + " (" + String (newValue, 4) + ")";
  58. appendToLog ("parameter change", *param, value);
  59. }
  60. void parameterGestureChanged (int parameterIndex, bool gestureIsStarting) override
  61. {
  62. auto* param = audioProc.getParameters()[parameterIndex];
  63. appendToLog ("gesture", *param, gestureIsStarting ? "start" : "end");
  64. }
  65. private:
  66. void appendToLog (StringRef action, AudioProcessorParameter& param, StringRef value)
  67. {
  68. String entry (action + " " + param.getName (30).quoted() + " [" + String (param.getParameterIndex()) + "]: " + value);
  69. {
  70. ScopedLock lock (pendingLogLock);
  71. pendingLogEntries.add (entry);
  72. }
  73. triggerAsyncUpdate();
  74. }
  75. void resized() override
  76. {
  77. list.setBounds(getLocalBounds());
  78. }
  79. int getNumRows() override
  80. {
  81. return log.size();
  82. }
  83. void paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool) override
  84. {
  85. g.setColour (getLookAndFeel().findColour (TextEditor::textColourId));
  86. if (isPositiveAndBelow (rowNumber, log.size()))
  87. g.drawText (log[rowNumber], Rectangle<int> { 0, 0, width, height }, Justification::left, true);
  88. }
  89. void handleAsyncUpdate() override
  90. {
  91. if (log.size() > logSizeTrimThreshold)
  92. log.removeRange (0, log.size() - maxLogSize);
  93. {
  94. ScopedLock lock (pendingLogLock);
  95. log.addArray (pendingLogEntries);
  96. pendingLogEntries.clear();
  97. }
  98. list.updateContent();
  99. list.scrollToEnsureRowIsOnscreen (log.size() - 1);
  100. }
  101. constexpr static const int maxLogSize = 300;
  102. constexpr static const int logSizeTrimThreshold = 400;
  103. ListBox list { "Log", this };
  104. StringArray log;
  105. StringArray pendingLogEntries;
  106. CriticalSection pendingLogLock;
  107. AudioProcessor& audioProc;
  108. };
  109. //==============================================================================
  110. /**
  111. A desktop window containing a plugin's GUI.
  112. */
  113. class PluginWindow : public DocumentWindow
  114. {
  115. public:
  116. enum class Type
  117. {
  118. normal = 0,
  119. generic,
  120. programs,
  121. audioIO,
  122. debug,
  123. numTypes
  124. };
  125. PluginWindow (AudioProcessorGraph::Node* n, Type t, OwnedArray<PluginWindow>& windowList)
  126. : DocumentWindow (n->getProcessor()->getName() + getFormatSuffix (n->getProcessor()),
  127. LookAndFeel::getDefaultLookAndFeel().findColour (ResizableWindow::backgroundColourId),
  128. DocumentWindow::minimiseButton | DocumentWindow::closeButton),
  129. activeWindowList (windowList),
  130. node (n), type (t)
  131. {
  132. setSize (400, 300);
  133. if (auto* ui = createProcessorEditor (*node->getProcessor(), type))
  134. {
  135. setContentOwned (ui, true);
  136. setResizable (ui->isResizable(), false);
  137. }
  138. #if JUCE_IOS || JUCE_ANDROID
  139. auto screenBounds = Desktop::getInstance().getDisplays().getTotalBounds (true).toFloat();
  140. auto scaleFactor = jmin ((screenBounds.getWidth() - 50) / getWidth(), (screenBounds.getHeight() - 50) / getHeight());
  141. if (scaleFactor < 1.0f)
  142. setSize ((int) (getWidth() * scaleFactor), (int) (getHeight() * scaleFactor));
  143. setTopLeftPosition (20, 20);
  144. #else
  145. setTopLeftPosition (node->properties.getWithDefault (getLastXProp (type), Random::getSystemRandom().nextInt (500)),
  146. node->properties.getWithDefault (getLastYProp (type), Random::getSystemRandom().nextInt (500)));
  147. #endif
  148. node->properties.set (getOpenProp (type), true);
  149. setVisible (true);
  150. }
  151. ~PluginWindow() override
  152. {
  153. clearContentComponent();
  154. }
  155. void moved() override
  156. {
  157. node->properties.set (getLastXProp (type), getX());
  158. node->properties.set (getLastYProp (type), getY());
  159. }
  160. void closeButtonPressed() override
  161. {
  162. node->properties.set (getOpenProp (type), false);
  163. activeWindowList.removeObject (this);
  164. }
  165. static String getLastXProp (Type type) { return "uiLastX_" + getTypeName (type); }
  166. static String getLastYProp (Type type) { return "uiLastY_" + getTypeName (type); }
  167. static String getOpenProp (Type type) { return "uiopen_" + getTypeName (type); }
  168. OwnedArray<PluginWindow>& activeWindowList;
  169. const AudioProcessorGraph::Node::Ptr node;
  170. const Type type;
  171. BorderSize<int> getBorderThickness() override
  172. {
  173. #if JUCE_IOS || JUCE_ANDROID
  174. const int border = 10;
  175. return { border, border, border, border };
  176. #else
  177. return DocumentWindow::getBorderThickness();
  178. #endif
  179. }
  180. private:
  181. float getDesktopScaleFactor() const override { return 1.0f; }
  182. static AudioProcessorEditor* createProcessorEditor (AudioProcessor& processor,
  183. PluginWindow::Type type)
  184. {
  185. if (type == PluginWindow::Type::normal)
  186. {
  187. if (processor.hasEditor())
  188. if (auto* ui = processor.createEditorIfNeeded())
  189. return ui;
  190. type = PluginWindow::Type::generic;
  191. }
  192. if (type == PluginWindow::Type::generic) return new GenericAudioProcessorEditor (processor);
  193. if (type == PluginWindow::Type::programs) return new ProgramAudioProcessorEditor (processor);
  194. if (type == PluginWindow::Type::audioIO) return new IOConfigurationWindow (processor);
  195. if (type == PluginWindow::Type::debug) return new PluginDebugWindow (processor);
  196. jassertfalse;
  197. return {};
  198. }
  199. static String getTypeName (Type type)
  200. {
  201. switch (type)
  202. {
  203. case Type::normal: return "Normal";
  204. case Type::generic: return "Generic";
  205. case Type::programs: return "Programs";
  206. case Type::audioIO: return "IO";
  207. case Type::debug: return "Debug";
  208. case Type::numTypes:
  209. default: return {};
  210. }
  211. }
  212. //==============================================================================
  213. struct ProgramAudioProcessorEditor : public AudioProcessorEditor
  214. {
  215. ProgramAudioProcessorEditor (AudioProcessor& p) : AudioProcessorEditor (p)
  216. {
  217. setOpaque (true);
  218. addAndMakeVisible (panel);
  219. Array<PropertyComponent*> programs;
  220. auto numPrograms = p.getNumPrograms();
  221. int totalHeight = 0;
  222. for (int i = 0; i < numPrograms; ++i)
  223. {
  224. auto name = p.getProgramName (i).trim();
  225. if (name.isEmpty())
  226. name = "Unnamed";
  227. auto pc = new PropertyComp (name, p);
  228. programs.add (pc);
  229. totalHeight += pc->getPreferredHeight();
  230. }
  231. panel.addProperties (programs);
  232. setSize (400, jlimit (25, 400, totalHeight));
  233. }
  234. void paint (Graphics& g) override
  235. {
  236. g.fillAll (Colours::grey);
  237. }
  238. void resized() override
  239. {
  240. panel.setBounds (getLocalBounds());
  241. }
  242. private:
  243. struct PropertyComp : public PropertyComponent,
  244. private AudioProcessorListener
  245. {
  246. PropertyComp (const String& name, AudioProcessor& p) : PropertyComponent (name), owner (p)
  247. {
  248. owner.addListener (this);
  249. }
  250. ~PropertyComp() override
  251. {
  252. owner.removeListener (this);
  253. }
  254. void refresh() override {}
  255. void audioProcessorChanged (AudioProcessor*, const ChangeDetails&) override {}
  256. void audioProcessorParameterChanged (AudioProcessor*, int, float) override {}
  257. AudioProcessor& owner;
  258. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertyComp)
  259. };
  260. PropertyPanel panel;
  261. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProgramAudioProcessorEditor)
  262. };
  263. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PluginWindow)
  264. };