Skip to content

Commit eccccc9

Browse files
author
Dmytro Ivanov
committed
Add mouse indexer for enum
1 parent 5ffb592 commit eccccc9

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

Assets/Tests/InputSystem/CoreTests_Devices.cs

+15
Original file line numberDiff line numberDiff line change
@@ -5731,4 +5731,19 @@ public unsafe void Devices_DoesntErrorOutOnMaxTouchCount()
57315731
BeginTouch(i, new Vector2(i * 1.0f, i * 2.0f), time: 0);
57325732
}, Throws.Nothing);
57335733
}
5734+
5735+
[Test]
5736+
[Category("Devices")]
5737+
public void Devices_CanQueryMouseButtonsViaEnum()
5738+
{
5739+
var mouse = InputSystem.AddDevice<Mouse>();
5740+
5741+
foreach (MouseButton btn in Enum.GetValues(typeof(MouseButton)))
5742+
{
5743+
InputSystem.QueueStateEvent(mouse, new MouseState().WithButton(btn));
5744+
InputSystem.Update();
5745+
5746+
Assert.That(mouse[btn].isPressed, Is.True);
5747+
}
5748+
}
57345749
}

Packages/com.unity.inputsystem/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ however, it has to be formatted properly to pass verification tests.
1414

1515
- Fixed unclosed profiler marker in `InvokeCallbacksSafe_AnyCallbackReturnsTrue` which would lead to eventually broken profiler traces in some cases like using `PlayerInput` (case ISXB-393).
1616

17+
### Added
18+
- 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).
19+
1720
## [1.5.0] - 2023-01-24
1821

1922
### Added

Packages/com.unity.inputsystem/InputSystem/Devices/Mouse.cs

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Runtime.InteropServices;
23
using UnityEngine.InputSystem.Controls;
34
using UnityEngine.InputSystem.Layouts;
@@ -224,6 +225,29 @@ public class Mouse : Pointer, IInputStateCallbackReceiver
224225
/// </summary>
225226
/// <value>Control representing the mouse click count.</value>
226227
public IntegerControl clickCount { get; protected set; }
228+
229+
/// <summary>
230+
/// Retrieve a mouse button by its <see cref="MouseButton"/> enumeration constant.
231+
/// </summary>
232+
/// <param name="button">Button to retrieve.</param>
233+
/// <exception cref="ArgumentOutOfRangeException"><paramref name="button"/> is not a valid mouse
234+
/// button value.</exception>
235+
public ButtonControl this[MouseButton button]
236+
{
237+
get
238+
{
239+
switch (button)
240+
{
241+
case MouseButton.Left: return leftButton;
242+
case MouseButton.Right: return rightButton;
243+
case MouseButton.Middle: return middleButton;
244+
case MouseButton.Forward: return forwardButton;
245+
case MouseButton.Back: return backButton;
246+
default:
247+
throw new ArgumentOutOfRangeException(nameof(button), button, null);
248+
}
249+
}
250+
}
227251

228252
/// <summary>
229253
/// The mouse that was added or updated last or null if there is no mouse

0 commit comments

Comments
 (0)