From ad5a8dfc25a42d2e2f952415640768d33fee833a Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 27 Jul 2018 13:25:07 +0300 Subject: [PATCH 1/4] Fix laggy connections --- Scripts/Editor/NodeEditorAction.cs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index 2996938..a13d2d7 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -81,11 +81,39 @@ namespace XNodeEditor { for (int i = 0; i < Selection.objects.Length; i++) { if (Selection.objects[i] is XNode.Node) { XNode.Node node = Selection.objects[i] as XNode.Node; + Vector2 initial = node.position; node.position = mousePos + dragOffset[i]; if (gridSnap) { node.position.x = (Mathf.Round((node.position.x + 8) / 16) * 16) - 8; node.position.y = (Mathf.Round((node.position.y + 8) / 16) * 16) - 8; } + + Vector2 offset = node.position - initial; + if (offset.sqrMagnitude > 0){ + foreach (XNode.NodePort output in node.Outputs){ + Rect rect; + if (portConnectionPoints.TryGetValue(output, out rect)) { + rect.position += offset; + portConnectionPoints[output] = rect; + } + + for (int k = 0; k < output.ConnectionCount; k++) { + List reroutePoints = output.GetReroutePoints(k); + for (int index = 0; index < reroutePoints.Count; index++) { + reroutePoints[index] += offset; + } + } + + } + + foreach (XNode.NodePort input in node.Inputs) { + Rect rect; + if (portConnectionPoints.TryGetValue(input, out rect)) { + rect.position += offset; + portConnectionPoints[input] = rect; + } + } + } } } // Move selected reroutes with offset From ea14f27a23f2382e9cc96fbda13f322d0480b40b Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 27 Jul 2018 14:07:13 +0300 Subject: [PATCH 2/4] Fixed missing connections after serialization/deserialization --- Scripts/Editor/NodeEditorWindow.cs | 58 ++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/Scripts/Editor/NodeEditorWindow.cs b/Scripts/Editor/NodeEditorWindow.cs index 7fdc49d..b9ea2a5 100644 --- a/Scripts/Editor/NodeEditorWindow.cs +++ b/Scripts/Editor/NodeEditorWindow.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using UnityEditor; using UnityEditor.Callbacks; using UnityEngine; @@ -10,7 +10,61 @@ namespace XNodeEditor { /// Stores node positions for all nodePorts. public Dictionary portConnectionPoints { get { return _portConnectionPoints; } } + private Dictionary _portConnectionPoints = new Dictionary(); + + [System.Serializable] + private class NodePortReference { + [SerializeField] + private XNode.Node _node; + [SerializeField] + private string _name; + + + public NodePortReference(XNode.NodePort nodePort) { + _node = nodePort.node; + _name = nodePort.fieldName; + } + + public XNode.NodePort GetNodePort() { + if (_node == null) { + return null; + } + + return _node.GetPort(_name); + } + + } + + [SerializeField] + private NodePortReference[] _references = new NodePortReference[0]; + [SerializeField] + private Rect[] _rects = new Rect[0]; + + private void OnDisable() { + int count = portConnectionPoints.Count; + _references = new NodePortReference[count]; + _rects = new Rect[count]; + int index = 0; + foreach (var portConnectionPoint in portConnectionPoints) { + _references[index] = new NodePortReference(portConnectionPoint.Key); + _rects[index] = portConnectionPoint.Value; + index++; + } + } + + private void OnEnable() { + int length = _references.Length; + + if (length == _rects.Length) { + for (int i = 0; i < length; i++) { + XNode.NodePort nodePort = _references[i].GetNodePort(); + if (nodePort != null) + _portConnectionPoints.Add(nodePort, _rects[i]); + } + } + } + public Dictionary nodeSizes { get { return _nodeSizes; } } private Dictionary _nodeSizes = new Dictionary(); public XNode.NodeGraph graph; @@ -25,7 +79,7 @@ namespace XNodeEditor { if (graphEditor != null && NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); } - partial void OnEnable(); + //partial void OnEnable(); /// Create editor window public static NodeEditorWindow Init() { NodeEditorWindow w = CreateInstance(); From 6d0ce544c9a222ada5bdd7cef81416371ea9f158 Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Fri, 27 Jul 2018 14:30:59 +0200 Subject: [PATCH 3/4] Small formatting changes and comments --- Scripts/Editor/NodeEditorAction.cs | 2 +- Scripts/Editor/NodeEditorWindow.cs | 24 +++++++----------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index a13d2d7..0ba2428 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -88,6 +88,7 @@ namespace XNodeEditor { node.position.y = (Mathf.Round((node.position.y + 8) / 16) * 16) - 8; } + // Offset portConnectionPoints instantly if a node is dragged so they aren't delayed by a frame. Vector2 offset = node.position - initial; if (offset.sqrMagnitude > 0){ foreach (XNode.NodePort output in node.Outputs){ @@ -103,7 +104,6 @@ namespace XNodeEditor { reroutePoints[index] += offset; } } - } foreach (XNode.NodePort input in node.Inputs) { diff --git a/Scripts/Editor/NodeEditorWindow.cs b/Scripts/Editor/NodeEditorWindow.cs index b9ea2a5..72fd4ce 100644 --- a/Scripts/Editor/NodeEditorWindow.cs +++ b/Scripts/Editor/NodeEditorWindow.cs @@ -10,16 +10,13 @@ namespace XNodeEditor { /// Stores node positions for all nodePorts. public Dictionary portConnectionPoints { get { return _portConnectionPoints; } } - private Dictionary _portConnectionPoints = new Dictionary(); + [SerializeField] private NodePortReference[] _references = new NodePortReference[0]; + [SerializeField] private Rect[] _rects = new Rect[0]; - [System.Serializable] - private class NodePortReference { - [SerializeField] - private XNode.Node _node; - [SerializeField] - private string _name; - + [System.Serializable] private class NodePortReference { + [SerializeField] private XNode.Node _node; + [SerializeField] private string _name; public NodePortReference(XNode.NodePort nodePort) { _node = nodePort.node; @@ -30,18 +27,12 @@ namespace XNodeEditor { if (_node == null) { return null; } - return _node.GetPort(_name); } - } - [SerializeField] - private NodePortReference[] _references = new NodePortReference[0]; - [SerializeField] - private Rect[] _rects = new Rect[0]; - private void OnDisable() { + // Cache portConnectionPoints before serialization starts int count = portConnectionPoints.Count; _references = new NodePortReference[count]; _rects = new Rect[count]; @@ -54,8 +45,8 @@ namespace XNodeEditor { } private void OnEnable() { + // Reload portConnectionPoints if there are any int length = _references.Length; - if (length == _rects.Length) { for (int i = 0; i < length; i++) { XNode.NodePort nodePort = _references[i].GetNodePort(); @@ -79,7 +70,6 @@ namespace XNodeEditor { if (graphEditor != null && NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); } - //partial void OnEnable(); /// Create editor window public static NodeEditorWindow Init() { NodeEditorWindow w = CreateInstance(); From f65d4a2dd78aa433c2a8b643a9c6e679872f7d97 Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Fri, 27 Jul 2018 14:33:59 +0200 Subject: [PATCH 4/4] Removed offsetting reroute points with output node. Reroute points should only move if dragged. --- Scripts/Editor/NodeEditorAction.cs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index 0ba2428..12a48ec 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -97,13 +97,6 @@ namespace XNodeEditor { rect.position += offset; portConnectionPoints[output] = rect; } - - for (int k = 0; k < output.ConnectionCount; k++) { - List reroutePoints = output.GetReroutePoints(k); - for (int index = 0; index < reroutePoints.Count; index++) { - reroutePoints[index] += offset; - } - } } foreach (XNode.NodePort input in node.Inputs) {