From 7886a44dc5a017764fb2a525e58a64e6e4b60973 Mon Sep 17 00:00:00 2001
From: Josh Siegle <jsiegle@mit.edu>
Date: Sat, 11 Jan 2014 02:18:49 -0500
Subject: [PATCH] GraphViewer now reflects the structure of the ProcessorGraph

Splitters are supported, but Mergers are not. Still a bit buggy,
but it's definitely a start!
---
 Source/Processors/Editors/GenericEditor.cpp  | 31 +++++++
 Source/Processors/Editors/GenericEditor.h    | 19 +++++
 Source/Processors/Editors/MergerEditor.cpp   | 19 +++++
 Source/Processors/Editors/MergerEditor.h     |  2 +
 Source/Processors/Editors/SplitterEditor.cpp | 19 +++++
 Source/Processors/Editors/SplitterEditor.h   |  3 +
 Source/UI/GraphViewer.cpp                    | 88 +++++++++++++++++---
 Source/UI/GraphViewer.h                      |  3 +
 8 files changed, 173 insertions(+), 11 deletions(-)

diff --git a/Source/Processors/Editors/GenericEditor.cpp b/Source/Processors/Editors/GenericEditor.cpp
index 06d5210ff..ec60676fe 100755
--- a/Source/Processors/Editors/GenericEditor.cpp
+++ b/Source/Processors/Editors/GenericEditor.cpp
@@ -546,6 +546,37 @@ void GenericEditor::loadEditorParameters(XmlElement* xml)
 
 }
 
+GenericEditor* GenericEditor::getSourceEditor()
+{
+    
+    GenericProcessor* sourceNode = getProcessor()->getSourceNode();
+    
+    if (sourceNode != nullptr)
+        return sourceNode->getEditor();
+    else
+        return nullptr;
+}
+
+GenericEditor* GenericEditor::getDestEditor()
+{
+    GenericProcessor* destNode = getProcessor()->getDestNode();
+    
+    if (destNode != nullptr)
+        return destNode->getEditor();
+    else
+        return nullptr;
+}
+
+bool GenericEditor::isSplitter()
+{
+    return getProcessor()->isSplitter();
+}
+
+bool GenericEditor::isMerger()
+{
+    return getProcessor()->isMerger();
+}
+
 
 /////////////////////// BUTTONS ///////////////////////////////
 
diff --git a/Source/Processors/Editors/GenericEditor.h b/Source/Processors/Editors/GenericEditor.h
index dc0c24bf3..953edfd48 100755
--- a/Source/Processors/Editors/GenericEditor.h
+++ b/Source/Processors/Editors/GenericEditor.h
@@ -161,9 +161,21 @@ public:
 
     /** Required for SplitterEditor only.*/
     virtual void switchDest() { }
+    
 
     /** Required for SplitterEditor and MergerEditor only.*/
     virtual void switchIO(int) { }
+    
+    /** Required for SplitterEditor and MergerEditor only.*/
+    virtual int getPathForEditor(GenericEditor* editor) { return -1;}
+    
+    /** Used by GraphViewer */
+    bool isSplitter();
+    
+    /** Used by GraphViewer */
+    bool isMerger();
+    
+    
 
     /** Handles button clicks for all editors. Deals with clicks on the editor's
         title bar and channel selector drawer. */
@@ -242,6 +254,13 @@ public:
 
     /** Syncs parametereditor colors with parameter values */
     void updateParameterButtons(int parameterIndex = -1);
+    
+    /** Returns the editor of this processor's source */
+    GenericEditor* getSourceEditor();
+    
+    /** Returns the editor of this processor's destination */
+    GenericEditor* getDestEditor();
+    
 protected:
 
     /** A pointer to the button that opens the drawer for the ChannelSelector. */
diff --git a/Source/Processors/Editors/MergerEditor.cpp b/Source/Processors/Editors/MergerEditor.cpp
index b1fe60e50..bcfcd905d 100755
--- a/Source/Processors/Editors/MergerEditor.cpp
+++ b/Source/Processors/Editors/MergerEditor.cpp
@@ -186,6 +186,24 @@ void MergerEditor::switchSource(int source)
     }
 }
 
+int MergerEditor::getPathForEditor(GenericEditor* editor)
+{
+    Merger* processor = (Merger*) getProcessor();
+    
+    for (int pathNum = 0; pathNum < 2; pathNum++)
+    {
+    switchSource(pathNum);
+    
+    if (processor->getSourceNode() != nullptr)
+    {
+        if (processor->getEditor() == editor)
+            return pathNum;
+    }
+    }
+    
+    return -1;
+
+}
 
 void MergerEditor::switchIO(int source)
 {
@@ -194,6 +212,7 @@ void MergerEditor::switchIO(int source)
     select();
 }
 
+
 void MergerEditor::switchSource()
 {
 
diff --git a/Source/Processors/Editors/MergerEditor.h b/Source/Processors/Editors/MergerEditor.h
index b37818907..edcf05efe 100755
--- a/Source/Processors/Editors/MergerEditor.h
+++ b/Source/Processors/Editors/MergerEditor.h
@@ -51,6 +51,8 @@ public:
     void switchIO(int);
 
     void mouseDown(const MouseEvent& event);
+    
+    int getPathForEditor(GenericEditor* editor);
 
 private:
 
diff --git a/Source/Processors/Editors/SplitterEditor.cpp b/Source/Processors/Editors/SplitterEditor.cpp
index 265bb0240..d65b4befe 100755
--- a/Source/Processors/Editors/SplitterEditor.cpp
+++ b/Source/Processors/Editors/SplitterEditor.cpp
@@ -141,6 +141,25 @@ void SplitterEditor::switchIO(int dest)
     select();
 }
 
+int SplitterEditor::getPathForEditor(GenericEditor* editor)
+{
+    Splitter* processor = (Splitter*) getProcessor();
+    
+    for (int pathNum = 0; pathNum < 2; pathNum++)
+    {
+        processor->switchIO();
+        
+        if (processor->getDestNode() != nullptr)
+        {
+            if (processor->getDestNode()->getEditor() == editor)
+                return processor->getPath();
+        }
+    }
+    
+    return -1;
+    
+}
+
 
 void SplitterEditor::switchDest()
 {
diff --git a/Source/Processors/Editors/SplitterEditor.h b/Source/Processors/Editors/SplitterEditor.h
index d371debdd..db7b6896f 100755
--- a/Source/Processors/Editors/SplitterEditor.h
+++ b/Source/Processors/Editors/SplitterEditor.h
@@ -48,6 +48,9 @@ public:
     void switchDest();
 
     void switchIO(int i);
+    
+    int getPathForEditor(GenericEditor* editor);
+
 
 private:
 
diff --git a/Source/UI/GraphViewer.cpp b/Source/UI/GraphViewer.cpp
index 00536f2df..debf13c75 100644
--- a/Source/UI/GraphViewer.cpp
+++ b/Source/UI/GraphViewer.cpp
@@ -50,13 +50,7 @@ void GraphViewer::addNode(GenericEditor* editor)
 void GraphViewer::removeNode(GenericEditor* editor)
 {
     
-    for (int i = 0; i < availableNodes.size(); i++)
-    {
-        if (availableNodes[i]->hasEditor(editor))
-        {
-            availableNodes.remove(i);
-        }
-    }
+    availableNodes.remove(indexOfEditor(editor));
     
     updateNodeLocations();
 
@@ -66,8 +60,10 @@ void GraphViewer::updateNodeLocations()
 {
     for (int i = 0; i < availableNodes.size(); i++)
     {
-        availableNodes[i]->setBounds(25,10+40*i,200,20);
+        availableNodes[i]->updateBoundaries();
     }
+    
+    repaint();
 }
 
 void GraphViewer::removeAllNodes()
@@ -76,6 +72,21 @@ void GraphViewer::removeAllNodes()
     
 }
 
+int GraphViewer::indexOfEditor(GenericEditor* editor)
+{
+    int index = -1;
+    
+    for (int i = 0; i < availableNodes.size(); i++)
+    {
+        if (availableNodes[i]->hasEditor(editor))
+        {
+            return i;
+        }
+    }
+    
+    return index;
+}
+
 
 void GraphViewer::paint(Graphics& g)
 {
@@ -135,18 +146,73 @@ const String GraphNode::getName()
     return editor->getName();
 }
 
+void GraphNode::updateBoundaries()
+{
+    int level = -1;
+    int chain = 0;
+    
+    GenericEditor* ed = editor;
+    
+    while (ed != nullptr)
+    {
+        level++;
+
+        GenericEditor* sourceEditor = ed->getSourceEditor();
+        
+        if (sourceEditor != nullptr)
+        {
+            if (sourceEditor->isSplitter())
+            {
+                if (sourceEditor->getPathForEditor(ed) == 1)
+                    chain++;
+            }
+        }
+        ed = sourceEditor;
+    
+    }
+    
+    setBounds(20+chain*140, 20+level*40, 150, 50);
+}
+
 void GraphNode::paint(Graphics& g)
 {
+    if (editor->getDestEditor() != nullptr)
+    {
+        Line<float> line = Line<float>(10,10,10,50);
+        
+        g.setColour(Colours::grey);
+        g.drawLine(line, 2.0f);
+        
+        if (editor->isSplitter())
+        {
+            Path linePath;
+            float x1 = 10;
+            float y1 = 19;
+            float x2 = 150;
+            float y2 = 45;
+            linePath.startNewSubPath (x1, y1);
+            linePath.cubicTo (x1, y1 + (y2 - y1) * 0.33f,
+                              x2, y1 + (y2 - y1) * 0.66f,
+                              x2, y2);
+            
+            PathStrokeType stroke (2.0f);
+            g.strokePath(linePath, stroke);
+
+        }
+    }
+
     if (mouseOver)
     {
         g.setColour(Colours::yellow);
-        g.fillEllipse(0,0,getHeight(),getHeight());
+        g.fillEllipse(0,0,20,20);
     } else {
         g.setColour(Colours::lightgrey);
-        g.fillEllipse(1,1,getHeight()-2,getHeight()-2);
+        g.fillEllipse(1,1,18,18);
     
     }
     
-    g.drawText(getName(), getHeight()+5, 0, getWidth()-getHeight(), getHeight(), Justification::left, true);
+    g.drawText(getName(), 25, 0, getWidth()-25, 20, Justification::left, true);
+    
+  
     
 }
\ No newline at end of file
diff --git a/Source/UI/GraphViewer.h b/Source/UI/GraphViewer.h
index e9358c74a..bc34a942c 100644
--- a/Source/UI/GraphViewer.h
+++ b/Source/UI/GraphViewer.h
@@ -54,6 +54,8 @@ public:
     
     void paint(Graphics& g);
     
+    void updateBoundaries();
+    
     const String getName();
     
 private:
@@ -83,6 +85,7 @@ public:
 private:
     
     void updateNodeLocations();
+    int indexOfEditor(GenericEditor* editor);
     
     Font labelFont;
     
-- 
GitLab