Skip to content

Commit 3388da5

Browse files
Darren-Kelly-UnityekcohPauliusd01suearkinunityMorganHoarau
authored
FIX: uum 138143 by fixing how interactions work & extending their usage for actions that use magnitude (#2411)
Co-authored-by: Håkan Sidenvall <hakan.sidenvall@unity3d.com> Co-authored-by: Paulius Dervinis <54306142+Pauliusd01@users.noreply.github.com> Co-authored-by: Sue Arkin <85237015+suearkinunity@users.noreply.github.com> Co-authored-by: Morgan Hoarau <122548697+MorganHoarau@users.noreply.github.com>
1 parent 71e8103 commit 3388da5

13 files changed

Lines changed: 910 additions & 435 deletions

File tree

Assets/Tests/InputSystem/ActuationPressPointTests.cs

Lines changed: 570 additions & 0 deletions
Large diffs are not rendered by default.

Assets/Tests/InputSystem/ActuationPressPointTests.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Tests/InputSystem/CoreTests_Actions.cs

Lines changed: 128 additions & 218 deletions
Large diffs are not rendered by default.

Assets/Tests/InputSystem/CoreTests_Controls.cs

Lines changed: 28 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ public void Controls_CanHaveStickDeadzones()
214214
InputSystem.RegisterLayout(json);
215215
var device = (Gamepad)InputSystem.AddDevice("MyDevice");
216216

217-
var firstState = new GamepadState {leftStick = new Vector2(0.05f, 0.05f)};
218-
var secondState = new GamepadState {leftStick = new Vector2(0.5f, 0.5f)};
217+
var firstState = new GamepadState { leftStick = new Vector2(0.05f, 0.05f) };
218+
var secondState = new GamepadState { leftStick = new Vector2(0.5f, 0.5f) };
219219

220220
InputSystem.QueueStateEvent(device, firstState);
221221
InputSystem.Update();
@@ -257,12 +257,12 @@ public void Controls_CanHaveAxisDeadzones()
257257
////NOTE: Unfortunately, this relies on an internal method ATM.
258258
var processor = device.leftTrigger.TryGetProcessor<AxisDeadzoneProcessor>();
259259

260-
InputSystem.QueueStateEvent(device, new GamepadState {leftTrigger = 0.05f});
260+
InputSystem.QueueStateEvent(device, new GamepadState { leftTrigger = 0.05f });
261261
InputSystem.Update();
262262

263263
Assert.That(device.leftTrigger.ReadValue(), Is.Zero.Within(0.0001));
264264

265-
InputSystem.QueueStateEvent(device, new GamepadState {leftTrigger = 0.5f});
265+
InputSystem.QueueStateEvent(device, new GamepadState { leftTrigger = 0.5f });
266266
InputSystem.Update();
267267

268268
Assert.That(device.leftTrigger.ReadValue(),
@@ -284,13 +284,13 @@ public void Controls_CanChangeDefaultDeadzoneValuesOnTheFly()
284284
Set(gamepad.leftStick, new Vector2(0.5f, 0.5f));
285285

286286
Assert.That(gamepad.leftStick.ReadValue(),
287-
Is.EqualTo(new StickDeadzoneProcessor {min = 0.1f, max = 0.9f}.Process(new Vector2(0.5f, 0.5f))));
287+
Is.EqualTo(new StickDeadzoneProcessor { min = 0.1f, max = 0.9f }.Process(new Vector2(0.5f, 0.5f))));
288288

289289
InputSystem.settings.defaultDeadzoneMin = 0.2f;
290290
InputSystem.settings.defaultDeadzoneMax = 0.8f;
291291

292292
Assert.That(gamepad.leftStick.ReadValue(),
293-
Is.EqualTo(new StickDeadzoneProcessor {min = 0.2f, max = 0.8f}.Process(new Vector2(0.5f, 0.5f))));
293+
Is.EqualTo(new StickDeadzoneProcessor { min = 0.2f, max = 0.8f }.Process(new Vector2(0.5f, 0.5f))));
294294
}
295295

296296
[Test]
@@ -299,7 +299,7 @@ public void Controls_SticksProvideAccessToHalfAxes()
299299
{
300300
var gamepad = InputSystem.AddDevice<Gamepad>();
301301

302-
InputSystem.QueueStateEvent(gamepad, new GamepadState {leftStick = new Vector2(0.5f, 0.5f)});
302+
InputSystem.QueueStateEvent(gamepad, new GamepadState { leftStick = new Vector2(0.5f, 0.5f) });
303303
InputSystem.Update();
304304

305305
Assert.That(gamepad.leftStick.up.ReadValue(),
@@ -311,7 +311,7 @@ public void Controls_SticksProvideAccessToHalfAxes()
311311
Assert.That(gamepad.leftStick.left.ReadValue(),
312312
Is.EqualTo(new AxisDeadzoneProcessor().Process(0.0f)));
313313

314-
InputSystem.QueueStateEvent(gamepad, new GamepadState {leftStick = new Vector2(-0.5f, -0.5f)});
314+
InputSystem.QueueStateEvent(gamepad, new GamepadState { leftStick = new Vector2(-0.5f, -0.5f) });
315315
InputSystem.Update();
316316

317317
Assert.That(gamepad.leftStick.up.ReadValue(),
@@ -468,7 +468,7 @@ public unsafe void Controls_ValueIsReadFromStateMemoryOnlyWhenControlHasBeenMark
468468
gamepad.ApplyParameterChanges();
469469
Assert.That(gamepad.leftTrigger.value, Is.EqualTo(0.5f));
470470

471-
InputSystem.QueueStateEvent(gamepad, new GamepadState {leftTrigger = 0.75f});
471+
InputSystem.QueueStateEvent(gamepad, new GamepadState { leftTrigger = 0.75f });
472472
InputSystem.Update();
473473

474474
// but this time, we updated state through the system which *does* set the stale flag on controls that
@@ -611,7 +611,7 @@ public void Controls_CanReadValueFromStateEvents()
611611
Assert.That(gamepad.leftTrigger.ReadValueFromEventAsObject(eventPtr), Is.EqualTo(0.234f).Within(0.00001));
612612
};
613613

614-
InputSystem.QueueStateEvent(gamepad, new GamepadState {leftTrigger = 0.234f});
614+
InputSystem.QueueStateEvent(gamepad, new GamepadState { leftTrigger = 0.234f });
615615
InputSystem.Update();
616616

617617
Assert.That(receivedCalls, Is.EqualTo(1));
@@ -758,7 +758,7 @@ public void Controls_DpadVectorsAreCircular()
758758
var gamepad = InputSystem.AddDevice<Gamepad>();
759759

760760
// Up.
761-
InputSystem.QueueStateEvent(gamepad, new GamepadState {buttons = 1 << (int)GamepadButton.DpadUp});
761+
InputSystem.QueueStateEvent(gamepad, new GamepadState { buttons = 1 << (int)GamepadButton.DpadUp });
762762
InputSystem.Update();
763763

764764
Assert.That(gamepad.dpad.ReadValue(), Is.EqualTo(Vector2.up));
@@ -775,7 +775,7 @@ public void Controls_DpadVectorsAreCircular()
775775
Assert.That(gamepad.dpad.ReadValue().y, Is.EqualTo((Vector2.up + Vector2.left).normalized.y).Within(0.00001));
776776

777777
// Left.
778-
InputSystem.QueueStateEvent(gamepad, new GamepadState {buttons = 1 << (int)GamepadButton.DpadLeft});
778+
InputSystem.QueueStateEvent(gamepad, new GamepadState { buttons = 1 << (int)GamepadButton.DpadLeft });
779779
InputSystem.Update();
780780

781781
Assert.That(gamepad.dpad.ReadValue(), Is.EqualTo(Vector2.left));
@@ -792,7 +792,7 @@ public void Controls_DpadVectorsAreCircular()
792792
Assert.That(gamepad.dpad.ReadValue().y, Is.EqualTo((Vector2.down + Vector2.left).normalized.y).Within(0.00001));
793793

794794
// Down.
795-
InputSystem.QueueStateEvent(gamepad, new GamepadState {buttons = 1 << (int)GamepadButton.DpadDown});
795+
InputSystem.QueueStateEvent(gamepad, new GamepadState { buttons = 1 << (int)GamepadButton.DpadDown });
796796
InputSystem.Update();
797797

798798
Assert.That(gamepad.dpad.ReadValue(), Is.EqualTo(Vector2.down));
@@ -811,7 +811,7 @@ public void Controls_DpadVectorsAreCircular()
811811
Is.EqualTo((Vector2.down + Vector2.right).normalized.y).Within(0.00001));
812812

813813
// Right.
814-
InputSystem.QueueStateEvent(gamepad, new GamepadState {buttons = 1 << (int)GamepadButton.DpadRight});
814+
InputSystem.QueueStateEvent(gamepad, new GamepadState { buttons = 1 << (int)GamepadButton.DpadRight });
815815
InputSystem.Update();
816816

817817
Assert.That(gamepad.dpad.ReadValue(), Is.EqualTo(Vector2.right));
@@ -947,6 +947,7 @@ public void Controls_FindControl_FindsControlDespiteTurkishCulture()
947947
Assert.That(matches, Has.Count.EqualTo(1));
948948
Assert.That(matches, Has.Exactly(1).SameAs(gamepad.leftStick));
949949
}
950+
950951
Thread.CurrentThread.CurrentCulture = culture;
951952
}
952953

@@ -973,6 +974,7 @@ public void Controls_CanParseControlPath(string path, params string[] parts)
973974
case "displayName": return a.displayName == nameAndValue[1];
974975
case "wildcard": return a.isWildcard;
975976
}
977+
976978
return false;
977979
});
978980
}), Has.All.True);
@@ -1153,100 +1155,6 @@ public void Controls_CanFindControlsUsingWildcards_InMiddleOfNames()
11531155
}
11541156
}
11551157

1156-
[Test]
1157-
[Category("Controls")]
1158-
public void Controls_CanDetermineIfControlIsPressed()
1159-
{
1160-
InputSystem.settings.defaultButtonPressPoint = 0.5f;
1161-
1162-
var gamepad = InputSystem.AddDevice<Gamepad>();
1163-
1164-
Set(gamepad.leftStick, Vector2.one);
1165-
Set(gamepad.leftTrigger, 0.6f);
1166-
Press(gamepad.buttonSouth);
1167-
1168-
//// https://jira.unity3d.com/browse/ISX-926
1169-
////REVIEW: IsPressed() should probably be renamed. As is apparent from the calls here, it's not always
1170-
//// readily apparent that the way it is defined ("actuation level at least at button press threshold")
1171-
//// does not always connect to what it intuitively means for the specific control.
1172-
1173-
Assert.That(gamepad.leftTrigger.IsPressed(), Is.True);
1174-
Assert.That(gamepad.rightTrigger.IsPressed(), Is.False);
1175-
Assert.That(gamepad.buttonSouth.IsPressed(), Is.True);
1176-
Assert.That(gamepad.buttonNorth.IsPressed(), Is.False);
1177-
Assert.That(gamepad.leftStick.IsPressed(), Is.True); // Note how this diverges from the actual meaning of "is the left stick pressed?"
1178-
Assert.That(gamepad.rightStick.IsPressed(), Is.False);
1179-
1180-
// https://fogbugz.unity3d.com/f/cases/1374024/
1181-
// Calling it on the entire device should be false.
1182-
Assert.That(gamepad.IsPressed(), Is.False);
1183-
}
1184-
1185-
[Test]
1186-
[Category("Controls")]
1187-
public void Controls_CanCustomizeDefaultButtonPressPoint()
1188-
{
1189-
var gamepad = InputSystem.AddDevice<Gamepad>();
1190-
1191-
InputSystem.settings.defaultButtonPressPoint = 0.4f;
1192-
1193-
Set(gamepad.leftTrigger, 0.39f);
1194-
1195-
Assert.That(gamepad.leftTrigger.isPressed, Is.False);
1196-
1197-
Set(gamepad.leftTrigger, 0.4f);
1198-
1199-
Assert.That(gamepad.leftTrigger.isPressed, Is.True);
1200-
1201-
InputSystem.settings.defaultButtonPressPoint = 0.5f;
1202-
1203-
Assert.That(gamepad.leftTrigger.isPressed, Is.False);
1204-
1205-
InputSystem.settings.defaultButtonPressPoint = 0;
1206-
1207-
Assert.That(gamepad.leftTrigger.isPressed, Is.True);
1208-
1209-
// Setting the trigger to 0 requires the system to be "smart" enough to
1210-
// figure out that 0 as a default button press point doesn't make sense
1211-
// and that instead the press point should clamp off at some low, non-zero value.
1212-
// https://fogbugz.unity3d.com/f/cases/1349002/
1213-
Set(gamepad.leftTrigger, 0f);
1214-
1215-
Assert.That(gamepad.leftTrigger.isPressed, Is.False);
1216-
1217-
Set(gamepad.leftTrigger, 0.001f);
1218-
1219-
Assert.That(gamepad.leftTrigger.isPressed, Is.True);
1220-
1221-
InputSystem.settings.defaultButtonPressPoint = -1;
1222-
Set(gamepad.leftTrigger, 0f);
1223-
1224-
Assert.That(gamepad.leftTrigger.isPressed, Is.False);
1225-
}
1226-
1227-
[Test]
1228-
[Category("Controls")]
1229-
public void Controls_CanCustomizePressPointOfGamepadTriggers()
1230-
{
1231-
var json = @"
1232-
{
1233-
""name"" : ""CustomGamepad"",
1234-
""extend"" : ""Gamepad"",
1235-
""controls"" : [
1236-
{
1237-
""name"" : ""rightTrigger"",
1238-
""parameters"" : ""pressPoint=0.2""
1239-
}
1240-
]
1241-
}
1242-
";
1243-
1244-
InputSystem.RegisterLayout(json);
1245-
var gamepad = InputDevice.Build<Gamepad>("CustomGamepad");
1246-
1247-
Assert.That(gamepad.rightTrigger.pressPoint, Is.EqualTo(0.2f).Within(0.0001f));
1248-
}
1249-
12501158
[Test]
12511159
[Category("Controls")]
12521160
public void Controls_DisplayNameDefaultsToControlName()
@@ -1419,7 +1327,7 @@ public void Controls_CanKeepListsOfControls_WithoutAllocatingGCMemory()
14191327
Assert.That(list[3], Is.SameAs(keyboard));
14201328
Assert.That(() => list[4], Throws.TypeOf<ArgumentOutOfRangeException>());
14211329
Assert.That(list.ToArray(),
1422-
Is.EquivalentTo(new InputControl[] {gamepad.leftStick, null, keyboard.spaceKey, keyboard}));
1330+
Is.EquivalentTo(new InputControl[] { gamepad.leftStick, null, keyboard.spaceKey, keyboard }));
14231331
Assert.That(list.Contains(gamepad.leftStick));
14241332
Assert.That(list.Contains(null));
14251333
Assert.That(list.Contains(keyboard.spaceKey));
@@ -1436,27 +1344,27 @@ public void Controls_CanKeepListsOfControls_WithoutAllocatingGCMemory()
14361344
Assert.That(list[0], Is.SameAs(gamepad.leftStick));
14371345
Assert.That(list[1], Is.SameAs(keyboard.spaceKey));
14381346
Assert.That(() => list[2], Throws.TypeOf<ArgumentOutOfRangeException>());
1439-
Assert.That(list.ToArray(), Is.EquivalentTo(new InputControl[] {gamepad.leftStick, keyboard.spaceKey}));
1347+
Assert.That(list.ToArray(), Is.EquivalentTo(new InputControl[] { gamepad.leftStick, keyboard.spaceKey }));
14401348
Assert.That(list.Contains(gamepad.leftStick));
14411349
Assert.That(!list.Contains(null));
14421350
Assert.That(list.Contains(keyboard.spaceKey));
14431351
Assert.That(!list.Contains(keyboard));
14441352

1445-
list.AddRange(new InputControl[] {keyboard.aKey, keyboard.bKey}, count: 1, destinationIndex: 0);
1353+
list.AddRange(new InputControl[] { keyboard.aKey, keyboard.bKey }, count: 1, destinationIndex: 0);
14461354

14471355
Assert.That(list.Count, Is.EqualTo(3));
14481356
Assert.That(list.Capacity, Is.EqualTo(4));
14491357
Assert.That(list,
14501358
Is.EquivalentTo(new InputControl[]
1451-
{keyboard.aKey, gamepad.leftStick, keyboard.spaceKey}));
1359+
{ keyboard.aKey, gamepad.leftStick, keyboard.spaceKey }));
14521360

1453-
list.AddRange(new InputControl[] {keyboard.bKey, keyboard.cKey});
1361+
list.AddRange(new InputControl[] { keyboard.bKey, keyboard.cKey });
14541362

14551363
Assert.That(list.Count, Is.EqualTo(5));
14561364
Assert.That(list.Capacity, Is.EqualTo(10));
14571365
Assert.That(list,
14581366
Is.EquivalentTo(new InputControl[]
1459-
{keyboard.aKey, gamepad.leftStick, keyboard.spaceKey, keyboard.bKey, keyboard.cKey}));
1367+
{ keyboard.aKey, gamepad.leftStick, keyboard.spaceKey, keyboard.bKey, keyboard.cKey }));
14601368

14611369
using (var toAdd = new InputControlList<InputControl>(gamepad.buttonNorth, gamepad.buttonEast, gamepad.buttonWest))
14621370
list.AddSlice(toAdd, count: 1, destinationIndex: 1, sourceIndex: 2);
@@ -1465,13 +1373,17 @@ public void Controls_CanKeepListsOfControls_WithoutAllocatingGCMemory()
14651373
Assert.That(list.Capacity, Is.EqualTo(10));
14661374
Assert.That(list,
14671375
Is.EquivalentTo(new InputControl[]
1468-
{keyboard.aKey, gamepad.buttonWest, gamepad.leftStick, keyboard.spaceKey, keyboard.bKey, keyboard.cKey}));
1376+
{
1377+
keyboard.aKey, gamepad.buttonWest, gamepad.leftStick, keyboard.spaceKey, keyboard.bKey, keyboard.cKey
1378+
}));
14691379

14701380
list[0] = keyboard.zKey;
14711381

14721382
Assert.That(list,
14731383
Is.EquivalentTo(new InputControl[]
1474-
{keyboard.zKey, gamepad.buttonWest, gamepad.leftStick, keyboard.spaceKey, keyboard.bKey, keyboard.cKey}));
1384+
{
1385+
keyboard.zKey, gamepad.buttonWest, gamepad.leftStick, keyboard.spaceKey, keyboard.bKey, keyboard.cKey
1386+
}));
14751387

14761388
list.Clear();
14771389

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2828
- 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).
2929
- Fixed InputSystem.onAnyButtonPress fails to trigger when the device receives a touch [UUM-137930](https://issuetracker.unity3d.com/product/unity/issues/guid/UUM-137930).
3030
- Fixed an incorrect ArraysHelper.HaveDuplicateReferences implementation that didn't use its arguments right [ISXB-1792] (https://github.com/Unity-Technologies/InputSystem/pull/2376)
31+
- Fixed `InputAction.IsPressed`, `WasPressedThisFrame`, and `WasReleasedThisFrame` using a `ButtonControl`'s `pressPoint` when a binding also had an explicit `PressInteraction` with its own `pressPoint`, which could make those APIs disagree with the interaction's press and release behavior. Action-level press APIs now follow the interaction threshold when both are set explicitly.
3132

3233
### Changed
34+
- Action-level `IsPressed`, `WasPressedThisFrame`, and `WasReleasedThisFrame` for bindings to `Vector2Control` / `StickControl` no longer consult a per-control `pressPoint` on the vector (that field was removed). Use a `Press` interaction to set a custom threshold, or rely on `defaultButtonPressPoint`.
3335
- Removed 32-bit compilation check for HID on Windows players, which had no impact anymore. (ISX-2543)
3436
- Migrated sample scenes to use Universal Render Pipeline (URP) with Built-in Render Pipeline fallback shaders. The URP package is now required to run the samples. (ISX-2343)
3537
- Changed the UI for `Actions.inputactions` asset to use UI Toolkit framework.

Packages/com.unity.inputsystem/Documentation~/Interactions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ If you haven't specifically added an Interaction to a Binding or its Action, the
172172

173173
You can use a [`PressInteraction`](xref:UnityEngine.InputSystem.Interactions.PressInteraction) to explicitly force button-like interactions. Use the [`behavior`](xref:UnityEngine.InputSystem.Interactions.PressInteraction.behavior) parameter to select if the Interaction should trigger on button press, release, or both.
174174

175+
If you stack several `Press` interactions on the same binding, each still runs with its own parameters. Separately, [`IsPressed`](xref:UnityEngine.InputSystem.InputAction.IsPressed), [`WasPressedThisFrame`](xref:UnityEngine.InputSystem.InputAction.WasPressedThisFrame), and [`WasReleasedThisFrame`](xref:UnityEngine.InputSystem.InputAction.WasReleasedThisFrame) pick a single actuation threshold by scanning interactions in list order and using the `pressPoint` from the first `Press` interaction whose `pressPoint` is greater than zero.
176+
175177
|__Parameters__|Type|Default value|
176178
|---|---|---|
177179
|[`pressPoint`](xref:UnityEngine.InputSystem.Interactions.PressInteraction.pressPoint)|`float`|[`InputSettings.defaultButtonPressPoint`](xref:UnityEngine.InputSystem.InputSettings.defaultButtonPressPoint)|

0 commit comments

Comments
 (0)