11using System . Collections . Generic ;
2+ using System . Diagnostics . CodeAnalysis ;
23using System . Linq ;
34using Microsoft . Xna . Framework ;
45using Microsoft . Xna . Framework . Input ;
@@ -21,7 +22,7 @@ internal class GamePadStateBuilder : IInputStateBuilder<GamePadStateBuilder, Gam
2122 private GamePadState ? State ;
2223
2324 /// <summary>The current button states.</summary>
24- private readonly Dictionary < SButton , ButtonState > ButtonStates = [ ] ;
25+ private readonly Dictionary < Buttons , ButtonState > ButtonStates = [ ] ;
2526
2627 /// <summary>The left trigger value.</summary>
2728 private float LeftTrigger ;
@@ -53,21 +54,21 @@ public void Reset(GamePadState state)
5354
5455 var states = this . ButtonStates ;
5556 states . Clear ( ) ;
56- states [ SButton . DPadUp ] = pad . Up ;
57- states [ SButton . DPadDown ] = pad . Down ;
58- states [ SButton . DPadLeft ] = pad . Left ;
59- states [ SButton . DPadRight ] = pad . Right ;
60- states [ SButton . ControllerA ] = buttons . A ;
61- states [ SButton . ControllerB ] = buttons . B ;
62- states [ SButton . ControllerX ] = buttons . X ;
63- states [ SButton . ControllerY ] = buttons . Y ;
64- states [ SButton . LeftStick ] = buttons . LeftStick ;
65- states [ SButton . RightStick ] = buttons . RightStick ;
66- states [ SButton . LeftShoulder ] = buttons . LeftShoulder ;
67- states [ SButton . RightShoulder ] = buttons . RightShoulder ;
68- states [ SButton . ControllerBack ] = buttons . Back ;
69- states [ SButton . ControllerStart ] = buttons . Start ;
70- states [ SButton . BigButton ] = buttons . BigButton ;
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 ;
7172
7273 this . LeftTrigger = triggers . Left ;
7374 this . RightTrigger = triggers . Right ;
@@ -85,63 +86,81 @@ public void Reset(GamePadState state)
8586 }
8687 }
8788
88- /// <inheritdoc />
89- public GamePadStateBuilder OverrideButtons ( IDictionary < SButton , SButtonState > overrides )
89+ /// <summary>Override the state for a button.</summary>
90+ /// <param name="button">The button to override.</param>
91+ /// <param name="state">The new state to set.</param>
92+ public void OverrideButton ( Buttons button , SButtonState state )
9093 {
91- foreach ( var pair in overrides )
94+ bool isDown = state . IsDown ( ) ;
95+ bool changed = false ;
96+
97+ switch ( button )
9298 {
93- bool changed = true ;
99+ // left thumbstick
100+ case Buttons . LeftThumbstickUp :
101+ changed = Set ( ref this . LeftStickPos . Y , isDown ? 1 : 0 ) ;
102+ break ;
103+ case Buttons . LeftThumbstickDown :
104+ changed = Set ( ref this . LeftStickPos . Y , isDown ? - 1 : 0 ) ;
105+ break ;
106+ case Buttons . LeftThumbstickLeft :
107+ changed = Set ( ref this . LeftStickPos . X , isDown ? - 1 : 0 ) ;
108+ break ;
109+ case Buttons . LeftThumbstickRight :
110+ changed = Set ( ref this . LeftStickPos . X , isDown ? 1 : 0 ) ;
111+ break ;
112+
113+ // right thumbstick
114+ case Buttons . RightThumbstickUp :
115+ changed = Set ( ref this . RightStickPos . Y , isDown ? 1 : 0 ) ;
116+ break ;
117+ case Buttons . RightThumbstickDown :
118+ changed = Set ( ref this . RightStickPos . Y , isDown ? - 1 : 0 ) ;
119+ break ;
120+ case Buttons . RightThumbstickLeft :
121+ changed = Set ( ref this . RightStickPos . X , isDown ? - 1 : 0 ) ;
122+ break ;
123+ case Buttons . RightThumbstickRight :
124+ changed = Set ( ref this . RightStickPos . X , isDown ? 1 : 0 ) ;
125+ break ;
126+
127+ // triggers
128+ case Buttons . LeftTrigger :
129+ changed = Set ( ref this . LeftTrigger , isDown ? 1 : 0 ) ;
130+ break ;
131+ case Buttons . RightTrigger :
132+ changed = Set ( ref this . RightTrigger , isDown ? 1 : 0 ) ;
133+ break ;
134+
135+ // buttons
136+ 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+ }
146+ break ;
147+ }
94148
95- bool isDown = pair . Value . IsDown ( ) ;
96- switch ( pair . Key )
149+ if ( changed )
150+ this . State = null ;
151+ return ;
152+
153+ [ SuppressMessage ( "ReSharper" , "CompareOfFloatsByEqualityOperator" , Justification = "Floating points not an issue for the specific values we're checking." ) ]
154+ static bool Set ( ref float field , int newValue )
155+ {
156+ if ( field != newValue )
97157 {
98- // left thumbstick
99- case SButton . LeftThumbstickUp :
100- this . LeftStickPos . Y = isDown ? 1 : 0 ;
101- break ;
102- case SButton . LeftThumbstickDown :
103- this . LeftStickPos . Y = isDown ? - 1 : 0 ;
104- break ;
105- case SButton . LeftThumbstickLeft :
106- this . LeftStickPos . X = isDown ? - 1 : 0 ;
107- break ;
108- case SButton . LeftThumbstickRight :
109- this . LeftStickPos . X = isDown ? 1 : 0 ;
110- break ;
111-
112- // right thumbstick
113- case SButton . RightThumbstickUp :
114- this . RightStickPos . Y = isDown ? 1 : 0 ;
115- break ;
116- case SButton . RightThumbstickDown :
117- this . RightStickPos . Y = isDown ? - 1 : 0 ;
118- break ;
119- case SButton . RightThumbstickLeft :
120- this . RightStickPos . X = isDown ? - 1 : 0 ;
121- break ;
122- case SButton . RightThumbstickRight :
123- this . RightStickPos . X = isDown ? 1 : 0 ;
124- break ;
125-
126- // triggers
127- case SButton . LeftTrigger :
128- this . LeftTrigger = isDown ? 1 : 0 ;
129- break ;
130- case SButton . RightTrigger :
131- this . RightTrigger = isDown ? 1 : 0 ;
132- break ;
133-
134- // buttons
135- default :
136- this . ButtonStates [ pair . Key ] = isDown ? ButtonState . Pressed : ButtonState . Released ;
137- break ;
158+ field = newValue ;
159+ return true ;
138160 }
139161
140- if ( changed )
141- this . State = null ;
162+ return false ;
142163 }
143-
144- return this ;
145164 }
146165
147166 /// <inheritdoc />
@@ -200,9 +219,9 @@ public GamePadState GetState()
200219 /// <summary>Get the pressed gamepad buttons.</summary>
201220 private IEnumerable < Buttons > GetPressedGamePadButtons ( )
202221 {
203- foreach ( var pair in this . ButtonStates )
222+ foreach ( ( Buttons button , ButtonState state ) in this . ButtonStates )
204223 {
205- if ( pair . Value == ButtonState . Pressed && pair . Key . TryGetController ( out Buttons button ) )
224+ if ( state == ButtonState . Pressed )
206225 yield return button ;
207226 }
208227 }
0 commit comments