diff --git a/Assets/Tests/InputSystem/CoreTests_Devices.cs b/Assets/Tests/InputSystem/CoreTests_Devices.cs index 7c4006f6ee..7c5dbcb555 100644 --- a/Assets/Tests/InputSystem/CoreTests_Devices.cs +++ b/Assets/Tests/InputSystem/CoreTests_Devices.cs @@ -5885,4 +5885,19 @@ public unsafe void Devices_DoesntErrorOutOnMaxTouchCount() BeginTouch(i, new Vector2(i * 1.0f, i * 2.0f), time: 0); }, Throws.Nothing); } + + [Test] + [Category("Devices")] + public void Devices_CanQueryMouseButtonsViaEnum() + { + var mouse = InputSystem.AddDevice(); + + foreach (MouseButton btn in Enum.GetValues(typeof(MouseButton))) + { + InputSystem.QueueStateEvent(mouse, new MouseState().WithButton(btn)); + InputSystem.Update(); + + Assert.That(mouse[btn].isPressed, Is.True); + } + } } diff --git a/Packages/com.unity.inputsystem/CHANGELOG.md b/Packages/com.unity.inputsystem/CHANGELOG.md index aee42048d6..eaa28cd129 100644 --- a/Packages/com.unity.inputsystem/CHANGELOG.md +++ b/Packages/com.unity.inputsystem/CHANGELOG.md @@ -516,6 +516,9 @@ however, it has to be formatted properly to pass verification tests. - Fixed issue leading to a stack overflow crash during device initialization in `InsertControlBitRangeNode` (case ISXB-405). - Fixed the issue where saving and loading override bindings to JSON would set unassigned overrides (that were `null`) to assigned overrides (as an empty string `""`). +### Added +- Ability to query mouse buttons via enum, for example `mouse[MouseButton.Left]`. Based on the user contribution from [Drahsid](https://github.com/Drahsid) in [#1273](https://github.com/Unity-Technologies/InputSystem/pull/1273). + ## [1.5.0] - 2023-01-24 ### Added diff --git a/Packages/com.unity.inputsystem/InputSystem/Devices/Mouse.cs b/Packages/com.unity.inputsystem/InputSystem/Devices/Mouse.cs index 1740b9e3c2..50dd2972cf 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Devices/Mouse.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Devices/Mouse.cs @@ -1,3 +1,4 @@ +using System; using System.Runtime.InteropServices; using UnityEngine.InputSystem.Controls; using UnityEngine.InputSystem.Layouts; @@ -245,6 +246,29 @@ public class Mouse : Pointer, IInputStateCallbackReceiver /// the system-defined click time threshold. /// public IntegerControl clickCount { get; protected set; } + + /// + /// Retrieve a mouse button by its enumeration constant. + /// + /// Button to retrieve. + /// is not a valid mouse + /// button value. + public ButtonControl this[MouseButton button] + { + get + { + switch (button) + { + case MouseButton.Left: return leftButton; + case MouseButton.Right: return rightButton; + case MouseButton.Middle: return middleButton; + case MouseButton.Forward: return forwardButton; + case MouseButton.Back: return backButton; + default: + throw new ArgumentOutOfRangeException(nameof(button), button, null); + } + } + } /// /// The mouse that was added or updated last or null if there is no mouse