11using System . Collections . Generic ;
2- using System . Diagnostics . CodeAnalysis ;
32using System . Linq ;
43using Microsoft . Xna . Framework ;
54using Microsoft . Xna . Framework . Input ;
@@ -22,7 +21,7 @@ internal class GamePadStateBuilder : IInputStateBuilder<GamePadStateBuilder, Gam
2221 private GamePadState ? State ;
2322
2423 /// <summary>The current button states.</summary>
25- private readonly IDictionary < SButton , ButtonState > ? ButtonStates ;
24+ private readonly Dictionary < SButton , ButtonState > ButtonStates = [ ] ;
2625
2726 /// <summary>The left trigger value.</summary>
2827 private float LeftTrigger ;
@@ -37,62 +36,58 @@ internal class GamePadStateBuilder : IInputStateBuilder<GamePadStateBuilder, Gam
3736 private Vector2 RightStickPos ;
3837
3938
40- /*********
41- ** Accessors
42- *********/
43- /// <summary>Whether the gamepad is currently connected.</summary>
44- [ MemberNotNullWhen ( true , nameof ( GamePadStateBuilder . ButtonStates ) ) ]
45- public bool IsConnected { get ; }
46-
47-
4839 /*********
4940 ** Public methods
5041 *********/
51- /// <summary>Construct an instance.</summary>
52- /// <param name="state">The initial state.</param>
53- public GamePadStateBuilder ( GamePadState state )
42+ /// <inheritdoc />
43+ public void Reset ( GamePadState state )
5444 {
5545 this . State = state ;
56- this . IsConnected = state . IsConnected ;
57-
58- if ( ! this . IsConnected )
59- return ;
6046
61- GamePadDPad pad = state . DPad ;
62- GamePadButtons buttons = state . Buttons ;
63- GamePadTriggers triggers = state . Triggers ;
64- GamePadThumbSticks sticks = state . ThumbSticks ;
65- this . ButtonStates = new Dictionary < SButton , ButtonState >
47+ if ( state . IsConnected )
6648 {
67- [ SButton . DPadUp ] = pad . Up ,
68- [ SButton . DPadDown ] = pad . Down ,
69- [ SButton . DPadLeft ] = pad . Left ,
70- [ SButton . DPadRight ] = pad . Right ,
71-
72- [ SButton . ControllerA ] = buttons . A ,
73- [ SButton . ControllerB ] = buttons . B ,
74- [ SButton . ControllerX ] = buttons . X ,
75- [ SButton . ControllerY ] = buttons . Y ,
76- [ SButton . LeftStick ] = buttons . LeftStick ,
77- [ SButton . RightStick ] = buttons . RightStick ,
78- [ SButton . LeftShoulder ] = buttons . LeftShoulder ,
79- [ SButton . RightShoulder ] = buttons . RightShoulder ,
80- [ SButton . ControllerBack ] = buttons . Back ,
81- [ SButton . ControllerStart ] = buttons . Start ,
82- [ SButton . BigButton ] = buttons . BigButton
83- } ;
84- this . LeftTrigger = triggers . Left ;
85- this . RightTrigger = triggers . Right ;
86- this . LeftStickPos = sticks . Left ;
87- this . RightStickPos = sticks . Right ;
49+ GamePadDPad pad = state . DPad ;
50+ GamePadButtons buttons = state . Buttons ;
51+ GamePadTriggers triggers = state . Triggers ;
52+ GamePadThumbSticks sticks = state . ThumbSticks ;
53+
54+ var states = this . ButtonStates ;
55+ 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 ;
71+
72+ this . LeftTrigger = triggers . Left ;
73+ this . RightTrigger = triggers . Right ;
74+ this . LeftStickPos = sticks . Left ;
75+ this . RightStickPos = sticks . Right ;
76+ }
77+ else
78+ {
79+ this . ButtonStates . Clear ( ) ;
80+
81+ this . LeftTrigger = 0 ;
82+ this . RightTrigger = 0 ;
83+ this . LeftStickPos = Vector2 . Zero ;
84+ this . RightStickPos = Vector2 . Zero ;
85+ }
8886 }
8987
9088 /// <inheritdoc />
9189 public GamePadStateBuilder OverrideButtons ( IDictionary < SButton , SButtonState > overrides )
9290 {
93- if ( ! this . IsConnected )
94- return this ;
95-
9691 foreach ( var pair in overrides )
9792 {
9893 bool changed = true ;
@@ -138,10 +133,7 @@ public GamePadStateBuilder OverrideButtons(IDictionary<SButton, SButtonState> ov
138133
139134 // buttons
140135 default :
141- if ( this . ButtonStates . ContainsKey ( pair . Key ) )
142- this . ButtonStates [ pair . Key ] = isDown ? ButtonState . Pressed : ButtonState . Released ;
143- else
144- changed = false ;
136+ this . ButtonStates [ pair . Key ] = isDown ? ButtonState . Pressed : ButtonState . Released ;
145137 break ;
146138 }
147139
@@ -155,9 +147,6 @@ public GamePadStateBuilder OverrideButtons(IDictionary<SButton, SButtonState> ov
155147 /// <inheritdoc />
156148 public IEnumerable < SButton > GetPressedButtons ( )
157149 {
158- if ( ! this . IsConnected )
159- yield break ;
160-
161150 // buttons
162151 foreach ( Buttons button in this . GetPressedGamePadButtons ( ) )
163152 yield return button . ToSButton ( ) ;
@@ -213,9 +202,6 @@ public GamePadState GetState()
213202 /// <summary>Get the pressed gamepad buttons.</summary>
214203 private IEnumerable < Buttons > GetPressedGamePadButtons ( )
215204 {
216- if ( ! this . IsConnected )
217- yield break ;
218-
219205 foreach ( var pair in this . ButtonStates )
220206 {
221207 if ( pair . Value == ButtonState . Pressed && pair . Key . TryGetController ( out Buttons button ) )
0 commit comments