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.

1349 lines
43KB

  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. #include <JuceHeader.h>
  19. #include "GraphEditorPanel.h"
  20. #include "../Plugins/InternalPlugins.h"
  21. #include "MainHostWindow.h"
  22. //==============================================================================
  23. #if JUCE_IOS
  24. class AUScanner
  25. {
  26. public:
  27. AUScanner (KnownPluginList& list)
  28. : knownPluginList (list), pool (5)
  29. {
  30. knownPluginList.clearBlacklistedFiles();
  31. paths = formatToScan.getDefaultLocationsToSearch();
  32. startScan();
  33. }
  34. private:
  35. KnownPluginList& knownPluginList;
  36. AudioUnitPluginFormat formatToScan;
  37. std::unique_ptr<PluginDirectoryScanner> scanner;
  38. FileSearchPath paths;
  39. ThreadPool pool;
  40. void startScan()
  41. {
  42. auto deadMansPedalFile = getAppProperties().getUserSettings()
  43. ->getFile().getSiblingFile ("RecentlyCrashedPluginsList");
  44. scanner.reset (new PluginDirectoryScanner (knownPluginList, formatToScan, paths,
  45. true, deadMansPedalFile, true));
  46. for (int i = 5; --i >= 0;)
  47. pool.addJob (new ScanJob (*this), true);
  48. }
  49. bool doNextScan()
  50. {
  51. String pluginBeingScanned;
  52. if (scanner->scanNextFile (true, pluginBeingScanned))
  53. return true;
  54. return false;
  55. }
  56. struct ScanJob : public ThreadPoolJob
  57. {
  58. ScanJob (AUScanner& s) : ThreadPoolJob ("pluginscan"), scanner (s) {}
  59. JobStatus runJob()
  60. {
  61. while (scanner.doNextScan() && ! shouldExit())
  62. {}
  63. return jobHasFinished;
  64. }
  65. AUScanner& scanner;
  66. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScanJob)
  67. };
  68. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AUScanner)
  69. };
  70. #endif
  71. //==============================================================================
  72. struct GraphEditorPanel::PinComponent : public Component,
  73. public SettableTooltipClient
  74. {
  75. PinComponent (GraphEditorPanel& p, AudioProcessorGraph::NodeAndChannel pinToUse, bool isIn)
  76. : panel (p), graph (p.graph), pin (pinToUse), isInput (isIn)
  77. {
  78. if (auto node = graph.graph.getNodeForId (pin.nodeID))
  79. {
  80. String tip;
  81. if (pin.isMIDI())
  82. {
  83. tip = isInput ? "MIDI Input"
  84. : "MIDI Output";
  85. }
  86. else
  87. {
  88. auto& processor = *node->getProcessor();
  89. auto channel = processor.getOffsetInBusBufferForAbsoluteChannelIndex (isInput, pin.channelIndex, busIdx);
  90. if (auto* bus = processor.getBus (isInput, busIdx))
  91. tip = bus->getName() + ": " + AudioChannelSet::getAbbreviatedChannelTypeName (bus->getCurrentLayout().getTypeOfChannel (channel));
  92. else
  93. tip = (isInput ? "Main Input: "
  94. : "Main Output: ") + String (pin.channelIndex + 1);
  95. }
  96. setTooltip (tip);
  97. }
  98. setSize (16, 16);
  99. }
  100. void paint (Graphics& g) override
  101. {
  102. auto w = (float) getWidth();
  103. auto h = (float) getHeight();
  104. Path p;
  105. p.addEllipse (w * 0.25f, h * 0.25f, w * 0.5f, h * 0.5f);
  106. p.addRectangle (w * 0.4f, isInput ? (0.5f * h) : 0.0f, w * 0.2f, h * 0.5f);
  107. auto colour = (pin.isMIDI() ? Colours::red : Colours::green);
  108. g.setColour (colour.withRotatedHue ((float) busIdx / 5.0f));
  109. g.fillPath (p);
  110. }
  111. void mouseDown (const MouseEvent& e) override
  112. {
  113. AudioProcessorGraph::NodeAndChannel dummy { {}, 0 };
  114. panel.beginConnectorDrag (isInput ? dummy : pin,
  115. isInput ? pin : dummy,
  116. e);
  117. }
  118. void mouseDrag (const MouseEvent& e) override
  119. {
  120. panel.dragConnector (e);
  121. }
  122. void mouseUp (const MouseEvent& e) override
  123. {
  124. panel.endDraggingConnector (e);
  125. }
  126. GraphEditorPanel& panel;
  127. PluginGraph& graph;
  128. AudioProcessorGraph::NodeAndChannel pin;
  129. const bool isInput;
  130. int busIdx = 0;
  131. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PinComponent)
  132. };
  133. //==============================================================================
  134. struct GraphEditorPanel::PluginComponent : public Component,
  135. public Timer,
  136. private AudioProcessorParameter::Listener
  137. {
  138. PluginComponent (GraphEditorPanel& p, AudioProcessorGraph::NodeID id) : panel (p), graph (p.graph), pluginID (id)
  139. {
  140. shadow.setShadowProperties (DropShadow (Colours::black.withAlpha (0.5f), 3, { 0, 1 }));
  141. setComponentEffect (&shadow);
  142. if (auto f = graph.graph.getNodeForId (pluginID))
  143. {
  144. if (auto* processor = f->getProcessor())
  145. {
  146. if (auto* bypassParam = processor->getBypassParameter())
  147. bypassParam->addListener (this);
  148. }
  149. }
  150. setSize (150, 60);
  151. }
  152. PluginComponent (const PluginComponent&) = delete;
  153. PluginComponent& operator= (const PluginComponent&) = delete;
  154. ~PluginComponent() override
  155. {
  156. if (auto f = graph.graph.getNodeForId (pluginID))
  157. {
  158. if (auto* processor = f->getProcessor())
  159. {
  160. if (auto* bypassParam = processor->getBypassParameter())
  161. bypassParam->removeListener (this);
  162. }
  163. }
  164. }
  165. void mouseDown (const MouseEvent& e) override
  166. {
  167. originalPos = localPointToGlobal (Point<int>());
  168. toFront (true);
  169. if (isOnTouchDevice())
  170. {
  171. startTimer (750);
  172. }
  173. else
  174. {
  175. if (e.mods.isPopupMenu())
  176. showPopupMenu();
  177. }
  178. }
  179. void mouseDrag (const MouseEvent& e) override
  180. {
  181. if (isOnTouchDevice() && e.getDistanceFromDragStart() > 5)
  182. stopTimer();
  183. if (! e.mods.isPopupMenu())
  184. {
  185. auto pos = originalPos + e.getOffsetFromDragStart();
  186. if (getParentComponent() != nullptr)
  187. pos = getParentComponent()->getLocalPoint (nullptr, pos);
  188. pos += getLocalBounds().getCentre();
  189. graph.setNodePosition (pluginID,
  190. { pos.x / (double) getParentWidth(),
  191. pos.y / (double) getParentHeight() });
  192. panel.updateComponents();
  193. }
  194. }
  195. void mouseUp (const MouseEvent& e) override
  196. {
  197. if (isOnTouchDevice())
  198. {
  199. stopTimer();
  200. callAfterDelay (250, []() { PopupMenu::dismissAllActiveMenus(); });
  201. }
  202. if (e.mouseWasDraggedSinceMouseDown())
  203. {
  204. graph.setChangedFlag (true);
  205. }
  206. else if (e.getNumberOfClicks() == 2)
  207. {
  208. if (auto f = graph.graph.getNodeForId (pluginID))
  209. if (auto* w = graph.getOrCreateWindowFor (f, PluginWindow::Type::normal))
  210. w->toFront (true);
  211. }
  212. }
  213. bool hitTest (int x, int y) override
  214. {
  215. for (auto* child : getChildren())
  216. if (child->getBounds().contains (x, y))
  217. return true;
  218. return x >= 3 && x < getWidth() - 6 && y >= pinSize && y < getHeight() - pinSize;
  219. }
  220. void paint (Graphics& g) override
  221. {
  222. auto boxArea = getLocalBounds().reduced (4, pinSize);
  223. bool isBypassed = false;
  224. if (auto* f = graph.graph.getNodeForId (pluginID))
  225. isBypassed = f->isBypassed();
  226. auto boxColour = findColour (TextEditor::backgroundColourId);
  227. if (isBypassed)
  228. boxColour = boxColour.brighter();
  229. g.setColour (boxColour);
  230. g.fillRect (boxArea.toFloat());
  231. g.setColour (findColour (TextEditor::textColourId));
  232. g.setFont (font);
  233. g.drawFittedText (getName(), boxArea, Justification::centred, 2);
  234. }
  235. void resized() override
  236. {
  237. if (auto f = graph.graph.getNodeForId (pluginID))
  238. {
  239. if (auto* processor = f->getProcessor())
  240. {
  241. for (auto* pin : pins)
  242. {
  243. const bool isInput = pin->isInput;
  244. auto channelIndex = pin->pin.channelIndex;
  245. int busIdx = 0;
  246. processor->getOffsetInBusBufferForAbsoluteChannelIndex (isInput, channelIndex, busIdx);
  247. const int total = isInput ? numIns : numOuts;
  248. const int index = pin->pin.isMIDI() ? (total - 1) : channelIndex;
  249. auto totalSpaces = static_cast<float> (total) + (static_cast<float> (jmax (0, processor->getBusCount (isInput) - 1)) * 0.5f);
  250. auto indexPos = static_cast<float> (index) + (static_cast<float> (busIdx) * 0.5f);
  251. pin->setBounds (proportionOfWidth ((1.0f + indexPos) / (totalSpaces + 1.0f)) - pinSize / 2,
  252. pin->isInput ? 0 : (getHeight() - pinSize),
  253. pinSize, pinSize);
  254. }
  255. }
  256. }
  257. }
  258. Point<float> getPinPos (int index, bool isInput) const
  259. {
  260. for (auto* pin : pins)
  261. if (pin->pin.channelIndex == index && isInput == pin->isInput)
  262. return getPosition().toFloat() + pin->getBounds().getCentre().toFloat();
  263. return {};
  264. }
  265. void update()
  266. {
  267. const AudioProcessorGraph::Node::Ptr f (graph.graph.getNodeForId (pluginID));
  268. jassert (f != nullptr);
  269. numIns = f->getProcessor()->getTotalNumInputChannels();
  270. if (f->getProcessor()->acceptsMidi())
  271. ++numIns;
  272. numOuts = f->getProcessor()->getTotalNumOutputChannels();
  273. if (f->getProcessor()->producesMidi())
  274. ++numOuts;
  275. int w = 100;
  276. int h = 60;
  277. w = jmax (w, (jmax (numIns, numOuts) + 1) * 20);
  278. const int textWidth = font.getStringWidth (f->getProcessor()->getName());
  279. w = jmax (w, 16 + jmin (textWidth, 300));
  280. if (textWidth > 300)
  281. h = 100;
  282. setSize (w, h);
  283. setName (f->getProcessor()->getName());
  284. {
  285. auto p = graph.getNodePosition (pluginID);
  286. setCentreRelative ((float) p.x, (float) p.y);
  287. }
  288. if (numIns != numInputs || numOuts != numOutputs)
  289. {
  290. numInputs = numIns;
  291. numOutputs = numOuts;
  292. pins.clear();
  293. for (int i = 0; i < f->getProcessor()->getTotalNumInputChannels(); ++i)
  294. addAndMakeVisible (pins.add (new PinComponent (panel, { pluginID, i }, true)));
  295. if (f->getProcessor()->acceptsMidi())
  296. addAndMakeVisible (pins.add (new PinComponent (panel, { pluginID, AudioProcessorGraph::midiChannelIndex }, true)));
  297. for (int i = 0; i < f->getProcessor()->getTotalNumOutputChannels(); ++i)
  298. addAndMakeVisible (pins.add (new PinComponent (panel, { pluginID, i }, false)));
  299. if (f->getProcessor()->producesMidi())
  300. addAndMakeVisible (pins.add (new PinComponent (panel, { pluginID, AudioProcessorGraph::midiChannelIndex }, false)));
  301. resized();
  302. }
  303. }
  304. AudioProcessor* getProcessor() const
  305. {
  306. if (auto node = graph.graph.getNodeForId (pluginID))
  307. return node->getProcessor();
  308. return {};
  309. }
  310. void showPopupMenu()
  311. {
  312. menu.reset (new PopupMenu);
  313. menu->addItem (1, "Delete this filter");
  314. menu->addItem (2, "Disconnect all pins");
  315. menu->addItem (3, "Toggle Bypass");
  316. if (getProcessor()->hasEditor())
  317. {
  318. menu->addSeparator();
  319. menu->addItem (10, "Show plugin GUI");
  320. menu->addItem (11, "Show all programs");
  321. menu->addItem (12, "Show all parameters");
  322. #if JUCE_WINDOWS && JUCE_WIN_PER_MONITOR_DPI_AWARE
  323. auto isTicked = false;
  324. if (auto* node = graph.graph.getNodeForId (pluginID))
  325. isTicked = node->properties["DPIAware"];
  326. menu->addItem (13, "Enable DPI awareness", true, isTicked);
  327. #endif
  328. menu->addItem (14, "Show debug log");
  329. }
  330. menu->addSeparator();
  331. menu->addItem (20, "Configure Audio I/O");
  332. menu->addItem (21, "Test state save/load");
  333. menu->showMenuAsync ({}, ModalCallbackFunction::create
  334. ([this] (int r) {
  335. switch (r)
  336. {
  337. case 1: graph.graph.removeNode (pluginID); break;
  338. case 2: graph.graph.disconnectNode (pluginID); break;
  339. case 3:
  340. {
  341. if (auto* node = graph.graph.getNodeForId (pluginID))
  342. node->setBypassed (! node->isBypassed());
  343. repaint();
  344. break;
  345. }
  346. case 10: showWindow (PluginWindow::Type::normal); break;
  347. case 11: showWindow (PluginWindow::Type::programs); break;
  348. case 12: showWindow (PluginWindow::Type::generic) ; break;
  349. #if JUCE_WINDOWS && JUCE_WIN_PER_MONITOR_DPI_AWARE
  350. case 13:
  351. {
  352. if (auto* node = graph.graph.getNodeForId (pluginID))
  353. node->properties.set ("DPIAware", ! node->properties ["DPIAware"]);
  354. break;
  355. }
  356. #endif
  357. case 14: showWindow (PluginWindow::Type::debug); break;
  358. case 20: showWindow (PluginWindow::Type::audioIO); break;
  359. case 21: testStateSaveLoad(); break;
  360. default: break;
  361. }
  362. }));
  363. }
  364. void testStateSaveLoad()
  365. {
  366. if (auto* processor = getProcessor())
  367. {
  368. MemoryBlock state;
  369. processor->getStateInformation (state);
  370. processor->setStateInformation (state.getData(), (int) state.getSize());
  371. }
  372. }
  373. void showWindow (PluginWindow::Type type)
  374. {
  375. if (auto node = graph.graph.getNodeForId (pluginID))
  376. if (auto* w = graph.getOrCreateWindowFor (node, type))
  377. w->toFront (true);
  378. }
  379. void timerCallback() override
  380. {
  381. // this should only be called on touch devices
  382. jassert (isOnTouchDevice());
  383. stopTimer();
  384. showPopupMenu();
  385. }
  386. void parameterValueChanged (int, float) override
  387. {
  388. repaint();
  389. }
  390. void parameterGestureChanged (int, bool) override {}
  391. GraphEditorPanel& panel;
  392. PluginGraph& graph;
  393. const AudioProcessorGraph::NodeID pluginID;
  394. OwnedArray<PinComponent> pins;
  395. int numInputs = 0, numOutputs = 0;
  396. int pinSize = 16;
  397. Point<int> originalPos;
  398. Font font { 13.0f, Font::bold };
  399. int numIns = 0, numOuts = 0;
  400. DropShadowEffect shadow;
  401. std::unique_ptr<PopupMenu> menu;
  402. };
  403. //==============================================================================
  404. struct GraphEditorPanel::ConnectorComponent : public Component,
  405. public SettableTooltipClient
  406. {
  407. explicit ConnectorComponent (GraphEditorPanel& p)
  408. : panel (p), graph (p.graph)
  409. {
  410. setAlwaysOnTop (true);
  411. }
  412. void setInput (AudioProcessorGraph::NodeAndChannel newSource)
  413. {
  414. if (connection.source != newSource)
  415. {
  416. connection.source = newSource;
  417. update();
  418. }
  419. }
  420. void setOutput (AudioProcessorGraph::NodeAndChannel newDest)
  421. {
  422. if (connection.destination != newDest)
  423. {
  424. connection.destination = newDest;
  425. update();
  426. }
  427. }
  428. void dragStart (Point<float> pos)
  429. {
  430. lastInputPos = pos;
  431. resizeToFit();
  432. }
  433. void dragEnd (Point<float> pos)
  434. {
  435. lastOutputPos = pos;
  436. resizeToFit();
  437. }
  438. void update()
  439. {
  440. Point<float> p1, p2;
  441. getPoints (p1, p2);
  442. if (lastInputPos != p1 || lastOutputPos != p2)
  443. resizeToFit();
  444. }
  445. void resizeToFit()
  446. {
  447. Point<float> p1, p2;
  448. getPoints (p1, p2);
  449. auto newBounds = Rectangle<float> (p1, p2).expanded (4.0f).getSmallestIntegerContainer();
  450. if (newBounds != getBounds())
  451. setBounds (newBounds);
  452. else
  453. resized();
  454. repaint();
  455. }
  456. void getPoints (Point<float>& p1, Point<float>& p2) const
  457. {
  458. p1 = lastInputPos;
  459. p2 = lastOutputPos;
  460. if (auto* src = panel.getComponentForPlugin (connection.source.nodeID))
  461. p1 = src->getPinPos (connection.source.channelIndex, false);
  462. if (auto* dest = panel.getComponentForPlugin (connection.destination.nodeID))
  463. p2 = dest->getPinPos (connection.destination.channelIndex, true);
  464. }
  465. void paint (Graphics& g) override
  466. {
  467. if (connection.source.isMIDI() || connection.destination.isMIDI())
  468. g.setColour (Colours::red);
  469. else
  470. g.setColour (Colours::green);
  471. g.fillPath (linePath);
  472. }
  473. bool hitTest (int x, int y) override
  474. {
  475. auto pos = Point<int> (x, y).toFloat();
  476. if (hitPath.contains (pos))
  477. {
  478. double distanceFromStart, distanceFromEnd;
  479. getDistancesFromEnds (pos, distanceFromStart, distanceFromEnd);
  480. // avoid clicking the connector when over a pin
  481. return distanceFromStart > 7.0 && distanceFromEnd > 7.0;
  482. }
  483. return false;
  484. }
  485. void mouseDown (const MouseEvent&) override
  486. {
  487. dragging = false;
  488. }
  489. void mouseDrag (const MouseEvent& e) override
  490. {
  491. if (dragging)
  492. {
  493. panel.dragConnector (e);
  494. }
  495. else if (e.mouseWasDraggedSinceMouseDown())
  496. {
  497. dragging = true;
  498. graph.graph.removeConnection (connection);
  499. double distanceFromStart, distanceFromEnd;
  500. getDistancesFromEnds (getPosition().toFloat() + e.position, distanceFromStart, distanceFromEnd);
  501. const bool isNearerSource = (distanceFromStart < distanceFromEnd);
  502. AudioProcessorGraph::NodeAndChannel dummy { {}, 0 };
  503. panel.beginConnectorDrag (isNearerSource ? dummy : connection.source,
  504. isNearerSource ? connection.destination : dummy,
  505. e);
  506. }
  507. }
  508. void mouseUp (const MouseEvent& e) override
  509. {
  510. if (dragging)
  511. panel.endDraggingConnector (e);
  512. }
  513. void resized() override
  514. {
  515. Point<float> p1, p2;
  516. getPoints (p1, p2);
  517. lastInputPos = p1;
  518. lastOutputPos = p2;
  519. p1 -= getPosition().toFloat();
  520. p2 -= getPosition().toFloat();
  521. linePath.clear();
  522. linePath.startNewSubPath (p1);
  523. linePath.cubicTo (p1.x, p1.y + (p2.y - p1.y) * 0.33f,
  524. p2.x, p1.y + (p2.y - p1.y) * 0.66f,
  525. p2.x, p2.y);
  526. PathStrokeType wideStroke (8.0f);
  527. wideStroke.createStrokedPath (hitPath, linePath);
  528. PathStrokeType stroke (2.5f);
  529. stroke.createStrokedPath (linePath, linePath);
  530. auto arrowW = 5.0f;
  531. auto arrowL = 4.0f;
  532. Path arrow;
  533. arrow.addTriangle (-arrowL, arrowW,
  534. -arrowL, -arrowW,
  535. arrowL, 0.0f);
  536. arrow.applyTransform (AffineTransform()
  537. .rotated (MathConstants<float>::halfPi - (float) atan2 (p2.x - p1.x, p2.y - p1.y))
  538. .translated ((p1 + p2) * 0.5f));
  539. linePath.addPath (arrow);
  540. linePath.setUsingNonZeroWinding (true);
  541. }
  542. void getDistancesFromEnds (Point<float> p, double& distanceFromStart, double& distanceFromEnd) const
  543. {
  544. Point<float> p1, p2;
  545. getPoints (p1, p2);
  546. distanceFromStart = p1.getDistanceFrom (p);
  547. distanceFromEnd = p2.getDistanceFrom (p);
  548. }
  549. GraphEditorPanel& panel;
  550. PluginGraph& graph;
  551. AudioProcessorGraph::Connection connection { { {}, 0 }, { {}, 0 } };
  552. Point<float> lastInputPos, lastOutputPos;
  553. Path linePath, hitPath;
  554. bool dragging = false;
  555. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConnectorComponent)
  556. };
  557. //==============================================================================
  558. GraphEditorPanel::GraphEditorPanel (PluginGraph& g) : graph (g)
  559. {
  560. graph.addChangeListener (this);
  561. setOpaque (true);
  562. }
  563. GraphEditorPanel::~GraphEditorPanel()
  564. {
  565. graph.removeChangeListener (this);
  566. draggingConnector = nullptr;
  567. nodes.clear();
  568. connectors.clear();
  569. }
  570. void GraphEditorPanel::paint (Graphics& g)
  571. {
  572. g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
  573. }
  574. void GraphEditorPanel::mouseDown (const MouseEvent& e)
  575. {
  576. if (isOnTouchDevice())
  577. {
  578. originalTouchPos = e.position.toInt();
  579. startTimer (750);
  580. }
  581. if (e.mods.isPopupMenu())
  582. showPopupMenu (e.position.toInt());
  583. }
  584. void GraphEditorPanel::mouseUp (const MouseEvent&)
  585. {
  586. if (isOnTouchDevice())
  587. {
  588. stopTimer();
  589. callAfterDelay (250, []() { PopupMenu::dismissAllActiveMenus(); });
  590. }
  591. }
  592. void GraphEditorPanel::mouseDrag (const MouseEvent& e)
  593. {
  594. if (isOnTouchDevice() && e.getDistanceFromDragStart() > 5)
  595. stopTimer();
  596. }
  597. void GraphEditorPanel::createNewPlugin (const PluginDescription& desc, Point<int> position)
  598. {
  599. graph.addPlugin (desc, position.toDouble() / Point<double> ((double) getWidth(), (double) getHeight()));
  600. }
  601. GraphEditorPanel::PluginComponent* GraphEditorPanel::getComponentForPlugin (AudioProcessorGraph::NodeID nodeID) const
  602. {
  603. for (auto* fc : nodes)
  604. if (fc->pluginID == nodeID)
  605. return fc;
  606. return nullptr;
  607. }
  608. GraphEditorPanel::ConnectorComponent* GraphEditorPanel::getComponentForConnection (const AudioProcessorGraph::Connection& conn) const
  609. {
  610. for (auto* cc : connectors)
  611. if (cc->connection == conn)
  612. return cc;
  613. return nullptr;
  614. }
  615. GraphEditorPanel::PinComponent* GraphEditorPanel::findPinAt (Point<float> pos) const
  616. {
  617. for (auto* fc : nodes)
  618. {
  619. // NB: A Visual Studio optimiser error means we have to put this Component* in a local
  620. // variable before trying to cast it, or it gets mysteriously optimised away..
  621. auto* comp = fc->getComponentAt (pos.toInt() - fc->getPosition());
  622. if (auto* pin = dynamic_cast<PinComponent*> (comp))
  623. return pin;
  624. }
  625. return nullptr;
  626. }
  627. void GraphEditorPanel::resized()
  628. {
  629. updateComponents();
  630. }
  631. void GraphEditorPanel::changeListenerCallback (ChangeBroadcaster*)
  632. {
  633. updateComponents();
  634. }
  635. void GraphEditorPanel::updateComponents()
  636. {
  637. for (int i = nodes.size(); --i >= 0;)
  638. if (graph.graph.getNodeForId (nodes.getUnchecked(i)->pluginID) == nullptr)
  639. nodes.remove (i);
  640. for (int i = connectors.size(); --i >= 0;)
  641. if (! graph.graph.isConnected (connectors.getUnchecked(i)->connection))
  642. connectors.remove (i);
  643. for (auto* fc : nodes)
  644. fc->update();
  645. for (auto* cc : connectors)
  646. cc->update();
  647. for (auto* f : graph.graph.getNodes())
  648. {
  649. if (getComponentForPlugin (f->nodeID) == nullptr)
  650. {
  651. auto* comp = nodes.add (new PluginComponent (*this, f->nodeID));
  652. addAndMakeVisible (comp);
  653. comp->update();
  654. }
  655. }
  656. for (auto& c : graph.graph.getConnections())
  657. {
  658. if (getComponentForConnection (c) == nullptr)
  659. {
  660. auto* comp = connectors.add (new ConnectorComponent (*this));
  661. addAndMakeVisible (comp);
  662. comp->setInput (c.source);
  663. comp->setOutput (c.destination);
  664. }
  665. }
  666. }
  667. void GraphEditorPanel::showPopupMenu (Point<int> mousePos)
  668. {
  669. menu.reset (new PopupMenu);
  670. if (auto* mainWindow = findParentComponentOfClass<MainHostWindow>())
  671. {
  672. mainWindow->addPluginsToMenu (*menu);
  673. menu->showMenuAsync ({},
  674. ModalCallbackFunction::create ([this, mousePos] (int r)
  675. {
  676. if (r > 0)
  677. if (auto* mainWin = findParentComponentOfClass<MainHostWindow>())
  678. createNewPlugin (mainWin->getChosenType (r), mousePos);
  679. }));
  680. }
  681. }
  682. void GraphEditorPanel::beginConnectorDrag (AudioProcessorGraph::NodeAndChannel source,
  683. AudioProcessorGraph::NodeAndChannel dest,
  684. const MouseEvent& e)
  685. {
  686. auto* c = dynamic_cast<ConnectorComponent*> (e.originalComponent);
  687. connectors.removeObject (c, false);
  688. draggingConnector.reset (c);
  689. if (draggingConnector == nullptr)
  690. draggingConnector.reset (new ConnectorComponent (*this));
  691. draggingConnector->setInput (source);
  692. draggingConnector->setOutput (dest);
  693. addAndMakeVisible (draggingConnector.get());
  694. draggingConnector->toFront (false);
  695. dragConnector (e);
  696. }
  697. void GraphEditorPanel::dragConnector (const MouseEvent& e)
  698. {
  699. auto e2 = e.getEventRelativeTo (this);
  700. if (draggingConnector != nullptr)
  701. {
  702. draggingConnector->setTooltip ({});
  703. auto pos = e2.position;
  704. if (auto* pin = findPinAt (pos))
  705. {
  706. auto connection = draggingConnector->connection;
  707. if (connection.source.nodeID == AudioProcessorGraph::NodeID() && ! pin->isInput)
  708. {
  709. connection.source = pin->pin;
  710. }
  711. else if (connection.destination.nodeID == AudioProcessorGraph::NodeID() && pin->isInput)
  712. {
  713. connection.destination = pin->pin;
  714. }
  715. if (graph.graph.canConnect (connection))
  716. {
  717. pos = (pin->getParentComponent()->getPosition() + pin->getBounds().getCentre()).toFloat();
  718. draggingConnector->setTooltip (pin->getTooltip());
  719. }
  720. }
  721. if (draggingConnector->connection.source.nodeID == AudioProcessorGraph::NodeID())
  722. draggingConnector->dragStart (pos);
  723. else
  724. draggingConnector->dragEnd (pos);
  725. }
  726. }
  727. void GraphEditorPanel::endDraggingConnector (const MouseEvent& e)
  728. {
  729. if (draggingConnector == nullptr)
  730. return;
  731. draggingConnector->setTooltip ({});
  732. auto e2 = e.getEventRelativeTo (this);
  733. auto connection = draggingConnector->connection;
  734. draggingConnector = nullptr;
  735. if (auto* pin = findPinAt (e2.position))
  736. {
  737. if (connection.source.nodeID == AudioProcessorGraph::NodeID())
  738. {
  739. if (pin->isInput)
  740. return;
  741. connection.source = pin->pin;
  742. }
  743. else
  744. {
  745. if (! pin->isInput)
  746. return;
  747. connection.destination = pin->pin;
  748. }
  749. graph.graph.addConnection (connection);
  750. }
  751. }
  752. void GraphEditorPanel::timerCallback()
  753. {
  754. // this should only be called on touch devices
  755. jassert (isOnTouchDevice());
  756. stopTimer();
  757. showPopupMenu (originalTouchPos);
  758. }
  759. //==============================================================================
  760. struct GraphDocumentComponent::TooltipBar : public Component,
  761. private Timer
  762. {
  763. TooltipBar()
  764. {
  765. startTimer (100);
  766. }
  767. void paint (Graphics& g) override
  768. {
  769. g.setFont (Font ((float) getHeight() * 0.7f, Font::bold));
  770. g.setColour (Colours::black);
  771. g.drawFittedText (tip, 10, 0, getWidth() - 12, getHeight(), Justification::centredLeft, 1);
  772. }
  773. void timerCallback() override
  774. {
  775. String newTip;
  776. if (auto* underMouse = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse())
  777. if (auto* ttc = dynamic_cast<TooltipClient*> (underMouse))
  778. if (! (underMouse->isMouseButtonDown() || underMouse->isCurrentlyBlockedByAnotherModalComponent()))
  779. newTip = ttc->getTooltip();
  780. if (newTip != tip)
  781. {
  782. tip = newTip;
  783. repaint();
  784. }
  785. }
  786. String tip;
  787. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TooltipBar)
  788. };
  789. //==============================================================================
  790. class GraphDocumentComponent::TitleBarComponent : public Component,
  791. private Button::Listener
  792. {
  793. public:
  794. explicit TitleBarComponent (GraphDocumentComponent& graphDocumentComponent)
  795. : owner (graphDocumentComponent)
  796. {
  797. static const unsigned char burgerMenuPathData[]
  798. = { 110,109,0,0,128,64,0,0,32,65,108,0,0,224,65,0,0,32,65,98,254,212,232,65,0,0,32,65,0,0,240,65,252,
  799. 169,17,65,0,0,240,65,0,0,0,65,98,0,0,240,65,8,172,220,64,254,212,232,65,0,0,192,64,0,0,224,65,0,0,
  800. 192,64,108,0,0,128,64,0,0,192,64,98,16,88,57,64,0,0,192,64,0,0,0,64,8,172,220,64,0,0,0,64,0,0,0,65,
  801. 98,0,0,0,64,252,169,17,65,16,88,57,64,0,0,32,65,0,0,128,64,0,0,32,65,99,109,0,0,224,65,0,0,96,65,108,
  802. 0,0,128,64,0,0,96,65,98,16,88,57,64,0,0,96,65,0,0,0,64,4,86,110,65,0,0,0,64,0,0,128,65,98,0,0,0,64,
  803. 254,212,136,65,16,88,57,64,0,0,144,65,0,0,128,64,0,0,144,65,108,0,0,224,65,0,0,144,65,98,254,212,232,
  804. 65,0,0,144,65,0,0,240,65,254,212,136,65,0,0,240,65,0,0,128,65,98,0,0,240,65,4,86,110,65,254,212,232,
  805. 65,0,0,96,65,0,0,224,65,0,0,96,65,99,109,0,0,224,65,0,0,176,65,108,0,0,128,64,0,0,176,65,98,16,88,57,
  806. 64,0,0,176,65,0,0,0,64,2,43,183,65,0,0,0,64,0,0,192,65,98,0,0,0,64,254,212,200,65,16,88,57,64,0,0,208,
  807. 65,0,0,128,64,0,0,208,65,108,0,0,224,65,0,0,208,65,98,254,212,232,65,0,0,208,65,0,0,240,65,254,212,
  808. 200,65,0,0,240,65,0,0,192,65,98,0,0,240,65,2,43,183,65,254,212,232,65,0,0,176,65,0,0,224,65,0,0,176,
  809. 65,99,101,0,0 };
  810. static const unsigned char pluginListPathData[]
  811. = { 110,109,193,202,222,64,80,50,21,64,108,0,0,48,65,0,0,0,0,108,160,154,112,65,80,50,21,64,108,0,0,48,65,80,
  812. 50,149,64,108,193,202,222,64,80,50,21,64,99,109,0,0,192,64,251,220,127,64,108,160,154,32,65,165,135,202,
  813. 64,108,160,154,32,65,250,220,47,65,108,0,0,192,64,102,144,10,65,108,0,0,192,64,251,220,127,64,99,109,0,0,
  814. 128,65,251,220,127,64,108,0,0,128,65,103,144,10,65,108,96,101,63,65,251,220,47,65,108,96,101,63,65,166,135,
  815. 202,64,108,0,0,128,65,251,220,127,64,99,109,96,101,79,65,148,76,69,65,108,0,0,136,65,0,0,32,65,108,80,
  816. 77,168,65,148,76,69,65,108,0,0,136,65,40,153,106,65,108,96,101,79,65,148,76,69,65,99,109,0,0,64,65,63,247,
  817. 95,65,108,80,77,128,65,233,161,130,65,108,80,77,128,65,125,238,167,65,108,0,0,64,65,51,72,149,65,108,0,0,64,
  818. 65,63,247,95,65,99,109,0,0,176,65,63,247,95,65,108,0,0,176,65,51,72,149,65,108,176,178,143,65,125,238,167,65,
  819. 108,176,178,143,65,233,161,130,65,108,0,0,176,65,63,247,95,65,99,109,12,86,118,63,148,76,69,65,108,0,0,160,
  820. 64,0,0,32,65,108,159,154,16,65,148,76,69,65,108,0,0,160,64,40,153,106,65,108,12,86,118,63,148,76,69,65,99,
  821. 109,0,0,0,0,63,247,95,65,108,62,53,129,64,233,161,130,65,108,62,53,129,64,125,238,167,65,108,0,0,0,0,51,
  822. 72,149,65,108,0,0,0,0,63,247,95,65,99,109,0,0,32,65,63,247,95,65,108,0,0,32,65,51,72,149,65,108,193,202,190,
  823. 64,125,238,167,65,108,193,202,190,64,233,161,130,65,108,0,0,32,65,63,247,95,65,99,101,0,0 };
  824. {
  825. Path p;
  826. p.loadPathFromData (burgerMenuPathData, sizeof (burgerMenuPathData));
  827. burgerButton.setShape (p, true, true, false);
  828. }
  829. {
  830. Path p;
  831. p.loadPathFromData (pluginListPathData, sizeof (pluginListPathData));
  832. pluginButton.setShape (p, true, true, false);
  833. }
  834. burgerButton.addListener (this);
  835. addAndMakeVisible (burgerButton);
  836. pluginButton.addListener (this);
  837. addAndMakeVisible (pluginButton);
  838. titleLabel.setJustificationType (Justification::centredLeft);
  839. addAndMakeVisible (titleLabel);
  840. setOpaque (true);
  841. }
  842. private:
  843. void paint (Graphics& g) override
  844. {
  845. auto titleBarBackgroundColour = getLookAndFeel().findColour (ResizableWindow::backgroundColourId).darker();
  846. g.setColour (titleBarBackgroundColour);
  847. g.fillRect (getLocalBounds());
  848. }
  849. void resized() override
  850. {
  851. auto r = getLocalBounds();
  852. burgerButton.setBounds (r.removeFromLeft (40).withSizeKeepingCentre (20, 20));
  853. pluginButton.setBounds (r.removeFromRight (40).withSizeKeepingCentre (20, 20));
  854. titleLabel.setFont (Font (static_cast<float> (getHeight()) * 0.5f, Font::plain));
  855. titleLabel.setBounds (r);
  856. }
  857. void buttonClicked (Button* b) override
  858. {
  859. owner.showSidePanel (b == &burgerButton);
  860. }
  861. GraphDocumentComponent& owner;
  862. Label titleLabel {"titleLabel", "Plugin Host"};
  863. ShapeButton burgerButton {"burgerButton", Colours::lightgrey, Colours::lightgrey, Colours::white};
  864. ShapeButton pluginButton {"pluginButton", Colours::lightgrey, Colours::lightgrey, Colours::white};
  865. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TitleBarComponent)
  866. };
  867. //==============================================================================
  868. struct GraphDocumentComponent::PluginListBoxModel : public ListBoxModel,
  869. public ChangeListener,
  870. public MouseListener
  871. {
  872. PluginListBoxModel (ListBox& lb, KnownPluginList& kpl)
  873. : owner (lb),
  874. knownPlugins (kpl)
  875. {
  876. knownPlugins.addChangeListener (this);
  877. owner.addMouseListener (this, true);
  878. #if JUCE_IOS
  879. scanner.reset (new AUScanner (knownPlugins));
  880. #endif
  881. }
  882. int getNumRows() override
  883. {
  884. return knownPlugins.getNumTypes();
  885. }
  886. void paintListBoxItem (int rowNumber, Graphics& g,
  887. int width, int height, bool rowIsSelected) override
  888. {
  889. g.fillAll (rowIsSelected ? Colour (0xff42A2C8)
  890. : Colour (0xff263238));
  891. g.setColour (rowIsSelected ? Colours::black : Colours::white);
  892. if (rowNumber < knownPlugins.getNumTypes())
  893. g.drawFittedText (knownPlugins.getTypes()[rowNumber].name, { 0, 0, width, height - 2 }, Justification::centred, 1);
  894. g.setColour (Colours::black.withAlpha (0.4f));
  895. g.drawRect (0, height - 1, width, 1);
  896. }
  897. var getDragSourceDescription (const SparseSet<int>& selectedRows) override
  898. {
  899. if (! isOverSelectedRow)
  900. return var();
  901. return String ("PLUGIN: " + String (selectedRows[0]));
  902. }
  903. void changeListenerCallback (ChangeBroadcaster*) override
  904. {
  905. owner.updateContent();
  906. }
  907. void mouseDown (const MouseEvent& e) override
  908. {
  909. isOverSelectedRow = owner.getRowPosition (owner.getSelectedRow(), true)
  910. .contains (e.getEventRelativeTo (&owner).getMouseDownPosition());
  911. }
  912. ListBox& owner;
  913. KnownPluginList& knownPlugins;
  914. bool isOverSelectedRow = false;
  915. #if JUCE_IOS
  916. std::unique_ptr<AUScanner> scanner;
  917. #endif
  918. JUCE_DECLARE_NON_COPYABLE (PluginListBoxModel)
  919. };
  920. //==============================================================================
  921. GraphDocumentComponent::GraphDocumentComponent (AudioPluginFormatManager& fm,
  922. AudioDeviceManager& dm,
  923. KnownPluginList& kpl)
  924. : graph (new PluginGraph (fm)),
  925. deviceManager (dm),
  926. pluginList (kpl),
  927. graphPlayer (getAppProperties().getUserSettings()->getBoolValue ("doublePrecisionProcessing", false))
  928. {
  929. init();
  930. deviceManager.addChangeListener (graphPanel.get());
  931. deviceManager.addAudioCallback (&graphPlayer);
  932. deviceManager.addMidiInputDeviceCallback ({}, &graphPlayer.getMidiMessageCollector());
  933. deviceManager.addChangeListener (this);
  934. }
  935. void GraphDocumentComponent::init()
  936. {
  937. updateMidiOutput();
  938. graphPanel.reset (new GraphEditorPanel (*graph));
  939. addAndMakeVisible (graphPanel.get());
  940. graphPlayer.setProcessor (&graph->graph);
  941. keyState.addListener (&graphPlayer.getMidiMessageCollector());
  942. keyboardComp.reset (new MidiKeyboardComponent (keyState, MidiKeyboardComponent::horizontalKeyboard));
  943. addAndMakeVisible (keyboardComp.get());
  944. statusBar.reset (new TooltipBar());
  945. addAndMakeVisible (statusBar.get());
  946. graphPanel->updateComponents();
  947. if (isOnTouchDevice())
  948. {
  949. titleBarComponent.reset (new TitleBarComponent (*this));
  950. addAndMakeVisible (titleBarComponent.get());
  951. pluginListBoxModel.reset (new PluginListBoxModel (pluginListBox, pluginList));
  952. pluginListBox.setModel (pluginListBoxModel.get());
  953. pluginListBox.setRowHeight (40);
  954. pluginListSidePanel.setContent (&pluginListBox, false);
  955. mobileSettingsSidePanel.setContent (new AudioDeviceSelectorComponent (deviceManager,
  956. 0, 2, 0, 2,
  957. true, true, true, false));
  958. addAndMakeVisible (pluginListSidePanel);
  959. addAndMakeVisible (mobileSettingsSidePanel);
  960. }
  961. }
  962. GraphDocumentComponent::~GraphDocumentComponent()
  963. {
  964. if (midiOutput != nullptr)
  965. midiOutput->stopBackgroundThread();
  966. releaseGraph();
  967. keyState.removeListener (&graphPlayer.getMidiMessageCollector());
  968. }
  969. void GraphDocumentComponent::resized()
  970. {
  971. auto r = getLocalBounds();
  972. const int titleBarHeight = 40;
  973. const int keysHeight = 60;
  974. const int statusHeight = 20;
  975. if (isOnTouchDevice())
  976. titleBarComponent->setBounds (r.removeFromTop(titleBarHeight));
  977. keyboardComp->setBounds (r.removeFromBottom (keysHeight));
  978. statusBar->setBounds (r.removeFromBottom (statusHeight));
  979. graphPanel->setBounds (r);
  980. checkAvailableWidth();
  981. }
  982. void GraphDocumentComponent::createNewPlugin (const PluginDescription& desc, Point<int> pos)
  983. {
  984. graphPanel->createNewPlugin (desc, pos);
  985. }
  986. void GraphDocumentComponent::unfocusKeyboardComponent()
  987. {
  988. keyboardComp->unfocusAllComponents();
  989. }
  990. void GraphDocumentComponent::releaseGraph()
  991. {
  992. deviceManager.removeAudioCallback (&graphPlayer);
  993. deviceManager.removeMidiInputDeviceCallback ({}, &graphPlayer.getMidiMessageCollector());
  994. if (graphPanel != nullptr)
  995. {
  996. deviceManager.removeChangeListener (graphPanel.get());
  997. graphPanel = nullptr;
  998. }
  999. keyboardComp = nullptr;
  1000. statusBar = nullptr;
  1001. graphPlayer.setProcessor (nullptr);
  1002. graph = nullptr;
  1003. }
  1004. bool GraphDocumentComponent::isInterestedInDragSource (const SourceDetails& details)
  1005. {
  1006. return ((dynamic_cast<ListBox*> (details.sourceComponent.get()) != nullptr)
  1007. && details.description.toString().startsWith ("PLUGIN"));
  1008. }
  1009. void GraphDocumentComponent::itemDropped (const SourceDetails& details)
  1010. {
  1011. // don't allow items to be dropped behind the sidebar
  1012. if (pluginListSidePanel.getBounds().contains (details.localPosition))
  1013. return;
  1014. auto pluginTypeIndex = details.description.toString()
  1015. .fromFirstOccurrenceOf ("PLUGIN: ", false, false)
  1016. .getIntValue();
  1017. // must be a valid index!
  1018. jassert (isPositiveAndBelow (pluginTypeIndex, pluginList.getNumTypes()));
  1019. createNewPlugin (pluginList.getTypes()[pluginTypeIndex], details.localPosition);
  1020. }
  1021. void GraphDocumentComponent::showSidePanel (bool showSettingsPanel)
  1022. {
  1023. if (showSettingsPanel)
  1024. mobileSettingsSidePanel.showOrHide (true);
  1025. else
  1026. pluginListSidePanel.showOrHide (true);
  1027. checkAvailableWidth();
  1028. lastOpenedSidePanel = showSettingsPanel ? &mobileSettingsSidePanel
  1029. : &pluginListSidePanel;
  1030. }
  1031. void GraphDocumentComponent::hideLastSidePanel()
  1032. {
  1033. if (lastOpenedSidePanel != nullptr)
  1034. lastOpenedSidePanel->showOrHide (false);
  1035. if (mobileSettingsSidePanel.isPanelShowing()) lastOpenedSidePanel = &mobileSettingsSidePanel;
  1036. else if (pluginListSidePanel.isPanelShowing()) lastOpenedSidePanel = &pluginListSidePanel;
  1037. else lastOpenedSidePanel = nullptr;
  1038. }
  1039. void GraphDocumentComponent::checkAvailableWidth()
  1040. {
  1041. if (mobileSettingsSidePanel.isPanelShowing() && pluginListSidePanel.isPanelShowing())
  1042. {
  1043. if (getWidth() - (mobileSettingsSidePanel.getWidth() + pluginListSidePanel.getWidth()) < 150)
  1044. hideLastSidePanel();
  1045. }
  1046. }
  1047. void GraphDocumentComponent::setDoublePrecision (bool doublePrecision)
  1048. {
  1049. graphPlayer.setDoublePrecisionProcessing (doublePrecision);
  1050. }
  1051. bool GraphDocumentComponent::closeAnyOpenPluginWindows()
  1052. {
  1053. return graphPanel->graph.closeAnyOpenPluginWindows();
  1054. }
  1055. void GraphDocumentComponent::changeListenerCallback (ChangeBroadcaster*)
  1056. {
  1057. updateMidiOutput();
  1058. }
  1059. void GraphDocumentComponent::updateMidiOutput()
  1060. {
  1061. auto* defaultMidiOutput = deviceManager.getDefaultMidiOutput();
  1062. if (midiOutput != defaultMidiOutput)
  1063. {
  1064. midiOutput = defaultMidiOutput;
  1065. if (midiOutput != nullptr)
  1066. midiOutput->startBackgroundThread();
  1067. graphPlayer.setMidiOutput (midiOutput);
  1068. }
  1069. }