Skip to content
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 @@ -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<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 @@ -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
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 @@ -245,6 +246,29 @@
/// the system-defined click time threshold.
/// </summary>
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);

Check warning on line 268 in Packages/com.unity.inputsystem/InputSystem/Devices/Mouse.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

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

Added line #L268 was not covered by tests
}
}
}

/// <summary>
/// The mouse that was added or updated last or null if there is no mouse
Expand Down
Loading