Skip to content

Commit ff44a02

Browse files
FIX: [ISXB-1716] Add support for all existing InputControl types in InputTestFixture.Trigger (#2270)
Co-authored-by: u-pr-agent[bot] <205906871+u-pr-agent[bot]@users.noreply.github.com>
1 parent 9427975 commit ff44a02

File tree

2 files changed

+92
-17
lines changed

2 files changed

+92
-17
lines changed

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ however, it has to be formatted properly to pass verification tests.
1919
- Added an example of how to swap two similar controls to the `RebindingUISample`. This is accessible via a button with two arrows at the right hand-side of the screen. Pressing the button allows swapping the current bindings of the "Move" and "Look" gamepad bindings via the new `RebindActionUI.SwapBinding(RebindActionUI other)` method.
2020
- Added support for (MonoBehavior OnMouse events) [https://docs.unity3d.com/ScriptReference/MonoBehaviour.html] when running the Input System on Unity 6000.4 or newer.
2121
- Added tests and a sample for MonoBehavior OnMouse events using the InputSystem package.
22+
- Added support for all the existing InputControl types in InputTestFixture.Trigger [ISXB-1716]
2223

2324
### Fixed
2425
- Fixed warnings being generated on Unity 6.3 (beta). (ISXB-1718).

Packages/com.unity.inputsystem/Tests/TestFixture/InputTestFixture.cs

Lines changed: 91 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using UnityEngine.InputSystem.Utilities;
1212
using UnityEngine.TestTools;
1313
using UnityEngine.TestTools.Utils;
14+
using UnityEngine.InputSystem.XR;
15+
1416
#if UNITY_EDITOR
1517
using UnityEditor;
1618
using UnityEngine.InputSystem.Editor;
@@ -728,32 +730,104 @@ public void Trigger(InputAction action)
728730
throw new ArgumentException(
729731
$"Action '{action}' must be bound to controls in order to be able to trigger it", nameof(action));
730732

731-
// See if we have a button we can trigger.
733+
// We iterate controls to see if there are any that we know how to trigger
732734
for (var i = 0; i < controls.Count; ++i)
733735
{
734-
if (!(controls[i] is ButtonControl button))
735-
continue;
736+
var control = controls[i];
736737

737-
// Press and release button.
738-
Set(button, 1);
739-
Set(button, 0);
738+
// for buttons, we literally trigger them pressed and released
739+
if (control is ButtonControl buttonControl)
740+
{
741+
Set(buttonControl, 1);
742+
Set(buttonControl, 0);
740743

741-
return;
742-
}
744+
return;
745+
}
743746

744-
// See if we have an axis we can slide a bit.
745-
for (var i = 0; i < controls.Count; ++i)
746-
{
747-
if (!(controls[i] is AxisControl axis))
748-
continue;
747+
// for the other types of controls we simply perform a small change in value as applicable
748+
const float minorChange = 0.01f;
749749

750-
// We do, so nudge its value a bit.
751-
Set(axis, axis.ReadValue() + 0.01f);
750+
if (control is AxisControl axisControl)
751+
{
752+
Set(axisControl, axisControl.ReadValue() + minorChange);
752753

753-
return;
754+
return;
755+
}
756+
757+
if (control is Vector2Control vec2Control)
758+
{
759+
Set(vec2Control, vec2Control.ReadValue() + Vector2.one * minorChange);
760+
761+
return;
762+
}
763+
764+
if (control is Vector3Control vec3Control)
765+
{
766+
Set(vec3Control, vec3Control.ReadValue() + Vector3.one * minorChange);
767+
768+
return;
769+
}
770+
771+
if (control is QuaternionControl quatControl)
772+
{
773+
var q = quatControl.ReadValue();
774+
q.ToAngleAxis(out float angle, out Vector3 axis);
775+
Set(quatControl, Quaternion.AngleAxis(angle + minorChange, axis));
776+
777+
return;
778+
}
779+
780+
if (control is IntegerControl integerControl)
781+
{
782+
Set(integerControl, integerControl.ReadValue() + 1);
783+
784+
return;
785+
}
786+
787+
if (control is TouchControl touchControl)
788+
{
789+
var state = touchControl.ReadValue();
790+
state.position += Vector2.one * minorChange;
791+
Set(touchControl, state);
792+
793+
return;
794+
}
795+
796+
// this one is a bit weird, but there's not much to change other than picking the next phase in the list
797+
if (control is TouchPhaseControl touchPhaseControl)
798+
{
799+
var phase = touchPhaseControl.ReadValue();
800+
var values = Enum.GetValues(typeof(TouchPhase));
801+
var index = Array.IndexOf(values, phase);
802+
var newIndex = (index + 1) % values.Length;
803+
Set(touchPhaseControl, (TouchPhase)values.GetValue(newIndex));
804+
805+
return;
806+
}
807+
808+
if (control is BoneControl boneControl)
809+
{
810+
var bone = boneControl.ReadValue();
811+
bone.position += minorChange * Vector3.one;
812+
Set(boneControl, bone);
813+
814+
return;
815+
}
816+
817+
if (control is EyesControl eyesControl)
818+
{
819+
var eyes = eyesControl.ReadValue();
820+
eyes.leftEyePosition += minorChange * Vector3.one;
821+
Set(eyesControl, eyes);
822+
823+
return;
824+
}
754825
}
755826

756-
////TODO: support a wider range of controls
827+
// Initially I wanted to implement PoseControl too, but it's got a very complicated ifdef that enables it.
828+
// Didn't want to carry that #ifdef here. Let's see how many people really need to trigger PoseControl.
829+
830+
// If it's not a control that we know how to trigger - it's not implemented yet
757831
throw new NotImplementedException();
758832
}
759833

0 commit comments

Comments
 (0)