Skip to content

Commit

Permalink
feat: basic (tho buggy) control editing in place
Browse files Browse the repository at this point in the history
  • Loading branch information
russmatney committed Jan 16, 2024
1 parent 672784c commit ce14eba
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 7 deletions.
16 changes: 15 additions & 1 deletion src/menus/ControlsPanel.gd
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
@tool
extends CanvasLayer

@onready var edit_action_scene = preload("res://src/menus/EditActionRow.tscn")
## vars ###############################################3

@onready var edit_action_scene = preload("res://src/menus/EditActionRow.tscn")
@onready var action_rows = $%EditActionRows
@onready var reset_all_button = $%ResetButton

var displayed_actions = [
"ui_accept", "ui_undo", "pause", "close", "restart",
"ui_left", "ui_right", "ui_up", "ui_down"
]

## ready ###############################################3

func _ready():
Log.pr("displayed actions", displayed_actions)

render_action_rows()
reset_all_button.pressed.connect(on_reset_all_pressed)

## render ###############################################3

func render_action_rows():
U.remove_children(action_rows)
for action in displayed_actions:
var row = edit_action_scene.instantiate()
row.action_name = action
action_rows.add_child(row)

## reset all ###############################################3

func on_reset_all_pressed():
Log.pr("resetting all actions!")
InputHelper.reset_all_actions()
render_action_rows()
5 changes: 5 additions & 0 deletions src/menus/ControlsPanel.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ text = "[center]Controls[/center]"
fit_content = true
scroll_active = false

[node name="ResetButton" type="Button" parent="PanelContainer/VBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
text = "Reset All"

[node name="EditActionRows" type="VBoxContainer" parent="PanelContainer/VBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
50 changes: 45 additions & 5 deletions src/menus/EditActionRow.gd
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
@tool
extends HBoxContainer

## vars ###############################################3

@export var action_name = "ui_accept"

@onready var action_name_label = $%ActionName
@onready var action_inputs = $%ActionInputs

@onready var edit_button = $%EditButton

var input_icon_scene = preload("res://src/menus/ActionInputIcon.tscn")

var waiting_for_new_input = false
var current_key_input
var current_joy_input

## ready ###############################################3

func _ready():
action_name_label.text = "[center]%s[/center]" % action_name

render_action_icons()
edit_button.pressed.connect(on_edit_pressed)

## render action icons ###############################################3

func render_action_icons():
var keyboard_inputs = InputHelper.get_keyboard_inputs_for_action(action_name)
Expand All @@ -20,21 +32,49 @@ func render_action_icons():
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:
if not current_key_input:
current_key_input = inp
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
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:
icon.joy_axis = [inp.axis, inp.axis_value]
else:
var joy_button_idx = inp.button_index
Log.pr("joy_button_idx", InputHelper.guess_device_name(), joy_button_idx)
if not current_joy_input:
current_joy_input = inp
icon.joy_button = [InputHelper.guess_device_name(), inp.button_index]

action_inputs.add_child(icon)

## edit signals ###############################################3

func on_edit_pressed():
waiting_for_new_input = true
Log.pr("edit pressed, waiting for input!", action_name)
edit_button.text = "...listening for new key..."

## listening for new key ###############################################3

func _unhandled_input(event) -> void:
if not waiting_for_new_input: return

var accepted = false
if (event is InputEventKey or event is InputEventMouseButton) and event.is_pressed():
accept_event()
InputHelper.replace_keyboard_input_for_action(action_name, current_key_input, event, true)
accepted = true

if event is InputEventJoypadButton and event.is_pressed():
accept_event()
InputHelper.replace_joypad_input_for_action(action_name, current_joy_input, event, true)
accepted = true

if accepted:
render_action_icons()
edit_button.text = "Edit"
waiting_for_new_input = false
14 changes: 13 additions & 1 deletion src/menus/EditActionRow.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ alignment = 1
script = ExtResource("1_1si0w")

[node name="CenterContainer" type="CenterContainer" parent="."]
custom_minimum_size = Vector2(250, 0)
layout_mode = 2
size_flags_horizontal = 3

[node name="ActionName" type="RichTextLabel" parent="CenterContainer"]
unique_name_in_owner = true
Expand All @@ -29,6 +29,18 @@ text = "[center]ui_accept[/center]"
fit_content = true
scroll_active = false

[node name="HBoxContainer" type="HBoxContainer" parent="."]
layout_mode = 2
size_flags_vertical = 4
alignment = 1

[node name="EditButton" type="Button" parent="HBoxContainer"]
unique_name_in_owner = true
custom_minimum_size = Vector2(60, 0)
layout_mode = 2
text = "Edit
"

[node name="ActionInputs" type="HBoxContainer" parent="."]
unique_name_in_owner = true
layout_mode = 2
Expand Down

0 comments on commit ce14eba

Please sign in to comment.