feat : mobile input controller

This commit is contained in:
2026-04-13 18:15:49 +08:00
parent a478e3fc2e
commit a592eb1de0
25 changed files with 771 additions and 2058 deletions
+85 -1
View File
@@ -53,7 +53,13 @@ var settings = {
# Power Bar Controls / Special
"attack_mode": KEY_Q,
"attack_mode_alt": KEY_U
"attack_mode_alt": KEY_U,
# Controller Button Bindings (JOY_BUTTON_* indices)
"ctrl_grab": JOY_BUTTON_A,
"ctrl_tekton_grab": JOY_BUTTON_RIGHT_SHOULDER,
"ctrl_use_powerup": JOY_BUTTON_Y,
"ctrl_attack_mode": JOY_BUTTON_X
}
}
@@ -207,6 +213,21 @@ func apply_control_settings():
event.keycode = secondary_key
InputMap.action_add_event(action_name, event)
# Add Joypad defaults for actions - use saved ctrl_* bindings
var joy_button_mapped = -1
match action_name:
"action_grab": joy_button_mapped = settings.controls.get("ctrl_grab", JOY_BUTTON_A)
"use_powerup": joy_button_mapped = settings.controls.get("ctrl_use_powerup", JOY_BUTTON_Y)
"action_grab_tekton": joy_button_mapped = settings.controls.get("ctrl_tekton_grab", JOY_BUTTON_RIGHT_SHOULDER)
"action_knock_tekton": joy_button_mapped = settings.controls.get("ctrl_attack_mode", JOY_BUTTON_X)
if action_name == "action_put": joy_button_mapped = JOY_BUTTON_B
if joy_button_mapped != -1:
var joy_btn_event = InputEventJoypadButton.new()
joy_btn_event.button_index = joy_button_mapped
InputMap.action_add_event(action_name, joy_btn_event)
# Add Joypad defaults for movement
if action_name.begins_with("move_"):
var joy_axis = -1
@@ -230,6 +251,69 @@ func set_control(action_name: String, keycode: int):
emit_signal("control_remapped", action_name, keycode)
save_settings()
# --- Controller Binding ---
# Godot 4 JoyButton name map (var not const — enum keys can't be used in const dicts)
var CTRL_BUTTON_NAMES: Dictionary = {
JOY_BUTTON_A: "A / Cross",
JOY_BUTTON_B: "B / Circle",
JOY_BUTTON_X: "X / Square",
JOY_BUTTON_Y: "Y / Triangle",
JOY_BUTTON_LEFT_SHOULDER: "LB / L1",
JOY_BUTTON_RIGHT_SHOULDER: "RB / R1",
JOY_BUTTON_LEFT_STICK: "L3",
JOY_BUTTON_RIGHT_STICK: "R3",
JOY_BUTTON_BACK: "Select / Share",
JOY_BUTTON_START: "Start / Options",
JOY_BUTTON_DPAD_UP: "D-Up",
JOY_BUTTON_DPAD_DOWN: "D-Down",
JOY_BUTTON_DPAD_LEFT: "D-Left",
JOY_BUTTON_DPAD_RIGHT: "D-Right",
}
func get_joy_button_name(button_index: int) -> String:
return CTRL_BUTTON_NAMES.get(button_index, "Btn %d" % button_index)
func get_controller_binding_text(ctrl_key: String) -> String:
"""Return the button name for a ctrl_* settings key."""
var idx = settings.controls.get(ctrl_key, -1)
if idx == -1:
return "Unbound"
return get_joy_button_name(idx)
func get_action_display(action_key: String) -> String:
"""Return keyboard text or controller button name based on use_controller setting."""
if settings.controls.get("use_controller", false):
# Map action key to its ctrl_* key
var ctrl_key_map = {
"grab": "ctrl_grab",
"use_powerup": "ctrl_use_powerup",
"tekton_grab": "ctrl_tekton_grab",
"attack_mode": "ctrl_attack_mode",
}
if ctrl_key_map.has(action_key):
return get_controller_binding_text(ctrl_key_map[action_key])
# Movement axes on left stick
if action_key.begins_with("move_"):
return "L-Stick"
return get_control_text(action_key)
return get_control_text(action_key)
func set_controller_binding(ctrl_key: String, button_index: int):
"""Save a controller button binding and re-apply input maps."""
if settings.controls.has(ctrl_key):
settings.controls[ctrl_key] = button_index
apply_control_settings()
emit_signal("control_remapped", ctrl_key, button_index)
save_settings()
func is_controller_button_used(button_index: int) -> String:
"""Check if a controller button is already assigned."""
for key in settings.controls.keys():
if key.begins_with("ctrl_") and settings.controls[key] == button_index:
return key
return ""
func get_control_keycode(action_name: String) -> int:
return settings.controls.get(action_name, -1)