mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-21 01:36:03 +08:00
Ports losing connections (#171)
* clear connections before removing port, so the connected nodes don't have invalid references. * Try reconnecting the previous ports connections in the new port. Skip reconnecting if the port is dynamic or if the direction has changed.
This commit is contained in:
parent
6a629d159c
commit
723ecc43c1
@ -14,6 +14,7 @@ namespace XNode {
|
||||
if (!Initialized) BuildCache();
|
||||
|
||||
Dictionary<string, NodePort> staticPorts = new Dictionary<string, NodePort>();
|
||||
Dictionary<string, List<NodePort>> removedPorts = new Dictionary<string, List<NodePort>>();
|
||||
System.Type nodeType = node.GetType();
|
||||
|
||||
List<NodePort> typePortCache;
|
||||
@ -30,16 +31,33 @@ namespace XNode {
|
||||
NodePort staticPort;
|
||||
if (staticPorts.TryGetValue(port.fieldName, out staticPort)) {
|
||||
// If port exists but with wrong settings, remove it. Re-add it later.
|
||||
if (port.connectionType != staticPort.connectionType || port.IsDynamic || port.direction != staticPort.direction || port.typeConstraint != staticPort.typeConstraint) ports.Remove(port.fieldName);
|
||||
else port.ValueType = staticPort.ValueType;
|
||||
if (port.IsDynamic || port.direction != staticPort.direction || port.connectionType != staticPort.connectionType || port.typeConstraint != staticPort.typeConstraint) {
|
||||
// If port is not dynamic and direction hasn't changed, add it to the list so we can try reconnecting the ports connections.
|
||||
if (!port.IsDynamic && port.direction == staticPort.direction) removedPorts.Add(port.fieldName, port.GetConnections());
|
||||
port.ClearConnections();
|
||||
ports.Remove(port.fieldName);
|
||||
} else port.ValueType = staticPort.ValueType;
|
||||
}
|
||||
// If port doesn't exist anymore, remove it
|
||||
else if (port.IsStatic) ports.Remove(port.fieldName);
|
||||
else if (port.IsStatic) {
|
||||
port.ClearConnections();
|
||||
ports.Remove(port.fieldName);
|
||||
}
|
||||
}
|
||||
// Add missing ports
|
||||
foreach (NodePort staticPort in staticPorts.Values) {
|
||||
if (!ports.ContainsKey(staticPort.fieldName)) {
|
||||
ports.Add(staticPort.fieldName, new NodePort(staticPort, node));
|
||||
NodePort port = new NodePort(staticPort, node);
|
||||
//If we just removed the port, try re-adding the connections
|
||||
List<NodePort> reconnectConnections;
|
||||
if (removedPorts.TryGetValue(staticPort.fieldName, out reconnectConnections)) {
|
||||
for (int i = 0; i < reconnectConnections.Count; i++) {
|
||||
NodePort connection = reconnectConnections[i];
|
||||
if (connection == null) continue;
|
||||
if (port.CanConnectTo(connection)) port.Connect(connection);
|
||||
}
|
||||
}
|
||||
ports.Add(staticPort.fieldName, port);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user