feat: add gamepad support to escoria-ui-simplemouse (#518)
This commit starts by introducing a new action, `ESC_UI_PRIMARY_ACTION`, that is used to represent a "primary action" from an input device, such as a left-click on a mouse or the press of the primary button on a gamepad. Although this action is not added to `InputMap` by default in `escoria-core`, `_unhandled_input()` in `esc_item.gd` is updated to look for it. The other portion of this commit is an update to `addons/escoria-ui-simplemouse/game.gd` that adds a gamepad mapping to the `InputMap` for both `ESC_UI_PRIMARY_ACTION` as well as another new action `ESC_UI_CHANGE_VERB_ACTION`. These actions are mapped to X and Y on an XBox controller, respectively. Note that `game.gd` is also updated to implement `_process()` such that moving `JOY_AXIS_0` and `JOY_AXIS_1` on "gamepad 0" will move the cursor around on the screen as the mouse would. Overall, this makes the game fairly playable with a gamepad, though admittedly a mouse is still currently required to click the "New game" button on the initial menu because `BaseButton::gui_input()` appears to be hardcoded to check for mouse events exclusively: godotengine/godot@a09814e/scene/gui/base_button.cpp#L55-L81
This commit is contained in:
@@ -268,8 +268,8 @@ func _ready():
|
||||
#
|
||||
# #### Parameters
|
||||
#
|
||||
# - event: Triggered event
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
# - input_event: Triggered event
|
||||
func _unhandled_input(input_event: InputEvent) -> void:
|
||||
# If this is a trigger, then escoria.inputs_manager is not wired up to
|
||||
# receive the signals this function might dispatch. In particular,
|
||||
# calling get_tree().set_input_as_handled() unnecessarily will prevent
|
||||
@@ -278,6 +278,24 @@ func _unhandled_input(event: InputEvent) -> void:
|
||||
if is_trigger:
|
||||
return
|
||||
|
||||
var event = input_event
|
||||
# Note that event could be InputEventMouseButton, InputEventJoypadButton,
|
||||
# or something else. As such, the value of the `button_index` property
|
||||
# must be read in the context of the type of input event.
|
||||
if input_event is InputEventJoypadButton:
|
||||
if not input_event.is_action_pressed(escoria.inputs_manager.ESC_UI_PRIMARY_ACTION):
|
||||
return
|
||||
|
||||
# For now, rather than refactor input handling to be more generic
|
||||
# to accommodate gamepad support, we create a synthetic mouse event
|
||||
# based on the InputEventJoypadButton.
|
||||
event = InputEventMouseButton.new()
|
||||
event.button_index = BUTTON_LEFT
|
||||
event.doubleclick = false
|
||||
event.pressed = true
|
||||
# ESCActionManager expects to read the position off of the event.
|
||||
event.position = get_global_mouse_position()
|
||||
|
||||
if event is InputEventMouseButton and event.is_pressed():
|
||||
if not escoria.current_state == escoria.GAME_STATE.DEFAULT:
|
||||
escoria.logger.info("Game state doesn't accept interactions")
|
||||
|
||||
Reference in New Issue
Block a user