Skip to content

Commit

Permalink
FIX: GamepadButton.LeftTrigger and GamepadButton.RightTrigger enum re…
Browse files Browse the repository at this point in the history
…solving to wrong integers (ISXB-1270) (#2100)
  • Loading branch information
adrian-koretski-unity3d authored Jan 24, 2025
1 parent f156ad3 commit ff2ac9d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
1 change: 1 addition & 0 deletions Packages/com.unity.inputsystem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ however, it has to be formatted properly to pass verification tests.
## [1.12.0] - 2025-01-15

### Fixed
- Fixed GamepadButton.LeftTrigger and GamepadButton.RightTrigger enum values not matching displayed dropdown values in editor when using GamepadButtonPropertyDrawer [ISXB-1270](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1270).
- Fixed an issue causing the Action context menu to not show on right click when right clicking an action in the Input Action Editor [ISXB-1134](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1134).
- Reverted changes from 0ddd534d8 (ISXB-746) which introduced a regression [ISXB-1127](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1127).
- Fixed `ArgumentNullException: Value cannot be null.` during the migration of Project-wide Input Actions from `InputManager.asset` to `InputSystem_Actions.inputactions` asset which lead do the lost of the configuration [ISXB-1105](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1105).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#if UNITY_EDITOR

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

#if UNITY_EDITOR
namespace UnityEngine.InputSystem.Editor
{
/// <summary>
Expand All @@ -32,17 +30,17 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten

if (property.propertyType == SerializedPropertyType.Enum)
{
property.intValue = EditorGUI.Popup(position, label.text, property.intValue, m_EnumDisplayNames);
property.intValue = m_EnumValues[EditorGUI.Popup(position, label.text, GetEnumIndex(property.intValue), m_EnumDisplayNames)];
}

EditorGUI.EndProperty();
}

private void CreateEnumList()
{
var enumNamesAndValues = new Dictionary<string, int>();
var enumDisplayNames = Enum.GetNames(typeof(GamepadButton));
var enumValues = Enum.GetValues(typeof(GamepadButton)).Cast<GamepadButton>().ToArray();
string[] enumDisplayNames = Enum.GetNames(typeof(GamepadButton));
var enumValues = Enum.GetValues(typeof(GamepadButton));
var enumNamesAndValues = new Dictionary<string, int>(enumDisplayNames.Length);

for (var i = 0; i < enumDisplayNames.Length; ++i)
{
Expand Down Expand Up @@ -76,12 +74,36 @@ private void CreateEnumList()
}
enumNamesAndValues.Add(enumName, (int)enumValues.GetValue(i));
}
var sortedEntries = enumNamesAndValues.OrderBy(x => x.Value);
SetEnumDisplayNames(enumNamesAndValues);
}

// Sorts the values so that they get displayed consistently, and assigns them for being drawn.
private void SetEnumDisplayNames(Dictionary<string, int> enumNamesAndValues)
{
m_EnumValues = new int[enumNamesAndValues.Count];
enumNamesAndValues.Values.CopyTo(m_EnumValues, 0);

m_EnumDisplayNames = new string[enumNamesAndValues.Count];
enumNamesAndValues.Keys.CopyTo(m_EnumDisplayNames, 0);

m_EnumDisplayNames = sortedEntries.Select(x => x.Key).ToArray();
Array.Sort(m_EnumValues, m_EnumDisplayNames);
}

// Ensures mapping between displayed value and actual value is consistent. Issues arise when there are gaps in the enum values (ie 0, 1, 13).
private int GetEnumIndex(int enumValue)
{
for (int i = 0; i < m_EnumValues.Length; i++)
{
if (enumValue == m_EnumValues[i])
{
return i;
}
}
return 0;
}

private int[] m_EnumValues;
private string[] m_EnumDisplayNames;
}
}
#endif // UNITY_EDITOR
#endif // UNITY_EDITOR

0 comments on commit ff2ac9d

Please sign in to comment.