CustomEditorBase.cs
10 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/******************************************************************************
* Copyright (C) Leap Motion, Inc. 2011-2017. *
* Leap Motion proprietary and confidential. *
* *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
******************************************************************************/
using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
using Leap.Unity.Query;
namespace Leap.Unity {
public class CustomEditorBase<T> : CustomEditorBase where T : UnityEngine.Object {
protected new T target;
protected new T[] targets;
protected override void OnEnable() {
base.OnEnable();
target = base.target as T;
targets = base.targets.Query().
Where(t => t != null).
OfType<T>().
ToArray();
}
}
public class CustomEditorBase : Editor {
protected Dictionary<string, Action<SerializedProperty>> _specifiedDrawers;
protected Dictionary<string, List<Action<SerializedProperty>>> _specifiedDecorators;
protected Dictionary<string, List<Action<SerializedProperty>>> _specifiedPostDecorators;
protected Dictionary<string, List<Func<bool>>> _conditionalProperties;
protected List<string> _deferredProperties;
protected bool _showScriptField = true;
private bool _canCallSpecifyFunctions = false;
protected List<SerializedProperty> _modifiedProperties = new List<SerializedProperty>();
protected void dontShowScriptField() {
_showScriptField = false;
}
/// <summary>
/// Specify a callback to be used to draw a specific named property. Should be called in OnEnable.
/// </summary>
/// <param name="propertyName"></param>
/// <param name="propertyDrawer"></param>
protected void specifyCustomDrawer(string propertyName, Action<SerializedProperty> propertyDrawer) {
throwIfNotInOnEnable("specifyCustomDrawer");
if (!validateProperty(propertyName)) {
return;
}
_specifiedDrawers[propertyName] = propertyDrawer;
}
/// <summary>
/// Specify a callback to be used to draw a decorator for a specific named property. Should be called in OnEnable.
/// </summary>
protected void specifyCustomDecorator(string propertyName, Action<SerializedProperty> decoratorDrawer) {
throwIfNotInOnEnable("specifyCustomDecorator");
if (!validateProperty(propertyName)) {
return;
}
List<Action<SerializedProperty>> list;
if (!_specifiedDecorators.TryGetValue(propertyName, out list)) {
list = new List<Action<SerializedProperty>>();
_specifiedDecorators[propertyName] = list;
}
list.Add(decoratorDrawer);
}
/// <summary>
/// Specify a callback to be used to draw a decorator AFTER a specific named property.
///
/// Should be called in OnEnable.
/// </summary>
protected void specifyCustomPostDecorator(string propertyName, Action<SerializedProperty> decoratorDrawer) {
throwIfNotInOnEnable("specifyCustomPostDecorator");
if (!validateProperty(propertyName)) {
return;
}
List<Action<SerializedProperty>> list;
if (!_specifiedPostDecorators.TryGetValue(propertyName, out list)) {
list = new List<Action<SerializedProperty>>();
_specifiedPostDecorators[propertyName] = list;
}
list.Add(decoratorDrawer);
}
/// <summary>
/// Specify a list of properties that should only be displayed if the conditional property has a value of true.
/// Should be called in OnEnable.
/// </summary>
/// <param name="conditionalName"></param>
/// <param name="dependantProperties"></param>
protected void specifyConditionalDrawing(string conditionalName, params string[] dependantProperties) {
throwIfNotInOnEnable("specifyConditionalDrawing");
if (!validateProperty(conditionalName)) {
return;
}
SerializedProperty conditionalProp = serializedObject.FindProperty(conditionalName);
specifyConditionalDrawing(() => {
if (conditionalProp.hasMultipleDifferentValues) {
return false;
} else {
return conditionalProp.boolValue;
}
}, dependantProperties);
}
protected void specifyConditionalDrawing(string enumName, int enumValue, params string[] dependantProperties) {
throwIfNotInOnEnable("specifyConditionalDrawing");
if (!validateProperty(enumName)) {
return;
}
SerializedProperty enumProp = serializedObject.FindProperty(enumName);
specifyConditionalDrawing(() => {
if (enumProp.hasMultipleDifferentValues) {
return false;
} else {
return enumProp.intValue == enumValue;
}
}, dependantProperties);
}
protected void hideField(string propertyName) {
throwIfNotInOnEnable("hideField");
specifyConditionalDrawing(() => false, propertyName);
}
protected void specifyConditionalDrawing(Func<bool> conditional, params string[] dependantProperties) {
throwIfNotInOnEnable("specifyConditionalDrawing");
for (int i = 0; i < dependantProperties.Length; i++) {
string dependant = dependantProperties[i];
if (!validateProperty(dependant)) {
continue;
}
List<Func<bool>> list;
if (!_conditionalProperties.TryGetValue(dependant, out list)) {
list = new List<Func<bool>>();
_conditionalProperties[dependant] = list;
}
list.Add(conditional);
}
}
/// <summary>
/// Defer rendering of a property until the end of the inspector. Deferred properties
/// are drawn in the REVERSE order they are deferred! NOT by the order they appear in the
/// serialized object!
/// </summary>
protected void deferProperty(string propertyName) {
throwIfNotInOnEnable("deferProperty");
if (!validateProperty(propertyName)) {
return;
}
_deferredProperties.Insert(0, propertyName);
}
protected void drawScriptField(bool disable = true) {
var scriptProp = serializedObject.FindProperty("m_Script");
EditorGUI.BeginDisabledGroup(disable);
EditorGUILayout.PropertyField(scriptProp);
EditorGUI.EndDisabledGroup();
}
protected virtual void OnEnable() {
try {
if (serializedObject == null) { }
} catch (NullReferenceException) {
DestroyImmediate(this);
throw new Exception("Cleaning up an editor of type " + GetType() + ". Make sure to always destroy your editors when you are done with them!");
}
_specifiedDrawers = new Dictionary<string, Action<SerializedProperty>>();
_specifiedDecorators = new Dictionary<string, List<Action<SerializedProperty>>>();
_specifiedPostDecorators = new Dictionary<string, List<Action<SerializedProperty>>>();
_conditionalProperties = new Dictionary<string, List<Func<bool>>>();
_deferredProperties = new List<string>();
_canCallSpecifyFunctions = true;
}
protected bool validateProperty(string propertyName) {
if (serializedObject.FindProperty(propertyName) == null) {
Debug.LogWarning("Property " + propertyName + " does not exist, was it removed or renamed?");
return false;
}
return true;
}
/*
* This method draws all visible properties, mirroring the default behavior of OnInspectorGUI.
* Individual properties can be specified to have custom drawers.
*/
public override void OnInspectorGUI() {
_canCallSpecifyFunctions = false;
_modifiedProperties.Clear();
SerializedProperty iterator = serializedObject.GetIterator();
bool isFirst = true;
while (iterator.NextVisible(isFirst)) {
if (isFirst && !_showScriptField) {
isFirst = false;
continue;
}
if (_deferredProperties.Contains(iterator.name)) {
continue;
}
using (new EditorGUI.DisabledGroupScope(isFirst)) {
drawProperty(iterator);
}
isFirst = false;
}
foreach (var deferredProperty in _deferredProperties) {
drawProperty(serializedObject.FindProperty(deferredProperty));
}
serializedObject.ApplyModifiedProperties();
}
private void drawProperty(SerializedProperty property) {
List<Func<bool>> conditionalList;
if (_conditionalProperties.TryGetValue(property.name, out conditionalList)) {
bool allTrue = true;
for (int i = 0; i < conditionalList.Count; i++) {
allTrue &= conditionalList[i]();
}
if (!allTrue) {
return;
}
}
Action<SerializedProperty> customDrawer;
List<Action<SerializedProperty>> decoratorList;
if (_specifiedDecorators.TryGetValue(property.name, out decoratorList)) {
for (int i = 0; i < decoratorList.Count; i++) {
decoratorList[i](property);
}
}
EditorGUI.BeginChangeCheck();
if (_specifiedDrawers.TryGetValue(property.name, out customDrawer)) {
customDrawer(property);
} else {
EditorGUILayout.PropertyField(property, true);
}
if (EditorGUI.EndChangeCheck()) {
_modifiedProperties.Add(property.Copy());
}
List<Action<SerializedProperty>> postDecoratorList;
if (_specifiedPostDecorators.TryGetValue(property.name, out postDecoratorList)) {
for (int i = 0; i < postDecoratorList.Count; i++) {
postDecoratorList[i](property);
}
}
}
private void throwIfNotInOnEnable(string methodName) {
if (!_canCallSpecifyFunctions) {
throw new InvalidOperationException("Cannot call " + methodName + " from within any other function but OnEnable. Make sure you also call base.OnEnable as well!");
}
}
}
}