Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: ISXB-543 GamepadButton property drawer fix #1862

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
1 change: 1 addition & 0 deletions Packages/com.unity.inputsystem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ however, it has to be formatted properly to pass verification tests.
- Fixed Project Settings header title styling for Input Actions editor.
- Fixed Input Actions Editor losing reference to current ControlScheme upon entering Play Mode [ISXB-770](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-770).
- Fixed headers in InputActionEditor windows becoming squashed when there is a large number of Action Maps/Actions.
- Fixed an issue where aliased enum values for ´GamepadButton´ would be seemingly randomly picked from an Inspector value picker. This is resolved with a custom property drawer that maps to a designated aliased type. [ISXB-543](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-543).

## [1.8.0-pre.2] - 2023-11-09

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#if UNITY_EDITOR

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine.UIElements;

namespace UnityEngine.InputSystem.Editor
{
/// <summary>
/// Abstract base class for a generic property drawer for aliased enums.
/// </summary>
internal abstract class AliasedEnumPropertyDrawer<T> : PropertyDrawer where T : Enum
{
private string[] m_EnumDisplayNames;

public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
ProcessDisplayNamesForAliasedEnums();
return base.CreatePropertyGUI(property);
}

protected abstract string GetNonAliasedNames(string enumValue);

private void ProcessDisplayNamesForAliasedEnums()
{
var enumNamesAndValues = new Dictionary<string, int>();
var enumDisplayNames = Enum.GetNames(typeof(T));
var enumValues = Enum.GetValues(typeof(T)).Cast<T>().ToArray();
var enumStringValues = enumValues.Select(v => v.ToString()).ToArray();

for (var i = 0; i < enumDisplayNames.Length; ++i)
{
var enumName = enumDisplayNames[i];
var aliasedName = GetNonAliasedNames(enumStringValues[i]);
if (!string.IsNullOrEmpty(aliasedName) && enumName != aliasedName)
enumName = $"{enumName} ({aliasedName})";

enumNamesAndValues.Add(enumName, (int)enumValues.GetValue(i));
}

var sortedEntries = enumNamesAndValues
.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();
}
}
}
#endif // UNITY_EDITOR

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#if UNITY_EDITOR

using UnityEditor;
using UnityEngine.InputSystem.LowLevel;

namespace UnityEngine.InputSystem.Editor
{
/// <summary>
/// Property drawer for <see cref = "GamepadButton" />
/// </summary >
[CustomPropertyDrawer(typeof(GamepadButton))]
internal class GpadButtonPropertyDrawer : AliasedEnumPropertyDrawer<GamepadButton>
{
protected override string GetNonAliasedNames(string name)
{
switch (name)
{
case nameof(GamepadButton.North):
case nameof(GamepadButton.Y):
case nameof(GamepadButton.Triangle):
return nameof(GamepadButton.North);

case nameof(GamepadButton.South):
case nameof(GamepadButton.A):
case nameof(GamepadButton.Cross):
return nameof(GamepadButton.South);

case nameof(GamepadButton.East):
case nameof(GamepadButton.B):
case nameof(GamepadButton.Circle):
return nameof(GamepadButton.East);

case nameof(GamepadButton.West):
case nameof(GamepadButton.X):
case nameof(GamepadButton.Square):
return nameof(GamepadButton.West);

default: return string.Empty;
}
}
}
}
#endif // UNITY_EDITOR

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.