Skip to content

Commit

Permalink
improved graphics and ability to refresh just the connections so the …
Browse files Browse the repository at this point in the history
…layout is not lost
  • Loading branch information
MephestoKhaan committed Sep 17, 2018
1 parent 6ea6db3 commit 9711cc3
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 160 deletions.
25 changes: 10 additions & 15 deletions Assets/EventVisualizer/Editor/EdgeGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ public Graphs.Edge FindClosestEdge()

#region Edge drawer

const float kEdgeWidth = 4;
const float kNodeTitleSpace = 35;
const float kNodeEdgeSeparation = 11;
const float kEdgeWidth = 6;

static void DrawEdge(Edge edge, Vector2Int indexes, Color color)
{
Expand All @@ -167,14 +165,14 @@ static void DrawEdge(Vector2 p1, Vector2 p2, Color color, List<float> triggers)
var l = Mathf.Min(Mathf.Abs(p1.y - p2.y), 50);
Vector2 p3 = p1 + new Vector2(l, 0);
Vector2 p4 = p2 - new Vector2(l, 0);
var texture = (Texture2D)Graphs.Styles.connectionTexture.image;
var texture = (Texture2D)Graphs.Styles.selectedConnectionTexture.image;
Handles.DrawBezier(p1, p2, p3, p4, color, texture, kEdgeWidth);


foreach (var trigger in triggers)
{
Vector3 pos = CalculateBezierPoint(trigger, p1, p3, p4, p2);
Handles.DrawSolidArc(pos, Vector3.back, pos + Vector3.up, 360, kEdgeWidth * 2);
Handles.DrawSolidArc(pos, Vector3.back, pos + Vector3.up, 360, kEdgeWidth );

}

Expand All @@ -185,12 +183,18 @@ static void DrawEdge(Vector2 p1, Vector2 p2, Color color, List<float> triggers)

#region Utilities to access private members

const float kEdgeBottomMargin = 4;
const float kNodeTitleSpace = 36;
const float kNodeEdgeSeparation = 12;

static Vector2 GetPositionAsFromSlot(Slot slot, int index)
{
NodeGUI node = slot.node as NodeGUI;
Vector2 pos = node.position.position;
pos.y = node.position.yMax - kNodeEdgeSeparation * 0.5f;

pos.y = node.position.yMax - kEdgeBottomMargin;
pos.y -= kNodeEdgeSeparation * index;

pos.x = node.position.xMax;

return pos;
Expand All @@ -207,15 +211,6 @@ static Vector2 GetPositionAsToSlot(Slot slot, int index)
return pos;
}


// Caller for GUIClip.Clip
static Vector2 GUIClip(Vector2 pos)
{
var type = Type.GetType("UnityEngine.GUIClip,UnityEngine");
var method = type.GetMethod("Clip", new Type[] { typeof(Vector2) });
return ((Vector2)method.Invoke(null, new object[] { pos }));
}

#endregion

private static Vector3 CalculateBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
Expand Down
216 changes: 124 additions & 92 deletions Assets/EventVisualizer/Editor/EventsGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,96 +6,128 @@

namespace EventVisualizer.Base
{
[System.Serializable]
public class EventsGraph : Graph
{
static public EventsGraph Create()
{
var graph = CreateInstance<EventsGraph>();
graph.hideFlags = HideFlags.HideAndDontSave;
return graph;
}

public EventsGraphGUI GetEditor()
{
var gui = CreateInstance<EventsGraphGUI>();
gui.graph = this;
gui.hideFlags = HideFlags.HideAndDontSave;
return gui;

}

public void BuildGraph()
{
NodeData.Clear();
Clear(true);
foreach (EventCall call in EventsFinder.FindAllEvents())
{
NodeData.RegisterEvent(call);
}
foreach (NodeData data in NodeData.Nodes)
{
AddNode(NodeGUI.Create(data));
}

foreach (NodeGUI node in nodes)
{
node.PopulateEdges();
}

SortGraph();
}

#region sorting

[SerializeField]
private HashSet<Node> positionedNodes = new HashSet<Node>();
private const float VERTICAL_SPACING = 80f;
private const float HORIZONTAL_SPACING = 400f;
private void SortGraph()
{
positionedNodes.Clear();

List<Node> sortedNodes = new List<Node>(nodes); //cannot sort the original collection so a clone is needed
sortedNodes.Sort((x, y) =>
{
int xScore = x.outputEdges.Count() - x.inputEdges.Count();
int yScore = y.outputEdges.Count() - y.inputEdges.Count();
return yScore.CompareTo(xScore);
});

Vector2 position = Vector2.zero;
foreach (Node node in sortedNodes)
{
if (!positionedNodes.Contains(node))
{
positionedNodes.Add(node);
position.y += PositionNodeHierarchy(node, position);
}
}
}


private float PositionNodeHierarchy(Node currentNode, Vector2 masterPosition)
{
float height = VERTICAL_SPACING;
foreach (var outputEdge in currentNode.outputEdges)
{
Node node = outputEdge.toSlot.node;
if (!positionedNodes.Contains(node))
{
positionedNodes.Add(node);
height += PositionNodeHierarchy(node, masterPosition
+ Vector2.right * HORIZONTAL_SPACING
+ Vector2.up * height);
}
}
currentNode.position = new Rect(masterPosition + Vector2.up * height * 0.5f, currentNode.position.size);

return height;
}

#endregion

}
[System.Serializable]
public class EventsGraph : Graph
{
static public EventsGraph Create()
{
var graph = CreateInstance<EventsGraph>();
graph.hideFlags = HideFlags.HideAndDontSave;
return graph;
}

public EventsGraphGUI GetEditor()
{
var gui = CreateInstance<EventsGraphGUI>();
gui.graph = this;
gui.hideFlags = HideFlags.HideAndDontSave;
return gui;

}

public void RebuildGraph()
{
BuildGraph();
SortGraph(nodes);
}

public void RefreshGraphConnections()
{
Dictionary<string, Rect> positions = new Dictionary<string, Rect>();
List<Node> adriftNodes = new List<Node>();
foreach (NodeGUI node in nodes)
{
positions.Add(node.name, node.position);
}
BuildGraph();

foreach (NodeGUI node in nodes)
{
if(positions.ContainsKey(node.name))
{
node.position = positions[node.name];
}
else
{
adriftNodes.Add(node);
}
}
SortGraph(adriftNodes);
}

private void BuildGraph()
{
NodeData.ClearAll();
Clear(true);
foreach (EventCall call in EventsFinder.FindAllEvents())
{
NodeData.RegisterEvent(call);
}

foreach (NodeData data in NodeData.Nodes)
{
NodeGUI node = NodeGUI.Create(data);
if (!nodes.Contains(node))
{
AddNode(node);
}
}
foreach (NodeGUI node in nodes)
{
node.PopulateEdges();
}
}

#region sorting

[SerializeField]
private HashSet<Node> positionedNodes = new HashSet<Node>();
private const float VERTICAL_SPACING = 80f;
private const float HORIZONTAL_SPACING = 400f;
private void SortGraph(List<Node> nodes)
{
positionedNodes.Clear();

List<Node> sortedNodes = new List<Node>(nodes); //cannot sort the original collection so a clone is needed
sortedNodes.Sort((x, y) =>
{
int xScore = x.outputEdges.Count() - x.inputEdges.Count();
int yScore = y.outputEdges.Count() - y.inputEdges.Count();
return yScore.CompareTo(xScore);
});

Vector2 position = Vector2.zero;
foreach (Node node in sortedNodes)
{
if (!positionedNodes.Contains(node))
{
positionedNodes.Add(node);
position.y += PositionNodeHierarchy(node, position);
}
}
}


private float PositionNodeHierarchy(Node currentNode, Vector2 masterPosition)
{
float height = VERTICAL_SPACING;
foreach (var outputEdge in currentNode.outputEdges)
{
Node node = outputEdge.toSlot.node;
if (!positionedNodes.Contains(node))
{
positionedNodes.Add(node);
height += PositionNodeHierarchy(node, masterPosition
+ Vector2.right * HORIZONTAL_SPACING
+ Vector2.up * height);
}
}
currentNode.position = new Rect(masterPosition + Vector2.up * height * 0.5f, currentNode.position.size);

return height;
}

#endregion

}
}
31 changes: 23 additions & 8 deletions Assets/EventVisualizer/Editor/EventsGraphWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static void ShowEditor()
public void Initialize()
{
_graph = EventsGraph.Create();
_graph.BuildGraph();
_graph.RebuildGraph();

_graphGUI = _graph.GetEditor();
_graphGUI.CenterGraph();
Expand All @@ -43,7 +43,7 @@ void OnGUI()
{
var width = position.width;
var height = position.height;
_zoomArea = new Rect(0, 0, width, height - kBarHeight);
_zoomArea = new Rect(0, 0, width, height);
HandleEvents();

if (_graphGUI != null)
Expand All @@ -65,10 +65,16 @@ void OnGUI()


// Status bar
GUILayout.BeginArea(new Rect(0, height - kBarHeight, width, kBarHeight));
if (GUILayout.Button("Refresh"))
GUILayout.BeginArea(new Rect(0, 0, width, kBarHeight+5));
string[] toolbarStrings = new string[] { "Update connections", "Clear" };
int result = GUILayout.Toolbar(-1, toolbarStrings);
if (result == 0)
{
Refresh();
RefreshGraphConnections();
}
else if(result == 1)
{
RebuildGraph();
}
GUILayout.EndArea();

Expand Down Expand Up @@ -110,10 +116,19 @@ private void HandleEvents()
}
}

void Refresh()
void RebuildGraph()
{
if(_graph != null)
{
_graph.RebuildGraph();
}
}
void RefreshGraphConnections()
{
_graph.Clear();
_graph.BuildGraph();
if (_graph != null)
{
_graph.RefreshGraphConnections();
}
}
}
}
Expand Down
Loading

0 comments on commit 9711cc3

Please sign in to comment.