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.

365 lines
12KB

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