-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXNodeOrder.cs
More file actions
262 lines (216 loc) · 6.18 KB
/
XNodeOrder.cs
File metadata and controls
262 lines (216 loc) · 6.18 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditorInternal;
using UnityEditor;
using XNode;
using XNodeEditor;
namespace XNodeEditor
{
[CreateAssetMenu(menuName = "xNode/Order", fileName = "xNode Order")]
public class XNodeOrder : ScriptableObject
{
static XNodeOrder instance = null;
static XNodeOrder Instance
{
get
{
if (instance == null)
instance = AssetDatabase.FindAssets("t:" + typeof(XNodeOrder))
.Select(guid => AssetDatabase.LoadAssetAtPath<XNodeOrder>(AssetDatabase.GUIDToAssetPath(guid)))
.First();
return instance;
}
}
[Serializable]
struct Settings
{
public string Type;
public string Path;
}
[SerializeField] Settings[] order = null;
public static int GetNodeMenuOrder(Type type, int fallback)
{
if (Instance == null)
return fallback;
var stype = type.AssemblyQualifiedName;
for (int i = 0; i < Instance.order.Length; i++)
if (Instance.order[i].Type == stype)
return i;
return fallback;
}
public static string GetNodeMenuName(Type type, string fallback)
{
if (Instance == null)
return fallback;
var stype = type.AssemblyQualifiedName;
for (int i = 0; i < Instance.order.Length; i++)
if (Instance.order[i].Type == stype)
{
if (!String.IsNullOrEmpty(Instance.order[i].Path))
return Instance.order[i].Path;
break;
}
return fallback;
}
}
[CustomEditor(typeof(XNodeOrder))]
public class EditorXNodeOrder : Editor
{
const string PropertyOrder = "order";
const string PropertyType = "Type";
const string PropertyPath = "Path";
class Data
{
public Type type;
public string path;
public string defaultName;
public string defaultMenuName;
}
ReorderableList list = null;
Dictionary<int, Data> cache = new Dictionary<int, Data>();
void OnEnable()
{
Fill();
var prop = serializedObject.FindProperty(PropertyOrder);
list = new ReorderableList(serializedObject, prop, true, true, false, false)
{
drawHeaderCallback = (rect) =>
{
EditorGUI.LabelField(rect, prop.displayName);
},
drawElementCallback = (rect, index, isActive, isFocused) =>
{
if (prop.arraySize == 0)
return;
if (!cache.TryGetValue(index, out var data))
{
var element = prop.GetArrayElementAtIndex(index);
if (element == null)
return;
var ptype = element.FindPropertyRelative(PropertyType);
var ppath = element.FindPropertyRelative(PropertyPath);
var type = Type.GetType(ptype.stringValue);
var defaultName = NodeEditorUtilities.NodeDefaultName(type);
var defaultMenuName = GetNodeMenuName(type);
data = new Data()
{
type = type,
path = ppath.stringValue,
defaultName = defaultName,
defaultMenuName = defaultMenuName
};
cache.Add(index, data);
}
float w = rect.width / 3f;
EditorGUI.SelectableLabel(new Rect(rect.x, rect.y, w, rect.height), data.defaultName);
string setpath = null;
if (string.IsNullOrEmpty(data.path))
{
var newpath = EditorGUI.TextField(new Rect(rect.x + w, rect.y, w + w, rect.height), data.defaultMenuName);
if (newpath == data.defaultMenuName)
{
//nothing
}
else if (newpath != data.defaultMenuName)
setpath = newpath;
}
else
{
var newpath = EditorGUI.TextField(new Rect(rect.x + w, rect.y, w + w, rect.height), data.path);
if (newpath == data.path)
{
//nothing
}
else if (newpath == data.defaultMenuName)
setpath = "";
else
setpath = newpath;
}
if (setpath != null)
{
var element = prop.GetArrayElementAtIndex(index);
var ppath = element.FindPropertyRelative(PropertyPath);
ppath.stringValue = setpath;
data.path = setpath;
}
},
elementHeightCallback = (index) =>
{
return EditorGUIUtility.singleLineHeight;
}
};
list.onReorderCallback += (index) => cache.Clear();
}
public override void OnInspectorGUI()
{
EditorGUILayout.LabelField("Order is Top-Down");
EditorGUILayout.LabelField("Clear Path to restore Node default path.");
EditorGUILayout.Space();
serializedObject.Update();
list.DoLayoutList();
serializedObject.ApplyModifiedProperties();
}
//Copy of NodeGraphEditor.cs
/// <summary> Returns context node menu path. Null or empty strings for hidden nodes. </summary>
public virtual string GetNodeMenuName(Type type)
{
//Check if type has the CreateNodeMenuAttribute
XNode.Node.CreateNodeMenuAttribute attrib;
if (NodeEditorUtilities.GetAttrib(type, out attrib)) // Return custom path
return attrib.menuName;
else // Return generated path
return NodeEditorUtilities.NodeDefaultPath(type);
}
void Fill()
{
var nodes = GetAllNodes();
var list = serializedObject.FindProperty(PropertyOrder);
//remove old
for (int i = list.arraySize - 1; i >= 0; i--)
{
var ptype = list.GetArrayElementAtIndex(i).FindPropertyRelative(PropertyType);
var type = Type.GetType(ptype.stringValue);
if (!nodes.Any(t => t == type))
list.DeleteArrayElementAtIndex(i);
}
//add new
nodes.ForEach(
(t) =>
{
var stype = t.AssemblyQualifiedName;
bool found = false;
for (int i = 0; i < list.arraySize; i++)
{
var ptype = list.GetArrayElementAtIndex(i).FindPropertyRelative(PropertyType);
if (ptype.stringValue == stype)
{
found = true;
break;
}
}
if (!found)
{
list.InsertArrayElementAtIndex(list.arraySize);
var ele = list.GetArrayElementAtIndex(list.arraySize - 1);
var ptype = ele.FindPropertyRelative(PropertyType);
var ppath = ele.FindPropertyRelative(PropertyPath);
ptype.stringValue = stype;
ppath.stringValue = null;
}
}
);
serializedObject.ApplyModifiedProperties();
}
List<Type> GetAllNodes()
{
List<Type> types = new List<Type>();
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
foreach (Type type in assembly.GetTypes())
if (!type.IsAbstract && type.IsSubclassOf(typeof(Node)))
types.Add(type);
return types;
}
}
}