@@ -19,10 +19,11 @@ internal class GamePadStateBuilder : IInputStateBuilder<GamePadStateBuilder, Gam
1919 private const float RightThumbstickDeadZone = 0.9f ;
2020
2121 /// <summary>The underlying controller state.</summary>
22+ /// <remarks>This value is null if it needs to be regenerated for overrides. Most code should call <see cref="GetState"/> instead.</remarks>
2223 private GamePadState ? State ;
2324
24- /// <summary>The current button states .</summary>
25- private readonly Dictionary < Buttons , ButtonState > ButtonStates = [ ] ;
25+ /// <summary>The pressed buttons .</summary>
26+ private readonly HashSet < Buttons > PressedButtons = [ ] ;
2627
2728 /// <summary>The left trigger value.</summary>
2829 private float LeftTrigger ;
@@ -33,7 +34,7 @@ internal class GamePadStateBuilder : IInputStateBuilder<GamePadStateBuilder, Gam
3334 /// <summary>The left thumbstick position.</summary>
3435 private Vector2 LeftStickPos ;
3536
36- /// <summary>The left thumbstick position.</summary>
37+ /// <summary>The right thumbstick position.</summary>
3738 private Vector2 RightStickPos ;
3839
3940
@@ -45,30 +46,31 @@ public void Reset(GamePadState state)
4546 {
4647 this . State = state ;
4748
49+ // reset tracked values
50+ this . PressedButtons . Clear ( ) ;
4851 if ( state . IsConnected )
4952 {
5053 GamePadDPad pad = state . DPad ;
5154 GamePadButtons buttons = state . Buttons ;
5255 GamePadTriggers triggers = state . Triggers ;
5356 GamePadThumbSticks sticks = state . ThumbSticks ;
5457
55- var states = this . ButtonStates ;
56- states . Clear ( ) ;
57- states [ Buttons . DPadUp ] = pad . Up ;
58- states [ Buttons . DPadDown ] = pad . Down ;
59- states [ Buttons . DPadLeft ] = pad . Left ;
60- states [ Buttons . DPadRight ] = pad . Right ;
61- states [ Buttons . A ] = buttons . A ;
62- states [ Buttons . B ] = buttons . B ;
63- states [ Buttons . X ] = buttons . X ;
64- states [ Buttons . Y ] = buttons . Y ;
65- states [ Buttons . LeftStick ] = buttons . LeftStick ;
66- states [ Buttons . RightStick ] = buttons . RightStick ;
67- states [ Buttons . LeftShoulder ] = buttons . LeftShoulder ;
68- states [ Buttons . RightShoulder ] = buttons . RightShoulder ;
69- states [ Buttons . Back ] = buttons . Back ;
70- states [ Buttons . Start ] = buttons . Start ;
71- states [ Buttons . BigButton ] = buttons . BigButton ;
58+ HashSet < Buttons > pressed = this . PressedButtons ;
59+ AddIfPressed ( pressed , Buttons . DPadUp , pad . Up ) ;
60+ AddIfPressed ( pressed , Buttons . DPadDown , pad . Down ) ;
61+ AddIfPressed ( pressed , Buttons . DPadLeft , pad . Left ) ;
62+ AddIfPressed ( pressed , Buttons . DPadRight , pad . Right ) ;
63+ AddIfPressed ( pressed , Buttons . A , buttons . A ) ;
64+ AddIfPressed ( pressed , Buttons . B , buttons . B ) ;
65+ AddIfPressed ( pressed , Buttons . X , buttons . X ) ;
66+ AddIfPressed ( pressed , Buttons . Y , buttons . Y ) ;
67+ AddIfPressed ( pressed , Buttons . LeftStick , buttons . LeftStick ) ;
68+ AddIfPressed ( pressed , Buttons . RightStick , buttons . RightStick ) ;
69+ AddIfPressed ( pressed , Buttons . LeftShoulder , buttons . LeftShoulder ) ;
70+ AddIfPressed ( pressed , Buttons . RightShoulder , buttons . RightShoulder ) ;
71+ AddIfPressed ( pressed , Buttons . Back , buttons . Back ) ;
72+ AddIfPressed ( pressed , Buttons . Start , buttons . Start ) ;
73+ AddIfPressed ( pressed , Buttons . BigButton , buttons . BigButton ) ;
7274
7375 this . LeftTrigger = triggers . Left ;
7476 this . RightTrigger = triggers . Right ;
@@ -77,13 +79,18 @@ public void Reset(GamePadState state)
7779 }
7880 else
7981 {
80- this . ButtonStates . Clear ( ) ;
81-
8282 this . LeftTrigger = 0 ;
8383 this . RightTrigger = 0 ;
8484 this . LeftStickPos = Vector2 . Zero ;
8585 this . RightStickPos = Vector2 . Zero ;
8686 }
87+
88+ return ;
89+ static void AddIfPressed ( HashSet < Buttons > pressed , Buttons button , ButtonState state )
90+ {
91+ if ( state == ButtonState . Pressed )
92+ pressed . Add ( button ) ;
93+ }
8794 }
8895
8996 /// <summary>Override the state for a button.</summary>
@@ -92,7 +99,7 @@ public void Reset(GamePadState state)
9299 public void OverrideButton ( Buttons button , SButtonState state )
93100 {
94101 bool isDown = state . IsDown ( ) ;
95- bool changed = false ;
102+ bool changed ;
96103
97104 switch ( button )
98105 {
@@ -134,22 +141,16 @@ public void OverrideButton(Buttons button, SButtonState state)
134141
135142 // buttons
136143 default :
137- {
138- ButtonState newState = isDown ? ButtonState . Pressed : ButtonState . Released ;
139-
140- if ( ! this . ButtonStates . TryGetValue ( button , out ButtonState oldState ) || newState != oldState )
141- {
142- this . ButtonStates [ button ] = newState ;
143- changed = true ;
144- }
145- }
144+ changed = isDown
145+ ? this . PressedButtons . Add ( button )
146+ : this . PressedButtons . Remove ( button ) ;
146147 break ;
147148 }
148149
149150 if ( changed )
150151 this . State = null ;
151- return ;
152152
153+ return ;
153154 [ SuppressMessage ( "ReSharper" , "CompareOfFloatsByEqualityOperator" , Justification = "Floating points not an issue for the specific values we're checking." ) ]
154155 static bool Set ( ref float field , int newValue )
155156 {
@@ -167,7 +168,7 @@ static bool Set(ref float field, int newValue)
167168 public void FillPressedButtons ( HashSet < SButton > set )
168169 {
169170 // buttons
170- foreach ( Buttons button in this . GetPressedGamePadButtons ( ) )
171+ foreach ( Buttons button in this . PressedButtons )
171172 set . Add ( button . ToSButton ( ) ) ;
172173
173174 // triggers
@@ -208,21 +209,9 @@ public GamePadState GetState()
208209 rightThumbStick : this . RightStickPos ,
209210 leftTrigger : this . LeftTrigger ,
210211 rightTrigger : this . RightTrigger ,
211- buttons : this . GetPressedGamePadButtons ( ) . ToArray ( )
212+ buttons : this . PressedButtons . Count > 0
213+ ? this . PressedButtons . ToArray ( )
214+ : [ ]
212215 ) ;
213216 }
214-
215-
216- /*********
217- ** Private methods
218- *********/
219- /// <summary>Get the pressed gamepad buttons.</summary>
220- private IEnumerable < Buttons > GetPressedGamePadButtons ( )
221- {
222- foreach ( ( Buttons button , ButtonState state ) in this . ButtonStates )
223- {
224- if ( state == ButtonState . Pressed )
225- yield return button ;
226- }
227- }
228217}
0 commit comments