From eccccc9e7324b9fad8b4b8fd133031cc5d9ed134 Mon Sep 17 00:00:00 2001
From: Dmytro Ivanov <dmytro@unity3d.com>
Date: Mon, 13 Feb 2023 16:05:33 +0100
Subject: [PATCH] Add mouse indexer for enum

---
 Assets/Tests/InputSystem/CoreTests_Devices.cs | 15 ++++++++++++
 Packages/com.unity.inputsystem/CHANGELOG.md   |  3 +++
 .../InputSystem/Devices/Mouse.cs              | 24 +++++++++++++++++++
 3 files changed, 42 insertions(+)

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<Mouse>();
+
+        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
         /// </summary>
         /// <value>Control representing the mouse click count.</value>
         public IntegerControl clickCount { get; protected set;  }
+        
+        /// <summary>
+        /// Retrieve a mouse button by its <see cref="MouseButton"/> enumeration constant.
+        /// </summary>
+        /// <param name="button">Button to retrieve.</param>
+        /// <exception cref="ArgumentOutOfRangeException"><paramref name="button"/> is not a valid mouse
+        /// button value.</exception>
+        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);
+                }
+            }
+        }
 
         /// <summary>
         /// The mouse that was added or updated last or null if there is no mouse