Tanoda
CustomPropertyDrawerBase.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Collections.Generic;
11using UnityEngine;
12using UnityEditor;
13
14namespace Leap.Unity {
15
16 public class CustomPropertyDrawerBase : PropertyDrawer {
17 public const float INDENT_AMOUNT = 12;
18
19 private List<IDrawable> _drawables;
20 private SerializedProperty _property;
21
22 private string _onGuiSampleName;
23 private string _getHeightSampleName;
24
26 _onGuiSampleName = "OnGUI for " + GetType().Name;
27 _getHeightSampleName = "GetPropertyHeight for " + GetType().Name;
28 }
29
30 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
31 using (new ProfilerSample(_onGuiSampleName)) {
32 init(property);
33
34 foreach (var drawable in _drawables) {
35 drawable.Draw(ref position);
36 }
37 }
38 }
39
40 public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
41 using (new ProfilerSample(_getHeightSampleName)) {
42 init(property);
43
44 float height = 0;
45 foreach (var drawable in _drawables) {
46 if (drawable is PropertyContainer) {
47 height += ((PropertyContainer)drawable).getHeight();
48 }
49 }
50
51 return height;
52 }
53 }
54
55 protected virtual void init(SerializedProperty property) {
56 if (_property == property) {
57 return;
58 }
59
60 _drawables = new List<IDrawable>();
61 _property = property;
62 }
63
64 protected void drawPropertyConditionally(string propertyName, string conditionalName, bool includeChildren = true) {
65 SerializedProperty property, condition;
66 if (!tryGetProperty(propertyName, out property) || !tryGetProperty(conditionalName, out condition)) {
67 return;
68 }
69
70 _drawables.Add(new PropertyContainer() {
71 draw = rect => {
72 if (condition.boolValue) {
73 EditorGUI.PropertyField(rect, property, includeChildren);
74 }
75 },
76 getHeight = () => {
77 return condition.boolValue ? EditorGUI.GetPropertyHeight(property, GUIContent.none, includeChildren) : 0;
78 }
79 });
80 }
81
82 protected void drawPropertyConditionally(string propertyName, Func<bool> condition, bool includeChildren = true) {
83 SerializedProperty property;
84 if (!tryGetProperty(propertyName, out property)) {
85 return;
86 }
87
88 _drawables.Add(new PropertyContainer() {
89 draw = rect => {
90 if (condition()) {
91 EditorGUI.PropertyField(rect, property, includeChildren);
92 }
93 },
94 getHeight = () => {
95 return condition() ? EditorGUI.GetPropertyHeight(property, GUIContent.none, includeChildren) : 0;
96 }
97 });
98 }
99
100 protected void drawProperty(string name, bool includeChildren = true, bool disable = false) {
101 SerializedProperty property;
102 if (!tryGetProperty(name, out property)) {
103 return;
104 }
105
106 GUIContent content = new GUIContent(property.displayName, property.tooltip);
107 _drawables.Add(new PropertyContainer() {
108 draw = rect => {
109 EditorGUI.BeginDisabledGroup(disable);
110 EditorGUI.PropertyField(rect, property, content, includeChildren);
111 EditorGUI.EndDisabledGroup();
112 },
113 getHeight = () => EditorGUI.GetPropertyHeight(property, GUIContent.none, includeChildren)
114 });
115 }
116
117 protected void drawProperty(string name, Func<string> nameFunc, bool includeChildren = true) {
118 SerializedProperty property;
119 if (!tryGetProperty(name, out property)) {
120 return;
121 }
122
123 GUIContent content = new GUIContent(nameFunc(), property.tooltip);
124
125 _drawables.Add(new PropertyContainer() {
126 draw = rect => {
127 content.text = nameFunc() ?? property.displayName;
128 EditorGUI.PropertyField(rect, property, content, includeChildren);
129 },
130 getHeight = () => EditorGUI.GetPropertyHeight(property, content, includeChildren)
131 });
132 }
133
134 protected void drawCustom(Action<Rect> drawFunc, float height) {
135 _drawables.Add(new PropertyContainer() {
136 draw = drawFunc,
137 getHeight = () => height
138 });
139 }
140
141 protected void drawCustom(Action<Rect> drawFunc, Func<float> heightFunc) {
142 _drawables.Add(new PropertyContainer() {
143 draw = drawFunc,
144 getHeight = heightFunc
145 });
146 }
147
148 protected void increaseIndent() {
149 _drawables.Add(new IndentDrawable() {
150 indent = INDENT_AMOUNT
151 });
152 }
153
154 protected void decreaseIndent() {
155 _drawables.Add(new IndentDrawable() {
156 indent = -INDENT_AMOUNT
157 });
158 }
159
160 protected bool tryGetProperty(string name, out SerializedProperty property) {
161 property = _property.FindPropertyRelative(name);
162
163 if (property == null) {
164 Debug.LogWarning("Could not find property " + name + ", was it renamed or removed?");
165 return false;
166 } else {
167 return true;
168 }
169 }
170
171 protected bool validateProperty(string name) {
172 if (_property.FindPropertyRelative(name) == null) {
173 Debug.LogWarning("Could not find property " + name + ", was it renamed or removed?");
174 return false;
175 }
176
177 return true;
178 }
179
180 private interface IDrawable {
181 void Draw(ref Rect rect);
182 }
183
184 private struct PropertyContainer : IDrawable {
185 public Action<Rect> draw;
186 public Func<float> getHeight;
187
188 public void Draw(ref Rect rect) {
189 rect.height = getHeight();
190 draw(rect);
191 rect.y += rect.height;
192 }
193 }
194
195 private struct IndentDrawable : IDrawable {
196 public float indent;
197
198 public void Draw(ref Rect rect) {
199 rect.x += indent;
200 rect.width -= indent;
201 }
202 }
203 }
204}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
virtual void init(SerializedProperty property)
override float GetPropertyHeight(SerializedProperty property, GUIContent label)
void drawProperty(string name, bool includeChildren=true, bool disable=false)
void drawProperty(string name, Func< string > nameFunc, bool includeChildren=true)
void drawCustom(Action< Rect > drawFunc, Func< float > heightFunc)
override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
void drawCustom(Action< Rect > drawFunc, float height)
bool tryGetProperty(string name, out SerializedProperty property)
void drawPropertyConditionally(string propertyName, Func< bool > condition, bool includeChildren=true)
void drawPropertyConditionally(string propertyName, string conditionalName, bool includeChildren=true)
A utility struct for ease of use when you want to wrap a piece of code in a Profiler....