@@ -104,14 +104,22 @@ private void LoadControlScheme()
104104 {
105105 string inputName = selectedScheme [ arrow . Key ] ;
106106
107- if ( inputName . StartsWith ( "Joypad" ) ) // Controller input
107+ if ( inputName . StartsWith ( "Joypad" ) ) // Controller input (D-pad or Stick)
108108 {
109- InputEventJoypadButton eventJoypad = new InputEventJoypadButton ( ) ;
110- eventJoypad . ButtonIndex = GetJoypadButton ( inputName ) ;
109+ if ( inputName . Contains ( "Stick" ) ) // Left Stick Motion
110+ {
111+ InputEventJoypadMotion eventJoypadMotion = new InputEventJoypadMotion ( ) ;
112+ eventJoypadMotion . Axis = GetJoypadAxis ( inputName ) ;
113+ InputMap . ActionAddEvent ( arrow . Key , eventJoypadMotion ) ;
114+ }
111115
112- if ( eventJoypad . ButtonIndex != JoyButton . Invalid ) // Ensure it's valid
116+ // Add D-pad button mapping
117+ InputEventJoypadButton eventJoypadButton = new InputEventJoypadButton ( ) ;
118+ eventJoypadButton . ButtonIndex = GetJoypadButton ( inputName ) ;
119+
120+ if ( eventJoypadButton . ButtonIndex != JoyButton . Invalid )
113121 {
114- InputMap . ActionAddEvent ( arrow . Key , eventJoypad ) ;
122+ InputMap . ActionAddEvent ( arrow . Key , eventJoypadButton ) ;
115123 }
116124 else
117125 {
@@ -120,7 +128,7 @@ private void LoadControlScheme()
120128 }
121129 else // Keyboard input
122130 {
123- if ( Enum . TryParse ( inputName , out Key keycode ) ) // Check if valid keyboard key
131+ if ( Enum . TryParse ( inputName , out Key keycode ) )
124132 {
125133 InputEventKey eventKey = new InputEventKey ( ) ;
126134 eventKey . Keycode = keycode ;
@@ -135,6 +143,18 @@ private void LoadControlScheme()
135143 }
136144 }
137145
146+ private JoyAxis GetJoypadAxis ( string action )
147+ {
148+ return action switch
149+ {
150+ "Joypad_Left_Stick_Up" => JoyAxis . LeftY ,
151+ "Joypad_Left_Stick_Down" => JoyAxis . LeftY ,
152+ "Joypad_Left_Stick_Left" => JoyAxis . LeftX ,
153+ "Joypad_Left_Stick_Right" => JoyAxis . LeftX ,
154+ _ => JoyAxis . Invalid , // Return an invalid axis if unknown
155+ } ;
156+ }
157+
138158 private JoyButton GetJoypadButton ( string action )
139159 {
140160 return action switch
@@ -166,22 +186,56 @@ public override void _Process(double delta)
166186
167187 public override void _Input ( InputEvent @event )
168188 {
169- // Detect if a gamepad button is pressed
189+ // Switch to CONTROLLER scheme if gamepad is used
170190 if ( @event is InputEventJoypadButton || @event is InputEventJoypadMotion )
171191 {
172- // Get the currently active control scheme
173192 string currentScheme = ( string ) ProjectSettings . GetSetting ( "game/input_scheme" ) ;
174-
175- // Switch to "CONTROLLER" scheme if not already active
176193 if ( currentScheme != "CONTROLLER" )
177194 {
178195 GD . Print ( "Gamepad detected, switching to CONTROLLER scheme." ) ;
179196 ProjectSettings . SetSetting ( "game/input_scheme" , "CONTROLLER" ) ;
180197 ProjectSettings . Save ( ) ;
181-
182- // Reload the control scheme to apply changes
183198 LoadControlScheme ( ) ;
184199 }
185200 }
201+
202+ // Detect left stick movement and trigger button presses
203+ if ( @event is InputEventJoypadMotion motionEvent )
204+ {
205+ float deadzone = 0.5f ; // Stick must be moved past this threshold
206+
207+ if ( motionEvent . Axis == JoyAxis . LeftX )
208+ {
209+ if ( motionEvent . AxisValue < - deadzone )
210+ {
211+ Input . ActionPress ( "arrowLeft" ) ;
212+ }
213+ else if ( motionEvent . AxisValue > deadzone )
214+ {
215+ Input . ActionPress ( "arrowRight" ) ;
216+ }
217+ else
218+ {
219+ Input . ActionRelease ( "arrowLeft" ) ;
220+ Input . ActionRelease ( "arrowRight" ) ;
221+ }
222+ }
223+ else if ( motionEvent . Axis == JoyAxis . LeftY )
224+ {
225+ if ( motionEvent . AxisValue < - deadzone )
226+ {
227+ Input . ActionPress ( "arrowUp" ) ;
228+ }
229+ else if ( motionEvent . AxisValue > deadzone )
230+ {
231+ Input . ActionPress ( "arrowDown" ) ;
232+ }
233+ else
234+ {
235+ Input . ActionRelease ( "arrowUp" ) ;
236+ Input . ActionRelease ( "arrowDown" ) ;
237+ }
238+ }
239+ }
186240 }
187241}
0 commit comments