@@ -739,6 +739,42 @@ public void Actions_CanReadValueFromAction_InCallback()
739739 Assert . That ( receivedValue , Is . EqualTo ( 1 ) . Within ( 0.00001 ) ) ;
740740 }
741741
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+
742778 // Some code needs to be able to just generically transfer values from A to B. For this, the
743779 // generic ReadValue<TValue>() API isn't sufficient.
744780 [ Test ]
@@ -1161,53 +1197,33 @@ public void Actions_CanByPassControlActuationChecks_UsingPasshtroughAction()
11611197 var action = new InputAction ( type : InputActionType . PassThrough , binding : "<Gamepad>/*stick" ) ;
11621198 action . Enable ( ) ;
11631199
1164- using ( var trace = new InputActionTrace ( ) )
1200+ using ( var trace = new InputActionTrace ( action ) )
11651201 {
1166- trace . SubscribeTo ( action ) ;
1167-
11681202 Set ( gamepad . leftStick , new Vector2 ( 0.123f , 0.234f ) ) ;
11691203
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 ) ) ) ) ;
11771206
11781207 trace . Clear ( ) ;
11791208
11801209 Set ( gamepad . leftStick , new Vector2 ( 0.234f , 0.345f ) ) ;
11811210
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 ) ) ) ) ;
11891213
11901214 trace . Clear ( ) ;
11911215
11921216 Set ( gamepad . rightStick , new Vector2 ( 0.123f , 0.234f ) ) ;
11931217
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 ) ) ) ) ;
12011220
12021221 trace . Clear ( ) ;
12031222
12041223 Set ( gamepad . rightStick , Vector2 . zero ) ;
12051224
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 ) ) ;
12111227 }
12121228 }
12131229
@@ -2784,6 +2800,46 @@ public void Actions_CanAddProcessorsToBindings()
27842800 Assert . That ( receivedVector . Value . y , Is . EqualTo ( 0.5678 ) . Within ( 0.00001 ) ) ;
27852801 }
27862802
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+
27872843 [ Test ]
27882844 [ Category ( "Actions" ) ]
27892845 public void Actions_CanAddScaleProcessor ( )
@@ -3588,6 +3644,69 @@ public void Actions_CanEnableActionThatHasNoControls()
35883644 Assert . That ( action2 . enabled , Is . True ) ;
35893645 }
35903646
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+
35913710 [ Test ]
35923711 [ Category ( "Actions" ) ]
35933712 public void Actions_CanTargetSingleDeviceWithMultipleActions ( )
@@ -4762,7 +4881,7 @@ public void Actions_WhenGettingDisplayTextForBindingsOnAction_InteractionsAreSho
47624881
47634882 // Can suppress.
47644883 Assert . That ( action1 . GetBindingDisplayString ( InputBinding . DisplayStringOptions . DontIncludeInteractions ) ,
4765- Is . EqualTo ( "Hold " + GamepadState . ButtonSouthShortDisplayName ) ) ;
4884+ Is . EqualTo ( GamepadState . ButtonSouthShortDisplayName ) ) ;
47664885 }
47674886
47684887 [ Test ]
@@ -5724,7 +5843,6 @@ public void Actions_CompositesInDifferentMapsTiedToSameControlsWork()
57245843 asset . AddActionMap ( map2 ) ;
57255844 asset . Enable ( ) ;
57265845
5727-
57285846 InputControl performedControl1 = null ;
57295847 InputControl performedControl2 = null ;
57305848 action1 . performed += ctx => performedControl1 = ctx . control ;
@@ -6266,6 +6384,28 @@ public void Actions_CanEnableAndDisableEntireMap()
62666384 Assert . That ( action2 . enabled , Is . False ) ;
62676385 }
62686386
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+
62696409 [ Test ]
62706410 [ Category ( "Actions" ) ]
62716411 public void Actions_CanEnableAndDisableSingleActionFromMap ( )
0 commit comments