diff --git a/README.md b/README.md index 51b98d6..b2f8dde 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ You can [download a copy](/~https://github.com/nathanhoad/godot_input_helper/archi - [Input Mapping](docs/Mapping.md) - [Rumbling the joypad](docs/Rumbling.md) - [C#](docs/CSharp.md) -- [Updating from Input Helper 3 to 4](3to4.md) +- [Updating from Input Helper 3 to 4](3To4.md) ## Video overview diff --git a/addons/input_helper/input_helper.gd b/addons/input_helper/input_helper.gd index 83f4fa1..5c1037f 100644 --- a/addons/input_helper/input_helper.gd +++ b/addons/input_helper/input_helper.gd @@ -15,6 +15,10 @@ const DEVICE_SWITCH_JOYCON_RIGHT_CONTROLLER = "switch_right_joycon" const DEVICE_PLAYSTATION_CONTROLLER = "playstation" const DEVICE_GENERIC = "generic" +const XBOX_BUTTON_LABELS = ["A", "B", "X", "Y", "Back", "Home", "Menu", "Left Stick", "Right Stick", "Left Shoulder", "Right Shoulder", "Up", "Down", "Left", "Right", "Share"] +const SWITCH_BUTTON_LABELS = ["B", "A", "Y", "X", "-", "", "+", "Left Stick", "Right Stick", "Left Shoulder", "Right Shoulder", "Up", "Down", "Left", "Right", "Capture"] +const PLAYSTATION_BUTTON_LABELS = ["Cross", "Circle", "Square", "Triangle", "Select", "PS", "Options", "L3", "R3", "L1", "R1", "Up", "Down", "Left", "Right", "Microphone"] + @onready var device: String = guess_device_name() @onready var device_index: int = 0 if has_joypad() else -1 @@ -33,7 +37,7 @@ func _input(event: InputEvent) -> void: var next_device_index: int = device_index # Did we just press a key on the keyboard? - if event is InputEventKey or event is InputEventMouse: + if event is InputEventKey or event is InputEventMouseButton: next_device = DEVICE_KEYBOARD next_device_index = -1 @@ -139,6 +143,7 @@ func get_label_for_input(input: InputEvent) -> String: return OS.get_keycode_string(input.keycode) else: return input.as_text() + elif input is InputEventMouseButton: match input.button_index: MOUSE_BUTTON_LEFT: @@ -149,6 +154,30 @@ func get_label_for_input(input: InputEvent) -> String: return "Mouse Right Button" return "Mouse Button %d" % input + elif input is InputEventJoypadButton: + match device: + DEVICE_XBOX_CONTROLLER, DEVICE_GENERIC: + return "%s Button" % XBOX_BUTTON_LABELS[input.button_index] + DEVICE_SWITCH_CONTROLLER, DEVICE_SWITCH_JOYCON_LEFT_CONTROLLER, DEVICE_SWITCH_JOYCON_RIGHT_CONTROLLER: + return "%s Button" % SWITCH_BUTTON_LABELS[input.button_index] + DEVICE_PLAYSTATION_CONTROLLER: + return "%s Button" % PLAYSTATION_BUTTON_LABELS[input.button_index] + elif input is InputEventJoypadMotion: + var motion: InputEventJoypadMotion = input as InputEventJoypadMotion + match motion.axis: + JOY_AXIS_LEFT_X: + return "Left Stick %s" % ("Left" if motion.axis_value < 0 else "Right") + JOY_AXIS_LEFT_Y: + return "Left Stick %s" % ("Up" if motion.axis_value < 0 else "Down") + JOY_AXIS_RIGHT_X: + return "Right Stick %s" % ("Left" if motion.axis_value < 0 else "Right") + JOY_AXIS_RIGHT_Y: + return "Right Stick %s" % ("Up" if motion.axis_value < 0 else "Down") + JOY_AXIS_TRIGGER_LEFT: + return "Left Trigger" + JOY_AXIS_TRIGGER_RIGHT: + return "Right Trigger" + return input.as_text() diff --git a/examples/remap.gd b/examples/remap.gd index 980b0bf..56afb24 100644 --- a/examples/remap.gd +++ b/examples/remap.gd @@ -1,7 +1,7 @@ extends CenterContainer -@export var action_name: String = "ui_accept" +@export var action_name: String = &"ui_accept" @onready var press_something_label: Label = $PressSomething @onready var vbox := $VBox @@ -29,16 +29,24 @@ func _ready() -> void: func _unhandled_input(event) -> void: if changing_input_index == -1: return + var did_update: bool = false + if (event is InputEventKey or event is InputEventMouseButton) and event.is_pressed(): - accept_event() InputHelper.replace_keyboard_input_at_index(action_name, changing_input_index, event, true) - update_labels() + did_update = true + elif (event is InputEventJoypadButton or event is InputEventJoypadMotion) and event.is_pressed(): + InputHelper.replace_joypad_input_at_index(action_name, changing_input_index, event, true) + did_update = true + + if did_update: + accept_event() + update_labels() self.changing_input_index = -1 func update_labels() -> void: - var inputs: Array = InputHelper.get_keyboard_inputs_for_action(action_name) + var inputs: Array = InputHelper.get_keyboard_or_joypad_inputs_for_action(action_name) change_button_1.text = "Change..." change_button_2.text = "Change..." diff --git a/examples/remap.tscn b/examples/remap.tscn index 611b557..cb2f6e3 100644 --- a/examples/remap.tscn +++ b/examples/remap.tscn @@ -10,7 +10,7 @@ script = ExtResource("1_tju27") [node name="PressSomething" type="Label" parent="."] layout_mode = 2 -text = "Press a key or mouse button..." +text = "Press a key, mouse button, or joypad button..." [node name="VBox" type="HBoxContainer" parent="."] layout_mode = 2 diff --git a/project.godot b/project.godot index 9951d4a..6c79d9e 100644 --- a/project.godot +++ b/project.godot @@ -11,8 +11,8 @@ config_version=5 [application] config/name="Input Helper" -config/features=PackedStringArray("4.1", "C#", "Forward Plus") run/main_scene="res://tests/tests.tscn" +config/features=PackedStringArray("4.2", "Forward Plus") config/icon="res://icon.svg" [autoload] @@ -45,5 +45,6 @@ ui_accept={ "deadzone": 0.5, "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194309,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) , Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"canceled":false,"pressed":false,"double_click":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":1,"pressure":0.0,"pressed":true,"script":null) ] }