mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 17:26:02 +08:00
VerifyConnectionCache
Still WIP
This commit is contained in:
parent
803b327522
commit
75be340a96
@ -210,13 +210,11 @@ namespace XNodeEditor {
|
|||||||
if (graph.nodes.Count != 0) draggedOutput.Connect(draggedOutputTarget);
|
if (graph.nodes.Count != 0) draggedOutput.Connect(draggedOutputTarget);
|
||||||
|
|
||||||
// ConnectionIndex can be -1 if the connection is removed instantly after creation
|
// ConnectionIndex can be -1 if the connection is removed instantly after creation
|
||||||
int connectionIndex = draggedOutput.GetConnectionIndex(draggedOutputTarget);
|
List<Vector2> reroutePoints = draggedOutput.GetReroutePoints(draggedOutputTarget);
|
||||||
if (connectionIndex != -1) {
|
reroutePoints.AddRange(draggedOutputReroutes);
|
||||||
draggedOutput.GetReroutePoints(connectionIndex).AddRange(draggedOutputReroutes);
|
|
||||||
if (NodeEditor.onUpdateNode != null) NodeEditor.onUpdateNode(node);
|
if (NodeEditor.onUpdateNode != null) NodeEditor.onUpdateNode(node);
|
||||||
EditorUtility.SetDirty(graph);
|
EditorUtility.SetDirty(graph);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
//Release dragged connection
|
//Release dragged connection
|
||||||
draggedOutput = null;
|
draggedOutput = null;
|
||||||
draggedOutputTarget = null;
|
draggedOutputTarget = null;
|
||||||
|
|||||||
@ -24,7 +24,13 @@ namespace XNode {
|
|||||||
public IO direction { get { return _direction; } }
|
public IO direction { get { return _direction; } }
|
||||||
public Node.ConnectionType connectionType { get { return _connectionType; } }
|
public Node.ConnectionType connectionType { get { return _connectionType; } }
|
||||||
public Node.TypeConstraint typeConstraint { get { return _typeConstraint; } }
|
public Node.TypeConstraint typeConstraint { get { return _typeConstraint; } }
|
||||||
public ReadOnlyCollection<NodePort> Connections { get; private set; }
|
public ReadOnlyCollection<NodePort> Connections {
|
||||||
|
get {
|
||||||
|
VerifyConnectionCache();
|
||||||
|
return _Connections;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private ReadOnlyCollection<NodePort> _Connections;
|
||||||
|
|
||||||
/// <summary> Is this port connected to anytihng? </summary>
|
/// <summary> Is this port connected to anytihng? </summary>
|
||||||
public bool IsConnected { get { return connectionCache.Count != 0; } }
|
public bool IsConnected { get { return connectionCache.Count != 0; } }
|
||||||
@ -56,12 +62,19 @@ namespace XNode {
|
|||||||
[SerializeField] private Node.TypeConstraint _typeConstraint;
|
[SerializeField] private Node.TypeConstraint _typeConstraint;
|
||||||
[SerializeField] private bool _dynamic;
|
[SerializeField] private bool _dynamic;
|
||||||
|
|
||||||
public List<NodePort> connectionCache = new List<NodePort>();
|
[NonSerialized] readonly private List<NodePort> connectionCache = new List<NodePort>();
|
||||||
public Dictionary<NodePort, List<Vector2>> rerouteCache = new Dictionary<NodePort, List<Vector2>>();
|
[NonSerialized] readonly private Dictionary<NodePort, List<Vector2>> rerouteCache = new Dictionary<NodePort, List<Vector2>>();
|
||||||
|
[NonSerialized] private bool initializedCache;
|
||||||
|
|
||||||
|
private NodePort() {
|
||||||
|
connectionCache = new List<NodePort>();
|
||||||
|
rerouteCache = new Dictionary<NodePort, List<Vector2>>();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary> Construct a static targetless nodeport. Used as a template. </summary>
|
/// <summary> Construct a static targetless nodeport. Used as a template. </summary>
|
||||||
public NodePort(FieldInfo fieldInfo) {
|
public NodePort(FieldInfo fieldInfo) {
|
||||||
Debug.Log("Create, " + connectionCache + ", " + rerouteCache, node);
|
connectionCache = new List<NodePort>();
|
||||||
|
rerouteCache = new Dictionary<NodePort, List<Vector2>>();
|
||||||
_fieldName = fieldInfo.Name;
|
_fieldName = fieldInfo.Name;
|
||||||
ValueType = fieldInfo.FieldType;
|
ValueType = fieldInfo.FieldType;
|
||||||
_dynamic = false;
|
_dynamic = false;
|
||||||
@ -80,7 +93,8 @@ namespace XNode {
|
|||||||
|
|
||||||
/// <summary> Copy a nodePort but assign it to another node. </summary>
|
/// <summary> Copy a nodePort but assign it to another node. </summary>
|
||||||
public NodePort(NodePort nodePort, Node node) {
|
public NodePort(NodePort nodePort, Node node) {
|
||||||
Debug.Log("Create");
|
connectionCache = new List<NodePort>();
|
||||||
|
rerouteCache = new Dictionary<NodePort, List<Vector2>>();
|
||||||
_fieldName = nodePort._fieldName;
|
_fieldName = nodePort._fieldName;
|
||||||
ValueType = nodePort.valueType;
|
ValueType = nodePort.valueType;
|
||||||
_direction = nodePort.direction;
|
_direction = nodePort.direction;
|
||||||
@ -102,7 +116,22 @@ namespace XNode {
|
|||||||
_typeConstraint = typeConstraint;
|
_typeConstraint = typeConstraint;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnBeforeSerialize() {
|
private void VerifyConnectionCache() {
|
||||||
|
if (!initializedCache) {
|
||||||
|
connectionCache.Clear();
|
||||||
|
rerouteCache.Clear();
|
||||||
|
for (int i = 0; i < connections.Count; i++) {
|
||||||
|
NodePort port = connections[i].GetPort();
|
||||||
|
connectionCache.Add(port);
|
||||||
|
rerouteCache.Add(port, connections[i].reroutePoints);
|
||||||
|
}
|
||||||
|
_Connections = new ReadOnlyCollection<NodePort>(connectionCache);
|
||||||
|
initializedCache = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ISerializationCallbackReceiver.OnBeforeSerialize() {
|
||||||
|
if (initializedCache) {
|
||||||
connections = new List<PortConnection>();
|
connections = new List<PortConnection>();
|
||||||
for (int i = 0; i < connectionCache.Count; i++) {
|
for (int i = 0; i < connectionCache.Count; i++) {
|
||||||
List<Vector2> reroutes;
|
List<Vector2> reroutes;
|
||||||
@ -113,20 +142,13 @@ namespace XNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void OnAfterDeserialize() {
|
void ISerializationCallbackReceiver.OnAfterDeserialize() { }
|
||||||
connectionCache.Clear();
|
|
||||||
rerouteCache.Clear();
|
|
||||||
for (int i = 0; i < connections.Count; i++) {
|
|
||||||
NodePort port = connections[i].GetPort();
|
|
||||||
connectionCache.Add(port);
|
|
||||||
rerouteCache.Add(port, connections[i].reroutePoints);
|
|
||||||
}
|
|
||||||
Connections = new ReadOnlyCollection<NodePort>(connectionCache);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary> Checks all connections for invalid references, and removes them. </summary>
|
/// <summary> Checks all connections for invalid references, and removes them. </summary>
|
||||||
public void VerifyConnections() {
|
public void VerifyConnections() {
|
||||||
|
VerifyConnectionCache();
|
||||||
for (int i = connectionCache.Count - 1; i >= 0; i--) {
|
for (int i = connectionCache.Count - 1; i >= 0; i--) {
|
||||||
if (connectionCache[i].node != null &&
|
if (connectionCache[i].node != null &&
|
||||||
!string.IsNullOrEmpty(connectionCache[i].fieldName) &&
|
!string.IsNullOrEmpty(connectionCache[i].fieldName) &&
|
||||||
@ -154,6 +176,7 @@ namespace XNode {
|
|||||||
/// <summary> Return the output values of all connected ports. </summary>
|
/// <summary> Return the output values of all connected ports. </summary>
|
||||||
/// <returns> <see cref="NodePort.GetOutputValue"/> </returns>
|
/// <returns> <see cref="NodePort.GetOutputValue"/> </returns>
|
||||||
public object[] GetInputValues() {
|
public object[] GetInputValues() {
|
||||||
|
VerifyConnectionCache();
|
||||||
object[] objs = new object[ConnectionCount];
|
object[] objs = new object[ConnectionCount];
|
||||||
for (int i = 0; i < ConnectionCount; i++) {
|
for (int i = 0; i < ConnectionCount; i++) {
|
||||||
NodePort connectedPort = connectionCache[i];
|
NodePort connectedPort = connectionCache[i];
|
||||||
@ -225,6 +248,7 @@ namespace XNode {
|
|||||||
/// <summary> Connect this <see cref="NodePort"/> to another </summary>
|
/// <summary> Connect this <see cref="NodePort"/> to another </summary>
|
||||||
/// <param name="port">The <see cref="NodePort"/> to connect to</param>
|
/// <param name="port">The <see cref="NodePort"/> to connect to</param>
|
||||||
public void Connect(NodePort port) {
|
public void Connect(NodePort port) {
|
||||||
|
VerifyConnectionCache();
|
||||||
if (port == null) { Debug.LogWarning("Cannot connect to null port"); return; }
|
if (port == null) { Debug.LogWarning("Cannot connect to null port"); return; }
|
||||||
if (port == this) { Debug.LogWarning("Cannot connect port to self."); return; }
|
if (port == this) { Debug.LogWarning("Cannot connect port to self."); return; }
|
||||||
if (IsConnectedTo(port)) { Debug.LogWarning("Port already connected. "); return; }
|
if (IsConnectedTo(port)) { Debug.LogWarning("Port already connected. "); return; }
|
||||||
@ -239,6 +263,7 @@ namespace XNode {
|
|||||||
|
|
||||||
[Obsolete("Use Connections property instead")]
|
[Obsolete("Use Connections property instead")]
|
||||||
public List<NodePort> GetConnections() {
|
public List<NodePort> GetConnections() {
|
||||||
|
VerifyConnectionCache();
|
||||||
List<NodePort> result = new List<NodePort>();
|
List<NodePort> result = new List<NodePort>();
|
||||||
for (int i = 0; i < connectionCache.Count; i++) {
|
for (int i = 0; i < connectionCache.Count; i++) {
|
||||||
NodePort port = connectionCache[i];
|
NodePort port = connectionCache[i];
|
||||||
@ -249,16 +274,19 @@ namespace XNode {
|
|||||||
|
|
||||||
[Obsolete("Use Connections[i] instead")]
|
[Obsolete("Use Connections[i] instead")]
|
||||||
public NodePort GetConnection(int i) {
|
public NodePort GetConnection(int i) {
|
||||||
|
VerifyConnectionCache();
|
||||||
return connectionCache[i];
|
return connectionCache[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
[Obsolete("Use Connections.IndexOf(port) instead")]
|
[Obsolete("Use Connections.IndexOf(port) instead")]
|
||||||
/// <summary> Get index of the connection connecting this and specified ports </summary>
|
/// <summary> Get index of the connection connecting this and specified ports </summary>
|
||||||
public int GetConnectionIndex(NodePort port) {
|
public int GetConnectionIndex(NodePort port) {
|
||||||
|
VerifyConnectionCache();
|
||||||
return connectionCache.IndexOf(port);
|
return connectionCache.IndexOf(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsConnectedTo(NodePort port) {
|
public bool IsConnectedTo(NodePort port) {
|
||||||
|
VerifyConnectionCache();
|
||||||
return connectionCache.Contains(port);
|
return connectionCache.Contains(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -281,6 +309,8 @@ namespace XNode {
|
|||||||
|
|
||||||
/// <summary> Disconnect this port from another port </summary>
|
/// <summary> Disconnect this port from another port </summary>
|
||||||
public void Disconnect(NodePort port) {
|
public void Disconnect(NodePort port) {
|
||||||
|
|
||||||
|
VerifyConnectionCache();
|
||||||
// Remove this ports connection to the other
|
// Remove this ports connection to the other
|
||||||
for (int i = connectionCache.Count - 1; i >= 0; i--) {
|
for (int i = connectionCache.Count - 1; i >= 0; i--) {
|
||||||
connectionCache.Remove(port);
|
connectionCache.Remove(port);
|
||||||
@ -290,7 +320,7 @@ namespace XNode {
|
|||||||
for (int i = 0; i < port.connectionCache.Count; i++) {
|
for (int i = 0; i < port.connectionCache.Count; i++) {
|
||||||
port.connectionCache.Remove(this);
|
port.connectionCache.Remove(this);
|
||||||
}
|
}
|
||||||
}
|
} else Debug.LogWarning("Trying to disconnect a null port");
|
||||||
// Trigger OnRemoveConnection
|
// Trigger OnRemoveConnection
|
||||||
node.OnRemoveConnection(this);
|
node.OnRemoveConnection(this);
|
||||||
if (port != null) port.node.OnRemoveConnection(port);
|
if (port != null) port.node.OnRemoveConnection(port);
|
||||||
@ -310,10 +340,12 @@ namespace XNode {
|
|||||||
|
|
||||||
/// <summary> Get reroute points for a given connection. This is used for organization </summary>
|
/// <summary> Get reroute points for a given connection. This is used for organization </summary>
|
||||||
public List<Vector2> GetReroutePoints(NodePort port) {
|
public List<Vector2> GetReroutePoints(NodePort port) {
|
||||||
|
VerifyConnectionCache();
|
||||||
|
if (!rerouteCache.ContainsKey(port)) {
|
||||||
List<Vector2> reroutes = new List<Vector2>();
|
List<Vector2> reroutes = new List<Vector2>();
|
||||||
Debug.Log(port + " " + rerouteCache, node);
|
rerouteCache.Add(port, reroutes);
|
||||||
rerouteCache.TryGetValue(port, out reroutes);
|
}
|
||||||
return reroutes;
|
return rerouteCache[port];
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Get reroute points for a given connection. This is used for organization </summary>
|
/// <summary> Get reroute points for a given connection. This is used for organization </summary>
|
||||||
@ -323,6 +355,7 @@ namespace XNode {
|
|||||||
|
|
||||||
/// <summary> Swap connections with another node </summary>
|
/// <summary> Swap connections with another node </summary>
|
||||||
public void SwapConnections(NodePort targetPort) {
|
public void SwapConnections(NodePort targetPort) {
|
||||||
|
VerifyConnectionCache();
|
||||||
int aConnectionCount = connectionCache.Count;
|
int aConnectionCount = connectionCache.Count;
|
||||||
int bConnectionCount = targetPort.connectionCache.Count;
|
int bConnectionCount = targetPort.connectionCache.Count;
|
||||||
|
|
||||||
@ -352,6 +385,7 @@ namespace XNode {
|
|||||||
|
|
||||||
/// <summary> Copy all connections pointing to a node and add them to this one </summary>
|
/// <summary> Copy all connections pointing to a node and add them to this one </summary>
|
||||||
public void AddConnections(NodePort targetPort) {
|
public void AddConnections(NodePort targetPort) {
|
||||||
|
VerifyConnectionCache();
|
||||||
int connectionCount = targetPort.ConnectionCount;
|
int connectionCount = targetPort.ConnectionCount;
|
||||||
for (int i = 0; i < connectionCount; i++) {
|
for (int i = 0; i < connectionCount; i++) {
|
||||||
NodePort otherPort = targetPort.connectionCache[i];
|
NodePort otherPort = targetPort.connectionCache[i];
|
||||||
@ -361,6 +395,7 @@ namespace XNode {
|
|||||||
|
|
||||||
/// <summary> Move all connections pointing to this node, to another node </summary>
|
/// <summary> Move all connections pointing to this node, to another node </summary>
|
||||||
public void MoveConnections(NodePort targetPort) {
|
public void MoveConnections(NodePort targetPort) {
|
||||||
|
VerifyConnectionCache();
|
||||||
int connectionCount = connectionCache.Count;
|
int connectionCount = connectionCache.Count;
|
||||||
|
|
||||||
// Add connections to target port
|
// Add connections to target port
|
||||||
@ -373,6 +408,7 @@ namespace XNode {
|
|||||||
|
|
||||||
/// <summary> Swap connected nodes from the old list with nodes from the new list </summary>
|
/// <summary> Swap connected nodes from the old list with nodes from the new list </summary>
|
||||||
public void Redirect(List<Node> oldNodes, List<Node> newNodes) {
|
public void Redirect(List<Node> oldNodes, List<Node> newNodes) {
|
||||||
|
VerifyConnectionCache();
|
||||||
foreach (NodePort port in connectionCache) {
|
foreach (NodePort port in connectionCache) {
|
||||||
int index = oldNodes.IndexOf(port._node);
|
int index = oldNodes.IndexOf(port._node);
|
||||||
if (index >= 0) port._node = newNodes[index];
|
if (index >= 0) port._node = newNodes[index];
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user