Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mouse indexer for enum #1640

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Assets/Tests/InputSystem/CoreTests_Devices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
3 changes: 3 additions & 0 deletions Packages/com.unity.inputsystem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions Packages/com.unity.inputsystem/InputSystem/Devices/Mouse.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Runtime.InteropServices;
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.Layouts;
Expand Down Expand Up @@ -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
Expand Down