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.

1140 lines
34KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../JuceLibraryCode/JuceHeader.h"
  18. #include "GraphEditorPanel.h"
  19. #include "InternalFilters.h"
  20. #include "MainHostWindow.h"
  21. //==============================================================================
  22. class PluginWindow;
  23. static Array <PluginWindow*> activePluginWindows;
  24. PluginWindow::PluginWindow (Component* const pluginEditor,
  25. AudioProcessorGraph::Node* const o,
  26. WindowFormatType t)
  27. : DocumentWindow (pluginEditor->getName(), Colours::lightblue,
  28. DocumentWindow::minimiseButton | DocumentWindow::closeButton),
  29. owner (o),
  30. type (t)
  31. {
  32. setSize (400, 300);
  33. setContentOwned (pluginEditor, true);
  34. setTopLeftPosition (owner->properties.getWithDefault (getLastXProp (type), Random::getSystemRandom().nextInt (500)),
  35. owner->properties.getWithDefault (getLastYProp (type), Random::getSystemRandom().nextInt (500)));
  36. owner->properties.set (getOpenProp (type), true);
  37. setVisible (true);
  38. activePluginWindows.add (this);
  39. }
  40. void PluginWindow::closeCurrentlyOpenWindowsFor (const uint32 nodeId)
  41. {
  42. for (int i = activePluginWindows.size(); --i >= 0;)
  43. if (activePluginWindows.getUnchecked(i)->owner->nodeId == nodeId)
  44. delete activePluginWindows.getUnchecked (i);
  45. }
  46. void PluginWindow::closeAllCurrentlyOpenWindows()
  47. {
  48. if (activePluginWindows.size() > 0)
  49. {
  50. for (int i = activePluginWindows.size(); --i >= 0;)
  51. delete activePluginWindows.getUnchecked (i);
  52. Component dummyModalComp;
  53. dummyModalComp.enterModalState();
  54. MessageManager::getInstance()->runDispatchLoopUntil (50);
  55. }
  56. }
  57. //==============================================================================
  58. class ProcessorProgramPropertyComp : public PropertyComponent,
  59. private AudioProcessorListener
  60. {
  61. public:
  62. ProcessorProgramPropertyComp (const String& name, AudioProcessor& p, int index_)
  63. : PropertyComponent (name),
  64. owner (p),
  65. index (index_)
  66. {
  67. owner.addListener (this);
  68. }
  69. ~ProcessorProgramPropertyComp()
  70. {
  71. owner.removeListener (this);
  72. }
  73. void refresh() { }
  74. void audioProcessorChanged (AudioProcessor*) { }
  75. void audioProcessorParameterChanged (AudioProcessor*, int, float) { }
  76. private:
  77. AudioProcessor& owner;
  78. const int index;
  79. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProcessorProgramPropertyComp)
  80. };
  81. class ProgramAudioProcessorEditor : public AudioProcessorEditor
  82. {
  83. public:
  84. ProgramAudioProcessorEditor (AudioProcessor* const p)
  85. : AudioProcessorEditor (p)
  86. {
  87. jassert (p != nullptr);
  88. setOpaque (true);
  89. addAndMakeVisible (panel);
  90. Array<PropertyComponent*> programs;
  91. const int numPrograms = p->getNumPrograms();
  92. int totalHeight = 0;
  93. for (int i = 0; i < numPrograms; ++i)
  94. {
  95. String name (p->getProgramName (i).trim());
  96. if (name.isEmpty())
  97. name = "Unnamed";
  98. ProcessorProgramPropertyComp* const pc = new ProcessorProgramPropertyComp (name, *p, i);
  99. programs.add (pc);
  100. totalHeight += pc->getPreferredHeight();
  101. }
  102. panel.addProperties (programs);
  103. setSize (400, jlimit (25, 400, totalHeight));
  104. }
  105. void paint (Graphics& g)
  106. {
  107. g.fillAll (Colours::grey);
  108. }
  109. void resized()
  110. {
  111. panel.setBounds (getLocalBounds());
  112. }
  113. private:
  114. PropertyPanel panel;
  115. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProgramAudioProcessorEditor)
  116. };
  117. //==============================================================================
  118. PluginWindow* PluginWindow::getWindowFor (AudioProcessorGraph::Node* const node,
  119. WindowFormatType type)
  120. {
  121. jassert (node != nullptr);
  122. for (int i = activePluginWindows.size(); --i >= 0;)
  123. if (activePluginWindows.getUnchecked(i)->owner == node
  124. && activePluginWindows.getUnchecked(i)->type == type)
  125. return activePluginWindows.getUnchecked(i);
  126. AudioProcessor* processor = node->getProcessor();
  127. AudioProcessorEditor* ui = nullptr;
  128. if (type == Normal)
  129. {
  130. ui = processor->createEditorIfNeeded();
  131. if (ui == nullptr)
  132. type = Generic;
  133. }
  134. if (ui == nullptr)
  135. {
  136. if (type == Generic || type == Parameters)
  137. ui = new GenericAudioProcessorEditor (processor);
  138. else if (type == Programs)
  139. ui = new ProgramAudioProcessorEditor (processor);
  140. }
  141. if (ui != nullptr)
  142. {
  143. if (AudioPluginInstance* const plugin = dynamic_cast<AudioPluginInstance*> (processor))
  144. ui->setName (plugin->getName());
  145. return new PluginWindow (ui, node, type);
  146. }
  147. return nullptr;
  148. }
  149. PluginWindow::~PluginWindow()
  150. {
  151. activePluginWindows.removeFirstMatchingValue (this);
  152. clearContentComponent();
  153. }
  154. void PluginWindow::moved()
  155. {
  156. owner->properties.set (getLastXProp (type), getX());
  157. owner->properties.set (getLastYProp (type), getY());
  158. }
  159. void PluginWindow::closeButtonPressed()
  160. {
  161. owner->properties.set (getOpenProp (type), false);
  162. delete this;
  163. }
  164. //==============================================================================
  165. class PinComponent : public Component,
  166. public SettableTooltipClient
  167. {
  168. public:
  169. PinComponent (FilterGraph& graph_,
  170. const uint32 filterID_, const int index_, const bool isInput_)
  171. : filterID (filterID_),
  172. index (index_),
  173. isInput (isInput_),
  174. graph (graph_)
  175. {
  176. if (const AudioProcessorGraph::Node::Ptr node = graph.getNodeForId (filterID_))
  177. {
  178. String tip;
  179. if (index_ == FilterGraph::midiChannelNumber)
  180. {
  181. tip = isInput ? "MIDI Input" : "MIDI Output";
  182. }
  183. else
  184. {
  185. if (isInput)
  186. tip = node->getProcessor()->getInputChannelName (index_);
  187. else
  188. tip = node->getProcessor()->getOutputChannelName (index_);
  189. if (tip.isEmpty())
  190. tip = (isInput ? "Input " : "Output ") + String (index_ + 1);
  191. }
  192. setTooltip (tip);
  193. }
  194. setSize (16, 16);
  195. }
  196. void paint (Graphics& g)
  197. {
  198. const float w = (float) getWidth();
  199. const float h = (float) getHeight();
  200. Path p;
  201. p.addEllipse (w * 0.25f, h * 0.25f, w * 0.5f, h * 0.5f);
  202. p.addRectangle (w * 0.4f, isInput ? (0.5f * h) : 0.0f, w * 0.2f, h * 0.5f);
  203. g.setColour (index == FilterGraph::midiChannelNumber ? Colours::red : Colours::green);
  204. g.fillPath (p);
  205. }
  206. void mouseDown (const MouseEvent& e)
  207. {
  208. getGraphPanel()->beginConnectorDrag (isInput ? 0 : filterID,
  209. index,
  210. isInput ? filterID : 0,
  211. index,
  212. e);
  213. }
  214. void mouseDrag (const MouseEvent& e)
  215. {
  216. getGraphPanel()->dragConnector (e);
  217. }
  218. void mouseUp (const MouseEvent& e)
  219. {
  220. getGraphPanel()->endDraggingConnector (e);
  221. }
  222. const uint32 filterID;
  223. const int index;
  224. const bool isInput;
  225. private:
  226. FilterGraph& graph;
  227. GraphEditorPanel* getGraphPanel() const noexcept
  228. {
  229. return findParentComponentOfClass<GraphEditorPanel>();
  230. }
  231. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PinComponent)
  232. };
  233. //==============================================================================
  234. class FilterComponent : public Component
  235. {
  236. public:
  237. FilterComponent (FilterGraph& graph_,
  238. const uint32 filterID_)
  239. : graph (graph_),
  240. filterID (filterID_),
  241. numInputs (0),
  242. numOutputs (0),
  243. pinSize (16),
  244. font (13.0f, Font::bold),
  245. numIns (0),
  246. numOuts (0)
  247. {
  248. shadow.setShadowProperties (DropShadow (Colours::black.withAlpha (0.5f), 3, Point<int> (0, 1)));
  249. setComponentEffect (&shadow);
  250. setSize (150, 60);
  251. }
  252. ~FilterComponent()
  253. {
  254. deleteAllChildren();
  255. }
  256. void mouseDown (const MouseEvent& e)
  257. {
  258. originalPos = localPointToGlobal (Point<int>());
  259. toFront (true);
  260. if (e.mods.isPopupMenu())
  261. {
  262. PopupMenu m;
  263. m.addItem (1, "Delete this filter");
  264. m.addItem (2, "Disconnect all pins");
  265. m.addSeparator();
  266. m.addItem (3, "Show plugin UI");
  267. m.addItem (4, "Show all programs");
  268. m.addItem (5, "Show all parameters");
  269. m.addItem (6, "Test state save/load");
  270. const int r = m.show();
  271. if (r == 1)
  272. {
  273. graph.removeFilter (filterID);
  274. return;
  275. }
  276. else if (r == 2)
  277. {
  278. graph.disconnectFilter (filterID);
  279. }
  280. else
  281. {
  282. if (AudioProcessorGraph::Node::Ptr f = graph.getNodeForId (filterID))
  283. {
  284. AudioProcessor* const processor = f->getProcessor();
  285. jassert (processor != nullptr);
  286. if (r == 6)
  287. {
  288. MemoryBlock state;
  289. processor->getStateInformation (state);
  290. processor->setStateInformation (state.getData(), (int) state.getSize());
  291. }
  292. else
  293. {
  294. PluginWindow::WindowFormatType type = processor->hasEditor() ? PluginWindow::Normal
  295. : PluginWindow::Generic;
  296. switch (r)
  297. {
  298. case 4: type = PluginWindow::Programs; break;
  299. case 5: type = PluginWindow::Parameters; break;
  300. default: break;
  301. };
  302. if (PluginWindow* const w = PluginWindow::getWindowFor (f, type))
  303. w->toFront (true);
  304. }
  305. }
  306. }
  307. }
  308. }
  309. void mouseDrag (const MouseEvent& e)
  310. {
  311. if (! e.mods.isPopupMenu())
  312. {
  313. Point<int> pos (originalPos + Point<int> (e.getDistanceFromDragStartX(), e.getDistanceFromDragStartY()));
  314. if (getParentComponent() != nullptr)
  315. pos = getParentComponent()->getLocalPoint (nullptr, pos);
  316. graph.setNodePosition (filterID,
  317. (pos.getX() + getWidth() / 2) / (double) getParentWidth(),
  318. (pos.getY() + getHeight() / 2) / (double) getParentHeight());
  319. getGraphPanel()->updateComponents();
  320. }
  321. }
  322. void mouseUp (const MouseEvent& e)
  323. {
  324. if (e.mouseWasClicked() && e.getNumberOfClicks() == 2)
  325. {
  326. if (const AudioProcessorGraph::Node::Ptr f = graph.getNodeForId (filterID))
  327. if (PluginWindow* const w = PluginWindow::getWindowFor (f, PluginWindow::Normal))
  328. w->toFront (true);
  329. }
  330. else if (! e.mouseWasClicked())
  331. {
  332. graph.setChangedFlag (true);
  333. }
  334. }
  335. bool hitTest (int x, int y)
  336. {
  337. for (int i = getNumChildComponents(); --i >= 0;)
  338. if (getChildComponent(i)->getBounds().contains (x, y))
  339. return true;
  340. return x >= 3 && x < getWidth() - 6 && y >= pinSize && y < getHeight() - pinSize;
  341. }
  342. void paint (Graphics& g)
  343. {
  344. g.setColour (Colours::lightgrey);
  345. const int x = 4;
  346. const int y = pinSize;
  347. const int w = getWidth() - x * 2;
  348. const int h = getHeight() - pinSize * 2;
  349. g.fillRect (x, y, w, h);
  350. g.setColour (Colours::black);
  351. g.setFont (font);
  352. g.drawFittedText (getName(), getLocalBounds().reduced (4, 2), Justification::centred, 2);
  353. g.setColour (Colours::grey);
  354. g.drawRect (x, y, w, h);
  355. }
  356. void resized()
  357. {
  358. for (int i = 0; i < getNumChildComponents(); ++i)
  359. {
  360. if (PinComponent* const pc = dynamic_cast <PinComponent*> (getChildComponent(i)))
  361. {
  362. const int total = pc->isInput ? numIns : numOuts;
  363. const int index = pc->index == FilterGraph::midiChannelNumber ? (total - 1) : pc->index;
  364. pc->setBounds (proportionOfWidth ((1 + index) / (total + 1.0f)) - pinSize / 2,
  365. pc->isInput ? 0 : (getHeight() - pinSize),
  366. pinSize, pinSize);
  367. }
  368. }
  369. }
  370. void getPinPos (const int index, const bool isInput, float& x, float& y)
  371. {
  372. for (int i = 0; i < getNumChildComponents(); ++i)
  373. {
  374. if (PinComponent* const pc = dynamic_cast <PinComponent*> (getChildComponent(i)))
  375. {
  376. if (pc->index == index && isInput == pc->isInput)
  377. {
  378. x = getX() + pc->getX() + pc->getWidth() * 0.5f;
  379. y = getY() + pc->getY() + pc->getHeight() * 0.5f;
  380. break;
  381. }
  382. }
  383. }
  384. }
  385. void update()
  386. {
  387. const AudioProcessorGraph::Node::Ptr f (graph.getNodeForId (filterID));
  388. if (f == nullptr)
  389. {
  390. delete this;
  391. return;
  392. }
  393. numIns = f->getProcessor()->getNumInputChannels();
  394. if (f->getProcessor()->acceptsMidi())
  395. ++numIns;
  396. numOuts = f->getProcessor()->getNumOutputChannels();
  397. if (f->getProcessor()->producesMidi())
  398. ++numOuts;
  399. int w = 100;
  400. int h = 60;
  401. w = jmax (w, (jmax (numIns, numOuts) + 1) * 20);
  402. const int textWidth = font.getStringWidth (f->getProcessor()->getName());
  403. w = jmax (w, 16 + jmin (textWidth, 300));
  404. if (textWidth > 300)
  405. h = 100;
  406. setSize (w, h);
  407. setName (f->getProcessor()->getName());
  408. {
  409. double x, y;
  410. graph.getNodePosition (filterID, x, y);
  411. setCentreRelative ((float) x, (float) y);
  412. }
  413. if (numIns != numInputs || numOuts != numOutputs)
  414. {
  415. numInputs = numIns;
  416. numOutputs = numOuts;
  417. deleteAllChildren();
  418. int i;
  419. for (i = 0; i < f->getProcessor()->getNumInputChannels(); ++i)
  420. addAndMakeVisible (new PinComponent (graph, filterID, i, true));
  421. if (f->getProcessor()->acceptsMidi())
  422. addAndMakeVisible (new PinComponent (graph, filterID, FilterGraph::midiChannelNumber, true));
  423. for (i = 0; i < f->getProcessor()->getNumOutputChannels(); ++i)
  424. addAndMakeVisible (new PinComponent (graph, filterID, i, false));
  425. if (f->getProcessor()->producesMidi())
  426. addAndMakeVisible (new PinComponent (graph, filterID, FilterGraph::midiChannelNumber, false));
  427. resized();
  428. }
  429. }
  430. FilterGraph& graph;
  431. const uint32 filterID;
  432. int numInputs, numOutputs;
  433. private:
  434. int pinSize;
  435. Point<int> originalPos;
  436. Font font;
  437. int numIns, numOuts;
  438. DropShadowEffect shadow;
  439. GraphEditorPanel* getGraphPanel() const noexcept
  440. {
  441. return findParentComponentOfClass<GraphEditorPanel>();
  442. }
  443. FilterComponent (const FilterComponent&);
  444. FilterComponent& operator= (const FilterComponent&);
  445. };
  446. //==============================================================================
  447. class ConnectorComponent : public Component,
  448. public SettableTooltipClient
  449. {
  450. public:
  451. ConnectorComponent (FilterGraph& graph_)
  452. : sourceFilterID (0),
  453. destFilterID (0),
  454. sourceFilterChannel (0),
  455. destFilterChannel (0),
  456. graph (graph_),
  457. lastInputX (0),
  458. lastInputY (0),
  459. lastOutputX (0),
  460. lastOutputY (0)
  461. {
  462. setAlwaysOnTop (true);
  463. }
  464. void setInput (const uint32 sourceFilterID_, const int sourceFilterChannel_)
  465. {
  466. if (sourceFilterID != sourceFilterID_ || sourceFilterChannel != sourceFilterChannel_)
  467. {
  468. sourceFilterID = sourceFilterID_;
  469. sourceFilterChannel = sourceFilterChannel_;
  470. update();
  471. }
  472. }
  473. void setOutput (const uint32 destFilterID_, const int destFilterChannel_)
  474. {
  475. if (destFilterID != destFilterID_ || destFilterChannel != destFilterChannel_)
  476. {
  477. destFilterID = destFilterID_;
  478. destFilterChannel = destFilterChannel_;
  479. update();
  480. }
  481. }
  482. void dragStart (int x, int y)
  483. {
  484. lastInputX = (float) x;
  485. lastInputY = (float) y;
  486. resizeToFit();
  487. }
  488. void dragEnd (int x, int y)
  489. {
  490. lastOutputX = (float) x;
  491. lastOutputY = (float) y;
  492. resizeToFit();
  493. }
  494. void update()
  495. {
  496. float x1, y1, x2, y2;
  497. getPoints (x1, y1, x2, y2);
  498. if (lastInputX != x1
  499. || lastInputY != y1
  500. || lastOutputX != x2
  501. || lastOutputY != y2)
  502. {
  503. resizeToFit();
  504. }
  505. }
  506. void resizeToFit()
  507. {
  508. float x1, y1, x2, y2;
  509. getPoints (x1, y1, x2, y2);
  510. const Rectangle<int> newBounds ((int) jmin (x1, x2) - 4,
  511. (int) jmin (y1, y2) - 4,
  512. (int) std::abs (x1 - x2) + 8,
  513. (int) std::abs (y1 - y2) + 8);
  514. if (newBounds != getBounds())
  515. setBounds (newBounds);
  516. else
  517. resized();
  518. repaint();
  519. }
  520. void getPoints (float& x1, float& y1, float& x2, float& y2) const
  521. {
  522. x1 = lastInputX;
  523. y1 = lastInputY;
  524. x2 = lastOutputX;
  525. y2 = lastOutputY;
  526. if (GraphEditorPanel* const hostPanel = getGraphPanel())
  527. {
  528. if (FilterComponent* srcFilterComp = hostPanel->getComponentForFilter (sourceFilterID))
  529. srcFilterComp->getPinPos (sourceFilterChannel, false, x1, y1);
  530. if (FilterComponent* dstFilterComp = hostPanel->getComponentForFilter (destFilterID))
  531. dstFilterComp->getPinPos (destFilterChannel, true, x2, y2);
  532. }
  533. }
  534. void paint (Graphics& g)
  535. {
  536. if (sourceFilterChannel == FilterGraph::midiChannelNumber
  537. || destFilterChannel == FilterGraph::midiChannelNumber)
  538. {
  539. g.setColour (Colours::red);
  540. }
  541. else
  542. {
  543. g.setColour (Colours::green);
  544. }
  545. g.fillPath (linePath);
  546. }
  547. bool hitTest (int x, int y)
  548. {
  549. if (hitPath.contains ((float) x, (float) y))
  550. {
  551. double distanceFromStart, distanceFromEnd;
  552. getDistancesFromEnds (x, y, distanceFromStart, distanceFromEnd);
  553. // avoid clicking the connector when over a pin
  554. return distanceFromStart > 7.0 && distanceFromEnd > 7.0;
  555. }
  556. return false;
  557. }
  558. void mouseDown (const MouseEvent&)
  559. {
  560. dragging = false;
  561. }
  562. void mouseDrag (const MouseEvent& e)
  563. {
  564. if ((! dragging) && ! e.mouseWasClicked())
  565. {
  566. dragging = true;
  567. graph.removeConnection (sourceFilterID, sourceFilterChannel, destFilterID, destFilterChannel);
  568. double distanceFromStart, distanceFromEnd;
  569. getDistancesFromEnds (e.x, e.y, distanceFromStart, distanceFromEnd);
  570. const bool isNearerSource = (distanceFromStart < distanceFromEnd);
  571. getGraphPanel()->beginConnectorDrag (isNearerSource ? 0 : sourceFilterID,
  572. sourceFilterChannel,
  573. isNearerSource ? destFilterID : 0,
  574. destFilterChannel,
  575. e);
  576. }
  577. else if (dragging)
  578. {
  579. getGraphPanel()->dragConnector (e);
  580. }
  581. }
  582. void mouseUp (const MouseEvent& e)
  583. {
  584. if (dragging)
  585. getGraphPanel()->endDraggingConnector (e);
  586. }
  587. void resized()
  588. {
  589. float x1, y1, x2, y2;
  590. getPoints (x1, y1, x2, y2);
  591. lastInputX = x1;
  592. lastInputY = y1;
  593. lastOutputX = x2;
  594. lastOutputY = y2;
  595. x1 -= getX();
  596. y1 -= getY();
  597. x2 -= getX();
  598. y2 -= getY();
  599. linePath.clear();
  600. linePath.startNewSubPath (x1, y1);
  601. linePath.cubicTo (x1, y1 + (y2 - y1) * 0.33f,
  602. x2, y1 + (y2 - y1) * 0.66f,
  603. x2, y2);
  604. PathStrokeType wideStroke (8.0f);
  605. wideStroke.createStrokedPath (hitPath, linePath);
  606. PathStrokeType stroke (2.5f);
  607. stroke.createStrokedPath (linePath, linePath);
  608. const float arrowW = 5.0f;
  609. const float arrowL = 4.0f;
  610. Path arrow;
  611. arrow.addTriangle (-arrowL, arrowW,
  612. -arrowL, -arrowW,
  613. arrowL, 0.0f);
  614. arrow.applyTransform (AffineTransform::identity
  615. .rotated (float_Pi * 0.5f - (float) atan2 (x2 - x1, y2 - y1))
  616. .translated ((x1 + x2) * 0.5f,
  617. (y1 + y2) * 0.5f));
  618. linePath.addPath (arrow);
  619. linePath.setUsingNonZeroWinding (true);
  620. }
  621. uint32 sourceFilterID, destFilterID;
  622. int sourceFilterChannel, destFilterChannel;
  623. private:
  624. FilterGraph& graph;
  625. float lastInputX, lastInputY, lastOutputX, lastOutputY;
  626. Path linePath, hitPath;
  627. bool dragging;
  628. GraphEditorPanel* getGraphPanel() const noexcept
  629. {
  630. return findParentComponentOfClass<GraphEditorPanel>();
  631. }
  632. void getDistancesFromEnds (int x, int y, double& distanceFromStart, double& distanceFromEnd) const
  633. {
  634. float x1, y1, x2, y2;
  635. getPoints (x1, y1, x2, y2);
  636. distanceFromStart = juce_hypot (x - (x1 - getX()), y - (y1 - getY()));
  637. distanceFromEnd = juce_hypot (x - (x2 - getX()), y - (y2 - getY()));
  638. }
  639. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConnectorComponent)
  640. };
  641. //==============================================================================
  642. GraphEditorPanel::GraphEditorPanel (FilterGraph& graph_)
  643. : graph (graph_)
  644. {
  645. graph.addChangeListener (this);
  646. setOpaque (true);
  647. }
  648. GraphEditorPanel::~GraphEditorPanel()
  649. {
  650. graph.removeChangeListener (this);
  651. draggingConnector = nullptr;
  652. deleteAllChildren();
  653. }
  654. void GraphEditorPanel::paint (Graphics& g)
  655. {
  656. g.fillAll (Colours::white);
  657. }
  658. void GraphEditorPanel::mouseDown (const MouseEvent& e)
  659. {
  660. if (e.mods.isPopupMenu())
  661. {
  662. PopupMenu m;
  663. if (MainHostWindow* const mainWindow = findParentComponentOfClass<MainHostWindow>())
  664. {
  665. mainWindow->addPluginsToMenu (m);
  666. const int r = m.show();
  667. createNewPlugin (mainWindow->getChosenType (r), e.x, e.y);
  668. }
  669. }
  670. }
  671. void GraphEditorPanel::createNewPlugin (const PluginDescription* desc, int x, int y)
  672. {
  673. graph.addFilter (desc, x / (double) getWidth(), y / (double) getHeight());
  674. }
  675. FilterComponent* GraphEditorPanel::getComponentForFilter (const uint32 filterID) const
  676. {
  677. for (int i = getNumChildComponents(); --i >= 0;)
  678. {
  679. if (FilterComponent* const fc = dynamic_cast <FilterComponent*> (getChildComponent (i)))
  680. if (fc->filterID == filterID)
  681. return fc;
  682. }
  683. return nullptr;
  684. }
  685. ConnectorComponent* GraphEditorPanel::getComponentForConnection (const AudioProcessorGraph::Connection& conn) const
  686. {
  687. for (int i = getNumChildComponents(); --i >= 0;)
  688. {
  689. if (ConnectorComponent* const c = dynamic_cast <ConnectorComponent*> (getChildComponent (i)))
  690. if (c->sourceFilterID == conn.sourceNodeId
  691. && c->destFilterID == conn.destNodeId
  692. && c->sourceFilterChannel == conn.sourceChannelIndex
  693. && c->destFilterChannel == conn.destChannelIndex)
  694. return c;
  695. }
  696. return nullptr;
  697. }
  698. PinComponent* GraphEditorPanel::findPinAt (const int x, const int y) const
  699. {
  700. for (int i = getNumChildComponents(); --i >= 0;)
  701. {
  702. if (FilterComponent* fc = dynamic_cast <FilterComponent*> (getChildComponent (i)))
  703. {
  704. if (PinComponent* pin = dynamic_cast <PinComponent*> (fc->getComponentAt (x - fc->getX(),
  705. y - fc->getY())))
  706. return pin;
  707. }
  708. }
  709. return nullptr;
  710. }
  711. void GraphEditorPanel::resized()
  712. {
  713. updateComponents();
  714. }
  715. void GraphEditorPanel::changeListenerCallback (ChangeBroadcaster*)
  716. {
  717. updateComponents();
  718. }
  719. void GraphEditorPanel::updateComponents()
  720. {
  721. for (int i = getNumChildComponents(); --i >= 0;)
  722. {
  723. if (FilterComponent* const fc = dynamic_cast <FilterComponent*> (getChildComponent (i)))
  724. fc->update();
  725. }
  726. for (int i = getNumChildComponents(); --i >= 0;)
  727. {
  728. ConnectorComponent* const cc = dynamic_cast <ConnectorComponent*> (getChildComponent (i));
  729. if (cc != nullptr && cc != draggingConnector)
  730. {
  731. if (graph.getConnectionBetween (cc->sourceFilterID, cc->sourceFilterChannel,
  732. cc->destFilterID, cc->destFilterChannel) == nullptr)
  733. {
  734. delete cc;
  735. }
  736. else
  737. {
  738. cc->update();
  739. }
  740. }
  741. }
  742. for (int i = graph.getNumFilters(); --i >= 0;)
  743. {
  744. const AudioProcessorGraph::Node::Ptr f (graph.getNode (i));
  745. if (getComponentForFilter (f->nodeId) == 0)
  746. {
  747. FilterComponent* const comp = new FilterComponent (graph, f->nodeId);
  748. addAndMakeVisible (comp);
  749. comp->update();
  750. }
  751. }
  752. for (int i = graph.getNumConnections(); --i >= 0;)
  753. {
  754. const AudioProcessorGraph::Connection* const c = graph.getConnection (i);
  755. if (getComponentForConnection (*c) == 0)
  756. {
  757. ConnectorComponent* const comp = new ConnectorComponent (graph);
  758. addAndMakeVisible (comp);
  759. comp->setInput (c->sourceNodeId, c->sourceChannelIndex);
  760. comp->setOutput (c->destNodeId, c->destChannelIndex);
  761. }
  762. }
  763. }
  764. void GraphEditorPanel::beginConnectorDrag (const uint32 sourceFilterID, const int sourceFilterChannel,
  765. const uint32 destFilterID, const int destFilterChannel,
  766. const MouseEvent& e)
  767. {
  768. draggingConnector = dynamic_cast <ConnectorComponent*> (e.originalComponent);
  769. if (draggingConnector == nullptr)
  770. draggingConnector = new ConnectorComponent (graph);
  771. draggingConnector->setInput (sourceFilterID, sourceFilterChannel);
  772. draggingConnector->setOutput (destFilterID, destFilterChannel);
  773. addAndMakeVisible (draggingConnector);
  774. draggingConnector->toFront (false);
  775. dragConnector (e);
  776. }
  777. void GraphEditorPanel::dragConnector (const MouseEvent& e)
  778. {
  779. const MouseEvent e2 (e.getEventRelativeTo (this));
  780. if (draggingConnector != nullptr)
  781. {
  782. draggingConnector->setTooltip (String::empty);
  783. int x = e2.x;
  784. int y = e2.y;
  785. if (PinComponent* const pin = findPinAt (x, y))
  786. {
  787. uint32 srcFilter = draggingConnector->sourceFilterID;
  788. int srcChannel = draggingConnector->sourceFilterChannel;
  789. uint32 dstFilter = draggingConnector->destFilterID;
  790. int dstChannel = draggingConnector->destFilterChannel;
  791. if (srcFilter == 0 && ! pin->isInput)
  792. {
  793. srcFilter = pin->filterID;
  794. srcChannel = pin->index;
  795. }
  796. else if (dstFilter == 0 && pin->isInput)
  797. {
  798. dstFilter = pin->filterID;
  799. dstChannel = pin->index;
  800. }
  801. if (graph.canConnect (srcFilter, srcChannel, dstFilter, dstChannel))
  802. {
  803. x = pin->getParentComponent()->getX() + pin->getX() + pin->getWidth() / 2;
  804. y = pin->getParentComponent()->getY() + pin->getY() + pin->getHeight() / 2;
  805. draggingConnector->setTooltip (pin->getTooltip());
  806. }
  807. }
  808. if (draggingConnector->sourceFilterID == 0)
  809. draggingConnector->dragStart (x, y);
  810. else
  811. draggingConnector->dragEnd (x, y);
  812. }
  813. }
  814. void GraphEditorPanel::endDraggingConnector (const MouseEvent& e)
  815. {
  816. if (draggingConnector == nullptr)
  817. return;
  818. draggingConnector->setTooltip (String::empty);
  819. const MouseEvent e2 (e.getEventRelativeTo (this));
  820. uint32 srcFilter = draggingConnector->sourceFilterID;
  821. int srcChannel = draggingConnector->sourceFilterChannel;
  822. uint32 dstFilter = draggingConnector->destFilterID;
  823. int dstChannel = draggingConnector->destFilterChannel;
  824. draggingConnector = nullptr;
  825. if (PinComponent* const pin = findPinAt (e2.x, e2.y))
  826. {
  827. if (srcFilter == 0)
  828. {
  829. if (pin->isInput)
  830. return;
  831. srcFilter = pin->filterID;
  832. srcChannel = pin->index;
  833. }
  834. else
  835. {
  836. if (! pin->isInput)
  837. return;
  838. dstFilter = pin->filterID;
  839. dstChannel = pin->index;
  840. }
  841. graph.addConnection (srcFilter, srcChannel, dstFilter, dstChannel);
  842. }
  843. }
  844. //==============================================================================
  845. class TooltipBar : public Component,
  846. private Timer
  847. {
  848. public:
  849. TooltipBar()
  850. {
  851. startTimer (100);
  852. }
  853. void paint (Graphics& g)
  854. {
  855. g.setFont (Font (getHeight() * 0.7f, Font::bold));
  856. g.setColour (Colours::black);
  857. g.drawFittedText (tip, 10, 0, getWidth() - 12, getHeight(), Justification::centredLeft, 1);
  858. }
  859. void timerCallback()
  860. {
  861. Component* const underMouse = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();
  862. TooltipClient* const ttc = dynamic_cast <TooltipClient*> (underMouse);
  863. String newTip;
  864. if (ttc != nullptr && ! (underMouse->isMouseButtonDown() || underMouse->isCurrentlyBlockedByAnotherModalComponent()))
  865. newTip = ttc->getTooltip();
  866. if (newTip != tip)
  867. {
  868. tip = newTip;
  869. repaint();
  870. }
  871. }
  872. private:
  873. String tip;
  874. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TooltipBar)
  875. };
  876. //==============================================================================
  877. GraphDocumentComponent::GraphDocumentComponent (AudioPluginFormatManager& formatManager,
  878. AudioDeviceManager* deviceManager_)
  879. : graph (formatManager), deviceManager (deviceManager_),
  880. graphPlayer (getAppProperties().getUserSettings()->getBoolValue ("doublePrecisionProcessing", false))
  881. {
  882. addAndMakeVisible (graphPanel = new GraphEditorPanel (graph));
  883. deviceManager->addChangeListener (graphPanel);
  884. graphPlayer.setProcessor (&graph.getGraph());
  885. keyState.addListener (&graphPlayer.getMidiMessageCollector());
  886. addAndMakeVisible (keyboardComp = new MidiKeyboardComponent (keyState,
  887. MidiKeyboardComponent::horizontalKeyboard));
  888. addAndMakeVisible (statusBar = new TooltipBar());
  889. deviceManager->addAudioCallback (&graphPlayer);
  890. deviceManager->addMidiInputCallback (String::empty, &graphPlayer.getMidiMessageCollector());
  891. graphPanel->updateComponents();
  892. }
  893. GraphDocumentComponent::~GraphDocumentComponent()
  894. {
  895. deviceManager->removeAudioCallback (&graphPlayer);
  896. deviceManager->removeMidiInputCallback (String::empty, &graphPlayer.getMidiMessageCollector());
  897. deviceManager->removeChangeListener (graphPanel);
  898. deleteAllChildren();
  899. graphPlayer.setProcessor (nullptr);
  900. keyState.removeListener (&graphPlayer.getMidiMessageCollector());
  901. graph.clear();
  902. }
  903. void GraphDocumentComponent::resized()
  904. {
  905. const int keysHeight = 60;
  906. const int statusHeight = 20;
  907. graphPanel->setBounds (0, 0, getWidth(), getHeight() - keysHeight);
  908. statusBar->setBounds (0, getHeight() - keysHeight - statusHeight, getWidth(), statusHeight);
  909. keyboardComp->setBounds (0, getHeight() - keysHeight, getWidth(), keysHeight);
  910. }
  911. void GraphDocumentComponent::createNewPlugin (const PluginDescription* desc, int x, int y)
  912. {
  913. graphPanel->createNewPlugin (desc, x, y);
  914. }