Skip to content

Commit

Permalink
Add Joypad labels (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanhoad authored Jan 28, 2024
1 parent cc27618 commit ed765a5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 30 additions & 1 deletion addons/input_helper/input_helper.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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()


Expand Down
16 changes: 12 additions & 4 deletions examples/remap.gd
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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..."
Expand Down
2 changes: 1 addition & 1 deletion examples/remap.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
]
}

0 comments on commit ed765a5

Please sign in to comment.