mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 09:16:01 +08:00
315 lines
18 KiB
C#
315 lines
18 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace XNodeEditor {
|
|
/// <summary> xNode-specific version of <see cref="EditorGUILayout"/> </summary>
|
|
public static class NodeEditorGUILayout {
|
|
|
|
/// <summary> Make a field for a serialized property. Automatically displays relevant node port. </summary>
|
|
public static void PropertyField(SerializedProperty property, bool includeChildren = true, params GUILayoutOption[] options) {
|
|
PropertyField(property, (GUIContent) null, includeChildren, options);
|
|
}
|
|
|
|
/// <summary> Make a field for a serialized property. Automatically displays relevant node port. </summary>
|
|
public static void PropertyField(SerializedProperty property, GUIContent label, bool includeChildren = true, params GUILayoutOption[] options) {
|
|
if (property == null) throw new NullReferenceException();
|
|
XNode.Node node = property.serializedObject.targetObject as XNode.Node;
|
|
XNode.NodePort port = node.GetPort(property.name);
|
|
PropertyField(property, label, port, includeChildren);
|
|
}
|
|
|
|
/// <summary> Make a field for a serialized property. Manual node port override. </summary>
|
|
public static void PropertyField(SerializedProperty property, XNode.NodePort port, bool includeChildren = true, params GUILayoutOption[] options) {
|
|
PropertyField(property, null, port, includeChildren, options);
|
|
}
|
|
|
|
/// <summary> Make a field for a serialized property. Manual node port override. </summary>
|
|
public static void PropertyField(SerializedProperty property, GUIContent label, XNode.NodePort port, bool includeChildren = true, params GUILayoutOption[] options) {
|
|
if (property == null) throw new NullReferenceException();
|
|
|
|
// If property is not a port, display a regular property field
|
|
if (port == null) EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
|
|
else {
|
|
Rect rect = new Rect();
|
|
|
|
// If property is an input, display a regular property field and put a port handle on the left side
|
|
if (port.direction == XNode.NodePort.IO.Input) {
|
|
// Get data from [Input] attribute
|
|
XNode.Node.ShowBackingValue showBacking = XNode.Node.ShowBackingValue.Unconnected;
|
|
XNode.Node.InputAttribute inputAttribute;
|
|
bool instancePortList = false;
|
|
if (NodeEditorUtilities.GetAttrib(port.node.GetType(), property.name, out inputAttribute)) {
|
|
instancePortList = inputAttribute.instancePortList;
|
|
showBacking = inputAttribute.backingValue;
|
|
}
|
|
|
|
if (instancePortList) {
|
|
Type type = GetType(property);
|
|
XNode.Node.ConnectionType connectionType = inputAttribute != null ? inputAttribute.connectionType : XNode.Node.ConnectionType.Multiple;
|
|
InstancePortList(property.name, type, property.serializedObject, port.direction, connectionType);
|
|
return;
|
|
}
|
|
switch (showBacking) {
|
|
case XNode.Node.ShowBackingValue.Unconnected:
|
|
// Display a label if port is connected
|
|
if (port.IsConnected) EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName));
|
|
// Display an editable property field if port is not connected
|
|
else EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
|
|
break;
|
|
case XNode.Node.ShowBackingValue.Never:
|
|
// Display a label
|
|
EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName));
|
|
break;
|
|
case XNode.Node.ShowBackingValue.Always:
|
|
// Display an editable property field
|
|
EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
|
|
break;
|
|
}
|
|
|
|
rect = GUILayoutUtility.GetLastRect();
|
|
rect.position = rect.position - new Vector2(16, 0);
|
|
// If property is an output, display a text label and put a port handle on the right side
|
|
} else if (port.direction == XNode.NodePort.IO.Output) {
|
|
// Get data from [Output] attribute
|
|
XNode.Node.ShowBackingValue showBacking = XNode.Node.ShowBackingValue.Unconnected;
|
|
XNode.Node.OutputAttribute outputAttribute;
|
|
bool instancePortList = false;
|
|
if (NodeEditorUtilities.GetAttrib(port.node.GetType(), property.name, out outputAttribute)) {
|
|
instancePortList = outputAttribute.instancePortList;
|
|
showBacking = outputAttribute.backingValue;
|
|
}
|
|
|
|
if (instancePortList) {
|
|
Type type = GetType(property);
|
|
XNode.Node.ConnectionType connectionType = outputAttribute != null ? outputAttribute.connectionType : XNode.Node.ConnectionType.Multiple;
|
|
InstancePortList(property.name, type, property.serializedObject, port.direction, connectionType);
|
|
return;
|
|
}
|
|
switch (showBacking) {
|
|
case XNode.Node.ShowBackingValue.Unconnected:
|
|
// Display a label if port is connected
|
|
if (port.IsConnected) EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName), NodeEditorResources.OutputPort, GUILayout.MinWidth(30));
|
|
// Display an editable property field if port is not connected
|
|
else EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
|
|
break;
|
|
case XNode.Node.ShowBackingValue.Never:
|
|
// Display a label
|
|
EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName), NodeEditorResources.OutputPort, GUILayout.MinWidth(30));
|
|
break;
|
|
case XNode.Node.ShowBackingValue.Always:
|
|
// Display an editable property field
|
|
EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
|
|
break;
|
|
}
|
|
|
|
rect = GUILayoutUtility.GetLastRect();
|
|
rect.position = rect.position + new Vector2(rect.width, 0);
|
|
}
|
|
|
|
rect.size = new Vector2(16, 16);
|
|
|
|
Color backgroundColor = new Color32(90, 97, 105, 255);
|
|
Color tint;
|
|
if (NodeEditorWindow.nodeTint.TryGetValue(port.node.GetType(), out tint)) backgroundColor *= tint;
|
|
Color col = NodeEditorWindow.current.graphEditor.GetTypeColor(port.ValueType);
|
|
DrawPortHandle(rect, backgroundColor, col);
|
|
|
|
// Register the handle position
|
|
Vector2 portPos = rect.center;
|
|
if (NodeEditor.portPositions.ContainsKey(port)) NodeEditor.portPositions[port] = portPos;
|
|
else NodeEditor.portPositions.Add(port, portPos);
|
|
}
|
|
}
|
|
|
|
private static System.Type GetType(SerializedProperty property) {
|
|
System.Type parentType = property.serializedObject.targetObject.GetType();
|
|
System.Reflection.FieldInfo fi = parentType.GetField(property.propertyPath);
|
|
return fi.FieldType;
|
|
}
|
|
|
|
/// <summary> Make a simple port field. </summary>
|
|
public static void PortField(XNode.NodePort port, params GUILayoutOption[] options) {
|
|
PortField(null, port, options);
|
|
}
|
|
|
|
/// <summary> Make a simple port field. </summary>
|
|
public static void PortField(GUIContent label, XNode.NodePort port, params GUILayoutOption[] options) {
|
|
if (port == null) return;
|
|
if (options == null) options = new GUILayoutOption[] { GUILayout.MinWidth(30) };
|
|
Rect rect = new Rect();
|
|
GUIContent content = label != null ? label : new GUIContent(ObjectNames.NicifyVariableName(port.fieldName));
|
|
|
|
// If property is an input, display a regular property field and put a port handle on the left side
|
|
if (port.direction == XNode.NodePort.IO.Input) {
|
|
// Display a label
|
|
EditorGUILayout.LabelField(content, options);
|
|
|
|
rect = GUILayoutUtility.GetLastRect();
|
|
rect.position = rect.position - new Vector2(16, 0);
|
|
// If property is an output, display a text label and put a port handle on the right side
|
|
} else if (port.direction == XNode.NodePort.IO.Output) {
|
|
// Display a label
|
|
EditorGUILayout.LabelField(content, NodeEditorResources.OutputPort, options);
|
|
|
|
rect = GUILayoutUtility.GetLastRect();
|
|
rect.position = rect.position + new Vector2(rect.width, 0);
|
|
}
|
|
|
|
rect.size = new Vector2(16, 16);
|
|
|
|
Color backgroundColor = new Color32(90, 97, 105, 255);
|
|
Color tint;
|
|
if (NodeEditorWindow.nodeTint.TryGetValue(port.node.GetType(), out tint)) backgroundColor *= tint;
|
|
Color col = NodeEditorWindow.current.graphEditor.GetTypeColor(port.ValueType);
|
|
DrawPortHandle(rect, backgroundColor, col);
|
|
|
|
// Register the handle position
|
|
Vector2 portPos = rect.center;
|
|
if (NodeEditor.portPositions.ContainsKey(port)) NodeEditor.portPositions[port] = portPos;
|
|
else NodeEditor.portPositions.Add(port, portPos);
|
|
}
|
|
|
|
/// <summary> Add a port field to previous layout element. </summary>
|
|
public static void AddPortField(XNode.NodePort port) {
|
|
if (port == null) return;
|
|
Rect rect = new Rect();
|
|
|
|
// If property is an input, display a regular property field and put a port handle on the left side
|
|
if (port.direction == XNode.NodePort.IO.Input) {
|
|
rect = GUILayoutUtility.GetLastRect();
|
|
rect.position = rect.position - new Vector2(16, 0);
|
|
// If property is an output, display a text label and put a port handle on the right side
|
|
} else if (port.direction == XNode.NodePort.IO.Output) {
|
|
rect = GUILayoutUtility.GetLastRect();
|
|
rect.position = rect.position + new Vector2(rect.width, 0);
|
|
}
|
|
|
|
rect.size = new Vector2(16, 16);
|
|
|
|
Color backgroundColor = new Color32(90, 97, 105, 255);
|
|
Color tint;
|
|
if (NodeEditorWindow.nodeTint.TryGetValue(port.node.GetType(), out tint)) backgroundColor *= tint;
|
|
Color col = NodeEditorWindow.current.graphEditor.GetTypeColor(port.ValueType);
|
|
DrawPortHandle(rect, backgroundColor, col);
|
|
|
|
// Register the handle position
|
|
Vector2 portPos = rect.center;
|
|
if (NodeEditor.portPositions.ContainsKey(port)) NodeEditor.portPositions[port] = portPos;
|
|
else NodeEditor.portPositions.Add(port, portPos);
|
|
}
|
|
|
|
/// <summary> Draws an input and an output port on the same line </summary>
|
|
public static void PortPair(XNode.NodePort input, XNode.NodePort output) {
|
|
GUILayout.BeginHorizontal();
|
|
NodeEditorGUILayout.PortField(input, GUILayout.MinWidth(0));
|
|
NodeEditorGUILayout.PortField(output, GUILayout.MinWidth(0));
|
|
GUILayout.EndHorizontal();
|
|
}
|
|
|
|
public static void DrawPortHandle(Rect rect, Color backgroundColor, Color typeColor) {
|
|
Color col = GUI.color;
|
|
GUI.color = backgroundColor;
|
|
GUI.DrawTexture(rect, NodeEditorResources.dotOuter);
|
|
GUI.color = typeColor;
|
|
GUI.DrawTexture(rect, NodeEditorResources.dot);
|
|
GUI.color = col;
|
|
}
|
|
|
|
[Obsolete("Use InstancePortList(string, Type, SerializedObject, NodePort.IO, Node.ConnectionType) instead")]
|
|
public static void InstancePortList(string fieldName, Type type, SerializedObject serializedObject, XNode.Node.ConnectionType connectionType = XNode.Node.ConnectionType.Multiple) {
|
|
InstancePortList(fieldName, type, serializedObject, XNode.NodePort.IO.Output, connectionType);
|
|
}
|
|
|
|
/// <summary> Draw an editable list of instance 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 instance ports</param>
|
|
/// <param name="serializedObject">The serializedObject of the node</param>
|
|
/// <param name="connectionType">Connection type of added instance ports</param>
|
|
public static void InstancePortList(string fieldName, Type type, SerializedObject serializedObject, XNode.NodePort.IO io, XNode.Node.ConnectionType connectionType = XNode.Node.ConnectionType.Multiple) {
|
|
XNode.Node node = serializedObject.targetObject as XNode.Node;
|
|
SerializedProperty arrayData = serializedObject.FindProperty(fieldName);
|
|
bool hasArrayData = arrayData != null && arrayData.isArray;
|
|
int arraySize = hasArrayData ? arrayData.arraySize : 0;
|
|
|
|
Predicate<string> isMatchingInstancePort =
|
|
x => {
|
|
string[] split = x.Split(' ');
|
|
if (split != null && split.Length == 2) return split[0] == fieldName;
|
|
else return false;
|
|
};
|
|
List<XNode.NodePort> instancePorts = node.InstancePorts.Where(x => isMatchingInstancePort(x.fieldName)).OrderBy(x => x.fieldName).ToList();
|
|
|
|
for (int i = 0; i < instancePorts.Count(); i++) {
|
|
GUILayout.BeginHorizontal();
|
|
// 'Remove' button
|
|
if (GUILayout.Button("-", EditorStyles.miniButton, GUILayout.ExpandWidth(false))) {
|
|
// Clear the removed ports connections
|
|
instancePorts[i].ClearConnections();
|
|
// Move following connections one step up to replace the missing connection
|
|
for (int k = i + 1; k < instancePorts.Count(); k++) {
|
|
for (int j = 0; j < instancePorts[k].ConnectionCount; j++) {
|
|
XNode.NodePort other = instancePorts[k].GetConnection(j);
|
|
instancePorts[k].Disconnect(other);
|
|
instancePorts[k - 1].Connect(other);
|
|
}
|
|
}
|
|
// Remove the last instance port, to avoid messing up the indexing
|
|
node.RemoveInstancePort(instancePorts[instancePorts.Count() - 1].fieldName);
|
|
serializedObject.Update();
|
|
EditorUtility.SetDirty(node);
|
|
if (hasArrayData) {
|
|
arrayData.DeleteArrayElementAtIndex(i);
|
|
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);
|
|
}
|
|
Debug.LogWarning("Array size exceeded instance ports size. Excess items removed.");
|
|
}
|
|
serializedObject.ApplyModifiedProperties();
|
|
serializedObject.Update();
|
|
}
|
|
i--;
|
|
GUILayout.EndHorizontal();
|
|
} else {
|
|
if (hasArrayData) {
|
|
if (i < arraySize) {
|
|
SerializedProperty itemData = arrayData.GetArrayElementAtIndex(i);
|
|
if (itemData != null) EditorGUILayout.PropertyField(itemData, new GUIContent(ObjectNames.NicifyVariableName(fieldName) + " " + i), true);
|
|
else EditorGUILayout.LabelField("[Missing array data]");
|
|
} else EditorGUILayout.LabelField("[Out of bounds]");
|
|
|
|
} else {
|
|
EditorGUILayout.LabelField(ObjectNames.NicifyVariableName(instancePorts[i].fieldName));
|
|
}
|
|
|
|
GUILayout.EndHorizontal();
|
|
NodeEditorGUILayout.AddPortField(node.GetPort(instancePorts[i].fieldName));
|
|
}
|
|
// GUILayout.EndHorizontal();
|
|
}
|
|
GUILayout.BeginHorizontal();
|
|
GUILayout.FlexibleSpace();
|
|
// 'Add' button
|
|
if (GUILayout.Button("+", EditorStyles.miniButton, GUILayout.ExpandWidth(false))) {
|
|
|
|
string newName = fieldName + " 0";
|
|
int i = 0;
|
|
while (node.HasPort(newName)) newName = fieldName + " " + (++i);
|
|
|
|
if (io == XNode.NodePort.IO.Output) node.AddInstanceOutput(type, connectionType, newName);
|
|
else node.AddInstanceInput(type, connectionType, newName);
|
|
serializedObject.Update();
|
|
EditorUtility.SetDirty(node);
|
|
if (hasArrayData) arrayData.InsertArrayElementAtIndex(arraySize);
|
|
serializedObject.ApplyModifiedProperties();
|
|
}
|
|
GUILayout.EndHorizontal();
|
|
}
|
|
}
|
|
} |