Skip to content

Commit fe90a10

Browse files
author
Antoine Lelievre
committed
Replaced OnConnect/Disconnect by events on node views
1 parent 2d13017 commit fe90a10

12 files changed

Lines changed: 155 additions & 82 deletions

File tree

Assets/Examples/DefaultNodes/Editor/CircleRadiansView.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ public override void Enable()
2121
// Create your fields using node's variables and add them to the controlsContainer
2222

2323
controlsContainer.Add(listContainer);
24+
onPortConnected += OnPortUpdate;
25+
onPortDisconnected += OnPortUpdate;
2426

25-
// TODO: find a way to get PortView from here
2627
UpdateOutputRadians(GetPortFromFieldName("outputRadians").connectionCount);
2728
}
2829

@@ -40,14 +41,9 @@ void UpdateOutputRadians(int count)
4041
}
4142
}
4243

43-
public override void OnPortConnected(PortView port)
44+
public new void OnPortUpdate(PortView port)
4445
{
4546
// There is only one port on this node so it can only be the output
4647
UpdateOutputRadians(port.connectionCount);
4748
}
48-
49-
public override void OnPortDisconnected(PortView port)
50-
{
51-
UpdateOutputRadians(port.connectionCount);
52-
}
5349
}

Assets/Examples/DefaultNodes/Editor/MutliAddNodeView.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,4 @@ public override void Enable()
2121

2222
controlsContainer.Add(floatField);
2323
}
24-
25-
[CustomPortView(nameof(MultiAddNode.inputs))]
26-
public IEnumerable< PortView > Multiport(NodePort port, PortCreationAttributes attrs)
27-
{
28-
int index = 0;
29-
string portName = "Input ";
30-
31-
foreach (var edge in port.GetEdges())
32-
{
33-
yield return AddPort(portName + index++, attrs);
34-
}
35-
36-
yield return AddPort(portName + index, attrs);
37-
}
38-
39-
public override void OnPortConnected(PortView port)
40-
{
41-
// re-create ports:
42-
43-
}
44-
45-
public override void OnPortDisconnected(PortView port)
46-
{
47-
48-
}
4924
}

Assets/Examples/DefaultNodes/Editor/PrintNodeView.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public override void Enable()
2222
controlsContainer.Add(printLabel);
2323

2424
nodeTarget.onProcessed += UpdatePrintLabel;
25+
onPortConnected += (p) => UpdatePrintLabel();
26+
onPortDisconnected += (p) => UpdatePrintLabel();
2527

2628
UpdatePrintLabel();
2729
}
@@ -33,14 +35,4 @@ void UpdatePrintLabel()
3335
else
3436
printLabel.text = "null";
3537
}
36-
37-
public override void OnPortConnected(PortView port)
38-
{
39-
UpdatePrintLabel();
40-
}
41-
42-
public override void OnPortDisconnected(PortView port)
43-
{
44-
UpdatePrintLabel();
45-
}
4638
}

Assets/Examples/DefaultNodes/Nodes/MultiAddNode.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,23 @@ protected override void Process()
2929
[CustomPortBehavior(nameof(inputs))]
3030
IEnumerable< PortData > GetPortsForInputs(List< SerializableEdge > edges)
3131
{
32+
PortData pd = new PortData();
33+
int index = 0;
34+
3235
foreach (var edgeOfInput in edges)
3336
{
34-
PortData pd;
35-
// unique port key that will be serialized into the edges and used to re-connect the
36-
// the edges when the graph is reloaded
37-
pd.name = edgeOfInput.GUID;
38-
pd.type = typeof(float);
39-
40-
yield return pd;
37+
// unique port key that will be serialized into the edges and used to re-connect the
38+
// the edges when the graph is reloaded
39+
yield return new PortData{
40+
displayName = "In " + index++,
41+
displayType = typeof(float),
42+
identifier = edgeOfInput.GUID,
43+
input = true
44+
};
4145
}
4246

4347
// Dummy last port to allow connecting additional edges
44-
yield return new PortData{ name = "", type = typeof(float)};
48+
yield return new PortData{ displayName = "In " + index, displayType = typeof(float)};
4549
}
4650

4751
[CustomPortInput(nameof(inputs), typeof(float), allowCast = true)]

Assets/Examples/ExposedPropertiesExample.asset

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,19 @@ MonoBehaviour:
5959
serializedType: GraphProcessor.ProcessorView, com.alelievr.NodeGraphProcessor,
6060
Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
6161
exposedParameters:
62-
- name: float
62+
- guid:
63+
name: float
6364
type: System.Single, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
6465
serializedValue:
6566
serializedType: System.Single, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
6667
serializedValue: 42
6768
input: 1
68-
- name: text
69+
- guid:
70+
name: text
6971
type: System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
7072
serializedValue:
7173
serializedType: System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
7274
serializedValue: '"Hello World !"'
7375
input: 1
74-
position: {x: -182, y: -34, z: 0}
76+
position: {x: -436, y: -302, z: 0}
7577
scale: {x: 1.3225, y: 1.3225, z: 1}

Assets/Examples/Multi-Port.asset

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,44 @@ MonoBehaviour:
1212
m_Script: {fileID: 11500000, guid: 31390c44c9f3a3d40b424bf6821999a2, type: 3}
1313
m_Name: Multi-Port
1414
m_EditorClassIdentifier:
15-
serializedNodes: []
16-
edges: []
15+
serializedNodes:
16+
- type: MultiAddNode, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
17+
jsonDatas: '{"GUID":"50c3eea6-ac9e-4f87-8c1d-4db89bed3ef6","computeOrder":2,"canProcess":true,"position":{"serializedVersion":"2","x":470.30084228515627,"y":-7.771687030792236,"width":125.0,"height":101.0},"expanded":false,"debug":false,"output":0.0}'
18+
- type: FloatNode, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
19+
jsonDatas: '{"GUID":"2f236b55-86f6-4620-9c5b-08ffcfce738b","computeOrder":1,"canProcess":true,"position":{"serializedVersion":"2","x":214.9364471435547,"y":-122.57205200195313,"width":73.0,"height":101.0},"expanded":false,"debug":false,"output":42.0}'
20+
- type: FloatNode, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
21+
jsonDatas: '{"GUID":"fd0502d1-49cf-4491-aa43-1e22e5ad8d2c","computeOrder":1,"canProcess":true,"position":{"serializedVersion":"2","x":214.98304748535157,"y":0.6059228777885437,"width":73.0,"height":101.0},"expanded":false,"debug":false,"output":21.0}'
22+
- type: FloatNode, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
23+
jsonDatas: '{"GUID":"e9d66bf0-09b9-40c9-a0ef-91532d921152","computeOrder":1,"canProcess":true,"position":{"serializedVersion":"2","x":213.72882080078126,"y":127.56866455078125,"width":73.0,"height":101.0},"expanded":false,"debug":false,"output":10.5}'
24+
edges:
25+
- GUID: be9cf41a-a131-40c9-9c13-3f4c24427ec2
26+
owner: {fileID: 11400000}
27+
inputNodeGUID: 50c3eea6-ac9e-4f87-8c1d-4db89bed3ef6
28+
outputNodeGUID: 2f236b55-86f6-4620-9c5b-08ffcfce738b
29+
inputFieldName: inputs
30+
outputFieldName: output
1731
commentBlocks: []
18-
pinnedElements: []
32+
pinnedElements:
33+
- position:
34+
serializedVersion: 2
35+
x: 20
36+
y: 201
37+
width: 150
38+
height: 200
39+
opened: 1
40+
editorType:
41+
serializedType: GraphProcessor.ExposedParameterView, com.alelievr.NodeGraphProcessor,
42+
Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
43+
- position:
44+
serializedVersion: 2
45+
x: 13
46+
y: 45
47+
width: 150
48+
height: 200
49+
opened: 0
50+
editorType:
51+
serializedType: GraphProcessor.ProcessorView, com.alelievr.NodeGraphProcessor,
52+
Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
1953
exposedParameters: []
20-
position: {x: 0, y: 0, z: 0}
21-
scale: {x: 1, y: 1, z: 1}
54+
position: {x: 30, y: 179.5, z: 0}
55+
scale: {x: 0.9402391, y: 0.9402391, z: 1}

Assets/NodeGraphProcessor/Editor/PortBehaviors.meta

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

Assets/NodeGraphProcessor/Editor/Views/BaseNodeView.cs

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public class BaseNodeView : NodeView
3030

3131
Label computeOrderLabel = new Label();
3232

33+
public event Action< PortView > onPortConnected;
34+
public event Action< PortView > onPortDisconnected;
35+
3336
readonly string baseNodeStyle = "GraphProcessorStyles/BaseNodeView";
3437

3538
#region Initialization
@@ -54,11 +57,37 @@ public void Initialize(BaseGraphView owner, BaseNode node)
5457

5558
void InitializePorts()
5659
{
57-
foreach (var fieldInfo in nodeTarget.nodeFields)
60+
var listener = owner.connectorListener;
61+
62+
foreach (var fieldInfoKP in nodeTarget.nodeFields)
63+
{
64+
var fieldInfo = fieldInfoKP.Value;
65+
var direction = (fieldInfo.input) ? Direction.Input : Direction.Output;
66+
67+
if (fieldInfo.behavior != null)
68+
{
69+
UpdateNodePortsForField(fieldInfo);
70+
}
71+
else
72+
{
73+
AddPort(fieldInfo.info, direction, listener, fieldInfo.isMultiple, fieldInfo.name);
74+
}
75+
}
76+
}
77+
78+
void UpdateNodePortsForField(BaseNode.NodeFieldInformation fieldInfo)
79+
{
80+
if (fieldInfo.behavior == null)
81+
return ;
82+
83+
var listener = owner.connectorListener;
84+
var direction = (fieldInfo.input) ? Direction.Input : Direction.Output;
85+
var container = fieldInfo.input ? nodeTarget.inputPorts as NodePortContainer : nodeTarget.outputPorts as NodePortContainer;
86+
var nodePort = container.FirstOrDefault(np => np.fieldName == fieldInfo.fieldName);
87+
88+
foreach (var portData in fieldInfo.behavior(nodePort.GetEdges()))
5889
{
59-
if (fieldInfo.Value.)
60-
// TODO: handle custom port behavior per node fields
61-
nodeView.AddPort(fieldInfo, direction, listener, isMultiple, name);
90+
AddPort(fieldInfo.info, direction, listener, fieldInfo.isMultiple, portData.displayName);
6291
}
6392
}
6493

@@ -211,8 +240,29 @@ public virtual void DrawDefaultInspector()
211240
}
212241
}
213242

214-
public virtual void OnPortConnected(PortView port) {}
215-
public virtual void OnPortDisconnected(PortView port) {}
243+
public void OnPortConnected(PortView port)
244+
{
245+
// TODO: if the port have a custom behavior, then update it's ports
246+
var nodeField = nodeTarget.nodeFields.FirstOrDefault(n => n.Value.fieldName == port.fieldName).Value;
247+
248+
if (nodeField.behavior != null)
249+
{
250+
// Clear all the port of this field:
251+
// RemovePort()
252+
UpdateNodePortsForField(nodeField);
253+
}
254+
255+
onPortConnected?.Invoke(port);
256+
}
257+
258+
public void OnPortDisconnected(PortView port)
259+
{
260+
// TODO: if the port have a custom behavior, then update it's ports
261+
262+
onPortDisconnected?.Invoke(port);
263+
}
264+
265+
// TODO: a function to force to reload the custom behavior ports (if we want to do a button to add ports for example)
216266

217267
public virtual void OnRemoved() {}
218268
public virtual void OnCreated() {}

Assets/NodeGraphProcessor/Editor/Views/PortView.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class PortView : Port
1212
{
1313
public bool isMultiple;
1414
public string connectedFieldName { get; protected set; }
15+
public string fieldName => fieldInfo.Name;
1516
public new Type portType;
1617
public BaseNodeView owner { get; private set; }
1718

Assets/NodeGraphProcessor/Runtime/Elements/BaseNode.cs

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

99
namespace GraphProcessor
1010
{
11+
public delegate IEnumerable< PortData > CustomPortBehaviorDelegate(List< SerializableEdge > edges);
12+
1113
[Serializable]
1214
public abstract class BaseNode
1315
{
@@ -41,19 +43,21 @@ public abstract class BaseNode
4143

4244
public class NodeFieldInformation
4345
{
44-
public string name;
45-
public string fieldName;
46-
public FieldInfo info;
47-
public bool input;
48-
public bool isMultiple;
49-
50-
public NodeFieldInformation(FieldInfo info, string name, bool input, bool isMultiple)
46+
public string name;
47+
public string fieldName;
48+
public FieldInfo info;
49+
public bool input;
50+
public bool isMultiple;
51+
public CustomPortBehaviorDelegate behavior;
52+
53+
public NodeFieldInformation(FieldInfo info, string name, bool input, bool isMultiple, CustomPortBehaviorDelegate behavior)
5154
{
5255
this.input = input;
5356
this.isMultiple = isMultiple;
5457
this.info = info;
5558
this.name = name;
5659
this.fieldName = info.Name;
60+
this.behavior = behavior;
5761
}
5862
}
5963

@@ -115,6 +119,7 @@ public virtual void OnNodeCreated()
115119
void InitializeInOutDatas()
116120
{
117121
var fields = GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
122+
var methods = GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
118123

119124
foreach (var field in fields)
120125
{
@@ -136,7 +141,30 @@ void InitializeInOutDatas()
136141
if (!String.IsNullOrEmpty(outputAttribute?.name))
137142
name = outputAttribute.name;
138143

139-
nodeFields[field.Name] = new NodeFieldInformation(field, name, input, isMultiple);
144+
// By default we set the behavior to null, if the field have a custom behavior, it will be set in the loop just below
145+
nodeFields[field.Name] = new NodeFieldInformation(field, name, input, isMultiple, null);
146+
}
147+
148+
foreach (var method in methods)
149+
{
150+
var customPortBehaviorAttribute = method.GetCustomAttribute< CustomPortBehaviorAttribute >();
151+
CustomPortBehaviorDelegate behavior = null;
152+
153+
if (customPortBehaviorAttribute == null)
154+
continue ;
155+
156+
// Check if custom port behavior function is valid
157+
try {
158+
var referenceType = typeof(CustomPortBehaviorDelegate);
159+
behavior = (CustomPortBehaviorDelegate)Delegate.CreateDelegate(referenceType, this, method, true);
160+
} catch {
161+
Debug.LogError("The function " + method + " can be converted to the required delegate format: " + typeof(CustomPortBehaviorDelegate));
162+
}
163+
164+
if (nodeFields.ContainsKey(customPortBehaviorAttribute.fieldName))
165+
nodeFields[customPortBehaviorAttribute.fieldName].behavior = behavior;
166+
else
167+
Debug.LogError("Invalid field name for custom port behavior: " + method + ", " + customPortBehaviorAttribute.fieldName);
140168
}
141169
}
142170

0 commit comments

Comments
 (0)