diff --git a/Assets/Tests/InputSystem/CoreTests_Devices.cs b/Assets/Tests/InputSystem/CoreTests_Devices.cs index 96c5f2b366..9695bc7804 100644 --- a/Assets/Tests/InputSystem/CoreTests_Devices.cs +++ b/Assets/Tests/InputSystem/CoreTests_Devices.cs @@ -5731,4 +5731,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 36548dfd48..f24fa523b2 100644 --- a/Packages/com.unity.inputsystem/CHANGELOG.md +++ b/Packages/com.unity.inputsystem/CHANGELOG.md @@ -14,6 +14,9 @@ however, it has to be formatted properly to pass verification tests. - Fixed unclosed profiler marker in `InvokeCallbacksSafe_AnyCallbackReturnsTrue` which would lead to eventually broken profiler traces in some cases like using `PlayerInput` (case ISXB-393). +### 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 552b3cb78e..28c69eda6c 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; @@ -224,6 +225,29 @@ public class Mouse : Pointer, IInputStateCallbackReceiver /// /// Control representing the mouse click count. 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