From bc5c060dee59c9fba6a0f80f6b789bffa620746f Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Fri, 13 Oct 2017 20:28:40 +0200 Subject: [PATCH] Improved broken connection cleanup. Example updates --- Example/ExampleNodeGraph.asset | Bin 0 -> 8728 bytes Example/ExampleNodeGraph.asset.meta | 9 +++++++ Example/Nodes/BaseNode.cs | 15 ------------ Example/Nodes/ConstantValue.cs | 13 +++++++++++ ...BaseNode.cs.meta => ConstantValue.cs.meta} | 4 ++-- Example/Nodes/DisplayValue.cs | 3 ++- Example/Nodes/Editor/DisplayValueEditor.cs | 17 ++------------ Example/Nodes/ExampleNodeBase.cs | 22 ++++++++++++++++++ Example/Nodes/ExampleNodeBase.cs.meta | 12 ++++++++++ Example/Nodes/MathNode.cs | 13 +++++------ Scripts/Editor/NodeEditorGUI.cs | 2 ++ Scripts/NodePort.cs | 9 +++++++ 12 files changed, 79 insertions(+), 40 deletions(-) create mode 100644 Example/ExampleNodeGraph.asset create mode 100644 Example/ExampleNodeGraph.asset.meta delete mode 100644 Example/Nodes/BaseNode.cs create mode 100644 Example/Nodes/ConstantValue.cs rename Example/Nodes/{BaseNode.cs.meta => ConstantValue.cs.meta} (76%) create mode 100644 Example/Nodes/ExampleNodeBase.cs create mode 100644 Example/Nodes/ExampleNodeBase.cs.meta diff --git a/Example/ExampleNodeGraph.asset b/Example/ExampleNodeGraph.asset new file mode 100644 index 0000000000000000000000000000000000000000..8e72bce34b6042278f4d4335cd95155fc108d7ba GIT binary patch literal 8728 zcmeHMYiv|S6h61zvb^LiQb2@)JOnFCL9jyErB9&HLWC#=%eGzUl6H5q-In%8$;AXC z5+PQ=DnBUChz5*EsIMprMvXuGBZ*N{L8C^aKhTH-AN71QcW!6aO>F%|;+|yo&U|;~ zn=@z5nVCCFH1R#6oN+`nl4#;(e$u?TzJ?hUYTR9b6vhyi+`nWsrT*;mOkK>pTNAT<7t+ zxJC1sr`h2_9|7^s)Mt=}>pX*9+@em_b0hOWzaijYB4vk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vuJ>wE_B3?Uf%T zk6pg^`%$+)qIh9vH6JPc#?C7wn)7b5-zY!T^#P}DQ|P1nHp=}KrDvb)cX>41@4Txk z`cmyx?CJVq?08>|iY&@6bWG{*@c2c=e*CEZGr5hjGsfcjJ#A-wZ@4-`Cy3rJ^aGEs dJL;%0ArH~pr1E?9UZ0~>ylQTh?vh>V@Hfa(MTGzW literal 0 HcmV?d00001 diff --git a/Example/ExampleNodeGraph.asset.meta b/Example/ExampleNodeGraph.asset.meta new file mode 100644 index 0000000..6ec3d2f --- /dev/null +++ b/Example/ExampleNodeGraph.asset.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: e8c47bc953732464a9bf3a76273d99ef +timeCreated: 1507916591 +licenseType: Free +NativeFormatImporter: + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Example/Nodes/BaseNode.cs b/Example/Nodes/BaseNode.cs deleted file mode 100644 index 26d4a8d..0000000 --- a/Example/Nodes/BaseNode.cs +++ /dev/null @@ -1,15 +0,0 @@ -using UnityEngine; - -[System.Serializable] -public class BaseNode : Node { - - [Input] public string input; - [Output] public string output; - public bool concat; - [TextArea] - public string SomeString; - [Header("New stuff")] - public Color col; - public AnimationCurve anim; - public Vector3 vec; -} diff --git a/Example/Nodes/ConstantValue.cs b/Example/Nodes/ConstantValue.cs new file mode 100644 index 0000000..b0b99ce --- /dev/null +++ b/Example/Nodes/ConstantValue.cs @@ -0,0 +1,13 @@ +[System.Serializable] +public class ConstantValue : ExampleNodeBase { + public float a; + [Output] public float value; + + protected override void Init() { + name = "Constant Value"; + } + + public override object GetValue(NodePort port) { + return a; + } +} diff --git a/Example/Nodes/BaseNode.cs.meta b/Example/Nodes/ConstantValue.cs.meta similarity index 76% rename from Example/Nodes/BaseNode.cs.meta rename to Example/Nodes/ConstantValue.cs.meta index e525d86..e836841 100644 --- a/Example/Nodes/BaseNode.cs.meta +++ b/Example/Nodes/ConstantValue.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 23665941e8cd89a48b1272ac5fd6510c -timeCreated: 1505462705 +guid: 707240ce8955a0240a7c0c4177d83bf5 +timeCreated: 1505937586 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Example/Nodes/DisplayValue.cs b/Example/Nodes/DisplayValue.cs index 01a2b0f..d19dd21 100644 --- a/Example/Nodes/DisplayValue.cs +++ b/Example/Nodes/DisplayValue.cs @@ -1,5 +1,6 @@ using UnityEngine; -public class DisplayValue : Node { +public class DisplayValue : ExampleNodeBase { [Input] public float value; + } diff --git a/Example/Nodes/Editor/DisplayValueEditor.cs b/Example/Nodes/Editor/DisplayValueEditor.cs index 34276a8..0530c18 100644 --- a/Example/Nodes/Editor/DisplayValueEditor.cs +++ b/Example/Nodes/Editor/DisplayValueEditor.cs @@ -12,20 +12,7 @@ public class DisplayValueEditor : NodeEditor { } public float GetResult() { - float result = 0f; - NodePort port = target.GetInputByFieldName("value"); - if (port == null) return result; - int connectionCount = port.ConnectionCount; - for (int i = 0; i < connectionCount; i++) { - - NodePort connection = port.GetConnection(i); - if (connection == null) continue; - object obj = connection.GetValue(); - if (obj == null) continue; - - if (connection.type == typeof(int)) result += (int)obj; - else if (connection.type == typeof(float)) result += (float)obj; - } - return result; + ExampleNodeBase t = target as ExampleNodeBase; + return t.GetInputFloat("value"); } } diff --git a/Example/Nodes/ExampleNodeBase.cs b/Example/Nodes/ExampleNodeBase.cs new file mode 100644 index 0000000..c47cff0 --- /dev/null +++ b/Example/Nodes/ExampleNodeBase.cs @@ -0,0 +1,22 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class ExampleNodeBase : Node { + + public float GetInputFloat(string fieldName) { + float result = 0f; + NodePort port = GetInputByFieldName(fieldName); + if (port == null) return result; + int connectionCount = port.ConnectionCount; + for (int i = 0; i < connectionCount; i++) { + NodePort connection = port.GetConnection(i); + if (connection == null) continue; + object obj = connection.GetValue(); + if (obj == null) continue; + if (connection.type == typeof(int)) result += (int)obj; + else if (connection.type == typeof(float)) result += (float)obj; + } + return result; + } +} diff --git a/Example/Nodes/ExampleNodeBase.cs.meta b/Example/Nodes/ExampleNodeBase.cs.meta new file mode 100644 index 0000000..1cc1c43 --- /dev/null +++ b/Example/Nodes/ExampleNodeBase.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 923bada49e668fd4a98b04fcb49999d7 +timeCreated: 1507917099 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Example/Nodes/MathNode.cs b/Example/Nodes/MathNode.cs index 32aa322..475698d 100644 --- a/Example/Nodes/MathNode.cs +++ b/Example/Nodes/MathNode.cs @@ -1,9 +1,9 @@ using UnityEngine; [System.Serializable] -public class MathNode : Node { - public float a; - public float b; +public class MathNode : ExampleNodeBase { + [Input] public float c; + [Input] public float b; [Output] public float result; public enum MathType { Add, Subtract, Multiply, Divide} public MathType mathType = MathType.Add; @@ -13,6 +13,9 @@ public class MathNode : Node { } public override object GetValue(NodePort port) { + float a = GetInputFloat("c"); + float b = GetInputFloat("b"); + switch(port.fieldName) { case "result": switch(mathType) { @@ -25,8 +28,4 @@ public class MathNode : Node { } return 0f; } - - public override void OnCreateConnection(NodePort from, NodePort to) { - - } } diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index cf0c44e..d04f747 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -128,7 +128,9 @@ public partial class NodeEditorWindow { if (!portConnectionPoints.ContainsKey(output)) continue; Vector2 from = _portConnectionPoints[output].center; for (int k = 0; k < output.ConnectionCount; k++) { + NodePort input = output.GetConnection(k); + if (input == null) return; //If a script has been updated and the port doesn't exist, it is removed and null is returned. If this happens, return. Vector2 to = _portConnectionPoints[input].center; DrawConnection(from, to, NodeEditorUtilities.GetTypeColor(output.type)); } diff --git a/Scripts/NodePort.cs b/Scripts/NodePort.cs index 808fabd..1980935 100644 --- a/Scripts/NodePort.cs +++ b/Scripts/NodePort.cs @@ -77,7 +77,16 @@ public class NodePort { } public NodePort GetConnection(int i) { + //If the connection is broken for some reason, remove it. + if (connections[i].node == null || string.IsNullOrEmpty(connections[i].fieldName)) { + connections.RemoveAt(i); + return null; + } NodePort port = connections[i].node.GetPortByFieldName(connections[i].fieldName); + if (port == null) { + connections.RemoveAt(i); + return null; + } return port; }