@@ -739,6 +739,42 @@ public void Actions_CanReadValueFromAction_InCallback()
739
739
Assert . That ( receivedValue , Is . EqualTo ( 1 ) . Within ( 0.00001 ) ) ;
740
740
}
741
741
742
+ [ Test ]
743
+ [ Category ( "Actions" ) ]
744
+ public void Actions_CanReadValueFromAction_InCallback_AsButton ( )
745
+ {
746
+ InputSystem . settings . defaultButtonPressPoint = 0.5f ;
747
+
748
+ var action = new InputAction ( binding : "<Gamepad>/leftTrigger" ) ;
749
+ var gamepad = InputSystem . AddDevice < Gamepad > ( ) ;
750
+
751
+ action . Enable ( ) ;
752
+
753
+ bool ? receivedValue = null ;
754
+ action . performed +=
755
+ ctx =>
756
+ {
757
+ Assert . That ( receivedValue , Is . Null ) ;
758
+ receivedValue = ctx . ReadValueAsButton ( ) ;
759
+ } ;
760
+
761
+ Set ( gamepad . leftTrigger , 0.25f ) ;
762
+
763
+ Assert . That ( receivedValue , Is . False ) ;
764
+
765
+ receivedValue = null ;
766
+
767
+ Set ( gamepad . leftTrigger , 0.75f ) ;
768
+
769
+ Assert . That ( receivedValue , Is . True ) ;
770
+
771
+ receivedValue = null ;
772
+
773
+ Set ( gamepad . leftTrigger , 0.15f ) ;
774
+
775
+ Assert . That ( receivedValue , Is . False ) ;
776
+ }
777
+
742
778
// Some code needs to be able to just generically transfer values from A to B. For this, the
743
779
// generic ReadValue<TValue>() API isn't sufficient.
744
780
[ Test ]
@@ -1161,53 +1197,33 @@ public void Actions_CanByPassControlActuationChecks_UsingPasshtroughAction()
1161
1197
var action = new InputAction ( type : InputActionType . PassThrough , binding : "<Gamepad>/*stick" ) ;
1162
1198
action . Enable ( ) ;
1163
1199
1164
- using ( var trace = new InputActionTrace ( ) )
1200
+ using ( var trace = new InputActionTrace ( action ) )
1165
1201
{
1166
- trace . SubscribeTo ( action ) ;
1167
-
1168
1202
Set ( gamepad . leftStick , new Vector2 ( 0.123f , 0.234f ) ) ;
1169
1203
1170
- var actions = trace . ToArray ( ) ;
1171
- Assert . That ( actions , Has . Length . EqualTo ( 1 ) ) ;
1172
- Assert . That ( actions [ 0 ] . phase , Is . EqualTo ( InputActionPhase . Performed ) ) ;
1173
- Assert . That ( actions [ 0 ] . control , Is . SameAs ( gamepad . leftStick ) ) ;
1174
- Assert . That ( actions [ 0 ] . ReadValue < Vector2 > ( ) ,
1175
- Is . EqualTo ( new StickDeadzoneProcessor ( ) . Process ( new Vector2 ( 0.123f , 0.234f ) ) )
1176
- . Using ( Vector2EqualityComparer . Instance ) ) ;
1204
+ Assert . That ( trace ,
1205
+ Performed ( action , gamepad . leftStick , new StickDeadzoneProcessor ( ) . Process ( new Vector2 ( 0.123f , 0.234f ) ) ) ) ;
1177
1206
1178
1207
trace . Clear ( ) ;
1179
1208
1180
1209
Set ( gamepad . leftStick , new Vector2 ( 0.234f , 0.345f ) ) ;
1181
1210
1182
- actions = trace . ToArray ( ) ;
1183
- Assert . That ( actions , Has . Length . EqualTo ( 1 ) ) ;
1184
- Assert . That ( actions [ 0 ] . phase , Is . EqualTo ( InputActionPhase . Performed ) ) ;
1185
- Assert . That ( actions [ 0 ] . control , Is . SameAs ( gamepad . leftStick ) ) ;
1186
- Assert . That ( actions [ 0 ] . ReadValue < Vector2 > ( ) ,
1187
- Is . EqualTo ( new StickDeadzoneProcessor ( ) . Process ( new Vector2 ( 0.234f , 0.345f ) ) )
1188
- . Using ( Vector2EqualityComparer . Instance ) ) ;
1211
+ Assert . That ( trace ,
1212
+ Performed ( action , gamepad . leftStick , new StickDeadzoneProcessor ( ) . Process ( new Vector2 ( 0.234f , 0.345f ) ) ) ) ;
1189
1213
1190
1214
trace . Clear ( ) ;
1191
1215
1192
1216
Set ( gamepad . rightStick , new Vector2 ( 0.123f , 0.234f ) ) ;
1193
1217
1194
- actions = trace . ToArray ( ) ;
1195
- Assert . That ( actions , Has . Length . EqualTo ( 1 ) ) ;
1196
- Assert . That ( actions [ 0 ] . phase , Is . EqualTo ( InputActionPhase . Performed ) ) ;
1197
- Assert . That ( actions [ 0 ] . control , Is . SameAs ( gamepad . rightStick ) ) ;
1198
- Assert . That ( actions [ 0 ] . ReadValue < Vector2 > ( ) ,
1199
- Is . EqualTo ( new StickDeadzoneProcessor ( ) . Process ( new Vector2 ( 0.123f , 0.234f ) ) )
1200
- . Using ( Vector2EqualityComparer . Instance ) ) ;
1218
+ Assert . That ( trace ,
1219
+ Performed ( action , gamepad . rightStick , new StickDeadzoneProcessor ( ) . Process ( new Vector2 ( 0.123f , 0.234f ) ) ) ) ;
1201
1220
1202
1221
trace . Clear ( ) ;
1203
1222
1204
1223
Set ( gamepad . rightStick , Vector2 . zero ) ;
1205
1224
1206
- actions = trace . ToArray ( ) ;
1207
- Assert . That ( actions , Has . Length . EqualTo ( 1 ) ) ;
1208
- Assert . That ( actions [ 0 ] . phase , Is . EqualTo ( InputActionPhase . Performed ) ) ;
1209
- Assert . That ( actions [ 0 ] . control , Is . SameAs ( gamepad . rightStick ) ) ;
1210
- Assert . That ( actions [ 0 ] . ReadValue < Vector2 > ( ) , Is . EqualTo ( Vector2 . zero ) . Using ( Vector2EqualityComparer . Instance ) ) ;
1225
+ Assert . That ( trace ,
1226
+ Performed ( action , gamepad . rightStick , Vector2 . zero ) ) ;
1211
1227
}
1212
1228
}
1213
1229
@@ -2784,6 +2800,46 @@ public void Actions_CanAddProcessorsToBindings()
2784
2800
Assert . That ( receivedVector . Value . y , Is . EqualTo ( 0.5678 ) . Within ( 0.00001 ) ) ;
2785
2801
}
2786
2802
2803
+ // https://fogbugz.unity3d.com/f/cases/1207082/
2804
+ [ Test ]
2805
+ [ Category ( "Actions" ) ]
2806
+ public void Actions_CanAddProcessorsToCompositeBindings ( )
2807
+ {
2808
+ var keyboard = InputSystem . AddDevice < Keyboard > ( ) ;
2809
+
2810
+ var action = new InputAction ( ) ;
2811
+ action . AddCompositeBinding ( "2DVector" , processors : "invertVector2(invertX=true,invertY=true)" )
2812
+ . With ( "Up" , "<Keyboard>/w" )
2813
+ . With ( "Down" , "<Keyboard>/s" )
2814
+ . With ( "Left" , "<Keyboard>/a" )
2815
+ . With ( "Right" , "<Keyboard>/d" ) ;
2816
+
2817
+ action . Enable ( ) ;
2818
+
2819
+ // Left -> Right.
2820
+ Press ( keyboard . aKey ) ;
2821
+
2822
+ Assert . That ( action . ReadValue < Vector2 > ( ) , Is . EqualTo ( new Vector2 ( 1 , 0 ) ) ) ;
2823
+
2824
+ // Right -> Left.
2825
+ Release ( keyboard . aKey ) ;
2826
+ Press ( keyboard . dKey ) ;
2827
+
2828
+ Assert . That ( action . ReadValue < Vector2 > ( ) , Is . EqualTo ( new Vector2 ( - 1 , 0 ) ) ) ;
2829
+
2830
+ // Up -> Down.
2831
+ Release ( keyboard . dKey ) ;
2832
+ Press ( keyboard . wKey ) ;
2833
+
2834
+ Assert . That ( action . ReadValue < Vector2 > ( ) , Is . EqualTo ( new Vector2 ( 0 , - 1 ) ) ) ;
2835
+
2836
+ // Down -> Up.
2837
+ Release ( keyboard . wKey ) ;
2838
+ Press ( keyboard . sKey ) ;
2839
+
2840
+ Assert . That ( action . ReadValue < Vector2 > ( ) , Is . EqualTo ( new Vector2 ( 0 , 1 ) ) ) ;
2841
+ }
2842
+
2787
2843
[ Test ]
2788
2844
[ Category ( "Actions" ) ]
2789
2845
public void Actions_CanAddScaleProcessor ( )
@@ -3588,6 +3644,69 @@ public void Actions_CanEnableActionThatHasNoControls()
3588
3644
Assert . That ( action2 . enabled , Is . True ) ;
3589
3645
}
3590
3646
3647
+ [ Test ]
3648
+ [ Category ( "Actions" ) ]
3649
+ public void Actions_CanMixEnablingSingleActionsAndEntireActionMaps ( )
3650
+ {
3651
+ var gamepad = InputSystem . AddDevice < Gamepad > ( ) ;
3652
+
3653
+ var asset = ScriptableObject . CreateInstance < InputActionAsset > ( ) ;
3654
+ var map1 = new InputActionMap ( "map1" ) ;
3655
+ var map2 = new InputActionMap ( "map2" ) ;
3656
+ var action1 = map1 . AddAction ( "action1" , binding : "<Gamepad>/buttonSouth" ) ;
3657
+ var action2 = map1 . AddAction ( "action2" , binding : "<Gamepad>/buttonNorth" ) ;
3658
+ var action3 = map2 . AddAction ( "action3" , binding : "<Gamepad>/buttonSouth" ) ;
3659
+ var action4 = map2 . AddAction ( "action4" , binding : "<Gamepad>/buttonNorth" ) ;
3660
+ asset . AddActionMap ( map1 ) ;
3661
+ asset . AddActionMap ( map2 ) ;
3662
+
3663
+ action3 . Enable ( ) ;
3664
+ map1 . Enable ( ) ;
3665
+
3666
+ using ( var trace1 = new InputActionTrace ( action1 ) )
3667
+ using ( var trace2 = new InputActionTrace ( action2 ) )
3668
+ using ( var trace3 = new InputActionTrace ( action3 ) )
3669
+ using ( var trace4 = new InputActionTrace ( action4 ) )
3670
+ {
3671
+ PressAndRelease ( gamepad . buttonSouth ) ;
3672
+
3673
+ Assert . That ( trace1 , Started ( action1 ) . AndThen ( Performed ( action1 ) ) . AndThen ( Canceled ( action1 ) ) ) ;
3674
+ Assert . That ( trace2 , Is . Empty ) ;
3675
+ Assert . That ( trace3 , Started ( action3 ) . AndThen ( Performed ( action3 ) ) . AndThen ( Canceled ( action3 ) ) ) ;
3676
+ Assert . That ( trace4 , Is . Empty ) ;
3677
+
3678
+ trace1 . Clear ( ) ;
3679
+ trace2 . Clear ( ) ;
3680
+ trace3 . Clear ( ) ;
3681
+ trace4 . Clear ( ) ;
3682
+
3683
+ map1 . Disable ( ) ;
3684
+ map2 . Enable ( ) ;
3685
+
3686
+ PressAndRelease ( gamepad . buttonSouth ) ;
3687
+
3688
+ Assert . That ( trace1 , Is . Empty ) ;
3689
+ Assert . That ( trace2 , Is . Empty ) ;
3690
+ Assert . That ( trace3 , Started ( action3 ) . AndThen ( Performed ( action3 ) ) . AndThen ( Canceled ( action3 ) ) ) ;
3691
+ Assert . That ( trace4 , Is . Empty ) ;
3692
+
3693
+ trace1 . Clear ( ) ;
3694
+ trace2 . Clear ( ) ;
3695
+ trace3 . Clear ( ) ;
3696
+ trace4 . Clear ( ) ;
3697
+
3698
+ map1 . Enable ( ) ;
3699
+ map2 . Disable ( ) ;
3700
+
3701
+ PressAndRelease ( gamepad . buttonSouth ) ;
3702
+
3703
+ Assert . That ( trace1 , Started ( action1 ) . AndThen ( Performed ( action1 ) ) . AndThen ( Canceled ( action1 ) ) ) ;
3704
+ Assert . That ( trace2 , Is . Empty ) ;
3705
+ Assert . That ( trace3 , Is . Empty ) ;
3706
+ Assert . That ( trace4 , Is . Empty ) ;
3707
+ }
3708
+ }
3709
+
3591
3710
[ Test ]
3592
3711
[ Category ( "Actions" ) ]
3593
3712
public void Actions_CanTargetSingleDeviceWithMultipleActions ( )
@@ -4762,7 +4881,7 @@ public void Actions_WhenGettingDisplayTextForBindingsOnAction_InteractionsAreSho
4762
4881
4763
4882
// Can suppress.
4764
4883
Assert . That ( action1 . GetBindingDisplayString ( InputBinding . DisplayStringOptions . DontIncludeInteractions ) ,
4765
- Is . EqualTo ( "Hold " + GamepadState . ButtonSouthShortDisplayName ) ) ;
4884
+ Is . EqualTo ( GamepadState . ButtonSouthShortDisplayName ) ) ;
4766
4885
}
4767
4886
4768
4887
[ Test ]
@@ -5724,7 +5843,6 @@ public void Actions_CompositesInDifferentMapsTiedToSameControlsWork()
5724
5843
asset . AddActionMap ( map2 ) ;
5725
5844
asset . Enable ( ) ;
5726
5845
5727
-
5728
5846
InputControl performedControl1 = null ;
5729
5847
InputControl performedControl2 = null ;
5730
5848
action1 . performed += ctx => performedControl1 = ctx . control ;
@@ -6266,6 +6384,28 @@ public void Actions_CanEnableAndDisableEntireMap()
6266
6384
Assert . That ( action2 . enabled , Is . False ) ;
6267
6385
}
6268
6386
6387
+ // https://fogbugz.unity3d.com/f/cases/1213085 (bindings that refer to non-existent actions should not lead to errors)
6388
+ [ Test ]
6389
+ [ Category ( "Actions" ) ]
6390
+ public void Actions_CanEnableAndDisableEntireMap_EvenWhenBindingsReferToNonExistentActions ( )
6391
+ {
6392
+ var gamepad = InputSystem . AddDevice < Gamepad > ( ) ;
6393
+
6394
+ var map = new InputActionMap ( ) ;
6395
+ map . AddAction ( "action" , binding : "<Gamepad>/buttonSouth" ) ;
6396
+ map . AddBinding ( "<Gamepad>/buttonNorth" , action : "DoesNotExist" ) ;
6397
+
6398
+ // Also try the same for a composite binding.
6399
+ map . AddBinding ( new InputBinding { path = "1DAxis" , isComposite = true , action = "DoesNotExist" } ) ;
6400
+ map . AddBinding ( new InputBinding { name = "Positive" , path = "<Gamepad>/leftTrigger" , isPartOfComposite = true } ) ;
6401
+ map . AddBinding ( new InputBinding { name = "Negative" , path = "<Gamepad>/rightTrigger" , isPartOfComposite = true } ) ;
6402
+
6403
+ Assert . That ( ( ) => map . Enable ( ) , Throws . Nothing ) ;
6404
+
6405
+ Assert . That ( ( ) => Press ( gamepad . buttonNorth ) , Throws . Nothing ) ;
6406
+ Assert . That ( ( ) => Press ( gamepad . leftTrigger ) , Throws . Nothing ) ;
6407
+ }
6408
+
6269
6409
[ Test ]
6270
6410
[ Category ( "Actions" ) ]
6271
6411
public void Actions_CanEnableAndDisableSingleActionFromMap ( )
0 commit comments