-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIfAttribute.cs
More file actions
333 lines (274 loc) · 11.6 KB
/
IfAttribute.cs
File metadata and controls
333 lines (274 loc) · 11.6 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
using System;
using System.Diagnostics;
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;
#if UNITY_EDITOR
using Jackey.Utilities.Attributes.PropertyDrawers;
using UnityEditor;
using UnityEditor.UIElements;
#endif
namespace Jackey.Utilities.Attributes {
/// <summary>
/// Base attribute for conditional drawing of serialized properties.
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
[Conditional("UNITY_EDITOR")]
public class IfAttribute : PropertyAttribute {
public string Other { get; }
internal Comparison Method { get; }
internal ValueType OtherType { get; }
public bool BoolValue { get; }
public double NumberValue { get; }
private IfAttribute(string other, Comparison comparison) {
Other = other;
Method = comparison;
}
protected IfAttribute(string other, BoolComparison comparison, bool value) : this(other, (Comparison)comparison) {
OtherType = ValueType.Boolean;
BoolValue = value;
}
protected IfAttribute(string other, NumberComparison comparison, double value) : this(other, (Comparison)comparison) {
OtherType = ValueType.Number;
NumberValue = value;
}
protected IfAttribute(string other, ObjectComparison comparison, Object _) : this(other, (Comparison)comparison) {
OtherType = ValueType.Object;
}
#if UNITY_EDITOR
public bool EvaluateCondition(SerializedProperty property) {
return IfAttributeDrawer.EvaluateCondition(property, this);
}
[CanBeNull]
public SerializedProperty FindOtherProperty(SerializedProperty property) {
return IfAttributeDrawer.FindOtherProperty(property, this);
}
#endif
public enum BoolComparison {
Equal = Comparison.Equal,
NotEqual = Comparison.NotEqual,
}
public enum NumberComparison {
Equal = Comparison.Equal,
NotEqual = Comparison.NotEqual,
Greater = Comparison.Greater,
GreaterOrEqual = Comparison.GreaterOrEqual,
Less = Comparison.Less,
LessOrEqual = Comparison.LessOrEqual,
AND = Comparison.AND,
NOT = Comparison.NOT,
OR = Comparison.OR,
}
public enum ObjectComparison {
Null = Comparison.Null,
NotNull = Comparison.NotNull,
}
internal enum Comparison {
Equal,
NotEqual,
Greater,
GreaterOrEqual,
Less,
LessOrEqual,
AND,
NOT,
OR,
Null,
NotNull,
}
internal enum ValueType {
Boolean,
Number,
Object,
}
}
/// <summary>
/// Show this field in the inspector only when a comparison with another field is true
/// </summary>
public class ShowIfAttribute : IfAttribute {
public ShowIfAttribute(string other, BoolComparison comparison, bool value) : base(other, comparison, value) { }
public ShowIfAttribute(string other, NumberComparison comparison, double value) : base(other, comparison, value) { }
public ShowIfAttribute(string other, ObjectComparison comparison) : base(other, comparison, null) { }
#if UNITY_EDITOR
public void Bind(VisualElement element, SerializedProperty property) {
element.style.display = EvaluateCondition(property) ? DisplayStyle.Flex : DisplayStyle.None;
element.TrackPropertyValue(
FindOtherProperty(property),
_ => element.style.display = EvaluateCondition(property) ? DisplayStyle.Flex : DisplayStyle.None
);
}
#endif
}
/// <summary>
/// Same as <see cref="ShowIfAttribute"/> but is meant to be used on fields that have another property drawer.
/// That drawer can then easily get the result of the condition by either using <see cref="ShowIfAttribute.Bind"/> for VisualElements or
/// <see cref="IfAttribute.EvaluateCondition(SerializedProperty)"/> for IMGUI
/// </summary>
public sealed class CustomShowIfAttribute : ShowIfAttribute {
public CustomShowIfAttribute(string other, BoolComparison comparison, bool value) : base(other, comparison, value) { }
public CustomShowIfAttribute(string other, NumberComparison comparison, double value) : base(other, comparison, value) { }
public CustomShowIfAttribute(string other, ObjectComparison comparison) : base(other, comparison) { }
}
/// <summary>
/// Have this field enabled for editing in the inspector only when a comparison with another field is true
/// </summary>
public class EnableIfAttribute : IfAttribute {
public EnableIfAttribute(string other, BoolComparison comparison, bool value) : base(other, comparison, value) { }
public EnableIfAttribute(string other, NumberComparison comparison, double value) : base(other, comparison, value) { }
public EnableIfAttribute(string other, ObjectComparison comparison) : base(other, comparison, null) { }
#if UNITY_EDITOR
public void Bind(VisualElement element, SerializedProperty property) {
element.SetEnabled(EvaluateCondition(property));
element.TrackPropertyValue(
FindOtherProperty(property),
_ => element.SetEnabled(EvaluateCondition(property))
);
}
#endif
}
/// <summary>
/// Same as <see cref="EnableIfAttribute"/> but is meant to be used on fields that have another property drawer.
/// That drawer can then easily get the result of the condition by either using <see cref="EnableIfAttribute.Bind"/> for VisualElements or
/// <see cref="IfAttribute.EvaluateCondition(SerializedProperty)"/> for IMGUI
/// </summary>
public sealed class CustomEnableIfAttribute : EnableIfAttribute {
public CustomEnableIfAttribute(string other, BoolComparison comparison, bool value) : base(other, comparison, value) { }
public CustomEnableIfAttribute(string other, NumberComparison comparison, double value) : base(other, comparison, value) { }
public CustomEnableIfAttribute(string other, ObjectComparison comparison) : base(other, comparison) { }
}
#if UNITY_EDITOR
namespace PropertyDrawers {
public abstract class IfAttributeDrawer : PropertyDrawer {
public static bool EvaluateCondition(SerializedProperty property, IfAttribute attr) {
SerializedProperty otherProperty = FindOtherProperty(property, attr);
if (!PropertyMatchesCheck(otherProperty, attr.OtherType))
return false;
return EvaluateComparison(otherProperty, attr);
}
[CanBeNull]
public static SerializedProperty FindOtherProperty(SerializedProperty property, IfAttribute attr) {
string propertyPath = property.propertyPath;
int lastDepthIndex = propertyPath.LastIndexOf('.');
string currentDepthPath = string.Empty;
if (lastDepthIndex != -1)
currentDepthPath = $"{propertyPath[..lastDepthIndex]}.";
string otherPath = $"{currentDepthPath}{attr.Other}";
return property.serializedObject.FindProperty(otherPath);
}
private static bool PropertyMatchesCheck(SerializedProperty property, IfAttribute.ValueType valueType) {
if (property == null)
return false;
return valueType switch {
IfAttribute.ValueType.Boolean => property.propertyType is SerializedPropertyType.Boolean,
IfAttribute.ValueType.Number => property.propertyType is SerializedPropertyType.Float or SerializedPropertyType.Integer or SerializedPropertyType.Enum or SerializedPropertyType.LayerMask,
IfAttribute.ValueType.Object => property.propertyType is SerializedPropertyType.ObjectReference,
_ => throw new ArgumentOutOfRangeException(nameof(valueType), valueType, null),
};
}
private static bool EvaluateComparison(SerializedProperty property, IfAttribute attr) {
IfAttribute.Comparison comparison = attr.Method;
switch (attr.OtherType) {
case IfAttribute.ValueType.Boolean:
if (comparison is IfAttribute.Comparison.Equal)
return property.boolValue == attr.BoolValue;
return property.boolValue != attr.BoolValue;
case IfAttribute.ValueType.Number:
double numberValue = GetNumberValue(property);
double otherValue = attr.NumberValue;
return attr.Method switch {
IfAttribute.Comparison.Equal => numberValue == otherValue,
IfAttribute.Comparison.NotEqual => numberValue != otherValue,
IfAttribute.Comparison.Greater => numberValue > otherValue,
IfAttribute.Comparison.GreaterOrEqual => numberValue >= otherValue,
IfAttribute.Comparison.LessOrEqual => numberValue <= otherValue,
IfAttribute.Comparison.Less => numberValue < otherValue,
IfAttribute.Comparison.AND => ((long)numberValue & (long)otherValue) == (long)otherValue,
IfAttribute.Comparison.NOT => ((long)numberValue & (long)otherValue) == 0L,
IfAttribute.Comparison.OR => ((long)numberValue & (long)otherValue) != 0L,
_ => throw new ArgumentOutOfRangeException(),
};
case IfAttribute.ValueType.Object:
if (comparison is IfAttribute.Comparison.Null)
return property.objectReferenceValue == null;
return property.objectReferenceValue != null;
default:
throw new ArgumentOutOfRangeException();
}
}
private static double GetNumberValue(SerializedProperty property) {
if (property.propertyType is SerializedPropertyType.Integer) {
int intValue = property.intValue;
if (intValue != 0)
return intValue;
uint uIntValue = property.uintValue;
if (uIntValue != 0)
return uIntValue;
long longValue = property.longValue;
if (longValue != 0)
return longValue;
return property.ulongValue;
}
if (property.propertyType is SerializedPropertyType.Float) {
float floatValue = property.floatValue;
if (floatValue != 0f)
return floatValue;
return property.doubleValue;
}
if (property.propertyType is SerializedPropertyType.Enum) {
return property.enumValueFlag;
}
if (property.propertyType is SerializedPropertyType.LayerMask) {
return property.intValue;
}
return 0;
}
}
[CustomPropertyDrawer(typeof(ShowIfAttribute), false)]
public sealed class ShowIfAttributeDrawer : IfAttributeDrawer {
public override VisualElement CreatePropertyGUI(SerializedProperty property) {
ShowIfAttribute attr = (ShowIfAttribute)attribute;
PropertyField field = new PropertyField(property);
attr.Bind(field, property);
return field;
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
if (EvaluateCondition(property, (IfAttribute)attribute))
return EditorGUI.GetPropertyHeight(property, label);
float height = 0f;
// Negate spacing. Even if this method returns zero height, property decorators can add height to it afterwards.
// This makes EditorGUI.GetPropertyHeight() give the "wrong" height. Only downside is that it removes the
// spacing if there are other visible properties
foreach (object customAttribute in fieldInfo.GetCustomAttributes(false)) {
if (customAttribute is SpaceAttribute spaceAttribute)
height -= spaceAttribute.height;
}
return height;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
if (!EvaluateCondition(property, (IfAttribute)attribute))
return;
label = EditorGUI.BeginProperty(position, label, property);
EditorGUI.PropertyField(position, property, label);
EditorGUI.EndProperty();
}
}
[CustomPropertyDrawer(typeof(EnableIfAttribute), false)]
public sealed class EnableIfAttributeDrawer : IfAttributeDrawer {
public override VisualElement CreatePropertyGUI(SerializedProperty property) {
EnableIfAttribute attr = (EnableIfAttribute)attribute;
PropertyField field = new PropertyField(property);
attr.Bind(field, property);
return field;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
label = EditorGUI.BeginProperty(position, label, property);
EditorGUI.BeginDisabledGroup(!EvaluateCondition(property, (IfAttribute)attribute));
EditorGUI.PropertyField(position, property, label);
EditorGUI.EndDisabledGroup();
EditorGUI.EndProperty();
}
}
}
#endif
}