From 55b2c21f75a2f402e8af86fd763aa1da2b787cf2 Mon Sep 17 00:00:00 2001 From: Pushpalatha Pandi Date: Tue, 27 Feb 2024 12:48:18 +0530 Subject: [PATCH] [ISXB-544] Fix for 1.6.X: Wrong value is chosen when selecting GamepadButton in a dropdown menu --- .../PropertyDrawers/EnumPropertyDrawer.cs | 98 +++++++++++++++++++ .../EnumPropertyDrawer.cs.meta | 2 + 2 files changed, 100 insertions(+) create mode 100644 Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/EnumPropertyDrawer.cs create mode 100644 Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/EnumPropertyDrawer.cs.meta diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/EnumPropertyDrawer.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/EnumPropertyDrawer.cs new file mode 100644 index 0000000000..155d18fa4e --- /dev/null +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/EnumPropertyDrawer.cs @@ -0,0 +1,98 @@ +#if UNITY_EDITOR +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEditor; +using UnityEngine.InputSystem.LowLevel; +using UnityEngine.UIElements; + +namespace UnityEngine.InputSystem.Editor +{ + /// + /// Abstract base class for a generic property drawer for enums. + /// + internal abstract class EnumDrawer : PropertyDrawer where T : Enum + { + protected string[] m_EnumDisplayNames; + + public override VisualElement CreatePropertyGUI(SerializedProperty property) + { + ProcessDisplayNamesForAliasedEnums(); + return base.CreatePropertyGUI(property); + } + + protected abstract string GetNonAliasedNames(string enumValue); + + private void ProcessDisplayNamesForAliasedEnums() + { + Dictionary dictEnumNamesAndValues = new Dictionary(); + string[] enumDisplayNames = Enum.GetNames(typeof(T)); + Array enumValues = Enum.GetValues(typeof(T)); + List enumStringValues = enumValues.Cast().Select(v => v.ToString()).ToList(); + + for (int iEnumCounter = 0; iEnumCounter < enumDisplayNames.Length; iEnumCounter++) + { + string enumName = enumDisplayNames[iEnumCounter]; + string aliasedName = GetNonAliasedNames(enumStringValues[iEnumCounter]); + + if (!string.IsNullOrEmpty(aliasedName) && enumName != aliasedName) + { + enumName = enumName + " (" + aliasedName + ")"; + } + + dictEnumNamesAndValues.Add(enumName, (int)enumValues.GetValue(iEnumCounter)); + } + + var sortedEntries = dictEnumNamesAndValues.OrderBy(x => x.Value).ThenBy(x => x.Key.Contains("(")); + + m_EnumDisplayNames = sortedEntries.Select(x => x.Key).ToArray(); + } + + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + { + EditorGUI.BeginProperty(position, label, property); + + if (property.propertyType == SerializedPropertyType.Enum) + { + property.enumValueIndex = EditorGUI.Popup(position, label.text, property.enumValueIndex, m_EnumDisplayNames); + } + + EditorGUI.EndProperty(); + } + } + + /// + ///Property drawer for .s + //// + [CustomPropertyDrawer(typeof(GamepadButton))] + internal class GpadButtonDrawer : EnumDrawer + { + protected override string GetNonAliasedNames(string gpadValue) + { + switch(gpadValue) + { + case "North": + case "Y": + case "Triangle": + return "North"; + + case "South": + case "A": + case "Cross": + return "South"; + case "East": + case "B": + case "Circle": + return "East"; + case "West": + case "X": + case "Square": + return "West"; + + + default: return string.Empty; + } + } + } +} +#endif // UNITY_EDITOR \ No newline at end of file diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/EnumPropertyDrawer.cs.meta b/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/EnumPropertyDrawer.cs.meta new file mode 100644 index 0000000000..aac5eaba1b --- /dev/null +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/EnumPropertyDrawer.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 496af666e7143fa49b0a653256e529b2 \ No newline at end of file