forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyHandler.cs
More file actions
417 lines (351 loc) · 17.3 KB
/
Copy pathPropertyHandler.cs
File metadata and controls
417 lines (351 loc) · 17.3 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEditorInternal;
namespace UnityEditor
{
internal class PropertyHandler
{
private PropertyDrawer m_PropertyDrawer = null;
private List<DecoratorDrawer> m_DecoratorDrawers = null;
public string tooltip = null;
public bool hasPropertyDrawer { get { return propertyDrawer != null; } }
internal PropertyDrawer propertyDrawer { get { return isCurrentlyNested ? null : m_PropertyDrawer; } }
internal static Dictionary<string, ReorderableListWrapper> s_reorderableLists = new Dictionary<string, ReorderableListWrapper>();
private static int s_LastInspectionTarget;
private static int s_LastInspectorNumComponents;
static PropertyHandler()
{
Undo.undoRedoPerformed += () =>
{
ReorderableList.ClearExistingListCaches();
};
}
public static void ClearCache()
{
s_reorderableLists.Clear();
s_LastInspectionTarget = 0;
}
public static void ClearListCacheIncludingChildren(string propertyPath)
{
foreach (var listEntry in s_reorderableLists)
{
if (listEntry.Key.Contains(propertyPath)) listEntry.Value.ClearCache();
}
}
private bool isCurrentlyNested
{
get
{
return (m_PropertyDrawer != null
&& ScriptAttributeUtility.s_DrawerStack.Count > 0
&& m_PropertyDrawer == ScriptAttributeUtility.s_DrawerStack.Peek());
}
}
public List<ContextMenuItemAttribute> contextMenuItems = null;
public bool empty
{
get
{
return m_DecoratorDrawers == null
&& tooltip == null
&& propertyDrawer == null
&& contextMenuItems == null;
}
}
public void HandleAttribute(SerializedProperty property, PropertyAttribute attribute, FieldInfo field, Type propertyType)
{
if (attribute is TooltipAttribute)
{
tooltip = (attribute as TooltipAttribute).tooltip;
return;
}
if (attribute is ContextMenuItemAttribute)
{
// Use context menu items on array elements, not on array itself
if (propertyType.IsArrayOrList())
return;
if (contextMenuItems == null)
contextMenuItems = new List<ContextMenuItemAttribute>();
contextMenuItems.Add(attribute as ContextMenuItemAttribute);
return;
}
// Look for its drawer type of this attribute
HandleDrawnType(property, attribute.GetType(), propertyType, field, attribute);
}
public void HandleDrawnType(SerializedProperty property, Type drawnType, Type propertyType, FieldInfo field, PropertyAttribute attribute)
{
Type drawerType = ScriptAttributeUtility.GetDrawerTypeForPropertyAndType(property, drawnType);
// If we found a drawer type, instantiate the drawer, cache it, and return it.
if (drawerType != null)
{
if (typeof(PropertyDrawer).IsAssignableFrom(drawerType))
{
// Use PropertyDrawer on array elements, not on array itself.
// If there's a PropertyAttribute on an array, we want to apply it to the individual array elements instead.
// This is the only convenient way we can let the user apply PropertyDrawer attributes to elements inside an array.
if (propertyType != null && propertyType.IsArrayOrList())
return;
m_PropertyDrawer = (PropertyDrawer)System.Activator.CreateInstance(drawerType);
m_PropertyDrawer.m_FieldInfo = field;
// Will be null by design if default type drawer!
m_PropertyDrawer.m_Attribute = attribute;
}
else if (typeof(DecoratorDrawer).IsAssignableFrom(drawerType))
{
// Draw decorators on array itself, not on each array elements
if (field != null && field.FieldType.IsArrayOrList() && !propertyType.IsArrayOrList())
return;
DecoratorDrawer decorator = (DecoratorDrawer)System.Activator.CreateInstance(drawerType);
decorator.m_Attribute = attribute;
if (m_DecoratorDrawers == null)
m_DecoratorDrawers = new List<DecoratorDrawer>();
m_DecoratorDrawers.Add(decorator);
}
}
}
// returns true if children needs to be drawn separately
public bool OnGUI(Rect position, SerializedProperty property, GUIContent label, bool includeChildren)
{
Rect visibleArea = new Rect(0, 0, float.MaxValue, float.MaxValue);
return OnGUI(position, property, label, includeChildren, visibleArea);
}
internal bool OnGUI(Rect position, SerializedProperty property, GUIContent label, bool includeChildren, Rect visibleArea)
{
TestInvalidateCache();
float oldLabelWidth, oldFieldWidth;
float propHeight = position.height;
position.height = 0;
if (m_DecoratorDrawers != null && !isCurrentlyNested)
{
foreach (DecoratorDrawer decorator in m_DecoratorDrawers)
{
position.height = decorator.GetHeight();
oldLabelWidth = EditorGUIUtility.labelWidth;
oldFieldWidth = EditorGUIUtility.fieldWidth;
decorator.OnGUI(position);
EditorGUIUtility.labelWidth = oldLabelWidth;
EditorGUIUtility.fieldWidth = oldFieldWidth;
position.y += position.height;
propHeight -= position.height;
}
}
position.height = propHeight;
if (propertyDrawer != null)
{
// Remember widths
oldLabelWidth = EditorGUIUtility.labelWidth;
oldFieldWidth = EditorGUIUtility.fieldWidth;
// Draw with custom drawer
propertyDrawer.OnGUISafe(position, property.Copy(), label ?? EditorGUIUtility.TempContent(property.localizedDisplayName));
// Restore widths
EditorGUIUtility.labelWidth = oldLabelWidth;
EditorGUIUtility.fieldWidth = oldFieldWidth;
return false;
}
else
{
if (IsNonStringArray(property))
{
ReorderableListWrapper reorderableList;
string key = ReorderableListWrapper.GetPropertyIdentifier(property);
if (!s_reorderableLists.TryGetValue(key, out reorderableList))
{
throw new IndexOutOfRangeException($"collection with name \"{property.name}\" doesn't have ReorderableList assigned to it.");
}
reorderableList.Property = property;
reorderableList.Draw(position, visibleArea);
return false;
}
if (!includeChildren)
return EditorGUI.DefaultPropertyField(position, property, label);
// Remember state
Vector2 oldIconSize = EditorGUIUtility.GetIconSize();
bool wasEnabled = GUI.enabled;
int origIndent = EditorGUI.indentLevel;
int relIndent = origIndent - property.depth;
SerializedProperty prop = property.Copy();
position.height = EditorGUI.GetSinglePropertyHeight(prop, label);
// First property with custom label
EditorGUI.indentLevel = prop.depth + relIndent;
bool childrenAreExpanded = EditorGUI.DefaultPropertyField(position, prop, label) && EditorGUI.HasVisibleChildFields(prop);
position.y += position.height + EditorGUI.kControlVerticalSpacing;
// Loop through all child properties
if (childrenAreExpanded)
{
SerializedProperty endProperty = prop.GetEndProperty();
while (prop.NextVisible(childrenAreExpanded) && !SerializedProperty.EqualContents(prop, endProperty))
{
var handler = ScriptAttributeUtility.GetHandler(prop);
EditorGUI.indentLevel = prop.depth + relIndent;
position.height = handler.GetHeight(prop, null, false);
if (position.Overlaps(visibleArea))
{
EditorGUI.BeginChangeCheck();
childrenAreExpanded = handler.OnGUI(position, prop, null, false) && EditorGUI.HasVisibleChildFields(prop);
// Changing child properties (like array size) may invalidate the iterator,
// so stop now, or we may get errors.
if (EditorGUI.EndChangeCheck())
break;
}
position.y += position.height + EditorGUI.kControlVerticalSpacing;
}
}
// Restore state
GUI.enabled = wasEnabled;
EditorGUIUtility.SetIconSize(oldIconSize);
EditorGUI.indentLevel = origIndent;
return false;
}
}
public bool OnGUILayout(SerializedProperty property, GUIContent label, bool includeChildren, params GUILayoutOption[] options)
{
Rect r;
if (property.propertyType == SerializedPropertyType.Boolean && propertyDrawer == null && (m_DecoratorDrawers == null || m_DecoratorDrawers.Count == 0))
r = EditorGUILayout.GetToggleRect(true, options);
else
r = EditorGUILayout.GetControlRect(EditorGUI.LabelHasContent(label), GetHeight(property, label, includeChildren), options);
EditorGUILayout.s_LastRect = r;
return OnGUI(r, property, label, includeChildren);
}
public float GetHeight(SerializedProperty property, GUIContent label, bool includeChildren)
{
float height = 0;
if (IsNonStringArray(property))
{
ReorderableListWrapper reorderableList;
string key = ReorderableListWrapper.GetPropertyIdentifier(property);
// If collection doesn't have a ReorderableList assigned to it, create one and assign it
if (!s_reorderableLists.TryGetValue(key, out reorderableList))
{
reorderableList = new ReorderableListWrapper(property, IsArrayReorderable(property));
s_reorderableLists[key] = reorderableList;
}
reorderableList.Property = property;
height = s_reorderableLists[key].GetHeight();
return height;
}
if (m_DecoratorDrawers != null && !isCurrentlyNested)
foreach (DecoratorDrawer drawer in m_DecoratorDrawers)
height += drawer.GetHeight();
if (propertyDrawer != null)
{
height += propertyDrawer.GetPropertyHeightSafe(property.Copy(), label ?? EditorGUIUtility.TempContent(property.displayName));
}
else if (!includeChildren)
{
height += EditorGUI.GetSinglePropertyHeight(property, label);
}
else
{
property = property.Copy();
// First property with custom label
height += EditorGUI.GetSinglePropertyHeight(property, label);
bool childrenAreExpanded = property.isExpanded && EditorGUI.HasVisibleChildFields(property);
// Loop through all child properties
var tc = EditorGUIUtility.TempContent(property.displayName);
if (childrenAreExpanded)
{
SerializedProperty endProperty = property.GetEndProperty();
while (property.NextVisible(childrenAreExpanded) && !SerializedProperty.EqualContents(property, endProperty))
{
height += ScriptAttributeUtility.GetHandler(property).GetHeight(property, tc, true);
childrenAreExpanded = false;
height += EditorGUI.kControlVerticalSpacing;
}
}
}
return height;
}
public bool CanCacheInspectorGUI(SerializedProperty property)
{
if (m_DecoratorDrawers != null &&
!isCurrentlyNested &&
m_DecoratorDrawers.Any(decorator => !decorator.CanCacheInspectorGUI()))
return false;
if (propertyDrawer != null)
return propertyDrawer.CanCacheInspectorGUISafe(property.Copy());
property = property.Copy();
bool childrenAreExpanded = property.isExpanded && EditorGUI.HasVisibleChildFields(property);
// Loop through all child properties
if (childrenAreExpanded)
{
PropertyHandler handler = null;
SerializedProperty endProperty = property.GetEndProperty();
while (property.NextVisible(childrenAreExpanded) && !SerializedProperty.EqualContents(property, endProperty))
{
if (handler == null)
handler = ScriptAttributeUtility.GetHandler(property);
if (!handler.CanCacheInspectorGUI(property))
return false;
childrenAreExpanded = false;
}
}
return true;
}
public void AddMenuItems(SerializedProperty property, GenericMenu menu)
{
if (contextMenuItems == null)
return;
Type scriptType = property.serializedObject.targetObject.GetType();
foreach (ContextMenuItemAttribute attribute in contextMenuItems)
{
MethodInfo method = scriptType.GetMethod(attribute.function, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (method == null)
continue;
menu.AddItem(new GUIContent(attribute.name), false, () => CallMenuCallback(property.serializedObject.targetObjects, method));
}
}
public void CallMenuCallback(object[] targets, MethodInfo method)
{
foreach (object target in targets)
method.Invoke(target, new object[] {});
}
internal void TestInvalidateCache()
{
GameObject activeObject = Selection.activeObject as GameObject;
if (activeObject != null)
{
var components = activeObject.GetComponents(typeof(Component));
if (s_LastInspectionTarget != activeObject.GetInstanceID() ||
s_LastInspectorNumComponents != components.Length)
{
ClearCache();
s_LastInspectionTarget = activeObject.GetInstanceID();
s_LastInspectorNumComponents = components.Length;
}
}
}
internal static bool IsNonStringArray(SerializedProperty property)
{
// Strings should not be represented with ReorderableList, they will use custom drawer therefore we don't treat them as other arrays
return property.isArray && property.propertyType != SerializedPropertyType.String;
}
static bool IsArrayReorderable(SerializedProperty property)
{
FieldInfo listInfo = null;
Queue<string> propertyName = new Queue<string>(property.propertyPath.Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
listInfo = property.serializedObject.targetObject.GetType().GetField(propertyName.Dequeue());
if (listInfo == null) return false;
// If we have a nested property we need to find it via reflection in order to verify
// if it has a non-reorderable attribute
while (propertyName.Count > 0)
{
Type t = listInfo.FieldType;
if (t.IsArray) t = t.GetElementType();
else if (t.IsArrayOrList()) t = t.GetGenericArguments().Single();
FieldInfo f = t.GetField(propertyName.Dequeue());
if (f != null)
{
listInfo = f;
}
}
return TypeCache.GetFieldsWithAttribute(typeof(ReorderableAttribute)).Any(f => f.Equals(listInfo)) || property.IsReorderable();
}
}
}