Skip to content

Commit 8bc67b6

Browse files
committed
Fixes #195: EnumToggleButtons prefers InspectorName attribute for display names
1 parent 715def5 commit 8bc67b6

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

Editor.Extras/Drawers/EnumToggleButtonsDrawer.cs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,35 @@ public override TriElement CreateElement(TriProperty property, TriElement next)
3232
private sealed class EnumToggleButtonsElement : TriElement
3333
{
3434
private readonly TriProperty _property;
35-
private readonly List<KeyValuePair<string, Enum>> _enumValues;
35+
private readonly List<EnumEntry> _enumValues;
3636
private readonly bool _isFlags;
3737

3838
public EnumToggleButtonsElement(TriProperty property)
3939
{
4040
_property = property;
4141
_enumValues = Enum.GetNames(property.FieldType)
42-
.Zip(Enum.GetValues(property.FieldType).OfType<Enum>(),
43-
(name, value) => new KeyValuePair<string, Enum>(name, value))
42+
.Zip(Enum.GetValues(property.FieldType).OfType<Enum>(), (name, value) => new EnumEntry
43+
{
44+
name = name,
45+
value = value,
46+
displayName = ObjectNames.NicifyVariableName(name),
47+
})
4448
.ToList();
4549
_isFlags = property.FieldType.GetCustomAttributes(typeof(FlagsAttribute), false).Length > 0;
4650

47-
_enumValues.Sort(new DeclarationOrderComparer(property.FieldType));
51+
var enumFields = property.FieldType.GetFields(BindingFlags.Static | BindingFlags.Public);
52+
53+
foreach (var enumValue in _enumValues)
54+
{
55+
var enumField = Array.Find(enumFields, it => it.Name == enumValue.name);
56+
var inspectorNameAttr = enumField?.GetCustomAttribute<InspectorNameAttribute>();
57+
if (inspectorNameAttr != null)
58+
{
59+
enumValue.displayName = inspectorNameAttr.displayName;
60+
}
61+
}
62+
63+
_enumValues.Sort(new DeclarationOrderComparer(enumFields));
4864
}
4965

5066
public override float GetHeight(float width)
@@ -65,11 +81,11 @@ public override void OnGUI(Rect position)
6581
{
6682
var itemRect = SplitRectWidth(position, _enumValues.Count, i);
6783
var itemStyle = GetButtonStyle(_enumValues.Count, i);
68-
var itemName = _enumValues[i].Key;
69-
var itemValue = _enumValues[i].Value;
84+
var itemDisplayName = _enumValues[i].displayName;
85+
var itemValue = _enumValues[i].value;
7086

7187
var oldSelected = value != null && (_isFlags ? value.HasFlag(itemValue) : value.Equals(itemValue));
72-
var newSelected = GUI.Toggle(itemRect, oldSelected, itemName, itemStyle);
88+
var newSelected = GUI.Toggle(itemRect, oldSelected, itemDisplayName, itemStyle);
7389

7490
if (oldSelected != newSelected)
7591
{
@@ -121,19 +137,26 @@ private static Rect SplitRectWidth(Rect rect, int total, int current)
121137
return rect;
122138
}
123139

124-
private class DeclarationOrderComparer : IComparer<KeyValuePair<string, Enum>>
140+
private class EnumEntry
141+
{
142+
public string name;
143+
public string displayName;
144+
public Enum value;
145+
}
146+
147+
private class DeclarationOrderComparer : IComparer<EnumEntry>
125148
{
126149
private readonly FieldInfo[] _fields;
127150

128-
public DeclarationOrderComparer(Type enumType)
151+
public DeclarationOrderComparer(FieldInfo[] fields)
129152
{
130-
_fields = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
153+
_fields = fields;
131154
}
132155

133-
public int Compare(KeyValuePair<string, Enum> x, KeyValuePair<string, Enum> y)
156+
public int Compare(EnumEntry x, EnumEntry y)
134157
{
135-
var orderX = Array.FindIndex(_fields, it => it.Name == x.Key);
136-
var orderY = Array.FindIndex(_fields, it => it.Name == y.Key);
158+
var orderX = Array.FindIndex(_fields, it => it.Name == x.name);
159+
var orderY = Array.FindIndex(_fields, it => it.Name == y.name);
137160
return orderX.CompareTo(orderY);
138161
}
139162
}

0 commit comments

Comments
 (0)