Skip to content

Commit d709955

Browse files
Rmojarro1LifeHckr
authored andcommitted
Added basic controller support
Added new button for controller in settings, will also swap to controller controls if gamepad buttons are pressed in battle if they were not selected. currently only for d-pad directions, added functionality for menu navigation, pausing game(start button), opening inventory(select button), confirming (A button). Testing with 8BitDo Ultimate Cotroller
1 parent d6ea540 commit d709955

12 files changed

Lines changed: 241 additions & 3 deletions

scenes/NoteManager/scripts/InputHandler.cs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,56 @@ private void LoadControlScheme()
9797
}
9898

9999
var selectedScheme = ControlSchemes.Schemes[scheme];
100+
100101
foreach (var arrow in Arrows)
101102
{
102103
if (selectedScheme.ContainsKey(arrow.Key))
103104
{
104-
InputEventKey eventKey = new InputEventKey();
105-
eventKey.Keycode = (Key)Enum.Parse(typeof(Key), selectedScheme[arrow.Key]);
106-
InputMap.ActionAddEvent(arrow.Key, eventKey);
105+
string inputName = selectedScheme[arrow.Key];
106+
107+
if (inputName.StartsWith("Joypad")) // Controller input
108+
{
109+
InputEventJoypadButton eventJoypad = new InputEventJoypadButton();
110+
eventJoypad.ButtonIndex = GetJoypadButton(inputName);
111+
112+
if (eventJoypad.ButtonIndex != JoyButton.Invalid) // Ensure it's valid
113+
{
114+
InputMap.ActionAddEvent(arrow.Key, eventJoypad);
115+
}
116+
else
117+
{
118+
GD.PrintErr($"Invalid joypad button mapping: {inputName}");
119+
}
120+
}
121+
else // Keyboard input
122+
{
123+
if (Enum.TryParse(inputName, out Key keycode)) // Check if valid keyboard key
124+
{
125+
InputEventKey eventKey = new InputEventKey();
126+
eventKey.Keycode = keycode;
127+
InputMap.ActionAddEvent(arrow.Key, eventKey);
128+
}
129+
else
130+
{
131+
GD.PrintErr($"Invalid key mapping: {inputName}");
132+
}
133+
}
107134
}
108135
}
109136
}
110137

138+
private JoyButton GetJoypadButton(string action)
139+
{
140+
return action switch
141+
{
142+
"Joypad_Dpad_Up" => JoyButton.DpadUp,
143+
"Joypad_Dpad_Down" => JoyButton.DpadDown,
144+
"Joypad_Dpad_Left" => JoyButton.DpadLeft,
145+
"Joypad_Dpad_Right" => JoyButton.DpadRight,
146+
_ => JoyButton.Invalid, // Return an invalid button if unknown
147+
};
148+
}
149+
111150
public override void _Process(double delta)
112151
{
113152
foreach (var arrow in Arrows)
@@ -124,4 +163,25 @@ public override void _Process(double delta)
124163
}
125164
}
126165
}
166+
167+
public override void _Input(InputEvent @event)
168+
{
169+
// Detect if a gamepad button is pressed
170+
if (@event is InputEventJoypadButton || @event is InputEventJoypadMotion)
171+
{
172+
// Get the currently active control scheme
173+
string currentScheme = (string)ProjectSettings.GetSetting("game/input_scheme");
174+
175+
// Switch to "CONTROLLER" scheme if not already active
176+
if (currentScheme != "CONTROLLER")
177+
{
178+
GD.Print("Gamepad detected, switching to CONTROLLER scheme.");
179+
ProjectSettings.SetSetting("game/input_scheme", "CONTROLLER");
180+
ProjectSettings.Save();
181+
182+
// Reload the control scheme to apply changes
183+
LoadControlScheme();
184+
}
185+
}
186+
}
127187
}

scenes/Remapping/ControlSchemes.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ public static class ControlSchemes
3838
{ "arrowRight", "R" },
3939
}
4040
},
41+
{
42+
"CONTROLLER",
43+
new Dictionary<string, string>()
44+
{
45+
{ "arrowUp", "Joypad_Dpad_Up" },
46+
{ "arrowDown", "Joypad_Dpad_Down" },
47+
{ "arrowLeft", "Joypad_Dpad_Left" },
48+
{ "arrowRight", "Joypad_Dpad_Right" },
49+
}
50+
},
4151
};
4252

4353
public static Dictionary<string, Dictionary<string, string>> SpriteMappings = new Dictionary<
@@ -75,5 +85,15 @@ public static class ControlSchemes
7585
{ "down", "res://scenes/Remapping/assets/E_Key_Light.png" },
7686
}
7787
},
88+
{
89+
"CONTROLLER",
90+
new Dictionary<string, string>()
91+
{
92+
{ "left", "res://scenes/Remapping/assets/360_Dpad_Left.png" },
93+
{ "right", "res://scenes/Remapping/assets/360_Dpad_Right.png" },
94+
{ "up", "res://scenes/Remapping/assets/360_Dpad_Up.png" },
95+
{ "down", "res://scenes/Remapping/assets/360_Dpad_Down.png" },
96+
}
97+
},
7898
};
7999
}

scenes/Remapping/ControlSettings.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public override void _Ready()
2828
.Connect("pressed", Callable.From(OnArrowButtonPressed));
2929
GetNode<Button>("Panel/QWERTButton")
3030
.Connect("pressed", Callable.From(OnQWERTButtonPressed));
31+
GetNode<Button>("Panel/ControllerButton")
32+
.Connect("pressed", Callable.From(OnControllerButtonPressed));
3133

3234
string scheme = SaveSystem.GetConfigValue(SaveSystem.ConfigSettings.InputKey).As<string>();
3335
switch (scheme)
@@ -44,6 +46,10 @@ public override void _Ready()
4446
OnWASDButtonPressed();
4547
GetNode<Button>("Panel/WASDButton").GrabFocus();
4648
break;
49+
case "CONTROLLER":
50+
OnControllerButtonPressed();
51+
GetNode<Button>("Panel/ControllerButton").GrabFocus();
52+
break;
4753
}
4854

4955
_closeButton.Pressed += CloseMenu;
@@ -95,6 +101,14 @@ private void OnQWERTButtonPressed()
95101
ChangeKeySprites("QWERT");
96102
}
97103

104+
private void OnControllerButtonPressed()
105+
{
106+
GetNode<Label>("Panel/Label").Text = "Controller Selected";
107+
ProjectSettings.SetSetting("game/input_scheme", "CONTROLLER");
108+
ProjectSettings.Save();
109+
ChangeKeySprites("CONTROLLER");
110+
}
111+
98112
private void ChangeKeySprites(string scheme)
99113
{
100114
var selectedScheme = ControlSchemes.SpriteMappings[scheme];

scenes/Remapping/Remap.tscn

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ offset_right = 603.0
6060
offset_bottom = 125.0
6161
text = "CONTROLS_QWER_BUTTON"
6262

63+
[node name="ControllerButton" type="Button" parent="Panel"]
64+
layout_mode = 0
65+
offset_left = 404.0
66+
offset_top = 57.0
67+
offset_right = 491.0
68+
offset_bottom = 88.0
69+
text = "Controller"
70+
6371
[node name="TitleButton" type="Button" parent="Panel"]
6472
layout_mode = 0
6573
offset_left = 232.0
5.13 KB
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://ccxvc4o887ugg"
6+
path="res://.godot/imported/360_Dpad_Down.png-38115a1bb92c3261d97311636caf8e7f.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://scenes/Remapping/assets/360_Dpad_Down.png"
14+
dest_files=["res://.godot/imported/360_Dpad_Down.png-38115a1bb92c3261d97311636caf8e7f.ctex"]
15+
16+
[params]
17+
18+
compress/mode=0
19+
compress/high_quality=false
20+
compress/lossy_quality=0.7
21+
compress/hdr_compression=1
22+
compress/normal_map=0
23+
compress/channel_pack=0
24+
mipmaps/generate=false
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
28+
process/fix_alpha_border=true
29+
process/premult_alpha=false
30+
process/normal_map_invert_y=false
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1
5.08 KB
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://gkgf0qk7t2qd"
6+
path="res://.godot/imported/360_Dpad_Left.png-927dcd2d6c67bc56b6e9b7829035b4dc.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://scenes/Remapping/assets/360_Dpad_Left.png"
14+
dest_files=["res://.godot/imported/360_Dpad_Left.png-927dcd2d6c67bc56b6e9b7829035b4dc.ctex"]
15+
16+
[params]
17+
18+
compress/mode=0
19+
compress/high_quality=false
20+
compress/lossy_quality=0.7
21+
compress/hdr_compression=1
22+
compress/normal_map=0
23+
compress/channel_pack=0
24+
mipmaps/generate=false
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
28+
process/fix_alpha_border=true
29+
process/premult_alpha=false
30+
process/normal_map_invert_y=false
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1
5.05 KB
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://crpuoy61vvk7l"
6+
path="res://.godot/imported/360_Dpad_Right.png-0aec790dee5a162107b568ed08de5343.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://scenes/Remapping/assets/360_Dpad_Right.png"
14+
dest_files=["res://.godot/imported/360_Dpad_Right.png-0aec790dee5a162107b568ed08de5343.ctex"]
15+
16+
[params]
17+
18+
compress/mode=0
19+
compress/high_quality=false
20+
compress/lossy_quality=0.7
21+
compress/hdr_compression=1
22+
compress/normal_map=0
23+
compress/channel_pack=0
24+
mipmaps/generate=false
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
28+
process/fix_alpha_border=true
29+
process/premult_alpha=false
30+
process/normal_map_invert_y=false
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1

0 commit comments

Comments
 (0)