Skip to content

Commit

Permalink
Fix flags support for EnumToggleButtons
Browse files Browse the repository at this point in the history
  • Loading branch information
vanifatovvlad committed Feb 16, 2023
1 parent 2018df9 commit 10cb1fc
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions Editor.Extras/Drawers/EnumToggleButtonsDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override float GetHeight(float width)
public override void OnGUI(Rect position)
{
var value = _property.TryGetSerializedProperty(out var serializedProperty)
? (Enum) Enum.ToObject(_property.FieldType, serializedProperty.intValue)
? (Enum) Enum.ToObject(_property.FieldType, serializedProperty.longValue)
: (Enum) _property.Value;

var controlId = GUIUtility.GetControlID(FocusType.Passive);
Expand All @@ -68,11 +68,23 @@ public override void OnGUI(Rect position)
var itemName = _enumValues[i].Key;
var itemValue = _enumValues[i].Value;

var selected = value != null && (_isFlags ? value.HasFlag(itemValue) : value.Equals(itemValue));
var oldSelected = value != null && (_isFlags ? value.HasFlag(itemValue) : value.Equals(itemValue));
var newSelected = GUI.Toggle(itemRect, oldSelected, itemName, itemStyle);

if (selected != GUI.Toggle(itemRect, selected, itemName, itemStyle))
if (oldSelected != newSelected)
{
_property.SetValue(itemValue);
if (_isFlags)
{
var newValue = newSelected
? (Convert.ToInt64(value) | Convert.ToInt64(itemValue))
: (Convert.ToInt64(value) & ~Convert.ToInt64(itemValue));

_property.SetValue((Enum) Enum.ToObject(_property.FieldType, newValue));
}
else
{
_property.SetValue(itemValue);
}
}
}
}
Expand Down

0 comments on commit 10cb1fc

Please sign in to comment.