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.

1329 lines
43KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../JuceLibraryCode/JuceHeader.h"
  20. #include "GraphEditorPanel.h"
  21. #include "../Plugins/InternalPlugins.h"
  22. #include "MainHostWindow.h"
  23. //==============================================================================
  24. #if JUCE_IOS
  25. class AUScanner
  26. {
  27. public:
  28. AUScanner (KnownPluginList& list)
  29. : knownPluginList (list), pool (5)
  30. {
  31. knownPluginList.clearBlacklistedFiles();
  32. paths = formatToScan.getDefaultLocationsToSearch();
  33. startScan();
  34. }
  35. private:
  36. KnownPluginList& knownPluginList;
  37. AudioUnitPluginFormat formatToScan;
  38. std::unique_ptr<PluginDirectoryScanner> scanner;
  39. FileSearchPath paths;
  40. ThreadPool pool;
  41. void startScan()
  42. {
  43. auto deadMansPedalFile = getAppProperties().getUserSettings()
  44. ->getFile().getSiblingFile ("RecentlyCrashedPluginsList");
  45. scanner.reset (new PluginDirectoryScanner (knownPluginList, formatToScan, paths,
  46. true, deadMansPedalFile, true));
  47. for (int i = 5; --i >= 0;)
  48. pool.addJob (new ScanJob (*this), true);
  49. }
  50. bool doNextScan()
  51. {
  52. String pluginBeingScanned;
  53. if (scanner->scanNextFile (true, pluginBeingScanned))
  54. return true;
  55. return false;
  56. }
  57. struct ScanJob : public ThreadPoolJob
  58. {
  59. ScanJob (AUScanner& s) : ThreadPoolJob ("pluginscan"), scanner (s) {}
  60. JobStatus runJob()
  61. {
  62. while (scanner.doNextScan() && ! shouldExit())
  63. {}
  64. return jobHasFinished;
  65. }
  66. AUScanner& scanner;
  67. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScanJob)
  68. };
  69. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AUScanner)
  70. };
  71. #endif
  72. //==============================================================================
  73. struct GraphEditorPanel::PinComponent : public Component,
  74. public SettableTooltipClient
  75. {
  76. PinComponent (GraphEditorPanel& p, AudioProcessorGraph::NodeAndChannel pinToUse, bool isIn)
  77. : panel (p), graph (p.graph), pin (pinToUse), isInput (isIn)
  78. {
  79. if (auto node = graph.graph.getNodeForId (pin.nodeID))
  80. {
  81. String tip;
  82. if (pin.isMIDI())
  83. {
  84. tip = isInput ? "MIDI Input"
  85. : "MIDI Output";
  86. }
  87. else
  88. {
  89. auto& processor = *node->getProcessor();
  90. auto channel = processor.getOffsetInBusBufferForAbsoluteChannelIndex (isInput, pin.channelIndex, busIdx);
  91. if (auto* bus = processor.getBus (isInput, busIdx))
  92. tip = bus->getName() + ": " + AudioChannelSet::getAbbreviatedChannelTypeName (bus->getCurrentLayout().getTypeOfChannel (channel));
  93. else
  94. tip = (isInput ? "Main Input: "
  95. : "Main Output: ") + String (pin.channelIndex + 1);
  96. }
  97. setTooltip (tip);
  98. }
  99. setSize (16, 16);
  100. }
  101. void paint (Graphics& g) override
  102. {
  103. auto w = (float) getWidth();
  104. auto h = (float) getHeight();
  105. Path p;
  106. p.addEllipse (w * 0.25f, h * 0.25f, w * 0.5f, h * 0.5f);
  107. p.addRectangle (w * 0.4f, isInput ? (0.5f * h) : 0.0f, w * 0.2f, h * 0.5f);
  108. auto colour = (pin.isMIDI() ? Colours::red : Colours::green);
  109. g.setColour (colour.withRotatedHue (busIdx / 5.0f));
  110. g.fillPath (p);
  111. }
  112. void mouseDown (const MouseEvent& e) override
  113. {
  114. AudioProcessorGraph::NodeAndChannel dummy { {}, 0 };
  115. panel.beginConnectorDrag (isInput ? dummy : pin,
  116. isInput ? pin : dummy,
  117. e);
  118. }
  119. void mouseDrag (const MouseEvent& e) override
  120. {
  121. panel.dragConnector (e);
  122. }
  123. void mouseUp (const MouseEvent& e) override
  124. {
  125. panel.endDraggingConnector (e);
  126. }
  127. GraphEditorPanel& panel;
  128. PluginGraph& graph;
  129. AudioProcessorGraph::NodeAndChannel pin;
  130. const bool isInput;
  131. int busIdx = 0;
  132. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PinComponent)
  133. };
  134. //==============================================================================
  135. struct GraphEditorPanel::PluginComponent : public Component,
  136. public Timer,
  137. private AudioProcessorParameter::Listener
  138. {
  139. PluginComponent (GraphEditorPanel& p, AudioProcessorGraph::NodeID id) : panel (p), graph (p.graph), pluginID (id)
  140. {
  141. shadow.setShadowProperties (DropShadow (Colours::black.withAlpha (0.5f), 3, { 0, 1 }));
  142. setComponentEffect (&shadow);
  143. if (auto f = graph.graph.getNodeForId (pluginID))
  144. {
  145. if (auto* processor = f->getProcessor())
  146. {
  147. if (auto* bypassParam = processor->getBypassParameter())
  148. bypassParam->addListener (this);
  149. }
  150. }
  151. setSize (150, 60);
  152. }
  153. PluginComponent (const PluginComponent&) = delete;
  154. PluginComponent& operator= (const PluginComponent&) = delete;
  155. ~PluginComponent() override
  156. {
  157. if (auto f = graph.graph.getNodeForId (pluginID))
  158. {
  159. if (auto* processor = f->getProcessor())
  160. {
  161. if (auto* bypassParam = processor->getBypassParameter())
  162. bypassParam->removeListener (this);
  163. }
  164. }
  165. }
  166. void mouseDown (const MouseEvent& e) override
  167. {
  168. originalPos = localPointToGlobal (Point<int>());
  169. toFront (true);
  170. if (isOnTouchDevice())
  171. {
  172. startTimer (750);
  173. }
  174. else
  175. {
  176. if (e.mods.isPopupMenu())
  177. showPopupMenu();
  178. }
  179. }
  180. void mouseDrag (const MouseEvent& e) override
  181. {
  182. if (isOnTouchDevice() && e.getDistanceFromDragStart() > 5)
  183. stopTimer();
  184. if (! e.mods.isPopupMenu())
  185. {
  186. auto pos = originalPos + e.getOffsetFromDragStart();
  187. if (getParentComponent() != nullptr)
  188. pos = getParentComponent()->getLocalPoint (nullptr, pos);
  189. pos += getLocalBounds().getCentre();
  190. graph.setNodePosition (pluginID,
  191. { pos.x / (double) getParentWidth(),
  192. pos.y / (double) getParentHeight() });
  193. panel.updateComponents();
  194. }
  195. }
  196. void mouseUp (const MouseEvent& e) override
  197. {
  198. if (isOnTouchDevice())
  199. {
  200. stopTimer();
  201. callAfterDelay (250, []() { PopupMenu::dismissAllActiveMenus(); });
  202. }
  203. if (e.mouseWasDraggedSinceMouseDown())
  204. {
  205. graph.setChangedFlag (true);
  206. }
  207. else if (e.getNumberOfClicks() == 2)
  208. {
  209. if (auto f = graph.graph.getNodeForId (pluginID))
  210. if (auto* w = graph.getOrCreateWindowFor (f, PluginWindow::Type::normal))
  211. w->toFront (true);
  212. }
  213. }
  214. bool hitTest (int x, int y) override
  215. {
  216. for (auto* child : getChildren())
  217. if (child->getBounds().contains (x, y))
  218. return true;
  219. return x >= 3 && x < getWidth() - 6 && y >= pinSize && y < getHeight() - pinSize;
  220. }
  221. void paint (Graphics& g) override
  222. {
  223. auto boxArea = getLocalBounds().reduced (4, pinSize);
  224. bool isBypassed = false;
  225. if (auto* f = graph.graph.getNodeForId (pluginID))
  226. isBypassed = f->isBypassed();
  227. auto boxColour = findColour (TextEditor::backgroundColourId);
  228. if (isBypassed)
  229. boxColour = boxColour.brighter();
  230. g.setColour (boxColour);
  231. g.fillRect (boxArea.toFloat());
  232. g.setColour (findColour (TextEditor::textColourId));
  233. g.setFont (font);
  234. g.drawFittedText (getName(), boxArea, Justification::centred, 2);
  235. }
  236. void resized() override
  237. {
  238. if (auto f = graph.graph.getNodeForId (pluginID))
  239. {
  240. if (auto* processor = f->getProcessor())
  241. {
  242. for (auto* pin : pins)
  243. {
  244. const bool isInput = pin->isInput;
  245. auto channelIndex = pin->pin.channelIndex;
  246. int busIdx = 0;
  247. processor->getOffsetInBusBufferForAbsoluteChannelIndex (isInput, channelIndex, busIdx);
  248. const int total = isInput ? numIns : numOuts;
  249. const int index = pin->pin.isMIDI() ? (total - 1) : channelIndex;
  250. auto totalSpaces = static_cast<float> (total) + (static_cast<float> (jmax (0, processor->getBusCount (isInput) - 1)) * 0.5f);
  251. auto indexPos = static_cast<float> (index) + (static_cast<float> (busIdx) * 0.5f);
  252. pin->setBounds (proportionOfWidth ((1.0f + indexPos) / (totalSpaces + 1.0f)) - pinSize / 2,
  253. pin->isInput ? 0 : (getHeight() - pinSize),
  254. pinSize, pinSize);
  255. }
  256. }
  257. }
  258. }
  259. Point<float> getPinPos (int index, bool isInput) const
  260. {
  261. for (auto* pin : pins)
  262. if (pin->pin.channelIndex == index && isInput == pin->isInput)
  263. return getPosition().toFloat() + pin->getBounds().getCentre().toFloat();
  264. return {};
  265. }
  266. void update()
  267. {
  268. const AudioProcessorGraph::Node::Ptr f (graph.graph.getNodeForId (pluginID));
  269. jassert (f != nullptr);
  270. numIns = f->getProcessor()->getTotalNumInputChannels();
  271. if (f->getProcessor()->acceptsMidi())
  272. ++numIns;
  273. numOuts = f->getProcessor()->getTotalNumOutputChannels();
  274. if (f->getProcessor()->producesMidi())
  275. ++numOuts;
  276. int w = 100;
  277. int h = 60;
  278. w = jmax (w, (jmax (numIns, numOuts) + 1) * 20);
  279. const int textWidth = font.getStringWidth (f->getProcessor()->getName());
  280. w = jmax (w, 16 + jmin (textWidth, 300));
  281. if (textWidth > 300)
  282. h = 100;
  283. setSize (w, h);
  284. setName (f->getProcessor()->getName());
  285. {
  286. auto p = graph.getNodePosition (pluginID);
  287. setCentreRelative ((float) p.x, (float) p.y);
  288. }
  289. if (numIns != numInputs || numOuts != numOutputs)
  290. {
  291. numInputs = numIns;
  292. numOutputs = numOuts;
  293. pins.clear();
  294. for (int i = 0; i < f->getProcessor()->getTotalNumInputChannels(); ++i)
  295. addAndMakeVisible (pins.add (new PinComponent (panel, { pluginID, i }, true)));
  296. if (f->getProcessor()->acceptsMidi())
  297. addAndMakeVisible (pins.add (new PinComponent (panel, { pluginID, AudioProcessorGraph::midiChannelIndex }, true)));
  298. for (int i = 0; i < f->getProcessor()->getTotalNumOutputChannels(); ++i)
  299. addAndMakeVisible (pins.add (new PinComponent (panel, { pluginID, i }, false)));
  300. if (f->getProcessor()->producesMidi())
  301. addAndMakeVisible (pins.add (new PinComponent (panel, { pluginID, AudioProcessorGraph::midiChannelIndex }, false)));
  302. resized();
  303. }
  304. }
  305. AudioProcessor* getProcessor() const
  306. {
  307. if (auto node = graph.graph.getNodeForId (pluginID))
  308. return node->getProcessor();
  309. return {};
  310. }
  311. void showPopupMenu()
  312. {
  313. menu.reset (new PopupMenu);
  314. menu->addItem (1, "Delete this filter");
  315. menu->addItem (2, "Disconnect all pins");
  316. menu->addItem (3, "Toggle Bypass");
  317. if (getProcessor()->hasEditor())
  318. {
  319. menu->addSeparator();
  320. menu->addItem (10, "Show plugin GUI");
  321. menu->addItem (11, "Show all programs");
  322. menu->addItem (12, "Show all parameters");
  323. #if JUCE_WINDOWS && JUCE_WIN_PER_MONITOR_DPI_AWARE
  324. auto isTicked = false;
  325. if (auto* node = graph.graph.getNodeForId (pluginID))
  326. isTicked = node->properties["DPIAware"];
  327. menu->addItem (13, "Enable DPI awareness", true, isTicked);
  328. #endif
  329. menu->addItem (14, "Show debug log");
  330. }
  331. menu->addSeparator();
  332. menu->addItem (20, "Configure Audio I/O");
  333. menu->addItem (21, "Test state save/load");
  334. menu->showMenuAsync ({}, ModalCallbackFunction::create
  335. ([this] (int r) {
  336. switch (r)
  337. {
  338. case 1: graph.graph.removeNode (pluginID); break;
  339. case 2: graph.graph.disconnectNode (pluginID); break;
  340. case 3:
  341. {
  342. if (auto* node = graph.graph.getNodeForId (pluginID))
  343. node->setBypassed (! node->isBypassed());
  344. repaint();
  345. break;
  346. }
  347. case 10: showWindow (PluginWindow::Type::normal); break;
  348. case 11: showWindow (PluginWindow::Type::programs); break;
  349. case 12: showWindow (PluginWindow::Type::generic) ; break;
  350. #if JUCE_WINDOWS && JUCE_WIN_PER_MONITOR_DPI_AWARE
  351. case 13:
  352. {
  353. if (auto* node = graph.graph.getNodeForId (pluginID))
  354. node->properties.set ("DPIAware", ! node->properties ["DPIAware"]);
  355. break;
  356. }
  357. #endif
  358. case 14: showWindow (PluginWindow::Type::debug); break;
  359. case 20: showWindow (PluginWindow::Type::audioIO); break;
  360. case 21: testStateSaveLoad(); break;
  361. default: break;
  362. }
  363. }));
  364. }
  365. void testStateSaveLoad()
  366. {
  367. if (auto* processor = getProcessor())
  368. {
  369. MemoryBlock state;
  370. processor->getStateInformation (state);
  371. processor->setStateInformation (state.getData(), (int) state.getSize());
  372. }
  373. }
  374. void showWindow (PluginWindow::Type type)
  375. {
  376. if (auto node = graph.graph.getNodeForId (pluginID))
  377. if (auto* w = graph.getOrCreateWindowFor (node, type))
  378. w->toFront (true);
  379. }
  380. void timerCallback() override
  381. {
  382. // this should only be called on touch devices
  383. jassert (isOnTouchDevice());
  384. stopTimer();
  385. showPopupMenu();
  386. }
  387. void parameterValueChanged (int, float) override
  388. {
  389. repaint();
  390. }
  391. void parameterGestureChanged (int, bool) override {}
  392. GraphEditorPanel& panel;
  393. PluginGraph& graph;
  394. const AudioProcessorGraph::NodeID pluginID;
  395. OwnedArray<PinComponent> pins;
  396. int numInputs = 0, numOutputs = 0;
  397. int pinSize = 16;
  398. Point<int> originalPos;
  399. Font font { 13.0f, Font::bold };
  400. int numIns = 0, numOuts = 0;
  401. DropShadowEffect shadow;
  402. std::unique_ptr<PopupMenu> menu;
  403. };
  404. //==============================================================================
  405. struct GraphEditorPanel::ConnectorComponent : public Component,
  406. public SettableTooltipClient
  407. {
  408. ConnectorComponent (GraphEditorPanel& p) : 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 (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. 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. }
  934. void GraphDocumentComponent::init()
  935. {
  936. graphPanel.reset (new GraphEditorPanel (*graph));
  937. addAndMakeVisible (graphPanel.get());
  938. graphPlayer.setProcessor (&graph->graph);
  939. keyState.addListener (&graphPlayer.getMidiMessageCollector());
  940. keyboardComp.reset (new MidiKeyboardComponent (keyState, MidiKeyboardComponent::horizontalKeyboard));
  941. addAndMakeVisible (keyboardComp.get());
  942. statusBar.reset (new TooltipBar());
  943. addAndMakeVisible (statusBar.get());
  944. graphPanel->updateComponents();
  945. if (isOnTouchDevice())
  946. {
  947. if (isOnTouchDevice())
  948. {
  949. titleBarComponent.reset (new TitleBarComponent (*this));
  950. addAndMakeVisible (titleBarComponent.get());
  951. }
  952. pluginListBoxModel.reset (new PluginListBoxModel (pluginListBox, pluginList));
  953. pluginListBox.setModel (pluginListBoxModel.get());
  954. pluginListBox.setRowHeight (40);
  955. pluginListSidePanel.setContent (&pluginListBox, false);
  956. mobileSettingsSidePanel.setContent (new AudioDeviceSelectorComponent (deviceManager,
  957. 0, 2, 0, 2,
  958. true, true, true, false));
  959. if (isOnTouchDevice())
  960. {
  961. addAndMakeVisible (pluginListSidePanel);
  962. addAndMakeVisible (mobileSettingsSidePanel);
  963. }
  964. }
  965. }
  966. GraphDocumentComponent::~GraphDocumentComponent()
  967. {
  968. releaseGraph();
  969. keyState.removeListener (&graphPlayer.getMidiMessageCollector());
  970. }
  971. void GraphDocumentComponent::resized()
  972. {
  973. auto r = getLocalBounds();
  974. const int titleBarHeight = 40;
  975. const int keysHeight = 60;
  976. const int statusHeight = 20;
  977. if (isOnTouchDevice())
  978. titleBarComponent->setBounds (r.removeFromTop(titleBarHeight));
  979. keyboardComp->setBounds (r.removeFromBottom (keysHeight));
  980. statusBar->setBounds (r.removeFromBottom (statusHeight));
  981. graphPanel->setBounds (r);
  982. checkAvailableWidth();
  983. }
  984. void GraphDocumentComponent::createNewPlugin (const PluginDescription& desc, Point<int> pos)
  985. {
  986. graphPanel->createNewPlugin (desc, pos);
  987. }
  988. void GraphDocumentComponent::unfocusKeyboardComponent()
  989. {
  990. keyboardComp->unfocusAllComponents();
  991. }
  992. void GraphDocumentComponent::releaseGraph()
  993. {
  994. deviceManager.removeAudioCallback (&graphPlayer);
  995. deviceManager.removeMidiInputDeviceCallback ({}, &graphPlayer.getMidiMessageCollector());
  996. if (graphPanel != nullptr)
  997. {
  998. deviceManager.removeChangeListener (graphPanel.get());
  999. graphPanel = nullptr;
  1000. }
  1001. keyboardComp = nullptr;
  1002. statusBar = nullptr;
  1003. graphPlayer.setProcessor (nullptr);
  1004. graph = nullptr;
  1005. }
  1006. bool GraphDocumentComponent::isInterestedInDragSource (const SourceDetails& details)
  1007. {
  1008. return ((dynamic_cast<ListBox*> (details.sourceComponent.get()) != nullptr)
  1009. && details.description.toString().startsWith ("PLUGIN"));
  1010. }
  1011. void GraphDocumentComponent::itemDropped (const SourceDetails& details)
  1012. {
  1013. // don't allow items to be dropped behind the sidebar
  1014. if (pluginListSidePanel.getBounds().contains (details.localPosition))
  1015. return;
  1016. auto pluginTypeIndex = details.description.toString()
  1017. .fromFirstOccurrenceOf ("PLUGIN: ", false, false)
  1018. .getIntValue();
  1019. // must be a valid index!
  1020. jassert (isPositiveAndBelow (pluginTypeIndex, pluginList.getNumTypes()));
  1021. createNewPlugin (pluginList.getTypes()[pluginTypeIndex], details.localPosition);
  1022. }
  1023. void GraphDocumentComponent::showSidePanel (bool showSettingsPanel)
  1024. {
  1025. if (showSettingsPanel)
  1026. mobileSettingsSidePanel.showOrHide (true);
  1027. else
  1028. pluginListSidePanel.showOrHide (true);
  1029. checkAvailableWidth();
  1030. lastOpenedSidePanel = showSettingsPanel ? &mobileSettingsSidePanel
  1031. : &pluginListSidePanel;
  1032. }
  1033. void GraphDocumentComponent::hideLastSidePanel()
  1034. {
  1035. if (lastOpenedSidePanel != nullptr)
  1036. lastOpenedSidePanel->showOrHide (false);
  1037. if (mobileSettingsSidePanel.isPanelShowing()) lastOpenedSidePanel = &mobileSettingsSidePanel;
  1038. else if (pluginListSidePanel.isPanelShowing()) lastOpenedSidePanel = &pluginListSidePanel;
  1039. else lastOpenedSidePanel = nullptr;
  1040. }
  1041. void GraphDocumentComponent::checkAvailableWidth()
  1042. {
  1043. if (mobileSettingsSidePanel.isPanelShowing() && pluginListSidePanel.isPanelShowing())
  1044. {
  1045. if (getWidth() - (mobileSettingsSidePanel.getWidth() + pluginListSidePanel.getWidth()) < 150)
  1046. hideLastSidePanel();
  1047. }
  1048. }
  1049. void GraphDocumentComponent::setDoublePrecision (bool doublePrecision)
  1050. {
  1051. graphPlayer.setDoublePrecisionProcessing (doublePrecision);
  1052. }
  1053. bool GraphDocumentComponent::closeAnyOpenPluginWindows()
  1054. {
  1055. return graphPanel->graph.closeAnyOpenPluginWindows();
  1056. }