@@ -15,6 +15,10 @@ const DEVICE_SWITCH_JOYCON_RIGHT_CONTROLLER = "switch_right_joycon"
15
15
const DEVICE_PLAYSTATION_CONTROLLER = "playstation"
16
16
const DEVICE_GENERIC = "generic"
17
17
18
+ const XBOX_BUTTON_LABELS = ["A" , "B" , "X" , "Y" , "Back" , "Home" , "Menu" , "Left Stick" , "Right Stick" , "Left Shoulder" , "Right Shoulder" , "Up" , "Down" , "Left" , "Right" , "Share" ]
19
+ const SWITCH_BUTTON_LABELS = ["B" , "A" , "Y" , "X" , "-" , "" , "+" , "Left Stick" , "Right Stick" , "Left Shoulder" , "Right Shoulder" , "Up" , "Down" , "Left" , "Right" , "Capture" ]
20
+ const PLAYSTATION_BUTTON_LABELS = ["Cross" , "Circle" , "Square" , "Triangle" , "Select" , "PS" , "Options" , "L3" , "R3" , "L1" , "R1" , "Up" , "Down" , "Left" , "Right" , "Microphone" ]
21
+
18
22
19
23
@onready var device : String = guess_device_name ()
20
24
@onready var device_index : int = 0 if has_joypad () else - 1
@@ -33,7 +37,7 @@ func _input(event: InputEvent) -> void:
33
37
var next_device_index : int = device_index
34
38
35
39
# Did we just press a key on the keyboard?
36
- if event is InputEventKey or event is InputEventMouse :
40
+ if event is InputEventKey or event is InputEventMouseButton :
37
41
next_device = DEVICE_KEYBOARD
38
42
next_device_index = - 1
39
43
@@ -139,6 +143,7 @@ func get_label_for_input(input: InputEvent) -> String:
139
143
return OS .get_keycode_string (input .keycode )
140
144
else :
141
145
return input .as_text ()
146
+
142
147
elif input is InputEventMouseButton :
143
148
match input .button_index :
144
149
MOUSE_BUTTON_LEFT :
@@ -149,6 +154,30 @@ func get_label_for_input(input: InputEvent) -> String:
149
154
return "Mouse Right Button"
150
155
return "Mouse Button %d " % input
151
156
157
+ elif input is InputEventJoypadButton :
158
+ match device :
159
+ DEVICE_XBOX_CONTROLLER , DEVICE_GENERIC :
160
+ return "%s Button" % XBOX_BUTTON_LABELS [input .button_index ]
161
+ DEVICE_SWITCH_CONTROLLER , DEVICE_SWITCH_JOYCON_LEFT_CONTROLLER , DEVICE_SWITCH_JOYCON_RIGHT_CONTROLLER :
162
+ return "%s Button" % SWITCH_BUTTON_LABELS [input .button_index ]
163
+ DEVICE_PLAYSTATION_CONTROLLER :
164
+ return "%s Button" % PLAYSTATION_BUTTON_LABELS [input .button_index ]
165
+ elif input is InputEventJoypadMotion :
166
+ var motion : InputEventJoypadMotion = input as InputEventJoypadMotion
167
+ match motion .axis :
168
+ JOY_AXIS_LEFT_X :
169
+ return "Left Stick %s " % ("Left" if motion .axis_value < 0 else "Right" )
170
+ JOY_AXIS_LEFT_Y :
171
+ return "Left Stick %s " % ("Up" if motion .axis_value < 0 else "Down" )
172
+ JOY_AXIS_RIGHT_X :
173
+ return "Right Stick %s " % ("Left" if motion .axis_value < 0 else "Right" )
174
+ JOY_AXIS_RIGHT_Y :
175
+ return "Right Stick %s " % ("Up" if motion .axis_value < 0 else "Down" )
176
+ JOY_AXIS_TRIGGER_LEFT :
177
+ return "Left Trigger"
178
+ JOY_AXIS_TRIGGER_RIGHT :
179
+ return "Right Trigger"
180
+
152
181
return input .as_text ()
153
182
154
183
@@ -334,7 +363,7 @@ func _update_input_for_action(action: String, input: InputEvent, swap_if_taken:
334
363
335
364
# Find the key based event for the target action
336
365
var action_events : Array [InputEvent ] = InputMap .action_get_events (action )
337
- var did_change : bool = false
366
+ var is_replacing : bool = false
338
367
for i in range (0 , action_events .size ()):
339
368
var event : InputEvent = action_events [i ]
340
369
if check_is_valid .call (event ):
@@ -347,11 +376,11 @@ func _update_input_for_action(action: String, input: InputEvent, swap_if_taken:
347
376
348
377
# Replace the event
349
378
action_events [i ] = input
350
- did_change = true
379
+ is_replacing = true
351
380
break
352
381
353
382
# If we were trying to replace something but didn't find it then just add it to the end
354
- if not did_change :
383
+ if not is_replacing :
355
384
action_events .append (input )
356
385
357
386
# Apply the changes
@@ -360,8 +389,7 @@ func _update_input_for_action(action: String, input: InputEvent, swap_if_taken:
360
389
if event != null :
361
390
InputMap .action_add_event (action , event )
362
391
363
- if did_change :
364
- did_change_signal .emit (action , input )
392
+ did_change_signal .emit (action , input )
365
393
366
394
return OK
367
395
0 commit comments