CustomPropertyDrawerBase.cs
6.25 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
/******************************************************************************
* 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 System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
namespace Leap.Unity {
public class CustomPropertyDrawerBase : PropertyDrawer {
public const float INDENT_AMOUNT = 12;
private List<IDrawable> _drawables;
private SerializedProperty _property;
private string _onGuiSampleName;
private string _getHeightSampleName;
public CustomPropertyDrawerBase() {
_onGuiSampleName = "OnGUI for " + GetType().Name;
_getHeightSampleName = "GetPropertyHeight for " + GetType().Name;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
using (new ProfilerSample(_onGuiSampleName)) {
init(property);
foreach (var drawable in _drawables) {
drawable.Draw(ref position);
}
}
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
using (new ProfilerSample(_getHeightSampleName)) {
init(property);
float height = 0;
foreach (var drawable in _drawables) {
if (drawable is PropertyContainer) {
height += ((PropertyContainer)drawable).getHeight();
}
}
return height;
}
}
protected virtual void init(SerializedProperty property) {
if (_property == property) {
return;
}
_drawables = new List<IDrawable>();
_property = property;
}
protected void drawPropertyConditionally(string propertyName, string conditionalName, bool includeChildren = true) {
SerializedProperty property, condition;
if (!tryGetProperty(propertyName, out property) || !tryGetProperty(conditionalName, out condition)) {
return;
}
_drawables.Add(new PropertyContainer() {
draw = rect => {
if (condition.boolValue) {
EditorGUI.PropertyField(rect, property, includeChildren);
}
},
getHeight = () => {
return condition.boolValue ? EditorGUI.GetPropertyHeight(property, GUIContent.none, includeChildren) : 0;
}
});
}
protected void drawPropertyConditionally(string propertyName, Func<bool> condition, bool includeChildren = true) {
SerializedProperty property;
if (!tryGetProperty(propertyName, out property)) {
return;
}
_drawables.Add(new PropertyContainer() {
draw = rect => {
if (condition()) {
EditorGUI.PropertyField(rect, property, includeChildren);
}
},
getHeight = () => {
return condition() ? EditorGUI.GetPropertyHeight(property, GUIContent.none, includeChildren) : 0;
}
});
}
protected void drawProperty(string name, bool includeChildren = true, bool disable = false) {
SerializedProperty property;
if (!tryGetProperty(name, out property)) {
return;
}
GUIContent content = new GUIContent(property.displayName, property.tooltip);
_drawables.Add(new PropertyContainer() {
draw = rect => {
EditorGUI.BeginDisabledGroup(disable);
EditorGUI.PropertyField(rect, property, content, includeChildren);
EditorGUI.EndDisabledGroup();
},
getHeight = () => EditorGUI.GetPropertyHeight(property, GUIContent.none, includeChildren)
});
}
protected void drawProperty(string name, Func<string> nameFunc, bool includeChildren = true) {
SerializedProperty property;
if (!tryGetProperty(name, out property)) {
return;
}
GUIContent content = new GUIContent(nameFunc(), property.tooltip);
_drawables.Add(new PropertyContainer() {
draw = rect => {
content.text = nameFunc() ?? property.displayName;
EditorGUI.PropertyField(rect, property, content, includeChildren);
},
getHeight = () => EditorGUI.GetPropertyHeight(property, content, includeChildren)
});
}
protected void drawCustom(Action<Rect> drawFunc, float height) {
_drawables.Add(new PropertyContainer() {
draw = drawFunc,
getHeight = () => height
});
}
protected void drawCustom(Action<Rect> drawFunc, Func<float> heightFunc) {
_drawables.Add(new PropertyContainer() {
draw = drawFunc,
getHeight = heightFunc
});
}
protected void increaseIndent() {
_drawables.Add(new IndentDrawable() {
indent = INDENT_AMOUNT
});
}
protected void decreaseIndent() {
_drawables.Add(new IndentDrawable() {
indent = -INDENT_AMOUNT
});
}
protected bool tryGetProperty(string name, out SerializedProperty property) {
property = _property.FindPropertyRelative(name);
if (property == null) {
Debug.LogWarning("Could not find property " + name + ", was it renamed or removed?");
return false;
} else {
return true;
}
}
protected bool validateProperty(string name) {
if (_property.FindPropertyRelative(name) == null) {
Debug.LogWarning("Could not find property " + name + ", was it renamed or removed?");
return false;
}
return true;
}
private interface IDrawable {
void Draw(ref Rect rect);
}
private struct PropertyContainer : IDrawable {
public Action<Rect> draw;
public Func<float> getHeight;
public void Draw(ref Rect rect) {
rect.height = getHeight();
draw(rect);
rect.y += rect.height;
}
}
private struct IndentDrawable : IDrawable {
public float indent;
public void Draw(ref Rect rect) {
rect.x += indent;
rect.width -= indent;
}
}
}
}