Skip to content

Commit a053421

Browse files
Rmojarro1LifeHckr
authored andcommitted
Control Stick support
Left stick is now usable during battle, can be used interchangably with the d-pad. helper function added in input handler to translate joystick input
1 parent d709955 commit a053421

2 files changed

Lines changed: 70 additions & 12 deletions

File tree

scenes/NoteManager/scripts/InputHandler.cs

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

scenes/Remapping/ControlSchemes.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public static class ControlSchemes
4646
{ "arrowDown", "Joypad_Dpad_Down" },
4747
{ "arrowLeft", "Joypad_Dpad_Left" },
4848
{ "arrowRight", "Joypad_Dpad_Right" },
49+
{ "leftStickUp", "Joypad_Left_Stick_Up" }, // Left Stick Up
50+
{ "leftStickDown", "Joypad_Left_Stick_Down" }, // Left Stick Down
51+
{ "leftStickLeft", "Joypad_Left_Stick_Left" }, // Left Stick Left
52+
{ "leftStickRight", "Joypad_Left_Stick_Right" }, // Left Stick Right
4953
}
5054
},
5155
};

0 commit comments

Comments
 (0)