Skip to content

Commit 3e63f71

Browse files
committed
Changed the code to make it work with version 5.6.0b5
1 parent 1efed16 commit 3e63f71

23 files changed

+389
-202
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine.Experimental.Director;
5+
6+
// Bridge between runtime and editor code: the graph created in runtime code can call GraphVisualizerClient.Show(...)
7+
// and the EditorWindow will register itself with the client to display any available graph.
8+
public class GraphVisualizerClient
9+
{
10+
private static GraphVisualizerClient s_Instance;
11+
private Dictionary<PlayableGraph, string> m_Graphs = new Dictionary<PlayableGraph, string>();
12+
13+
public static GraphVisualizerClient instance
14+
{
15+
get
16+
{
17+
if (s_Instance == null)
18+
s_Instance = new GraphVisualizerClient();
19+
return s_Instance;
20+
}
21+
}
22+
~GraphVisualizerClient()
23+
{
24+
m_Graphs.Clear();
25+
}
26+
public static void Show(PlayableGraph graph, string name)
27+
{
28+
if (!instance.m_Graphs.ContainsKey(graph))
29+
{
30+
instance.m_Graphs.Add(graph, name);
31+
}
32+
}
33+
public static void Hide(PlayableGraph graph)
34+
{
35+
if (instance.m_Graphs.ContainsKey(graph))
36+
{
37+
instance.m_Graphs.Remove(graph);
38+
}
39+
}
40+
41+
public static IEnumerable<KeyValuePair<PlayableGraph, string>> GetGraphs()
42+
{
43+
return instance.m_Graphs;
44+
}
45+
}

Assets/Clients/GraphVisualizerClient.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor/GraphVisualizer/Graph.meta

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor/GraphVisualizer/Graph/Graph.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,10 @@ IEnumerator IEnumerable.GetEnumerator()
6666
{
6767
return m_Nodes.GetEnumerator();
6868
}
69+
70+
public bool IsEmpty()
71+
{
72+
return m_Nodes.Count == 0;
73+
}
6974
}
7075
}

Assets/Editor/GraphVisualizer/Graph/Graph.cs.meta

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor/GraphVisualizer/Graph/Layouts.meta

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor/GraphVisualizer/Graph/Layouts/IGraphLayout.cs.meta

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor/GraphVisualizer/Graph/Layouts/ReingoldTilford.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ private float[] ComputeHorizontalPositionForEachLevel()
9797
nodes.Add(node);
9898
maxDepth = Mathf.Max(d, maxDepth);
9999
}
100-
100+
101101
// Bake the left to right horizontal positions.
102102
var horizontalPositionForDepth = new float[maxDepth];
103103
horizontalPositionForDepth[0] = 0;
104104
for (int d = 1; d < maxDepth; ++d)
105105
{
106-
IEnumerable<Node> nodesOnThisLevel = nodeDepths[d+1];
106+
IEnumerable<Node> nodesOnThisLevel = nodeDepths[d + 1];
107107

108108
int maxChildren = nodesOnThisLevel.Max(x => x.children.Count);
109109

Assets/Editor/GraphVisualizer/Graph/Layouts/ReingoldTilford.cs.meta

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor/GraphVisualizer/Graph/Node.cs.meta

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor/GraphVisualizer/Graph/Renderer.meta

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor/GraphVisualizer/Graph/Renderer/DefaultGraphRenderer.cs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
using System.Linq;
66
using System.Text.RegularExpressions;
77
using GraphVisualizer;
8-
using UnityEngine.Timeline;
9-
using UnityEngine.Playables;
8+
using UnityEngine.Experimental.Director;
109

1110
public class DefaultGraphRenderer : IGraphRenderer
1211
{
@@ -51,26 +50,33 @@ public DefaultGraphRenderer()
5150

5251
public void Draw(IGraphLayout graphLayout, Rect drawingArea)
5352
{
54-
NodeConstraints defaults;
53+
GraphSettings defaults;
5554
defaults.maximumNormalizedNodeSize = s_DefaultMaximumNormalizedNodeSize;
5655
defaults.maximumNodeSizeInPixels = s_DefaultMaximumNodeSizeInPixels;
5756
defaults.aspectRatio = s_DefaultAspectRatio;
57+
defaults.showLegend = true;
5858
Draw(graphLayout, drawingArea, defaults);
5959
}
6060

61-
public void Draw(IGraphLayout graphLayout, Rect totalDrawingArea, NodeConstraints nodeConstraints)
61+
public void Draw(IGraphLayout graphLayout, Rect totalDrawingArea, GraphSettings graphSettings)
6262
{
63-
PrepareLegend(graphLayout.vertices);
63+
var legendArea = new Rect();
64+
var drawingArea = new Rect(totalDrawingArea);
6465

65-
var legendArea = new Rect(totalDrawingArea)
66+
if (graphSettings.showLegend)
6667
{
67-
width = EstimateLegendWidth() + s_BorderSize * 2
68-
};
68+
PrepareLegend(graphLayout.vertices);
6969

70-
var drawingArea = new Rect(totalDrawingArea);
70+
legendArea = new Rect(totalDrawingArea)
71+
{
72+
width = EstimateLegendWidth() + s_BorderSize * 2
73+
};
7174

72-
legendArea.x = drawingArea.xMax - legendArea.width;
73-
drawingArea.width -= legendArea.width;// + s_BorderSize;
75+
legendArea.x = drawingArea.xMax - legendArea.width;
76+
drawingArea.width -= legendArea.width;// + s_BorderSize;
77+
78+
DrawLegend(legendArea);
79+
}
7480

7581
if (m_SelectedNode != null)
7682
{
@@ -85,8 +91,7 @@ public void Draw(IGraphLayout graphLayout, Rect totalDrawingArea, NodeConstraint
8591
}
8692
}
8793

88-
DrawGraph(graphLayout, drawingArea, nodeConstraints);
89-
DrawLegend(legendArea);
94+
DrawGraph(graphLayout, drawingArea, graphSettings);
9095
}
9196

9297
private void InitializeStyles()
@@ -132,6 +137,9 @@ private void PrepareLegend(IEnumerable<Vertex> vertices)
132137
m_LegendForType.Clear();
133138
foreach (Vertex v in vertices)
134139
{
140+
if (v.node == null)
141+
continue;
142+
135143
string nodeType = v.node.GetContentTypeName();
136144

137145
if (m_LegendForType.ContainsKey(nodeType))
@@ -199,7 +207,7 @@ private void DrawLegend(Rect legendArea)
199207
GUILayout.BeginVertical();
200208

201209
GUILayout.Label("Inspector", m_SubTitleStyle);
202-
210+
203211
if (m_SelectedNode != null)
204212
{
205213
GUILayout.Label(m_SelectedNode.ToString(), m_InspectorStyle);
@@ -277,7 +285,7 @@ private void DrawEdgeWeightColorBar(float width)
277285
}
278286

279287
// Draw the graph and returns the selected Node if there's any.
280-
private void DrawGraph(IGraphLayout graphLayout, Rect drawingArea, NodeConstraints nodeConstraints)
288+
private void DrawGraph(IGraphLayout graphLayout, Rect drawingArea, GraphSettings graphSettings)
281289
{
282290
// add border, except on right-hand side where the legend will provide necessary padding
283291
drawingArea = new Rect(drawingArea.x + s_BorderSize,
@@ -292,12 +300,12 @@ private void DrawGraph(IGraphLayout graphLayout, Rect drawingArea, NodeConstrain
292300
}
293301

294302
// Increase b by maximum node size (since b is measured between node centers)
295-
b.Expand(new Vector3(nodeConstraints.maximumNormalizedNodeSize, nodeConstraints.maximumNormalizedNodeSize, 0));
303+
b.Expand(new Vector3(graphSettings.maximumNormalizedNodeSize, graphSettings.maximumNormalizedNodeSize, 0));
296304

297305
var scale = new Vector2(drawingArea.width / b.size.x, drawingArea.height / b.size.y);
298306
var offset = new Vector2(-b.min.x, -b.min.y);
299307

300-
Vector2 nodeSize = ComputeNodeSize(scale, nodeConstraints);
308+
Vector2 nodeSize = ComputeNodeSize(scale, graphSettings);
301309

302310
GUI.BeginGroup(drawingArea);
303311

@@ -364,35 +372,35 @@ private void DrawGraph(IGraphLayout graphLayout, Rect drawingArea, NodeConstrain
364372
}
365373

366374
// Apply node constraints to node size
367-
private static Vector2 ComputeNodeSize(Vector2 scale, NodeConstraints nodeConstraints)
375+
private static Vector2 ComputeNodeSize(Vector2 scale, GraphSettings graphSettings)
368376
{
369377
var extraTickness = (s_SelectedNodeThickness + s_ActiveNodeThickness) * 2.0f;
370-
var nodeSize = new Vector2(nodeConstraints.maximumNormalizedNodeSize * scale.x - extraTickness,
371-
nodeConstraints.maximumNormalizedNodeSize * scale.y - extraTickness);
378+
var nodeSize = new Vector2(graphSettings.maximumNormalizedNodeSize * scale.x - extraTickness,
379+
graphSettings.maximumNormalizedNodeSize * scale.y - extraTickness);
372380

373381
// Adjust aspect ratio after scaling
374382
float currentAspectRatio = nodeSize.x / nodeSize.y;
375383

376-
if (currentAspectRatio > nodeConstraints.aspectRatio)
384+
if (currentAspectRatio > graphSettings.aspectRatio)
377385
{
378386
// Shrink x dimension
379-
nodeSize.x = nodeSize.y * nodeConstraints.aspectRatio;
387+
nodeSize.x = nodeSize.y * graphSettings.aspectRatio;
380388
}
381389
else
382390
{
383391
// Shrink y dimension
384-
nodeSize.y = nodeSize.x / nodeConstraints.aspectRatio;
392+
nodeSize.y = nodeSize.x / graphSettings.aspectRatio;
385393
}
386394

387395
// If node size is still too big, scale down
388-
if (nodeSize.x > nodeConstraints.maximumNodeSizeInPixels)
396+
if (nodeSize.x > graphSettings.maximumNodeSizeInPixels)
389397
{
390-
nodeSize *= nodeConstraints.maximumNodeSizeInPixels / nodeSize.x;
398+
nodeSize *= graphSettings.maximumNodeSizeInPixels / nodeSize.x;
391399
}
392400

393-
if (nodeSize.y > nodeConstraints.maximumNodeSizeInPixels)
401+
if (nodeSize.y > graphSettings.maximumNodeSizeInPixels)
394402
{
395-
nodeSize *= nodeConstraints.maximumNodeSizeInPixels / nodeSize.y;
403+
nodeSize *= graphSettings.maximumNodeSizeInPixels / nodeSize.y;
396404
}
397405
return nodeSize;
398406
}

Assets/Editor/GraphVisualizer/Graph/Renderer/DefaultGraphRenderer.cs.meta

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor/GraphVisualizer/Graph/Renderer/IGraphRenderer.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ namespace GraphVisualizer
66
public interface IGraphRenderer
77
{
88
void Draw(IGraphLayout graphLayout, Rect drawingArea);
9-
void Draw(IGraphLayout graphLayout, Rect drawingArea, NodeConstraints nodeConstraints);
9+
void Draw(IGraphLayout graphLayout, Rect drawingArea, GraphSettings graphSettings);
1010
}
1111

12-
// Customization of how the graph nodes should be rendered (size, distances and proportions)
13-
public struct NodeConstraints
12+
// Customization of how the graph will be displayed:
13+
// - size, distances and proportions of nodes
14+
// - legend
15+
public struct GraphSettings
1416
{
1517
// In layout units. If 1, node will be drawn as large as possible to avoid overlapping, and to respect aspect ratio
1618
public float maximumNormalizedNodeSize;
@@ -20,5 +22,8 @@ public struct NodeConstraints
2022

2123
// width / height; 1 represents a square node
2224
public float aspectRatio;
25+
26+
// Control the display of the legend.
27+
public bool showLegend;
2328
}
2429
}

Assets/Editor/GraphVisualizer/Graph/Renderer/IGraphRenderer.cs.meta

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)