Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 86a125e

Browse files
committed
Rewrite all custom editor property drawers
1 parent 9a5bc5c commit 86a125e

36 files changed

+630
-327
lines changed

Editor/BitmaskPropertyDrawer.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Zigurous.DataStructures.Editor
5+
{
6+
[CustomPropertyDrawer(typeof(Bitmask))]
7+
public class BitmaskPropertyDrawer : PropertyDrawer
8+
{
9+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
10+
{
11+
EditorGUI.BeginProperty(position, label, property);
12+
13+
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
14+
15+
int indentLevel = EditorGUI.indentLevel;
16+
EditorGUI.indentLevel = 0;
17+
18+
EditorGUI.BeginChangeCheck();
19+
20+
SerializedProperty mask = property.FindPropertyRelative("mask");
21+
int value = EditorGUI.IntField(position, mask.intValue);
22+
23+
if (EditorGUI.EndChangeCheck()) {
24+
mask.intValue = value;
25+
}
26+
27+
EditorGUI.indentLevel = indentLevel;
28+
EditorGUI.EndProperty();
29+
}
30+
31+
}
32+
33+
}

Editor/PropertyDrawers/RangePropertyDrawer.cs.meta renamed to Editor/BitmaskPropertyDrawer.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Bool3PropertyDrawer.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Zigurous.DataStructures.Editor
5+
{
6+
[CustomPropertyDrawer(typeof(Bool3))]
7+
public class Bool3PropertyDrawer : PropertyDrawer
8+
{
9+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
10+
{
11+
EditorGUI.BeginProperty(position, label, property);
12+
13+
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
14+
15+
int indentLevel = EditorGUI.indentLevel;
16+
EditorGUI.indentLevel = 0;
17+
18+
position = BoolField(position, property.FindPropertyRelative("x"));
19+
position = BoolField(position, property.FindPropertyRelative("y"));
20+
position = BoolField(position, property.FindPropertyRelative("z"));
21+
22+
EditorGUI.indentLevel = indentLevel;
23+
EditorGUI.EndProperty();
24+
}
25+
26+
private Rect BoolField(Rect position, SerializedProperty property)
27+
{
28+
Rect field = EditorGUIUtility.GetFieldRect(position, 3);
29+
position.x += field.width + EditorGUIUtility.standardHorizontalSpacing;
30+
31+
EditorGUI.BeginChangeCheck();
32+
33+
bool value = EditorGUIUtility.FieldWrapper(property.displayName, (label) => {
34+
return EditorGUI.Toggle(field, label, property.boolValue);
35+
});
36+
37+
if (EditorGUI.EndChangeCheck()) {
38+
property.boolValue = value;
39+
}
40+
41+
return position;
42+
}
43+
44+
}
45+
46+
}

Editor/PropertyDrawers/Bool3PropertyDrawer.cs.meta renamed to Editor/Bool3PropertyDrawer.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Zigurous.DataStructures.Editor
5+
{
6+
[CustomPropertyDrawer(typeof(ClampedRange))]
7+
public class ClampedRangePropertyDrawer : PropertyDrawer
8+
{
9+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
10+
{
11+
return (EditorGUIUtility.singleLineHeight * 3f) +
12+
(EditorGUIUtility.standardVerticalSpacing * 2f);
13+
}
14+
15+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
16+
{
17+
EditorGUI.BeginProperty(position, label, property);
18+
19+
position.height = EditorGUIUtility.singleLineHeight;
20+
EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
21+
22+
SerializedProperty range = property.FindPropertyRelative("range");
23+
SerializedProperty clamp = property.FindPropertyRelative("clamp");
24+
25+
position = RangeProperty(position, range);
26+
position = RangeProperty(position, clamp);
27+
28+
SerializedProperty rangeMin = range.FindPropertyRelative("_min");
29+
SerializedProperty rangeMax = range.FindPropertyRelative("_max");
30+
31+
SerializedProperty clampMin = clamp.FindPropertyRelative("_min");
32+
SerializedProperty clampMax = clamp.FindPropertyRelative("_max");
33+
34+
rangeMin.floatValue = Mathf.Clamp(rangeMin.floatValue, clampMin.floatValue, clampMax.floatValue);
35+
rangeMax.floatValue = Mathf.Clamp(rangeMax.floatValue, clampMin.floatValue, clampMax.floatValue);
36+
37+
EditorGUI.EndProperty();
38+
}
39+
40+
private Rect RangeProperty(Rect position, SerializedProperty property)
41+
{
42+
position.y += EditorGUIUtility.singleLineHeight;
43+
position.y += EditorGUIUtility.standardVerticalSpacing;
44+
45+
EditorGUI.BeginProperty(position, new GUIContent(property.displayName), property);
46+
47+
int indentLevel = EditorGUI.indentLevel;
48+
EditorGUI.indentLevel++;
49+
50+
Rect field = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), new GUIContent(property.displayName));
51+
52+
EditorGUI.indentLevel = 0;
53+
54+
field = FloatField(field, property.FindPropertyRelative("_min"));
55+
field = FloatField(field, property.FindPropertyRelative("_max"));
56+
57+
EditorGUI.indentLevel = indentLevel;
58+
EditorGUI.EndProperty();
59+
60+
return position;
61+
}
62+
63+
private Rect FloatField(Rect position, SerializedProperty property)
64+
{
65+
Rect field = EditorGUIUtility.GetFieldRect(position, 2);
66+
position.x += field.width + EditorGUIUtility.standardHorizontalSpacing;
67+
68+
EditorGUI.BeginChangeCheck();
69+
70+
float value = EditorGUIUtility.FieldWrapper(property.displayName, (label) => {
71+
return EditorGUI.FloatField(field, label, property.floatValue);
72+
});
73+
74+
if (EditorGUI.EndChangeCheck()) {
75+
property.floatValue = value;
76+
}
77+
78+
return position;
79+
}
80+
81+
}
82+
83+
}

Editor/PropertyDrawers/PropertyDrawerUtility.cs.meta renamed to Editor/ClampedRangePropertyDrawer.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/ColorRangePropertyDrawer.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Zigurous.DataStructures.Editor
5+
{
6+
[CustomPropertyDrawer(typeof(ColorRange))]
7+
public class ColorRangePropertyDrawer : PropertyDrawer
8+
{
9+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
10+
{
11+
EditorGUI.BeginProperty(position, label, property);
12+
13+
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
14+
15+
int indentLevel = EditorGUI.indentLevel;
16+
EditorGUI.indentLevel = 0;
17+
18+
position = ColorField(position, property.FindPropertyRelative("_min"));
19+
position = ColorField(position, property.FindPropertyRelative("_max"));
20+
21+
EditorGUI.indentLevel = indentLevel;
22+
EditorGUI.EndProperty();
23+
}
24+
25+
private Rect ColorField(Rect position, SerializedProperty property)
26+
{
27+
Rect field = EditorGUIUtility.GetFieldRect(position, 2);
28+
position.x += field.width + EditorGUIUtility.standardHorizontalSpacing;
29+
30+
EditorGUI.BeginChangeCheck();
31+
32+
Color value = EditorGUIUtility.FieldWrapper(property.displayName, (label) => {
33+
return EditorGUI.ColorField(field, label, property.colorValue);
34+
});
35+
36+
if (EditorGUI.EndChangeCheck()) {
37+
property.colorValue = value;
38+
}
39+
40+
return position;
41+
}
42+
43+
}
44+
45+
}

Editor/PropertyDrawers/BasePropertyDrawer.cs.meta renamed to Editor/ColorRangePropertyDrawer.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/EditorGUIUtility.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Zigurous.DataStructures.Editor
5+
{
6+
internal static class EditorGUIUtility
7+
{
8+
internal const float standardHorizontalSpacing = 4f;
9+
10+
internal static float singleLineHeight => UnityEditor.EditorGUIUtility.singleLineHeight;
11+
internal static float standardVerticalSpacing => UnityEditor.EditorGUIUtility.standardVerticalSpacing;
12+
13+
internal static Rect GetFieldRect(Rect position, int columns)
14+
{
15+
float spacing = standardHorizontalSpacing * (columns - 1);
16+
position.width = (position.width - spacing) / columns;
17+
return position;
18+
}
19+
20+
internal static T FieldWrapper<T>(string label, System.Func<GUIContent, T> draw)
21+
{
22+
GUIContent content = new GUIContent(label);
23+
24+
float labelWidth = EditorStyles.label.CalcSize(content).x;
25+
float originalWidth = UnityEditor.EditorGUIUtility.labelWidth;
26+
27+
UnityEditor.EditorGUIUtility.labelWidth = labelWidth;
28+
29+
T value = draw(content);
30+
31+
UnityEditor.EditorGUIUtility.labelWidth = originalWidth;
32+
33+
return value;
34+
}
35+
36+
}
37+
38+
}

Editor/PropertyDrawers/PropertyField.cs.meta renamed to Editor/EditorGUIUtility.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)