Skip to content

Commit

Permalink
feat: displaying joypad inputs for controls
Browse files Browse the repository at this point in the history
Hardcodes a mapping from input joypad events (buttons and motions) to
controller axes and vexed's EnterInput font characters.
  • Loading branch information
russmatney committed Jan 16, 2024
1 parent d940ab6 commit 672784c
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 13 deletions.
1 change: 1 addition & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pause={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194305,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":6,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":4,"pressure":0.0,"pressed":false,"script":null)
]
}
restart={
Expand Down
139 changes: 138 additions & 1 deletion src/menus/ActionInputIcon.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,63 @@ extends RichTextLabel
if Engine.is_editor_hint():
render_icon()

@export var joy_button = [] :
set(v):
if input_text == "" and v and len(v) == 2:
input_text = button_to_input_text(v)

func button_to_input_text(button):
var device_type = button[0]
var idx = button[1]
if device_type == "keyboard":
# not sure i love this
device_type = "generic"

var dev_map = device_button_idx_input_text.get(device_type, {})
var txt
if idx in dev_map:
txt = dev_map.get(idx, "")

return txt


@export var joy_axis = [] :
set(v):
if input_text == "" and v and len(v) == 2:
input_text = axis_to_input_text(v)

func axis_to_input_text(axis):
var idx = axis[0]
var val = axis[1]

var txt = ""
match idx:
JOY_AXIS_LEFT_X:
if val > 0:
txt = "Joystick right"
else:
txt = "Joystick left"
JOY_AXIS_LEFT_Y:
if val > 0:
txt = "Joystick down"
else:
txt = "Joystick up"
JOY_AXIS_RIGHT_X:
if val > 0:
txt = "Joystick right"
else:
txt = "Joystick left"
JOY_AXIS_RIGHT_Y:
if val > 0:
txt = "Joystick down"
else:
txt = "Joystick up"
[JOY_AXIS_INVALID, JOY_AXIS_TRIGGER_LEFT, JOY_AXIS_TRIGGER_RIGHT, JOY_AXIS_SDL_MAX, JOY_AXIS_MAX]:
Log.pr("Unsupported joystick axis")
return
return txt


func _ready():
render_icon()

Expand Down Expand Up @@ -185,11 +242,12 @@ var keymap_extra={
"B Button"="B",
"X Button"="X",
"Y Button"="Y",
"Cross Button"="X", # mapped to X button for now
"Square Button"="N",
"Triangle Button"="T",
"Circle Button"="C",
"Dpad"="D",
"Dpad Up"="^",
"Dpad up"="^",
"Dpad down"="V",
"Dpad left"="<",
"Dpad right"=">",
Expand Down Expand Up @@ -220,3 +278,82 @@ var keymap_extra={
"Joystick 'L'"="{",
"Joystick 'R'"="}",
}

# unfortunate we can't use constants to create dictionary keys here
var device_button_idx_input_text = {
"xbox"={
0: "A Button",
1: "B Button",
2: "X Button",
3: "Y Button",
4: "Pause", # back
5: "Home",
6: "Menu", # menu
7: "Left Stick",
8: "Right Stick",
9: "Left Button",
10: "Right Button",
11: "Dpad up",
12: "Dpad down",
13: "Dpad left",
14: "Dpad right",
15: "Share",
},
"switch"={
0: "B Button",
1: "A Button",
2: "Y Button",
3: "X Button",
4: "Menu", # -
5: "",
6: "Pause", # +
7: "Left Stick",
8: "Right Stick",
9: "Left Button",
10: "Right Button",
11: "Dpad up",
12: "Dpad down",
13: "Dpad left",
14: "Dpad right",
15: "Capture",
},
"switch_left_joycon"={},
"switch_right_joycon"={},
"playstation"={
0: "Cross Button",
1: "Circle Button",
2: "Square Button",
3: "Triangle Button",
4: "Menu", # Select
5: "PS",
6: "Pause", # Start
7: "Left Stick",
8: "Right Stick",
9: "Left Button",
10: "Right Button",
11: "Dpad up",
12: "Dpad down",
13: "Dpad left",
14: "Dpad right",
15: "Microphone",
20: "Touchpad",
},
"generic"={
0: "A Button",
1: "B Button",
2: "X Button",
3: "Y Button",
4: "Pause", # back
5: "Home",
6: "Menu", # menu
7: "Left Stick",
8: "Right Stick",
9: "Left Button",
10: "Right Button",
11: "Dpad up",
12: "Dpad down",
13: "Dpad left",
14: "Dpad right",
15: "Share",
},
}
6 changes: 4 additions & 2 deletions src/menus/ActionInputIcon.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
[node name="ActionInputIcon" type="RichTextLabel"]
visible = false
clip_contents = false
custom_minimum_size = Vector2(32, 0)
custom_minimum_size = Vector2(50, 0)
offset_top = -16.0
offset_right = 32.0
offset_bottom = 32.0
theme = ExtResource("1_v6qpj")
bbcode_enabled = true
text = "a"
text = "[b]N"
fit_content = true
scroll_active = false
script = ExtResource("2_lnakd")
joy_button = null
joy_axis = null
15 changes: 5 additions & 10 deletions src/menus/EditActionRow.gd
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,23 @@ func render_action_icons():
var joypad_inputs = InputHelper.get_joypad_inputs_for_action(action_name)

U.remove_children(action_inputs)
# TODO if no action icon is found, we ought to remove the icon

Log.pr("\n\nicons for", action_name)
for inp in keyboard_inputs:
var icon = input_icon_scene.instantiate()
var key_str_mods = OS.get_keycode_string(inp.get_keycode_with_modifiers())
icon.input_text = key_str_mods
# TODO if no action icon is found, we ought to remove the icon
action_inputs.add_child(icon)

Log.pr("joy inputs", joypad_inputs)
for inp in joypad_inputs:
var icon = input_icon_scene.instantiate()
if "axis" in inp:
var joy_axis_idx = inp.axis
Log.pr("joy_axis_idx", joy_axis_idx)
icon.input_text = "Joystick left"
icon.joy_axis = [inp.axis, inp.axis_value]
else:
var joy_button_idx = inp.button_index
Log.pr("joy_button_idx", joy_button_idx)
icon.input_text = "B Button"
# TODO if no action icon is found, we ought to remove the icon
action_inputs.add_child(icon)

Log.pr("joy_button_idx", InputHelper.guess_device_name(), joy_button_idx)
icon.joy_button = [InputHelper.guess_device_name(), inp.button_index]

# TODO pull controller icons for the given inputs
action_inputs.add_child(icon)

0 comments on commit 672784c

Please sign in to comment.