1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-03-26 22:49:02 +08:00

Removed unneeded code, clarified that this applies to ports in dynamic lists, and proper verification for dynamic list ports is now in place.

This commit is contained in:
Lumos 2019-12-25 13:35:25 +01:00
parent ecc5b3e0e1
commit 423d86293d
4 changed files with 39 additions and 39 deletions

View File

@ -312,6 +312,8 @@ namespace XNodeEditor {
}).Where(x => x.port != null); }).Where(x => x.port != null);
List<XNode.NodePort> dynamicPorts = indexedPorts.OrderBy(x => x.index).Select(x => x.port).ToList(); List<XNode.NodePort> dynamicPorts = indexedPorts.OrderBy(x => x.index).Select(x => x.port).ToList();
node.UpdatePorts();
ReorderableList list = null; ReorderableList list = null;
Dictionary<string, ReorderableList> rlc; Dictionary<string, ReorderableList> rlc;
if (reorderableListCache.TryGetValue(serializedObject.targetObject, out rlc)) { if (reorderableListCache.TryGetValue(serializedObject.targetObject, out rlc)) {
@ -327,7 +329,6 @@ namespace XNodeEditor {
list.list = dynamicPorts; list.list = dynamicPorts;
list.DoLayoutList(); list.DoLayoutList();
node.UpdatePorts();
} }
private static ReorderableList CreateReorderableList(string fieldName, List<XNode.NodePort> dynamicPorts, SerializedProperty arrayData, Type type, SerializedObject serializedObject, XNode.NodePort.IO io, XNode.Node.ConnectionType connectionType, XNode.Node.TypeConstraint typeConstraint, Action<ReorderableList> onCreation) { private static ReorderableList CreateReorderableList(string fieldName, List<XNode.NodePort> dynamicPorts, SerializedProperty arrayData, Type type, SerializedObject serializedObject, XNode.NodePort.IO io, XNode.Node.ConnectionType connectionType, XNode.Node.TypeConstraint typeConstraint, Action<ReorderableList> onCreation) {

View File

@ -123,7 +123,7 @@ namespace XNode {
Init(); Init();
} }
/// <summary> Update all ports to reflect class fields. This happens automatically on enable or on redrawing the list of ports. </summary> /// <summary> Update static ports and dynamic ports managed by DynamicPortLists to reflect class fields. This happens automatically on enable or on redrawing a dynamic port list. </summary>
public void UpdatePorts() { public void UpdatePorts() {
NodeDataCache.UpdatePorts(this, ports); NodeDataCache.UpdatePorts(this, ports);
} }

View File

@ -1,7 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using UnityEditor;
using UnityEngine; using UnityEngine;
namespace XNode { namespace XNode {
@ -10,7 +9,7 @@ namespace XNode {
private static PortDataCache portDataCache; private static PortDataCache portDataCache;
private static bool Initialized { get { return portDataCache != null; } } private static bool Initialized { get { return portDataCache != null; } }
/// <summary> Update static and dynamic ports to reflect class fields. </summary> /// <summary> Update static and dynamic ports managed by DynamicPortLists to reflect class fields. </summary>
public static void UpdatePorts(Node node, Dictionary<string, NodePort> ports) { public static void UpdatePorts(Node node, Dictionary<string, NodePort> ports) {
if (!Initialized) BuildCache(); if (!Initialized) BuildCache();
@ -18,7 +17,7 @@ namespace XNode {
Dictionary<string, List<NodePort>> removedPorts = new Dictionary<string, List<NodePort>>(); Dictionary<string, List<NodePort>> removedPorts = new Dictionary<string, List<NodePort>>();
System.Type nodeType = node.GetType(); System.Type nodeType = node.GetType();
List<NodePort> dynamicPorts = new List<NodePort>(); List<NodePort> dynamicListPorts = new List<NodePort>();
List<NodePort> typePortCache; List<NodePort> typePortCache;
if (portDataCache.TryGetValue(nodeType, out typePortCache)) { if (portDataCache.TryGetValue(nodeType, out typePortCache)) {
@ -28,7 +27,7 @@ namespace XNode {
} }
// Cleanup port dict - Remove nonexisting static ports - update static port types // Cleanup port dict - Remove nonexisting static ports - update static port types
// AND update dynamic ports too! // AND update dynamic ports (albeit only those in lists) too, in order to enforce proper serialisation.
// Loop through current node ports // Loop through current node ports
foreach (NodePort port in ports.Values.ToList()) { foreach (NodePort port in ports.Values.ToList()) {
// If port still exists, check it it has been changed // If port still exists, check it it has been changed
@ -47,9 +46,9 @@ namespace XNode {
port.ClearConnections(); port.ClearConnections();
ports.Remove(port.fieldName); ports.Remove(port.fieldName);
} }
// If the port is dynamic, flag it for reference updates // If the port is dynamic and is managed by a dynamic port list, flag it for reference updates
else { else if (IsDynamicListPort(port)) {
dynamicPorts.Add(port); dynamicListPorts.Add(port);
} }
} }
// Add missing ports // Add missing ports
@ -69,32 +68,41 @@ namespace XNode {
} }
} }
// Finally, make sure dynamic port settings correspond to the settings of their backing field // Finally, make sure dynamic list port settings correspond to the settings of their "backing port"
foreach (NodePort dynamicPort in dynamicPorts) { foreach (NodePort listPort in dynamicListPorts) {
string[] parts = dynamicPort.fieldName.Split(' '); // At this point we know that ports here are dynamic list ports
if (parts.Length != 2) { // which have passed name/"backing port" checks, ergo we can proceed more safely.
Debug.LogError("Dynamic port name " + dynamicPort.fieldName + " is invalid."); string backingPortName = listPort.fieldName.Split(' ')[0];
continue; NodePort backingPort = staticPorts[backingPortName];
}
string backingPortName = parts[0];
NodePort backingPort;
if (!staticPorts.TryGetValue(backingPortName, out backingPort)) {
Debug.LogError($"Could not find backing port \"{backingPortName}\" for port {dynamicPort.fieldName}");
continue;
}
// Update port constraints. Creating a new port instead will break the editor, mandating the need for setters. // Update port constraints. Creating a new port instead will break the editor, mandating the need for setters.
dynamicPort.ValueType = backingPort.ValueType; listPort.ValueType = backingPort.ValueType;
dynamicPort.direction = backingPort.direction; listPort.direction = backingPort.direction;
dynamicPort.connectionType = backingPort.connectionType; listPort.connectionType = backingPort.connectionType;
dynamicPort.typeConstraint = backingPort.typeConstraint; listPort.typeConstraint = backingPort.typeConstraint;
} }
} }
/// <summary>Returns true if the given port is in a dynamic port list.</summary>
private static bool IsDynamicListPort(NodePort port) {
// Ports flagged as "dynamicPortList = true" end up having a "backing port" and a name with an index, but we have
// no guarantee that a dynamic port called "output 0" is an element in a list backed by a static "output" port.
// Thus, we need to check for attributes... (but at least we don't need to look at all fields this time)
string[] fieldNameParts = port.fieldName.Split(' ');
if (fieldNameParts.Length != 2) return false;
FieldInfo backingPortInfo = port.node.GetType().GetField(fieldNameParts[0]);
if (backingPortInfo == null) return false;
object[] attribs = backingPortInfo.GetCustomAttributes(true);
return attribs.Any(x => {
Node.InputAttribute inputAttribute = x as Node.InputAttribute;
Node.OutputAttribute outputAttribute = x as Node.OutputAttribute;
return inputAttribute != null && inputAttribute.dynamicPortList ||
outputAttribute != null && outputAttribute.dynamicPortList;
});
}
/// <summary> Cache node types </summary> /// <summary> Cache node types </summary>
private static void BuildCache() { private static void BuildCache() {
portDataCache = new PortDataCache(); portDataCache = new PortDataCache();
@ -183,10 +191,5 @@ namespace XNode {
this.Add(keys[i], values[i]); this.Add(keys[i], values[i]);
} }
} }
[MenuItem("TOOLS/LOL")]
public static void LOL() {
portDataCache = null;
}
} }
} }

View File

@ -19,10 +19,6 @@ namespace XNode {
} }
} }
// public IO direction { get { return _direction; } }
// public Node.ConnectionType connectionType { get { return _connectionType; } }
// public Node.TypeConstraint typeConstraint { get { return _typeConstraint; } }
public IO direction { public IO direction {
get { return _direction; } get { return _direction; }
internal set { _direction = value; } internal set { _direction = value; }