Skip to content

Commit 8a9a1c9

Browse files
committed
feat: bug fix & sorting code
1 parent c5a7b40 commit 8a9a1c9

14 files changed

Lines changed: 248 additions & 149 deletions

Editor/Scripts/GraphView/GraphViewNode.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.

Editor/Scripts/GraphView/GraphViewNode.cs.meta

Lines changed: 0 additions & 3 deletions
This file was deleted.

Editor/Scripts/GraphView/PlayableGraphView.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using System.Collections.Generic;
2-
using System.Linq;
2+
using GBG.PlayableGraphMonitor.Editor.Node;
33
using UnityEditor.Experimental.GraphView;
44
using UnityEditor.Playables;
55
using UnityEngine;
6-
using UnityEngine.Assertions;
76
using UnityEngine.Playables;
87
using UnityEngine.UIElements;
98
using UGraphView = UnityEditor.Experimental.GraphView.GraphView;
@@ -39,13 +38,26 @@ public void SetPlayableGraph(PlayableGraph playableGraph, bool forceRepaint)
3938

4039
_playableGraph = playableGraph;
4140

42-
for (int i = 0; i < _playableOutputNodes.Count; i++)
41+
ClearView();
42+
43+
PopulateView();
44+
45+
// FrameAll();
46+
}
47+
48+
49+
private void ClearView()
50+
{
51+
foreach (var playableOutputNode in _playableOutputNodes)
4352
{
44-
RemoveElement(_playableOutputNodes[i]);
53+
playableOutputNode.RemoveFromContainer();
4554
}
4655

4756
_playableOutputNodes.Clear();
57+
}
4858

59+
private void PopulateView()
60+
{
4961
if (!_playableGraph.IsValid())
5062
{
5163
return;
@@ -56,16 +68,20 @@ public void SetPlayableGraph(PlayableGraph playableGraph, bool forceRepaint)
5668
var playableOutput = _playableGraph.GetOutput(i);
5769
var playableOutputTypeName = playableOutput.GetPlayableOutputType().Name;
5870
var playableOutputEditorName = playableOutput.GetEditorName();
59-
var playableOutputNode = new PlayableOutputNode(this, 0, playableOutput)
71+
var playableOutputNode = new PlayableOutputNode(0, playableOutput)
6072
{
6173
title = $"{playableOutputTypeName} ({playableOutputEditorName})"
6274
};
6375
playableOutputNode.SetPosition(new Rect(200, 200, 0, 0));
76+
playableOutputNode.AddToContainer(this);
77+
6478
_playableOutputNodes.Add(playableOutputNode);
65-
AddElement(playableOutputNode);
6679
}
6780

68-
FrameAll();
81+
for (int i = 0; i < _playableOutputNodes.Count; i++)
82+
{
83+
_playableOutputNodes[i].CreateAndConnectInputNodes();
84+
}
6985
}
7086

7187

Editor/Scripts/GraphView/PlayableNode.cs.meta

Lines changed: 0 additions & 3 deletions
This file was deleted.

Editor/Scripts/GraphView/PlayableOutputNode.cs

Lines changed: 0 additions & 62 deletions
This file was deleted.

Editor/Scripts/GraphView/PlayableOutputNode.cs.meta

Lines changed: 0 additions & 3 deletions
This file was deleted.

Editor/Scripts/Node.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System.Collections.Generic;
2+
using GBG.PlayableGraphMonitor.Editor.GraphView;
3+
using UnityEditor.Experimental.GraphView;
4+
using UnityEngine.Assertions;
5+
using UGraphView = UnityEditor.Experimental.GraphView.GraphView;
6+
using UNode = UnityEditor.Experimental.GraphView.Node;
7+
8+
namespace GBG.PlayableGraphMonitor.Editor.Node
9+
{
10+
public abstract class GraphViewNode : UNode
11+
{
12+
public int Depth { get; }
13+
14+
public IReadOnlyList<Port> InputPorts => InternalInputPorts;
15+
16+
protected List<Port> InternalInputPorts { get; } = new List<Port>();
17+
18+
public IReadOnlyList<Port> OutputPorts => InternalOutputPorts;
19+
20+
protected List<Port> InternalOutputPorts { get; } = new List<Port>();
21+
22+
protected UGraphView Container { get; set; }
23+
24+
// public IReadOnlyList<Edge> InputEdges => InternalInputEdges;
25+
26+
protected List<Edge> InternalInputEdges { get; } = new List<Edge>();
27+
28+
protected List<GraphViewNode> ChildNodes { get; } = new List<GraphViewNode>();
29+
30+
31+
public virtual void AddToContainer(UGraphView container)
32+
{
33+
Container = container;
34+
Container.AddElement(this);
35+
}
36+
37+
public virtual void RemoveFromContainer()
38+
{
39+
// self
40+
Container.RemoveElement(this);
41+
42+
// input edges
43+
for (int i = 0; i < InternalInputEdges.Count; i++)
44+
{
45+
Container.RemoveElement(InternalInputEdges[i]);
46+
}
47+
48+
InternalInputEdges.Clear();
49+
50+
// children
51+
foreach (var childNode in ChildNodes)
52+
{
53+
childNode.RemoveFromContainer();
54+
}
55+
56+
ChildNodes.Clear();
57+
58+
Container = null;
59+
}
60+
61+
public abstract void CreateAndConnectInputNodes();
62+
63+
64+
protected GraphViewNode(int depth)
65+
{
66+
Depth = depth;
67+
}
68+
69+
protected Port InstantiatePort<TPort>(Direction direction)
70+
{
71+
return InstantiatePort(Orientation.Horizontal, direction, Port.Capacity.Single, typeof(TPort));
72+
}
73+
}
74+
}

Editor/Scripts/Node/GraphViewNode.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,79 @@
22
using UnityEngine;
33
using UnityEngine.Playables;
44

5-
namespace GBG.PlayableGraphMonitor.Editor.GraphView
5+
namespace GBG.PlayableGraphMonitor.Editor.Node
66
{
77
public class PlayableNode : GraphViewNode
88
{
99
public Playable Playable { get; }
1010

11-
public PlayableNode(PlayableGraphView owner, int depth, Playable playable)
12-
: base(owner, depth)
11+
public PlayableNode(int depth, Playable playable)
12+
: base(depth)
1313
{
1414
Playable = playable;
1515

1616
CreatePorts();
1717
RefreshExpandedState();
1818
RefreshPorts();
19-
20-
CreateAndConnectInputNodes();
2119
}
2220

23-
24-
private void CreatePorts()
21+
public override void CreateAndConnectInputNodes()
2522
{
2623
if (!Playable.IsValid())
2724
{
2825
return;
2926
}
3027

28+
var inputPlayableDepth = Depth + 1;
3129
for (int i = 0; i < Playable.GetInputCount(); i++)
3230
{
33-
var inputPort = InstantiatePort<Playable>(Direction.Input);
34-
inputPort.portColor = Color.white;
35-
inputContainer.Add(inputPort);
36-
InternalInputPorts.Add(inputPort);
31+
var inputPlayable = Playable.GetInput(i);
32+
var inputPlayableTypeName = inputPlayable.GetPlayableType().Name;
33+
var inputPlayableNode = new PlayableNode(inputPlayableDepth, inputPlayable)
34+
{
35+
title = inputPlayableTypeName
36+
};
37+
inputPlayableNode.SetPosition(new Rect(-400 * inputPlayableDepth, 200, 0, 0));
38+
inputPlayableNode.AddToContainer(Container);
39+
ChildNodes.Add(inputPlayableNode);
40+
41+
var inputPlayableNodeOutputPort = inputPlayableNode.OutputPorts[0];
42+
var selfInputPort = InternalInputPorts[i];
43+
var edge = selfInputPort.ConnectTo(inputPlayableNodeOutputPort);
44+
Container.AddElement(edge);
45+
InternalInputEdges.Add(edge);
3746
}
3847

39-
for (int i = 0; i < Playable.GetOutputCount(); i++)
48+
for (int i = 0; i < ChildNodes.Count; i++)
4049
{
41-
var outputPort = InstantiatePort<Playable>(Direction.Output);
42-
outputPort.portColor = Color.white;
43-
outputContainer.Add(outputPort);
44-
InternalOutputPorts.Add(outputPort);
50+
ChildNodes[i].CreateAndConnectInputNodes();
4551
}
4652
}
4753

48-
private void CreateAndConnectInputNodes()
54+
55+
private void CreatePorts()
4956
{
5057
if (!Playable.IsValid())
5158
{
5259
return;
5360
}
5461

55-
var inputPlayableDepth = Depth + 1;
5662
for (int i = 0; i < Playable.GetInputCount(); i++)
5763
{
58-
var inputPlayable = Playable.GetInput(i);
59-
var inputPlayableTypeName = inputPlayable.GetPlayableType().Name;
60-
var inputPlayableNode = new PlayableNode(Owner, inputPlayableDepth, inputPlayable)
61-
{
62-
title = inputPlayableTypeName
63-
};
64-
inputPlayableNode.SetPosition(new Rect(-400 * inputPlayableDepth, 200, 0, 0));
65-
Owner.AddElement(inputPlayableNode);
64+
var inputPort = InstantiatePort<Playable>(Direction.Input);
65+
inputPort.portName = $"Input {i}";
66+
inputPort.portColor = Color.white;
67+
inputContainer.Add(inputPort);
68+
InternalInputPorts.Add(inputPort);
69+
}
6670

67-
var inputPlayableOutputPortIndex = 0;
68-
var inputPlayableOutputPort = InternalInputPorts[inputPlayableOutputPortIndex];
69-
var edge = inputPlayableOutputPort.ConnectTo(inputPlayableNode.OutputPorts[0]);
70-
Owner.AddElement(edge);
71+
for (int i = 0; i < Playable.GetOutputCount(); i++)
72+
{
73+
var outputPort = InstantiatePort<Playable>(Direction.Output);
74+
outputPort.portName = $"Output {i}";
75+
outputPort.portColor = Color.white;
76+
outputContainer.Add(outputPort);
77+
InternalOutputPorts.Add(outputPort);
7178
}
7279
}
7380
}

0 commit comments

Comments
 (0)