Skip to content
Merged
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
82 changes: 82 additions & 0 deletions Assets/Tests/InputSystem/CoreTests_Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,88 @@ public void Events_OnAnyButtonPressed_FiltersOutOtherControls()
Assert.That(callCount, Is.EqualTo(1));
}

[Test]
[Category("Events")]
public void Events_OnAnyButtonPressed_WorksWithTouchControls()
{
InputSystem.settings.defaultButtonPressPoint = 0.5f;

var touch = InputSystem.AddDevice<Touchscreen>();

var callCount = 0;

InputSystem.onAnyButtonPress
.Call(ctrl =>
{
Assert.That(ctrl, Is.SameAs(touch.touches[0].press));
++callCount;
});


Assert.That(callCount, Is.Zero);

InputSystem.Update();

SetTouch(0, TouchPhase.Began, new Vector2(12, 12));
InputSystem.Update();
Assert.That(callCount, Is.EqualTo(1));

// TouchPhase.Moved must not register as a new button press.
SetTouch(0, TouchPhase.Moved, new Vector2(13, 12), new Vector2(1, 0));
InputSystem.Update();
Assert.That(callCount, Is.EqualTo(1));

// TouchPhase.Canceled must not register as a new button press.
SetTouch(0, TouchPhase.Canceled, new Vector2(13, 12));
InputSystem.Update();
Assert.That(callCount, Is.EqualTo(1));
}

[Test]
[Category("Events")]
public void Events_OnAnyButtonPressed_WorksWithMultitouchTouchControls()
{
InputSystem.settings.defaultButtonPressPoint = 0.5f;

var touch = InputSystem.AddDevice<Touchscreen>();

var callCount = 0;

InputSystem.onAnyButtonPress
.Call(ctrl =>
{
++callCount;
});

Assert.That(callCount, Is.Zero);

InputSystem.Update();
Comment thread
ritamerkl marked this conversation as resolved.

SetTouch(1, TouchPhase.Began, new Vector2(10, 10), screen: touch);
InputSystem.Update();
Assert.That(callCount, Is.EqualTo(1));

SetTouch(1, TouchPhase.Moved, new Vector2(11, 10), new Vector2(1, 0), screen: touch);
InputSystem.Update();
Assert.That(callCount, Is.EqualTo(1));

SetTouch(2, TouchPhase.Began, new Vector2(100, 100), screen: touch);
InputSystem.Update();
Assert.That(callCount, Is.EqualTo(2));

SetTouch(2, TouchPhase.Moved, new Vector2(101, 100), new Vector2(1, 0), screen: touch);
InputSystem.Update();
Assert.That(callCount, Is.EqualTo(2));

SetTouch(1, TouchPhase.Canceled, new Vector2(11, 10), screen: touch);
InputSystem.Update();
Assert.That(callCount, Is.EqualTo(2));

SetTouch(2, TouchPhase.Canceled, new Vector2(101, 100), screen: touch);
InputSystem.Update();
Assert.That(callCount, Is.EqualTo(2));
}

[Test]
[Category("Events")]
public void Events_OnAnyButtonPressed_FiltersOutNonStateEvents()
Expand Down
1 change: 1 addition & 0 deletions Packages/com.unity.inputsystem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed `buttonSouth` returning the state of the east button (and so on for all the compass named buttons) when using a Nintendo Switch Pro Controller on iOS [ISXB-1632](issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1632)
- Fixed `aButton` returning the state of the east button (and so on for all the letter named buttons) when using a Nintendo Switch Pro Controller on Standalone & iOS [ISXB-1632](issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1632)
- Fixed an issue where `UIToolkit` `ClickEvent` could be fired on Android after device rotation due to inactive touch state being replayed during action initial state checks [UUM-100125](https://jira.unity3d.com/browse/UUM-100125).
- Fixed InputSystem.onAnyButtonPress fails to trigger when the device receives a touch [UUM-137930](https://issuetracker.unity3d.com/product/unity/issues/guid/UUM-137930).
- Fixed an incorrect ArraysHelper.HaveDuplicateReferences implementation that didn't use its arguments right [ISXB-1792] (https://github.com/Unity-Technologies/InputSystem/pull/2376)

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ public static bool HasButtonPress(this InputEventPtr eventPtr, float magnitude =
/// in the devices state memory. For example, in the gamepad state, button north (bit position 4) will be evaluated before button
/// east (bit position 5), so if both buttons were pressed in the given event, button north would be returned.
/// Note that the function returns null if the <paramref name="eventPtr"/> is not a StateEvent or DeltaStateEvent.</remarks>
public static InputControl GetFirstButtonPressOrNull(this InputEventPtr eventPtr, float magnitude = -1, bool buttonControlsOnly = true)
public static unsafe InputControl GetFirstButtonPressOrNull(this InputEventPtr eventPtr, float magnitude = -1, bool buttonControlsOnly = true)
Comment thread
ritamerkl marked this conversation as resolved.
{
if (eventPtr.type != StateEvent.Type && eventPtr.type != DeltaStateEvent.Type)
return null;
Expand All @@ -1114,7 +1114,13 @@ public static InputControl GetFirstButtonPressOrNull(this InputEventPtr eventPtr

foreach (var control in eventPtr.EnumerateControls(Enumerate.IgnoreControlsInDefaultState, magnitudeThreshold: magnitude))
{
if (!control.HasValueChangeInEvent(eventPtr))
// Skip if the value didn't change. For IInputStateCallbackReceiver devices (e.g. Touchscreen),
// the event may not carry full device state, so fall back to checking the control was at
// default (not pressed) before this event.
var stateInEvent = control.GetStatePtrFromStateEvent(eventPtr);
Comment thread
ritamerkl marked this conversation as resolved.
var currentState = control.currentStatePtr;
if (stateInEvent != null ? !control.CompareValue(currentState, stateInEvent)
: control.CompareValue(currentState, control.defaultStatePtr))
Comment thread
ritamerkl marked this conversation as resolved.
continue;
if (buttonControlsOnly && !control.isButton)
continue;
Expand Down
Loading