chore: remove gamepad code. Closes #159

This commit is contained in:
2025-10-06 02:37:51 +02:00
parent 28b54607c1
commit a142e7f16e

View File

@@ -51,31 +51,9 @@ Implement methods to react to inputs.
- _on_event_done(event_name: String)
"""
# Value to use for `device` argument to various `Input.get_joy` methods.
const JOY_DEVICE = 0
# See https://docs.godotengine.org/en/stable/tutorials/inputs/controllers_gamepads_joysticks.html?#dead-zone
const DEADZONE = 0.2
# Multiplier to apply to axis when it exceeds DEADZONE.
const AXIS_WEIGHT = 50.0
# JOY_BUTTON_X corresponds to the "X" button on an XBox controller
# or the Square button on a Playstation controller. These appear to
# map to the "primary action," in practice, so we treat it like a left click.
const PRIMARY_ACTION_BUTTON = JOY_BUTTON_X
# JOY_BUTTON_Y corresponds to the "Y" button on an XBox controller
# or the Triangle button on a Playstation controller. These appear to
# map to the "secondary action," in practice, so we treat it like a right click.
const CHANGE_VERB_BUTTON = JOY_BUTTON_Y
# Input action for use by InputMap
const ESC_UI_CHANGE_VERB_ACTION = "esc_change_verb"
# true when a gamepad is connected.
var _is_gamepad_connected = false
# Tracks the mouse's current position onscreen.
var _current_mouse_pos = Vector2.ZERO
@@ -115,17 +93,6 @@ func _ready():
func _enter_tree():
var input_handler = Callable(self, "_process_input")
escoria.inputs_manager.register_custom_input_handler(input_handler)
_is_gamepad_connected = Input.is_joy_known(JOY_DEVICE)
if _is_gamepad_connected:
_on_gamepad_connected()
if Input.connect("joy_connection_changed", Callable(self, "_on_joy_connection_changed")) != 0:
escoria.logger.error(self, "Error connecting joy_connection_changed")
# We get current day and month
var time = Time.get_datetime_dict_from_system()
var day = time["day"]
@@ -134,9 +101,6 @@ func _enter_tree():
func _exit_tree():
escoria.inputs_manager.register_custom_input_handler(null)
Input.disconnect("joy_connection_changed", Callable(self, "_on_joy_connection_changed"))
if _is_gamepad_connected:
_on_gamepad_disconnected()
func toggle_room_selector_visibility():
@@ -159,79 +123,7 @@ func _input(event: InputEvent) -> void:
if event.is_action_pressed("ui_dev_tools"):
dev_tools_node.visible = !dev_tools_node.visible
# https://github.com/godotengine/godot-demo-projects/blob/3.4-585455e/misc/joypads/joypads.gd
# was informative in wiring up the gamepad properly.
func _on_gamepad_connected():
set_physics_process(true)
var primary_event = InputEventJoypadButton.new()
primary_event.button_index = PRIMARY_ACTION_BUTTON
InputMap.add_action(escoria.inputs_manager.ESC_UI_PRIMARY_ACTION)
InputMap.action_add_event(escoria.inputs_manager.ESC_UI_PRIMARY_ACTION, primary_event)
var verb_event = InputEventJoypadButton.new()
verb_event.button_index = CHANGE_VERB_BUTTON
InputMap.add_action(ESC_UI_CHANGE_VERB_ACTION)
InputMap.action_add_event(ESC_UI_CHANGE_VERB_ACTION, verb_event)
func _on_gamepad_disconnected():
InputMap.action_erase_events(escoria.inputs_manager.ESC_UI_PRIMARY_ACTION)
InputMap.erase_action(escoria.inputs_manager.ESC_UI_PRIMARY_ACTION)
InputMap.action_erase_events(ESC_UI_CHANGE_VERB_ACTION)
InputMap.erase_action(ESC_UI_CHANGE_VERB_ACTION)
set_physics_process(false)
_is_gamepad_connected = false
func _on_joy_connection_changed(device: int, connected: bool) -> void:
if device != JOY_DEVICE:
return
elif connected:
_on_gamepad_connected()
else:
_on_gamepad_disconnected()
func _process(_delta) -> void:
if !_is_gamepad_connected:
return
var x = Input.get_joy_axis(JOY_DEVICE, JOY_AXIS_LEFT_X)
var y = Input.get_joy_axis(JOY_DEVICE, JOY_AXIS_LEFT_Y)
var delta_x = int(x * AXIS_WEIGHT) if abs(x) > DEADZONE else 0
var delta_y = int(y * AXIS_WEIGHT) if abs(y) > DEADZONE else 0
if delta_x or delta_y:
var direction = Vector2(delta_x, delta_y)
escoria.logger.trace(self, "gamepad direction: %s" % [direction])
var viewport = get_viewport()
viewport.warp_mouse(viewport.get_mouse_position() + direction)
func _process_input(event: InputEvent, is_default_state: bool) -> bool:
if not is_default_state:
# ESCBackground is not guaranteed to be set, as we may be on
# the "New Game" screen.
return false
elif _is_gamepad_connected and event is InputEventJoypadButton:
escoria.logger.trace(self, "InputEventJoypadButton: %s" % [event.as_text()])
if event.is_action_pressed(escoria.inputs_manager.ESC_UI_PRIMARY_ACTION):
# Admittedly, this breaks abstraction barriers and is completely
# inappropriate, but it's what works right now.
escoria.inputs_manager._on_left_click_on_bg(get_global_mouse_position())
return true
elif event.is_action_pressed(ESC_UI_CHANGE_VERB_ACTION):
escoria.logger.error(self, "Someone invoked ESC_UI_CHANGE_VERB_ACTION: %s" % [event.as_text()])
return true
return false
## BACKGROUND ##
func click_on_bg(position: Vector2) -> void:
var current_state = escoria.action_manager.action_state
@@ -267,7 +159,6 @@ func left_double_click_on_bg(position: Vector2) -> void:
)
## ITEM/HOTSPOT FOCUS ##
func element_focused(element_id: String) -> void:
var target_obj = escoria.object_manager.get_object(element_id).node
if target_obj is ESCItem or ESCItemWithTooltip: