-
Notifications
You must be signed in to change notification settings - Fork 3
/
ActionsEditor.cs
96 lines (85 loc) · 4 KB
/
ActionsEditor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using XNode;
using XNodeEditor;
namespace DelftToolkit {
[CustomNodeEditor(typeof(Actions))]
public class ActionsEditor : StateNodeBaseEditor {
protected override string description { get { return "Plays a list of configurable actions"; } }
private Actions node { get { return _node != null ? _node : _node = target as Actions; } }
private Actions _node;
public override void OnBodyGUI() {
GUI.color = Color.white;
NodeEditorGUILayout.PortPair(target.GetInputPort("enter"), target.GetOutputPort("exit"));
Rect rect = GUILayoutUtility.GetLastRect();
rect.x += (rect.width * 0.5f) - 50;
rect.width = 100;
EditorGUI.PropertyField(rect, serializedObject.FindProperty("device"), GUIContent.none);
GUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Repeats:", GUILayout.Width(60));
node.repeats = EditorGUILayout.IntField(node.repeats, GUILayout.Width(33));
EditorGUILayout.Space();
EditorGUILayout.LabelField("Random:", GUILayout.Width(60));
node.random = EditorGUILayout.Toggle(node.random);
GUILayout.EndHorizontal();
// Display the valueIn port.
NodePort valueInPort = node.GetInputPort("valueIn");
if (valueInPort.IsConnected) {
// Display an uneditable input value if connected
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.TextField(serializedObject.FindProperty("valueIn").displayName, valueInPort.GetInputValue<string>());
EditorGUI.EndDisabledGroup();
NodeEditorGUILayout.AddPortField(valueInPort);
} else NodeEditorGUILayout.PropertyField(serializedObject.FindProperty("valueIn"), valueInPort, true);
string title = "Actions";
if (Application.isPlaying) title = "Actions (" + node.repeatCount + "/" + node.repeats + " repeats)";
// Actions list
NodeEditorGUILayout.onCreateReorderableList += OnCreateReorderableList;
SerializedProperty actionsProperty = serializedObject.FindProperty("actions");
NodeEditorGUILayout.PropertyField(actionsProperty);
NodeEditorGUILayout.onCreateReorderableList -= OnCreateReorderableList;
serializedObject.ApplyModifiedProperties();
serializedObject.Update();
// Footer
DrawFooterGUI();
}
private void OnCreateReorderableList(ReorderableList list) {
list.drawHeaderCallback =
(Rect rect) => {
SerializedProperty expandedProperty = serializedObject.FindProperty("expanded");
string title = "Actions";
if (Application.isPlaying) title = "Actions (" + node.repeatCount + "/" + node.repeats + " repeats)";
EditorGUI.BeginChangeCheck();
expandedProperty.boolValue = EditorGUI.Foldout(rect, expandedProperty.boolValue, title, DelftStyles.foldoutNoHighlight);
if (EditorGUI.EndChangeCheck()) {
serializedObject.ApplyModifiedProperties();
serializedObject.SetIsDifferentCacheDirty();
serializedObject.Update();
}
};
list.drawElementCallback =
(Rect rect, int index, bool isActive, bool isFocused) => {
SerializedProperty expandedProperty = serializedObject.FindProperty("expanded");
if (!expandedProperty.boolValue) return;
XNode.NodePort port = node.GetPort("actions " + index);
SerializedProperty itemData = serializedObject.FindProperty("actions").GetArrayElementAtIndex(index);
if (node.currentAction == index) EditorGUI.DrawRect(rect, Color.gray);
EditorGUI.PropertyField(rect, itemData);
Vector2 pos = rect.position + (port.IsOutput ? new Vector2(rect.width + 6, 0) : new Vector2(-36, 0));
serializedObject.ApplyModifiedProperties();
serializedObject.Update();
NodeEditorGUILayout.PortField(pos, port);
};
list.elementHeightCallback =
(int index) => {
SerializedProperty expandedProperty = serializedObject.FindProperty("expanded");
if (!expandedProperty.boolValue) return 0f;
SerializedProperty itemData = serializedObject.FindProperty("actions").GetArrayElementAtIndex(index);
return EditorGUI.GetPropertyHeight(itemData);
};
}
}
}