mirror of
https://github.com/Siccity/xNode.git
synced 2026-02-23 18:21:24 +08:00
Added editor callback onUpdateNode
This commit is contained in:
parent
9f518572f8
commit
81f01cefc9
@ -7,7 +7,8 @@ using UnityEngine;
|
|||||||
|
|
||||||
/// <summary> Base class to derive custom Node editors from. Use this to create your own custom inspectors and editors for your nodes. </summary>
|
/// <summary> Base class to derive custom Node editors from. Use this to create your own custom inspectors and editors for your nodes. </summary>
|
||||||
public class NodeEditor {
|
public class NodeEditor {
|
||||||
|
/// <summary> Fires every whenever a node was modified through the editor </summary>
|
||||||
|
public static Action<Node> onUpdateNode;
|
||||||
public Node target;
|
public Node target;
|
||||||
|
|
||||||
/// <summary> Draws the node GUI.</summary>
|
/// <summary> Draws the node GUI.</summary>
|
||||||
@ -26,45 +27,17 @@ public class NodeEditor {
|
|||||||
protected void DrawDefaultNodeBodyGUI(out Dictionary<NodePort, Vector2> portPositions) {
|
protected void DrawDefaultNodeBodyGUI(out Dictionary<NodePort, Vector2> portPositions) {
|
||||||
portPositions = new Dictionary<NodePort, Vector2>();
|
portPositions = new Dictionary<NodePort, Vector2>();
|
||||||
|
|
||||||
|
EditorGUI.BeginChangeCheck();
|
||||||
FieldInfo[] fields = GetInspectorFields(target);
|
FieldInfo[] fields = GetInspectorFields(target);
|
||||||
for (int i = 0; i < fields.Length; i++) {
|
for (int i = 0; i < fields.Length; i++) {
|
||||||
object[] fieldAttribs = fields[i].GetCustomAttributes(false);
|
object[] fieldAttribs = fields[i].GetCustomAttributes(false);
|
||||||
if (fields[i].Name == "graph" || fields[i].Name == "rect") continue;
|
if (fields[i].Name == "graph" || fields[i].Name == "rect") continue;
|
||||||
NodeEditorGUILayout.PropertyField(target, fields[i], portPositions);
|
NodeEditorGUILayout.PropertyField(target, fields[i], portPositions);
|
||||||
}
|
}
|
||||||
|
//If user changed a value, notify other scripts through onUpdateNode
|
||||||
|
if (EditorGUI.EndChangeCheck()) {
|
||||||
|
if (onUpdateNode != null) onUpdateNode(target);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Draw node port GUI using automatic layouting. Returns port handle position. </summary>
|
|
||||||
protected Vector2 DrawNodePortGUI(NodePort port) {
|
|
||||||
GUIStyle style = port.direction == NodePort.IO.Input ? NodeEditorResources.styles.inputPort : NodeEditorResources.styles.outputPort;
|
|
||||||
Rect rect = GUILayoutUtility.GetRect(new GUIContent(port.fieldName.PrettifyCamelCase()), style);
|
|
||||||
return DrawNodePortGUI(rect, port);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary> Draw node port GUI in rect. Returns port handle position. </summary>
|
|
||||||
protected Vector2 DrawNodePortGUI(Rect rect, NodePort port) {
|
|
||||||
GUIStyle style = port.direction == NodePort.IO.Input ? NodeEditorResources.styles.inputPort : NodeEditorResources.styles.outputPort;
|
|
||||||
GUI.Label(rect, new GUIContent(port.fieldName.PrettifyCamelCase()), style);
|
|
||||||
|
|
||||||
Vector2 handlePoint = rect.center;
|
|
||||||
|
|
||||||
switch (port.direction) {
|
|
||||||
case NodePort.IO.Input:
|
|
||||||
handlePoint.x = rect.xMin;
|
|
||||||
break;
|
|
||||||
case NodePort.IO.Output:
|
|
||||||
handlePoint.x = rect.xMax;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
Color col = GUI.color;
|
|
||||||
Rect handleRect = new Rect(handlePoint.x - 8, handlePoint.y - 8, 16, 16);
|
|
||||||
GUI.color = new Color32(90, 97, 105, 255);
|
|
||||||
GUI.DrawTexture(handleRect, NodeEditorResources.dotOuter);
|
|
||||||
GUI.color = NodeEditorPreferences.GetTypeColor(port.type);
|
|
||||||
GUI.DrawTexture(handleRect, NodeEditorResources.dot);
|
|
||||||
GUI.color = col;
|
|
||||||
return handlePoint;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static FieldInfo[] GetInspectorFields(Node node) {
|
private static FieldInfo[] GetInspectorFields(Node node) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user