Skip to content

Commit fdcbb99

Browse files
committed
Begin to work on multi-port array fields
1 parent a089c30 commit fdcbb99

8 files changed

Lines changed: 109 additions & 28 deletions

File tree

Assets/Examples/DefaultNodes/Editor/IntNodeView.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using UnityEngine;
44
using UnityEditor;
55
using UnityEditor.Experimental.UIElements;
6+
using UnityEditor.Experimental.UIElements.GraphView;
67
using UnityEngine.Experimental.UIElements;
78

89
namespace GraphProcessor

Assets/Examples/DefaultNodes/Nodes/AddNode.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@
66
[System.Serializable, NodeMenuItem("Primitives/Add")]
77
public class AddNode : BaseNode
88
{
9-
[Input]
10-
public PortArray< float > input;
9+
[Input("Input")]
10+
public PortArray< float > inputs;
1111

1212
[Output]
1313
public float output;
1414

15-
public override string name { get { return "Add"; } }
15+
public override string name { get { return "Add"; } }
16+
17+
public override void Process()
18+
{
19+
output = 0;
20+
21+
foreach (float input in inputs)
22+
output += input;
23+
}
1624
}

Assets/NodeGraphProcessor/Editor/Views/BaseGraphView.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,25 @@ public override List<Port> GetCompatiblePorts(Port startPort, NodeAdapter nodeAd
156156
{
157157
var compatiblePorts = new List<Port>();
158158

159+
Type startPortType = startPort.portType;
160+
161+
if ((startPort as PortView).isMultiple)
162+
startPortType = startPortType.GetGenericArguments()[0];
163+
159164
compatiblePorts.AddRange(ports.ToList().Where(p => {
160-
if (p.direction == startPort.direction)
165+
var portView = p as PortView;
166+
167+
if (portView.direction == startPort.direction)
161168
return false;
162169

163-
if (!p.portType.IsAssignableFrom(startPort.portType))
170+
Type portType = portView.portType;
171+
172+
if (portView.isMultiple)
173+
portType = portType.GetGenericArguments()[0];
174+
175+
if (!portType.IsReallyAssignableFrom(startPortType))
164176
return false;
165-
177+
166178
return true;
167179
}));
168180

@@ -271,10 +283,8 @@ public void Connect(EdgeView e, bool serializeToGraph = true)
271283
{
272284
if (e.input == null || e.output == null)
273285
return ;
274-
275-
//Remove all edges connected to the input port:
276-
foreach (var edge in edgeViews.Where(ev => ev.input == e.input))
277-
Disconnect(edge);
286+
287+
var edgesToRemove = edgeViews.Where(ev => ev.input == e.input).ToList();
278288

279289
AddElement(e);
280290

@@ -283,16 +293,23 @@ public void Connect(EdgeView e, bool serializeToGraph = true)
283293

284294
var inputNodeView = e.input.node as BaseNodeView;
285295
var outputNodeView = e.output.node as BaseNodeView;
296+
297+
if (inputNodeView == null || outputNodeView == null)
298+
return ;
286299

287300
edgeViews.Add(e);
288301

289302
if (serializeToGraph)
290303
{
291304
e.userData = graph.Connect(
292-
inputNodeView.nodeTarget, e.input.portName,
293-
outputNodeView.nodeTarget, e.output.portName
305+
inputNodeView.nodeTarget, (e.input as PortView).fieldName,
306+
outputNodeView.nodeTarget, (e.output as PortView).fieldName
294307
);
295308
}
309+
310+
//Remove edges formerly connected to the same input port
311+
foreach (var edge in edgesToRemove)
312+
Disconnect(edge);
296313

297314
inputNodeView.RefreshPorts();
298315
outputNodeView.RefreshPorts();
@@ -309,13 +326,13 @@ public void Disconnect(EdgeView e)
309326

310327
RemoveElement(e);
311328

312-
if (e.input != null)
329+
if (e?.input?.node != null)
313330
{
314331
var inputNodeView = e.input.node as BaseNodeView;
315332
inputNodeView.RefreshPorts();
316333
e.input.Disconnect(e);
317334
}
318-
if (e.output != null)
335+
if (e?.output?.node != null)
319336
{
320337
var outputNodeView = e.output.node as BaseNodeView;
321338
e.output.Disconnect(e);

Assets/NodeGraphProcessor/Editor/Views/BaseNodeView.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using UnityEngine.Rendering;
77
using UnityEditor;
88
using System.Reflection;
9+
using System;
910

1011
using NodeView = UnityEditor.Experimental.UIElements.GraphView.Node;
1112

@@ -42,7 +43,7 @@ public void Initialize(BaseGraphView owner, BaseNode node)
4243
Enable();
4344
}
4445

45-
public void AddPort(Port p)
46+
public void AddPort(PortView p)
4647
{
4748
if (p.direction == Direction.Input)
4849
{
@@ -55,10 +56,10 @@ public void AddPort(Port p)
5556
outputContainer.Add(p);
5657
}
5758

58-
portsPerFieldName[p.portName] = p;
59+
portsPerFieldName[p.fieldName] = p;
5960
}
6061

61-
public void RemovePort(Port p)
62+
public void RemovePort(PortView p)
6263
{
6364
if (p.direction == Direction.Input)
6465
{
@@ -71,7 +72,7 @@ public void RemovePort(Port p)
7172
outputContainer.Remove(p);
7273
}
7374

74-
portsPerFieldName.Remove(p.portName);
75+
portsPerFieldName.Remove(p.fieldName);
7576
}
7677

7778
void InitializePorts()
@@ -96,15 +97,18 @@ void InitializePorts()
9697
this
9798
);
9899

99-
port.portName = field.Name;
100+
if (!String.IsNullOrEmpty(inputAttribute?.name))
101+
port.portName = inputAttribute.name;
102+
else if (!String.IsNullOrEmpty(outputAttribute?.name))
103+
port.portName = outputAttribute.name;
100104

101105
AddPort(port);
102106
}
103107
}
104108

105109
void InitializeView()
106110
{
107-
title = nodeTarget.name;
111+
title = (string.IsNullOrEmpty(nodeTarget.name)) ? nodeTarget.GetType().Name : nodeTarget.name;
108112

109113
SetPosition(nodeTarget.position);
110114
}

Assets/NodeGraphProcessor/Editor/Views/MiniMapView.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class MiniMapView : MiniMap
1010
{
1111
public MiniMapView()
1212
{
13+
SetPosition(new Rect(0, 0, 100, 100));
14+
SetSize(new Vector2(1000, 1000));
1315
}
1416
}
1517
}

Assets/NodeGraphProcessor/Editor/Views/PortView.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
namespace GraphProcessor
1010
{
11-
class PortView : Port
11+
public class PortView : Port
1212
{
1313
public bool isMultiple;
14-
public int index { get; private set; }
14+
public string fieldName { get { return field.Name; } }
1515
FieldInfo field;
1616
BaseNodeView owner;
1717
EdgeConnectorListener edgeConnectorListener;
@@ -27,40 +27,52 @@ public PortView(Orientation portOrientation, Direction portDirection, FieldInfo
2727

2828
this.AddManipulator(m_EdgeConnector);
2929

30-
portName = "Test";
30+
portName = field.Name;
3131
isMultiple = portType.IsGenericType && portType.GetGenericTypeDefinition() == typeof(PortArray<>);
3232
visualClass = "type";
3333

3434
if (isMultiple)
3535
visualClass += field.FieldType.GetGenericArguments()[0].Name;
3636
else
3737
visualClass += field.FieldType.Name;
38-
39-
Debug.Log("Created port with class: " + visualClass + ", from type: " + portType);
4038
}
4139

4240
public override void Connect(Edge edge)
4341
{
4442
base.Connect(edge);
4543

46-
if (direction == Direction.Output && isMultiple)
44+
if (direction == Direction.Input && isMultiple)
4745
{
48-
var portArray = field.GetValue(owner) as IList;
46+
var portArray = field.GetValue(owner.nodeTarget) as IList;
47+
48+
//Initialize the array if not
49+
if (portArray == null)
50+
{
51+
portArray = Activator.CreateInstance(field.FieldType) as IList;
52+
field.SetValue(owner.nodeTarget, portArray);
53+
}
54+
4955
var paramType = portArray.GetType().GetGenericArguments()[0];
5056
int index = portArray.Count;
5157
var newPort = new PortView(orientation, direction, field, edgeConnectorListener, owner);
5258

5359
portArray.Add(Activator.CreateInstance(paramType));
60+
61+
newPort.portName = portName;
5462

55-
newPort.index = index;
5663
owner.AddPort(newPort);
5764
}
5865
}
5966

6067
public override void Disconnect(Edge edge)
6168
{
69+
var edgeView = edge as EdgeView;
70+
6271
base.Disconnect(edge);
6372

73+
if (!edgeView.isConnected)
74+
return ;
75+
6476
if (direction == Direction.Input && isMultiple)
6577
{
6678
owner.RemovePort(this);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
4+
namespace GraphProcessor
5+
{
6+
public static class TypeExtension
7+
{
8+
public static bool IsReallyAssignableFrom(this Type type, Type otherType)
9+
{
10+
if (type.IsAssignableFrom(otherType))
11+
return true;
12+
13+
try
14+
{
15+
var v = Expression.Variable(otherType);
16+
var expr = Expression.Convert(v, type);
17+
return expr.Method == null || expr.Method.Name == "op_Implicit";
18+
}
19+
catch (InvalidOperationException)
20+
{
21+
return false;
22+
}
23+
}
24+
25+
}
26+
}

Assets/NodeGraphProcessor/Utils/TypeExtension.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.

0 commit comments

Comments
 (0)