From 01d7f782e41e145cb52bf9bd7bea8f4a7cf2e927 Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Mon, 4 Mar 2019 19:04:00 +0100 Subject: [PATCH] Fixed issues relating to InstancePortList --- Scripts/Editor/NodeEditorGUILayout.cs | 35 +++++++++++++++++---------- Scripts/Node.cs | 2 ++ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/Scripts/Editor/NodeEditorGUILayout.cs b/Scripts/Editor/NodeEditorGUILayout.cs index b2ac5e8..4eca18c 100644 --- a/Scripts/Editor/NodeEditorGUILayout.cs +++ b/Scripts/Editor/NodeEditorGUILayout.cs @@ -301,7 +301,6 @@ namespace XNodeEditor { private static ReorderableList CreateReorderableList(string fieldName, List instancePorts, SerializedProperty arrayData, Type type, SerializedObject serializedObject, XNode.NodePort.IO io, XNode.Node.ConnectionType connectionType, XNode.Node.TypeConstraint typeConstraint, Action onCreation) { bool hasArrayData = arrayData != null && arrayData.isArray; - int arraySize = hasArrayData ? arrayData.arraySize : 0; XNode.Node node = serializedObject.targetObject as XNode.Node; ReorderableList list = new ReorderableList(instancePorts, null, true, true, true, true); string label = arrayData != null ? arrayData.displayName : ObjectNames.NicifyVariableName(fieldName); @@ -317,8 +316,10 @@ namespace XNodeEditor { SerializedProperty itemData = arrayData.GetArrayElementAtIndex(index); EditorGUI.PropertyField(rect, itemData, true); } else EditorGUI.LabelField(rect, port.fieldName); - Vector2 pos = rect.position + (port.IsOutput?new Vector2(rect.width + 6, 0) : new Vector2(-36, 0)); - NodeEditorGUILayout.PortField(pos, port); + if (port != null) { + Vector2 pos = rect.position + (port.IsOutput?new Vector2(rect.width + 6, 0) : new Vector2(-36, 0)); + NodeEditorGUILayout.PortField(pos, port); + } }; list.elementHeightCallback = (int index) => { @@ -391,11 +392,22 @@ namespace XNodeEditor { else node.AddInstanceInput(type, connectionType, typeConstraint, newName); serializedObject.Update(); EditorUtility.SetDirty(node); - if (hasArrayData) arrayData.InsertArrayElementAtIndex(arraySize); + if (hasArrayData) { + arrayData.InsertArrayElementAtIndex(arrayData.arraySize); + } serializedObject.ApplyModifiedProperties(); }; list.onRemoveCallback = (ReorderableList rl) => { + + Predicate isMatchingInstancePort = + x => { + string[] split = x.Split(' '); + if (split != null && split.Length == 2) return split[0] == fieldName; + else return false; + }; + instancePorts = node.InstancePorts.Where(x => isMatchingInstancePort(x.fieldName)).OrderBy(x => x.fieldName).ToList(); + int index = rl.index; // Clear the removed ports connections instancePorts[index].ClearConnections(); @@ -413,23 +425,21 @@ namespace XNodeEditor { EditorUtility.SetDirty(node); if (hasArrayData) { arrayData.DeleteArrayElementAtIndex(index); - arraySize--; // Error handling. If the following happens too often, file a bug report at https://github.com/Siccity/xNode/issues - if (instancePorts.Count <= arraySize) { - while (instancePorts.Count <= arraySize) { - arrayData.DeleteArrayElementAtIndex(--arraySize); + if (instancePorts.Count <= arrayData.arraySize) { + while (instancePorts.Count <= arrayData.arraySize) { + arrayData.DeleteArrayElementAtIndex(arrayData.arraySize - 1); } UnityEngine.Debug.LogWarning("Array size exceeded instance ports size. Excess items removed."); } serializedObject.ApplyModifiedProperties(); serializedObject.Update(); } - }; if (hasArrayData) { int instancePortCount = instancePorts.Count; - while (instancePortCount < arraySize) { + while (instancePortCount < arrayData.arraySize) { // Add instance port postfixed with an index number string newName = arrayData.name + " 0"; int i = 0; @@ -439,9 +449,8 @@ namespace XNodeEditor { EditorUtility.SetDirty(node); instancePortCount++; } - while (arraySize < instancePortCount) { - arrayData.InsertArrayElementAtIndex(arraySize); - arraySize++; + while (arrayData.arraySize < instancePortCount) { + arrayData.InsertArrayElementAtIndex(arrayData.arraySize); } serializedObject.ApplyModifiedProperties(); serializedObject.Update(); diff --git a/Scripts/Node.cs b/Scripts/Node.cs index 8785280..74eb309 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -127,6 +127,8 @@ namespace XNode { /// Remove an instance port from the node public void RemoveInstancePort(string fieldName) { + NodePort instancePort = GetPort(fieldName); + if (instancePort == null) throw new ArgumentException("port " + fieldName + " doesn't exist"); RemoveInstancePort(GetPort(fieldName)); }