From 6d1492b220b4096260603f2ddb200d70f9f8add3 Mon Sep 17 00:00:00 2001 From: Insthync Date: Sat, 28 Jan 2023 20:55:48 +0700 Subject: [PATCH] feat: Add swapping state parameter to node connection and disconnection functions, may use it to detect how node connection/disconnection were made --- Scripts/Node.cs | 9 ++++++--- Scripts/NodePort.cs | 37 +++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/Scripts/Node.cs b/Scripts/Node.cs index 704e99d..2cfd569 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -250,12 +250,15 @@ namespace XNode { #endregion /// Called after a connection between two s is created - /// Output Input - public virtual void OnCreateConnection(NodePort from, NodePort to) { } + /// Output + /// Input + /// Creating while swapping or not? + public virtual void OnCreateConnection(NodePort from, NodePort to, bool swapping) { } /// Called after a connection is removed from this port /// Output or Input - public virtual void OnRemoveConnection(NodePort port) { } + /// Removing while swapping or not? + public virtual void OnRemoveConnection(NodePort port, bool swapping) { } /// Disconnect everything from this node public void ClearConnections() { diff --git a/Scripts/NodePort.cs b/Scripts/NodePort.cs index 6bcc638..8e0114f 100644 --- a/Scripts/NodePort.cs +++ b/Scripts/NodePort.cs @@ -202,7 +202,8 @@ namespace XNode { /// Connect this to another /// The to connect to - public void Connect(NodePort port) { + /// Creating while swapping or not? + public void Connect(NodePort port, bool swapping = false) { if (connections == null) connections = new List(); if (port == null) { Debug.LogWarning("Cannot connect to null port"); return; } if (port == this) { Debug.LogWarning("Cannot connect port to self."); return; } @@ -212,13 +213,13 @@ namespace XNode { UnityEditor.Undo.RecordObject(node, "Connect Port"); UnityEditor.Undo.RecordObject(port.node, "Connect Port"); #endif - if (port.connectionType == Node.ConnectionType.Override && port.ConnectionCount != 0) { port.ClearConnections(); } - if (connectionType == Node.ConnectionType.Override && ConnectionCount != 0) { ClearConnections(); } + if (port.connectionType == Node.ConnectionType.Override && port.ConnectionCount != 0) { port.ClearConnections(swapping); } + if (connectionType == Node.ConnectionType.Override && ConnectionCount != 0) { ClearConnections(swapping); } connections.Add(new PortConnection(port)); if (port.connections == null) port.connections = new List(); if (!port.IsConnectedTo(this)) port.connections.Add(new PortConnection(this)); - node.OnCreateConnection(this, port); - port.node.OnCreateConnection(this, port); + node.OnCreateConnection(this, port, swapping); + port.node.OnCreateConnection(this, port, swapping); } public List GetConnections() { @@ -284,7 +285,7 @@ namespace XNode { } /// Disconnect this port from another port - public void Disconnect(NodePort port) { + public void Disconnect(NodePort port, bool swapping = false) { // Remove this ports connection to the other for (int i = connections.Count - 1; i >= 0; i--) { if (connections[i].Port == port) { @@ -297,16 +298,16 @@ namespace XNode { if (port.connections[i].Port == this) { port.connections.RemoveAt(i); // Trigger OnRemoveConnection from this side port - port.node.OnRemoveConnection(port); + port.node.OnRemoveConnection(port, swapping); } } } // Trigger OnRemoveConnection - node.OnRemoveConnection(this); + node.OnRemoveConnection(this, swapping); } /// Disconnect this port from another port - public void Disconnect(int i) { + public void Disconnect(int i, bool swapping = false) { // Remove the other ports connection to this port NodePort otherPort = connections[i].Port; if (otherPort != null) { @@ -316,13 +317,13 @@ namespace XNode { connections.RemoveAt(i); // Trigger OnRemoveConnection - node.OnRemoveConnection(this); - if (otherPort != null) otherPort.node.OnRemoveConnection(otherPort); + node.OnRemoveConnection(this, swapping); + if (otherPort != null) otherPort.node.OnRemoveConnection(otherPort, swapping); } - public void ClearConnections() { + public void ClearConnections(bool swapping = false) { while (connections.Count > 0) { - Disconnect(connections[0].Port); + Disconnect(connections[0].Port, swapping); } } @@ -347,16 +348,16 @@ namespace XNode { for (int i = 0; i < bConnectionCount; i++) targetPortConnections.Add(targetPort.connections[i].Port); - ClearConnections(); - targetPort.ClearConnections(); + ClearConnections(true); + targetPort.ClearConnections(true); // Add port connections to targetPort for (int i = 0; i < portConnections.Count; i++) - targetPort.Connect(portConnections[i]); + targetPort.Connect(portConnections[i], true); // Add target port connections to this one for (int i = 0; i < targetPortConnections.Count; i++) - Connect(targetPortConnections[i]); + Connect(targetPortConnections[i], true); } @@ -380,7 +381,7 @@ namespace XNode { NodePort otherPort = connection.Port; Connect(otherPort); } - ClearConnections(); + ClearConnections(false); } /// Swap connected nodes from the old list with nodes from the new list