feat : mobile input controller
This commit is contained in:
@@ -24,15 +24,18 @@ signal effect_selected(effect: int)
|
||||
func _ready():
|
||||
print("[PowerUpUI] _ready called")
|
||||
|
||||
# Connect to SettingsManager to update labels
|
||||
if SettingsManager and not SettingsManager.control_remapped.is_connected(_on_control_remapped):
|
||||
SettingsManager.control_remapped.connect(_on_control_remapped)
|
||||
# Connect to SettingsManager to update labels on rebind OR controller toggle
|
||||
if SettingsManager:
|
||||
if not SettingsManager.control_remapped.is_connected(_on_control_remapped):
|
||||
SettingsManager.control_remapped.connect(_on_control_remapped)
|
||||
if not SettingsManager.settings_applied.is_connected(_on_control_remapped_refresh):
|
||||
SettingsManager.settings_applied.connect(_on_control_remapped_refresh)
|
||||
|
||||
# New Single Button UI
|
||||
power_up_button = get_node_or_null("PowerUpBtn")
|
||||
# New Single Button UI (moved to InteractionBtn)
|
||||
power_up_button = get_node_or_null("../InteractionBtn/PowerUpBtn")
|
||||
if not power_up_button:
|
||||
# Fallback to Container/PowerUpBtn just in case
|
||||
power_up_button = get_node_or_null("Container/PowerUpBtn")
|
||||
# Fallback just in case
|
||||
power_up_button = get_node_or_null("PowerUpBtn")
|
||||
|
||||
if not power_up_button:
|
||||
print("[PowerUpUI] ERROR: PowerUpBtn not found")
|
||||
@@ -51,6 +54,10 @@ func _on_control_remapped(_action: String, _key: int):
|
||||
# Refresh all labels
|
||||
_update_shortcuts_for_mode(LobbyManager.is_game_mode(GameMode.Mode.STOP_N_GO))
|
||||
|
||||
func _on_control_remapped_refresh():
|
||||
"""Called when settings_applied fires (e.g. use_controller toggled)."""
|
||||
_update_shortcuts_for_mode(LobbyManager.is_game_mode(GameMode.Mode.STOP_N_GO))
|
||||
|
||||
func _setup_powerup_btn(btn: Button):
|
||||
# Start DISABLED
|
||||
btn.disabled = true
|
||||
@@ -105,7 +112,7 @@ func _update_btn_shortcut(btn: Button):
|
||||
return
|
||||
|
||||
# Show universal powerup shortcut
|
||||
sc_lbl.text = " %s " % SettingsManager.get_control_text("use_powerup")
|
||||
sc_lbl.text = " %s " % SettingsManager.get_action_display("use_powerup")
|
||||
|
||||
func _on_btn_pressed(effect_id: int = -1):
|
||||
var target_effect = effect_id if effect_id != -1 else current_effect
|
||||
|
||||
+170
-14
@@ -26,6 +26,25 @@ var listening_action: String = "" # Set when waiting for a keypress
|
||||
# Keys removed from the game - skip them even if stale user config has them
|
||||
const DEPRECATED_ACTIONS: Array = ["put", "put_alt"]
|
||||
|
||||
# Touch Input Controls
|
||||
@onready var joystick_size_slider = %JoystickSizeSlider
|
||||
@onready var joystick_size_value = %JoystickSizeValue
|
||||
@onready var joystick_enabled_toggle = %JoystickEnabledToggle
|
||||
@onready var button_opacity_slider = %ButtonOpacitySlider
|
||||
@onready var button_opacity_value = %ButtonOpacityValue
|
||||
@onready var touch_buttons_enabled_toggle = %TouchButtonsEnabledToggle
|
||||
|
||||
const TOUCH_CONFIG_PATH = "user://touch_controls_settings.cfg"
|
||||
|
||||
# Controller Rebinding
|
||||
@onready var ctrl_grab_btn = %CtrlGrabBtn
|
||||
@onready var ctrl_powerup_btn = %CtrlPowerupBtn
|
||||
@onready var ctrl_tekton_grab_btn = %CtrlTektonGrabBtn
|
||||
@onready var ctrl_attack_btn = %CtrlAttackBtn
|
||||
@onready var ctrl_reset_btn = %CtrlResetBtn
|
||||
|
||||
var listening_ctrl_key: String = "" # ctrl_* key being rebound
|
||||
|
||||
func _ready():
|
||||
# Theme inheritance is broken by CanvasLayer root, no need for theme = null
|
||||
_load_ui_values()
|
||||
@@ -87,6 +106,33 @@ func _load_ui_values():
|
||||
# Controls
|
||||
use_controller_btn.button_pressed = s.controls.get("use_controller", false)
|
||||
_update_all_key_labels()
|
||||
|
||||
# Touch Input
|
||||
_load_touch_ui_values()
|
||||
|
||||
# Controller Bindings
|
||||
_update_all_ctrl_labels()
|
||||
|
||||
func _load_touch_ui_values():
|
||||
"""Load touch control settings from config file."""
|
||||
var config = ConfigFile.new()
|
||||
var joystick_size: float = 60.0
|
||||
var joystick_on: bool = true
|
||||
var btn_opacity: float = 0.7
|
||||
var touch_btns_on: bool = true
|
||||
|
||||
if config.load(TOUCH_CONFIG_PATH) == OK:
|
||||
joystick_size = config.get_value("touch_controls", "joystick_size", 60.0)
|
||||
joystick_on = config.get_value("touch_controls", "joystick_enabled", true)
|
||||
btn_opacity = config.get_value("touch_controls", "button_opacity", 0.7)
|
||||
touch_btns_on = config.get_value("touch_controls", "touch_buttons_enabled", true)
|
||||
|
||||
joystick_size_slider.value = joystick_size
|
||||
joystick_size_value.text = str(int(joystick_size))
|
||||
joystick_enabled_toggle.button_pressed = joystick_on
|
||||
button_opacity_slider.value = btn_opacity
|
||||
button_opacity_value.text = str(int(btn_opacity * 100)) + "%"
|
||||
touch_buttons_enabled_toggle.button_pressed = touch_btns_on
|
||||
|
||||
func _connect_signals():
|
||||
# Video
|
||||
@@ -105,8 +151,21 @@ func _connect_signals():
|
||||
# Controls
|
||||
use_controller_btn.toggled.connect(_on_control_setting_changed.bind("use_controller"))
|
||||
|
||||
# Touch Input
|
||||
joystick_size_slider.value_changed.connect(_on_joystick_size_changed)
|
||||
joystick_enabled_toggle.toggled.connect(_on_touch_toggle_changed.bind("joystick_enabled"))
|
||||
button_opacity_slider.value_changed.connect(_on_button_opacity_changed)
|
||||
touch_buttons_enabled_toggle.toggled.connect(_on_touch_toggle_changed.bind("touch_buttons_enabled"))
|
||||
|
||||
# Controller Rebinding
|
||||
ctrl_grab_btn.pressed.connect(_on_ctrl_remap_pressed.bind("ctrl_grab"))
|
||||
ctrl_powerup_btn.pressed.connect(_on_ctrl_remap_pressed.bind("ctrl_use_powerup"))
|
||||
ctrl_tekton_grab_btn.pressed.connect(_on_ctrl_remap_pressed.bind("ctrl_tekton_grab"))
|
||||
ctrl_attack_btn.pressed.connect(_on_ctrl_remap_pressed.bind("ctrl_attack_mode"))
|
||||
ctrl_reset_btn.pressed.connect(_on_ctrl_reset_pressed)
|
||||
|
||||
# Close
|
||||
close_button.pressed.connect(func(): visible = false)
|
||||
close_button.pressed.connect(func(): listening_ctrl_key = ""; visible = false)
|
||||
|
||||
# Connect remapping buttons
|
||||
for action_name in SettingsManager.settings.controls.keys():
|
||||
@@ -153,6 +212,54 @@ func _on_audio_setting_changed(value: float, key: String):
|
||||
func _on_control_setting_changed(value: bool, key: String):
|
||||
SettingsManager.settings.controls[key] = value
|
||||
SettingsManager.save_settings()
|
||||
# Emit settings_applied so all UI (touch buttons, prompts) refresh their shortcut labels
|
||||
if key == "use_controller":
|
||||
SettingsManager.apply_control_settings()
|
||||
SettingsManager.emit_signal("settings_applied")
|
||||
|
||||
func _on_joystick_size_changed(value: float):
|
||||
joystick_size_value.text = str(int(value))
|
||||
_save_touch_value("joystick_size", value)
|
||||
# Live update the joystick if it's in the scene
|
||||
var tc = _get_touch_controls()
|
||||
if tc:
|
||||
tc.joystick_size = value
|
||||
if tc.virtual_joystick and tc.virtual_joystick.has_method("set_radius"):
|
||||
tc.virtual_joystick.set_radius(value)
|
||||
|
||||
func _on_button_opacity_changed(value: float):
|
||||
button_opacity_value.text = str(int(value * 100)) + "%"
|
||||
_save_touch_value("button_opacity", value)
|
||||
var tc = _get_touch_controls()
|
||||
if tc:
|
||||
tc.button_opacity = value
|
||||
if tc.has_method("_apply_settings"):
|
||||
tc._apply_settings()
|
||||
|
||||
func _on_touch_toggle_changed(state: bool, key: String):
|
||||
_save_touch_value(key, state)
|
||||
var tc = _get_touch_controls()
|
||||
if tc:
|
||||
if key == "joystick_enabled":
|
||||
tc.joystick_enabled = state
|
||||
elif key == "touch_buttons_enabled":
|
||||
tc.touch_buttons_enabled = state
|
||||
if tc.has_method("_apply_settings"):
|
||||
tc._apply_settings()
|
||||
|
||||
func _save_touch_value(key: String, value) -> void:
|
||||
"""Save a single touch config key without overwriting other keys."""
|
||||
var config = ConfigFile.new()
|
||||
config.load(TOUCH_CONFIG_PATH) # Load existing (ignore error if not found)
|
||||
config.set_value("touch_controls", key, value)
|
||||
config.save(TOUCH_CONFIG_PATH)
|
||||
|
||||
func _get_touch_controls() -> Node:
|
||||
"""Try to find the TouchControls node in the Main scene."""
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if main:
|
||||
return main.get_node_or_null("TouchLayer/TouchControls")
|
||||
return null
|
||||
|
||||
func _on_remap_button_pressed(action_name: String):
|
||||
listening_action = action_name
|
||||
@@ -162,40 +269,51 @@ func _on_remap_button_pressed(action_name: String):
|
||||
btn.modulate = Color(0.5, 1.0, 0.5) # Signal "Listening"
|
||||
|
||||
func _input(event):
|
||||
# Keyboard remapping
|
||||
if listening_action != "" and event is InputEventKey and event.pressed:
|
||||
# Capture the key
|
||||
var keycode = event.keycode
|
||||
|
||||
# PREVENT DUPLICATES (User Request)
|
||||
var existing_action = SettingsManager.is_key_used(keycode)
|
||||
if existing_action != "" and existing_action != listening_action:
|
||||
# Visual Feedback for Error
|
||||
var error_btn = get_node_or_null("%" + listening_action.to_pascal_case() + "Btn")
|
||||
if error_btn:
|
||||
error_btn.text = "ALREADY USED!"
|
||||
error_btn.modulate = Color(1.0, 0.3, 0.3) # Red error
|
||||
|
||||
# Play sound if available
|
||||
error_btn.modulate = Color(1.0, 0.3, 0.3)
|
||||
var sfx = get_node_or_null("/root/SfxManager")
|
||||
if sfx and sfx.has_method("play_rpc"):
|
||||
sfx.rpc("play_rpc", "error") # Assumes an error sound exists
|
||||
|
||||
# Reset listening but keep text for briefly before reverting
|
||||
sfx.rpc("play_rpc", "error")
|
||||
listening_action = ""
|
||||
await get_tree().create_timer(1.2).timeout
|
||||
_update_all_key_labels()
|
||||
|
||||
get_viewport().set_input_as_handled()
|
||||
return
|
||||
|
||||
# Proceed with remapping if not a duplicate
|
||||
SettingsManager.set_control(listening_action, keycode)
|
||||
|
||||
# Feedback and Reset
|
||||
_update_key_label(listening_action)
|
||||
listening_action = ""
|
||||
get_viewport().set_input_as_handled()
|
||||
|
||||
# Controller button remapping
|
||||
if listening_ctrl_key != "" and event is InputEventJoypadButton and event.pressed:
|
||||
var btn_idx = event.button_index
|
||||
|
||||
# Consume event to prevent triggers
|
||||
# Duplicate check
|
||||
var existing = SettingsManager.is_controller_button_used(btn_idx)
|
||||
var ctrl_btn = _get_ctrl_btn(listening_ctrl_key)
|
||||
if existing != "" and existing != listening_ctrl_key:
|
||||
if ctrl_btn:
|
||||
ctrl_btn.text = "ALREADY USED!"
|
||||
ctrl_btn.modulate = Color(1.0, 0.3, 0.3)
|
||||
listening_ctrl_key = ""
|
||||
await get_tree().create_timer(1.2).timeout
|
||||
_update_all_ctrl_labels()
|
||||
get_viewport().set_input_as_handled()
|
||||
return
|
||||
|
||||
SettingsManager.set_controller_binding(listening_ctrl_key, btn_idx)
|
||||
listening_ctrl_key = ""
|
||||
_update_all_ctrl_labels()
|
||||
get_viewport().set_input_as_handled()
|
||||
|
||||
func _update_all_key_labels():
|
||||
@@ -210,6 +328,44 @@ func _update_key_label(action_name: String):
|
||||
btn.text = SettingsManager.get_control_text(action_name)
|
||||
btn.modulate = Color.WHITE
|
||||
|
||||
func _update_all_ctrl_labels():
|
||||
"""Update all controller button labels from saved settings."""
|
||||
if not SettingsManager: return
|
||||
ctrl_grab_btn.text = SettingsManager.get_controller_binding_text("ctrl_grab")
|
||||
ctrl_grab_btn.modulate = Color.WHITE
|
||||
ctrl_powerup_btn.text = SettingsManager.get_controller_binding_text("ctrl_use_powerup")
|
||||
ctrl_powerup_btn.modulate = Color.WHITE
|
||||
ctrl_tekton_grab_btn.text = SettingsManager.get_controller_binding_text("ctrl_tekton_grab")
|
||||
ctrl_tekton_grab_btn.modulate = Color.WHITE
|
||||
ctrl_attack_btn.text = SettingsManager.get_controller_binding_text("ctrl_attack_mode")
|
||||
ctrl_attack_btn.modulate = Color.WHITE
|
||||
|
||||
func _on_ctrl_remap_pressed(ctrl_key: String):
|
||||
listening_ctrl_key = ctrl_key
|
||||
var btn = _get_ctrl_btn(ctrl_key)
|
||||
if btn:
|
||||
btn.text = "Press a button..."
|
||||
btn.modulate = Color(0.5, 1.0, 0.5)
|
||||
|
||||
func _get_ctrl_btn(ctrl_key: String) -> Button:
|
||||
match ctrl_key:
|
||||
"ctrl_grab": return ctrl_grab_btn
|
||||
"ctrl_use_powerup": return ctrl_powerup_btn
|
||||
"ctrl_tekton_grab": return ctrl_tekton_grab_btn
|
||||
"ctrl_attack_mode": return ctrl_attack_btn
|
||||
return null
|
||||
|
||||
func _on_ctrl_reset_pressed():
|
||||
"""Reset controller bindings to defaults."""
|
||||
if not SettingsManager: return
|
||||
SettingsManager.settings.controls["ctrl_grab"] = JOY_BUTTON_A
|
||||
SettingsManager.settings.controls["ctrl_use_powerup"] = JOY_BUTTON_Y
|
||||
SettingsManager.settings.controls["ctrl_tekton_grab"] = JOY_BUTTON_RIGHT_SHOULDER
|
||||
SettingsManager.settings.controls["ctrl_attack_mode"] = JOY_BUTTON_X
|
||||
SettingsManager.apply_control_settings()
|
||||
SettingsManager.save_settings()
|
||||
_update_all_ctrl_labels()
|
||||
|
||||
func open():
|
||||
_load_ui_values()
|
||||
visible = true
|
||||
|
||||
@@ -27,14 +27,28 @@ var _repeat_timer: float = 0.0
|
||||
var _initial_repeat: bool = true
|
||||
|
||||
func _ready():
|
||||
# Set minimum size for touch target
|
||||
custom_minimum_size = Vector2(joystick_radius * 2 + 40, joystick_radius * 2 + 40)
|
||||
_update_minimum_size()
|
||||
center_position = size / 2
|
||||
|
||||
# Enable touch input
|
||||
mouse_filter = Control.MOUSE_FILTER_STOP
|
||||
set_process(true)
|
||||
|
||||
func set_radius(new_radius: float):
|
||||
joystick_radius = new_radius
|
||||
# Proportional knob
|
||||
knob_radius = new_radius * (25.0 / 60.0)
|
||||
|
||||
_update_minimum_size()
|
||||
# Optional: recalculate center if already initialized
|
||||
if is_inside_tree():
|
||||
center_position = size / 2
|
||||
queue_redraw()
|
||||
|
||||
func _update_minimum_size():
|
||||
custom_minimum_size = Vector2(joystick_radius * 2 + 40, joystick_radius * 2 + 40)
|
||||
size = custom_minimum_size
|
||||
|
||||
func _draw():
|
||||
# Draw base circle
|
||||
var base_circle_color = pressed_color if is_pressed else base_color
|
||||
|
||||
Reference in New Issue
Block a user