/// <summary> Draw an editable list of dynamic ports. Port names are named as "[fieldName] [index]" </summary> /// <param name="fieldName">Supply a list for editable values</param> /// <param name="type">Value type of added dynamic ports</param> /// <param name="serializedObject">The serializedObject of the node</param> /// <param name="connectionType">Connection type of added dynamic ports</param> /// <param name="onCreation">Called on the list on creation. Use this if you want to customize the created ReorderableList</param> public static void DynamicPortList(string fieldName, Type type, SerializedObject serializedObject, xNode.NodePort.IO io, xNode.Node.ConnectionType connectionType = xNode.Node.ConnectionType.Multiple, xNode.Node.TypeConstraint typeConstraint = xNode.Node.TypeConstraint.None, Action <ReorderableList> onCreation = null) { xNode.Node node = serializedObject.targetObject as xNode.Node; var indexedPorts = node.DynamicPorts.Select(x => { string[] split = x.fieldName.Split(' '); if (split != null && split.Length == 2 && split[0] == fieldName) { int i = -1; if (int.TryParse(split[1], out i)) { return(new { index = i, port = x }); } } return(new { index = -1, port = (xNode.NodePort)null }); }).Where(x => x.port != null); List <xNode.NodePort> dynamicPorts = indexedPorts.OrderBy(x => x.index).Select(x => x.port).ToList(); node.UpdatePorts(); ReorderableList list = null; Dictionary <string, ReorderableList> rlc; if (reorderableListCache.TryGetValue(serializedObject.targetObject, out rlc)) { if (!rlc.TryGetValue(fieldName, out list)) { list = null; } } // If a ReorderableList isn't cached for this array, do so. if (list == null) { SerializedProperty arrayData = serializedObject.FindProperty(fieldName); list = CreateReorderableList(fieldName, dynamicPorts, arrayData, type, serializedObject, io, connectionType, typeConstraint, onCreation); if (reorderableListCache.TryGetValue(serializedObject.targetObject, out rlc)) { rlc.Add(fieldName, list); } else { reorderableListCache.Add(serializedObject.targetObject, new Dictionary <string, ReorderableList>() { { fieldName, list } }); } } list.list = dynamicPorts; list.DoLayoutList(); }
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) { bool hasArrayData = arrayData != null && arrayData.isArray; xNode.Node node = serializedObject.targetObject as xNode.Node; ReorderableList list = new ReorderableList(dynamicPorts, null, true, true, true, true); string label = arrayData != null ? arrayData.displayName : ObjectNames.NicifyVariableName(fieldName); list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => { xNode.NodePort port = node.GetPort(fieldName + " " + index); if (hasArrayData && arrayData.propertyType != SerializedPropertyType.String) { if (arrayData.arraySize <= index) { EditorGUI.LabelField(rect, "Array[" + index + "] data out of range"); return; } SerializedProperty itemData = arrayData.GetArrayElementAtIndex(index); EditorGUI.PropertyField(rect, itemData, true); } else { EditorGUI.LabelField(rect, port != null ? port.fieldName : ""); } 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) => { if (hasArrayData) { if (arrayData.arraySize <= index) { return(EditorGUIUtility.singleLineHeight); } SerializedProperty itemData = arrayData.GetArrayElementAtIndex(index); return(EditorGUI.GetPropertyHeight(itemData)); } else { return(EditorGUIUtility.singleLineHeight); } }; list.drawHeaderCallback = (Rect rect) => { EditorGUI.LabelField(rect, label); }; list.onSelectCallback = (ReorderableList rl) => { reorderableListIndex = rl.index; }; list.onReorderCallback = (ReorderableList rl) => { bool hasRect = false; bool hasNewRect = false; Rect rect = Rect.zero; Rect newRect = Rect.zero; // Move up if (rl.index > reorderableListIndex) { for (int i = reorderableListIndex; i < rl.index; ++i) { xNode.NodePort port = node.GetPort(fieldName + " " + i); xNode.NodePort nextPort = node.GetPort(fieldName + " " + (i + 1)); port.SwapConnections(nextPort); // Swap cached positions to mitigate twitching hasRect = NodeEditorWindow.current.portConnectionPoints.TryGetValue(port, out rect); hasNewRect = NodeEditorWindow.current.portConnectionPoints.TryGetValue(nextPort, out newRect); NodeEditorWindow.current.portConnectionPoints[port] = hasNewRect ? newRect : rect; NodeEditorWindow.current.portConnectionPoints[nextPort] = hasRect ? rect : newRect; } } // Move down else { for (int i = reorderableListIndex; i > rl.index; --i) { xNode.NodePort port = node.GetPort(fieldName + " " + i); xNode.NodePort nextPort = node.GetPort(fieldName + " " + (i - 1)); port.SwapConnections(nextPort); // Swap cached positions to mitigate twitching hasRect = NodeEditorWindow.current.portConnectionPoints.TryGetValue(port, out rect); hasNewRect = NodeEditorWindow.current.portConnectionPoints.TryGetValue(nextPort, out newRect); NodeEditorWindow.current.portConnectionPoints[port] = hasNewRect ? newRect : rect; NodeEditorWindow.current.portConnectionPoints[nextPort] = hasRect ? rect : newRect; } } // Apply changes serializedObject.ApplyModifiedProperties(); serializedObject.Update(); // Move array data if there is any if (hasArrayData) { arrayData.MoveArrayElement(reorderableListIndex, rl.index); } // Apply changes serializedObject.ApplyModifiedProperties(); serializedObject.Update(); NodeEditorWindow.current.Repaint(); EditorApplication.delayCall += NodeEditorWindow.current.Repaint; }; list.onAddCallback = (ReorderableList rl) => { // Add dynamic port postfixed with an index number string newName = fieldName + " 0"; int i = 0; while (node.HasPort(newName)) { newName = fieldName + " " + (++i); } if (io == xNode.NodePort.IO.Output) { node.AddDynamicOutput(type, connectionType, xNode.Node.TypeConstraint.None, newName); } else { node.AddDynamicInput(type, connectionType, typeConstraint, newName); } serializedObject.Update(); EditorUtility.SetDirty(node); if (hasArrayData) { arrayData.InsertArrayElementAtIndex(arrayData.arraySize); } serializedObject.ApplyModifiedProperties(); }; list.onRemoveCallback = (ReorderableList rl) => { var indexedPorts = node.DynamicPorts.Select(x => { string[] split = x.fieldName.Split(' '); if (split != null && split.Length == 2 && split[0] == fieldName) { int i = -1; if (int.TryParse(split[1], out i)) { return(new { index = i, port = x }); } } return(new { index = -1, port = (xNode.NodePort)null }); }).Where(x => x.port != null); dynamicPorts = indexedPorts.OrderBy(x => x.index).Select(x => x.port).ToList(); int index = rl.index; if (dynamicPorts[index] == null) { Debug.LogWarning("No port found at index " + index + " - Skipped"); } else if (dynamicPorts.Count <= index) { Debug.LogWarning("DynamicPorts[" + index + "] out of range. Length was " + dynamicPorts.Count + " - Skipped"); } else { // Clear the removed ports connections dynamicPorts[index].ClearConnections(); // Move following connections one step up to replace the missing connection for (int k = index + 1; k < dynamicPorts.Count(); k++) { for (int j = 0; j < dynamicPorts[k].ConnectionCount; j++) { xNode.NodePort other = dynamicPorts[k].GetConnection(j); dynamicPorts[k].Disconnect(other); dynamicPorts[k - 1].Connect(other); } } // Remove the last dynamic port, to avoid messing up the indexing node.RemoveDynamicPort(dynamicPorts[dynamicPorts.Count() - 1].fieldName); serializedObject.Update(); EditorUtility.SetDirty(node); } if (hasArrayData && arrayData.propertyType != SerializedPropertyType.String) { if (arrayData.arraySize <= index) { Debug.LogWarning("Attempted to remove array index " + index + " where only " + arrayData.arraySize + " exist - Skipped"); Debug.Log(rl.list[0]); return; } arrayData.DeleteArrayElementAtIndex(index); // Error handling. If the following happens too often, file a bug report at https://github.com/Siccity/xNode/issues if (dynamicPorts.Count <= arrayData.arraySize) { while (dynamicPorts.Count <= arrayData.arraySize) { arrayData.DeleteArrayElementAtIndex(arrayData.arraySize - 1); } UnityEngine.Debug.LogWarning("Array size exceeded dynamic ports size. Excess items removed."); } serializedObject.ApplyModifiedProperties(); serializedObject.Update(); } }; if (hasArrayData) { int dynamicPortCount = dynamicPorts.Count; while (dynamicPortCount < arrayData.arraySize) { // Add dynamic port postfixed with an index number string newName = arrayData.name + " 0"; int i = 0; while (node.HasPort(newName)) { newName = arrayData.name + " " + (++i); } if (io == xNode.NodePort.IO.Output) { node.AddDynamicOutput(type, connectionType, typeConstraint, newName); } else { node.AddDynamicInput(type, connectionType, typeConstraint, newName); } EditorUtility.SetDirty(node); dynamicPortCount++; } while (arrayData.arraySize < dynamicPortCount) { arrayData.InsertArrayElementAtIndex(arrayData.arraySize); } serializedObject.ApplyModifiedProperties(); serializedObject.Update(); } if (onCreation != null) { onCreation(list); } return(list); }
public static void InstancePortList(string fieldName, Type type, SerializedObject serializedObject, xNode.NodePort.IO io, xNode.Node.ConnectionType connectionType = xNode.Node.ConnectionType.Multiple, xNode.Node.TypeConstraint typeConstraint = xNode.Node.TypeConstraint.None, Action <ReorderableList> onCreation = null) { DynamicPortList(fieldName, type, serializedObject, io, connectionType, typeConstraint, onCreation); }