Skip to content

Commit f421152

Browse files
committed
feat: improve node layout
1 parent d0a147c commit f421152

5 files changed

Lines changed: 32 additions & 58 deletions

File tree

Editor/Scripts/GraphView/PlayableGraphView.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using GBG.PlayableGraphMonitor.Editor.Node;
22
using System.Collections.Generic;
3-
using UnityEditor;
43
using UnityEditor.Experimental.GraphView;
54
using UnityEditor.Playables;
65
using UnityEngine;
@@ -20,8 +19,8 @@ public class PlayableGraphView : UGraphView
2019
public PlayableGraphView()
2120
{
2221
this.AddManipulator(new ContentDragger());
23-
this.AddManipulator(new SelectionDragger());
24-
this.AddManipulator(new RectangleSelector());
22+
//this.AddManipulator(new SelectionDragger());
23+
//this.AddManipulator(new RectangleSelector());
2524
SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale);
2625
}
2726

@@ -32,18 +31,16 @@ public PlayableGraph GetPlayableGraph()
3231

3332
public void SetPlayableGraph(PlayableGraph playableGraph)
3433
{
35-
if (IsEqual(ref _playableGraph, ref playableGraph))
36-
{
37-
return;
38-
}
34+
//if (IsEqual(ref _playableGraph, ref playableGraph))
35+
//{
36+
// return;
37+
//}
3938

4039
_playableGraph = playableGraph;
4140

4241
ClearView();
4342

4443
PopulateView();
45-
46-
EditorApplication.delayCall += () => FrameAll();
4744
}
4845

4946

@@ -55,8 +52,6 @@ private void ClearView()
5552
}
5653

5754
_playableOutputNodes.Clear();
58-
59-
GraphViewNode.LayoutInfo.Reset();
6055
}
6156

6257
private void PopulateView()
@@ -92,7 +87,8 @@ private void PopulateView()
9287
{
9388
var outputNode = _playableOutputNodes[i];
9489
var treeSize = outputNode.GetHierarchySize();
95-
outputNode.CalculateLayout(treeSize, origin);
90+
91+
outputNode.CalculateLayout(origin);
9692

9793
origin.y += treeSize.y + NodeLayoutInfo.VerticalSpace;
9894
}

Editor/Scripts/Node/GraphViewNode.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ namespace GBG.PlayableGraphMonitor.Editor.Node
88
{
99
public abstract class GraphViewNode : UNode
1010
{
11-
internal static NodeLayoutInfo LayoutInfo;
12-
1311
public int Depth { get; }
1412

1513
public IReadOnlyList<Port> InputPorts => InternalInputPorts;
@@ -20,12 +18,12 @@ public abstract class GraphViewNode : UNode
2018

2119
protected List<Port> InternalOutputPorts { get; } = new List<Port>();
2220

23-
protected UGraphView Container { get; set; }
24-
2521
// public IReadOnlyList<Edge> InputEdges => InternalInputEdges;
2622

2723
protected List<Edge> InternalInputEdges { get; } = new List<Edge>();
2824

25+
protected UGraphView Container { get; set; }
26+
2927
protected GraphViewNode Parent { get; private set; }
3028

3129
protected new List<GraphViewNode> Children { get; } = new List<GraphViewNode>();
@@ -34,6 +32,17 @@ public abstract class GraphViewNode : UNode
3432
private Vector2? _hierarchySize;
3533

3634

35+
protected GraphViewNode(int depth)
36+
{
37+
Depth = depth;
38+
}
39+
40+
protected Port InstantiatePort<TPort>(Direction direction)
41+
{
42+
return InstantiatePort(Orientation.Horizontal, direction, Port.Capacity.Single, typeof(TPort));
43+
}
44+
45+
3746
public Vector2 GetNodeSize()
3847
{
3948
return NodeLayoutInfo.StandardNodeSize;
@@ -69,24 +78,24 @@ public Vector2 GetHierarchySize()
6978
return _hierarchySize.Value;
7079
}
7180

72-
public void CalculateLayout(Vector2 treeSize, Vector2 origin)
81+
public void CalculateLayout(Vector2 origin)
7382
{
7483
var subTreeSize = GetHierarchySize();
75-
var nodePos = CalculateSubTreeRootPosition(treeSize, subTreeSize, origin);
84+
var nodePos = CalculateSubTreeRootNodePosition(subTreeSize, origin);
7685
SetPosition(new Rect(nodePos, Vector2.zero));
7786

7887
origin.x -= GetNodeSize().x - NodeLayoutInfo.HorizontalSpace;
7988
for (int i = 0; i < Children.Count; i++)
8089
{
8190
var childNode = Children[i];
8291
var childHierarchySize = childNode.GetHierarchySize();
83-
childNode.CalculateLayout(treeSize, origin);
92+
childNode.CalculateLayout(origin);
8493

8594
origin.y += childHierarchySize.y;
8695
}
8796
}
8897

89-
public static Vector2 CalculateSubTreeRootPosition(Vector2 treeSize, Vector2 subTreeSize, Vector2 subTreeOrigin)
98+
public static Vector2 CalculateSubTreeRootNodePosition(Vector2 subTreeSize, Vector2 subTreeOrigin)
9099
{
91100
var subTreePos = subTreeOrigin;
92101
subTreePos.y += subTreeSize.y / 2;
@@ -124,16 +133,5 @@ public virtual void RemoveFromContainer()
124133
}
125134

126135
public abstract void CreateAndConnectInputNodes();
127-
128-
129-
protected GraphViewNode(int depth)
130-
{
131-
Depth = depth;
132-
}
133-
134-
protected Port InstantiatePort<TPort>(Direction direction)
135-
{
136-
return InstantiatePort(Orientation.Horizontal, direction, Port.Capacity.Single, typeof(TPort));
137-
}
138136
}
139137
}

Editor/Scripts/Node/NodeLayoutInfo.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,12 @@
22

33
namespace GBG.PlayableGraphMonitor.Editor.Node
44
{
5-
public struct NodeLayoutInfo
5+
public static class NodeLayoutInfo
66
{
77
public const int HorizontalSpace = 80;
88

99
public const int VerticalSpace = 80;
1010

1111
public static readonly Vector2 StandardNodeSize = new Vector2(400, 150);
12-
13-
public int TreeWidth;
14-
15-
public Vector2 Origin;
16-
17-
18-
public void Reset()
19-
{
20-
TreeWidth = 0;
21-
22-
Origin = Vector2.zero;
23-
}
2412
}
2513
}

Editor/Scripts/Node/PlayableNode.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using UnityEditor.Experimental.GraphView;
1+
using System;
2+
using UnityEditor.Experimental.GraphView;
23
using UnityEngine;
34
using UnityEngine.Playables;
45

@@ -8,6 +9,9 @@ public class PlayableNode : GraphViewNode
89
{
910
public Playable Playable { get; }
1011

12+
public Type PlayableType => Playable.IsValid() ? Playable.GetPlayableType() : null;
13+
14+
1115
public PlayableNode(int depth, Playable playable)
1216
: base(depth)
1317
{
@@ -44,11 +48,6 @@ public override void CreateAndConnectInputNodes()
4448
InternalInputEdges.Add(edge);
4549
}
4650

47-
if (Children.Count > 1)
48-
{
49-
LayoutInfo.TreeWidth += Children.Count - 1;
50-
}
51-
5251
for (int i = 0; i < Children.Count; i++)
5352
{
5453
Children[i].CreateAndConnectInputNodes();

Editor/Scripts/Node/PlayableOutputNode.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ public PlayableOutputNode(int depth, PlayableOutput playableOutput)
1616
CreatePorts();
1717
RefreshExpandedState();
1818
RefreshPorts();
19-
20-
// root node occupies one width unit
21-
LayoutInfo.TreeWidth += 1;
2219
}
2320

21+
2422
public override void CreateAndConnectInputNodes()
2523
{
2624
if (!PlayableOutput.IsOutputValid())
@@ -44,11 +42,6 @@ public override void CreateAndConnectInputNodes()
4442
Container.AddElement(edge);
4543
InternalInputEdges.Add(edge);
4644

47-
if (Children.Count > 1)
48-
{
49-
LayoutInfo.TreeWidth += Children.Count - 1;
50-
}
51-
5245
for (int i = 0; i < Children.Count; i++)
5346
{
5447
Children[i].CreateAndConnectInputNodes();

0 commit comments

Comments
 (0)