diff options
Diffstat (limited to 'client/gui/menus/settings/input')
7 files changed, 241 insertions, 0 deletions
diff --git a/client/gui/menus/settings/input/input_manager.gd b/client/gui/menus/settings/input/input_manager.gd new file mode 100644 index 00000000..e3158a03 --- /dev/null +++ b/client/gui/menus/settings/input/input_manager.gd @@ -0,0 +1,101 @@ +# Hurry Curry! - a game about cooking +# Copyright (C) 2025 Hurry Curry! contributors +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, version 3 of the License only. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. +# +extends Node + +enum EventType { + KEYBOARD, + JOYPAD, + TOUCH, + OTHER +} + +var default_input_map = {} +var input_map: Dictionary + +func _init(): + default_input_map = get_input_map() + input_map = default_input_map.duplicate(true) + +func get_input_map() -> Dictionary: + var actions = InputMap.get_actions().filter(func isBuiltIn(k: String): return !k.begins_with("ui_")) + var kb = {} + for a in actions: + var input_events: Array[InputEvent] = InputMap.action_get_events(a).duplicate(true) + kb[a] = input_events + return kb + +func get_events(action_name: String) -> Array: + if not input_map.has(action_name): + push_error("Tried to get action %s in input map which does not exist" % action_name) + return [] + return input_map[action_name] + +func settings() -> Array: + var entries := [] + for k in input_map.keys(): entries.append(InputSetting.new(k)) + return entries + +func change_input_map_action(action_name: String, events: Array, save: bool = true): + if !InputMap.has_action(action_name): + push_error("Action %s does not exist" % action_name, false) + return + # Erase previous keybindings + InputMap.action_erase_events(action_name) + # Add new keybindings + for e in events: + InputMap.action_add_event(action_name, e) + + if save: + # Update input map dictionary + input_map = get_input_map() + # Save settings + Global.set_setting("input_map", input_map.duplicate(true)) + +func apply_input_map(new_input_map: Dictionary): + # Load into input map dictionary + for k in new_input_map.keys(): + input_map[k] = [] + for a in new_input_map[k]: + input_map[k].append(a) + + # Apply keybindings + for k in input_map.keys(): + change_input_map_action(k, input_map[k], false) + +func reset_input_map(): + Global.set_setting("input_map", default_input_map.duplicate()) + apply_input_map(Global.get_setting("input_map")) + +func get_event_type(input_event: InputEvent) -> EventType: + if input_event is InputEventKey or input_event is InputEventMouseButton: + return EventType.KEYBOARD + elif input_event is InputEventJoypadButton or input_event is InputEventJoypadMotion: + return EventType.JOYPAD + elif input_event is InputEventScreenTouch or input_event is InputEventScreenDrag: + return EventType.TOUCH + return EventType.OTHER + +func display_input_event(input_event: InputEvent) -> String: + if input_event is InputEventKey: + return tr("c.settings.input.keyboard").format([OS.get_keycode_string(input_event.physical_keycode)]) + elif input_event is InputEventMouseButton: + return tr("c.settings.input.mouse_button").format([input_event.button_index]) + elif input_event is InputEventJoypadButton: + return tr("c.settings.input.joypad").format([input_event.button_index]) + elif input_event is InputEventJoypadMotion: + return tr("c.settings.input.joypad_axis").format([input_event.axis]) + else: + return tr("c.settings.input.other_event") diff --git a/client/gui/menus/settings/input/input_manager.gd.uid b/client/gui/menus/settings/input/input_manager.gd.uid new file mode 100644 index 00000000..678c2192 --- /dev/null +++ b/client/gui/menus/settings/input/input_manager.gd.uid @@ -0,0 +1 @@ +uid://bfu78iwybbu2s diff --git a/client/gui/menus/settings/input/input_setting.gd b/client/gui/menus/settings/input/input_setting.gd new file mode 100644 index 00000000..fa903771 --- /dev/null +++ b/client/gui/menus/settings/input/input_setting.gd @@ -0,0 +1,39 @@ +# Hurry Curry! - a game about cooking +# Copyright (C) 2025 Hurry Curry! contributors +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, version 3 of the License only. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. +# +class_name InputSetting +extends GameSetting + +const INPUT_VALUE_NODE_SCENE = preload("res://gui/menus/settings/input/input_value_node.tscn") + +func _init(new_id: String): + super(new_id) + default = InputManager.default_input_map[new_id] + +func create_row(): + var row = super() + row.value_node = INPUT_VALUE_NODE_SCENE.instantiate() + Settings.hook_changed_init(key, true, + func(value): + if is_instance_valid(row): + row.value_node.value = value + ) + row.value_node.changed.connect(func(): Global.set_setting(key, row.value_node.value)) + return row + +func changed_keys(): + return [key] + # if Global.array_eq(Global.get_setting(key), default): return [key] + # else: return [] diff --git a/client/gui/menus/settings/input/input_setting.gd.uid b/client/gui/menus/settings/input/input_setting.gd.uid new file mode 100644 index 00000000..7866fc2f --- /dev/null +++ b/client/gui/menus/settings/input/input_setting.gd.uid @@ -0,0 +1 @@ +uid://d2xwn2u4ycpe8 diff --git a/client/gui/menus/settings/input/input_value_node.gd b/client/gui/menus/settings/input/input_value_node.gd new file mode 100644 index 00000000..7c718e25 --- /dev/null +++ b/client/gui/menus/settings/input/input_value_node.gd @@ -0,0 +1,74 @@ +# Hurry Curry! - a game about cooking +# Copyright (C) 2025 Hurry Curry! contributors +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, version 3 of the License only. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. +# +extends VBoxContainer +class_name InputValueNode + +var value: Array[InputEvent] = [] +var listening := false + +signal changed() + +@onready var actions_container: VBoxContainer = $ActionsContainer +@onready var add_button: Button = $Add +@onready var add_text = add_button.text + +func _ready(): + update() + +func update(fix_focus: bool = false): + for c in actions_container.get_children(): + c.queue_free() + + for e: InputEvent in value: + var description: String = InputManager.display_input_event(e) + var button := Button.new() + + button.text = description + button.pressed.connect(erase_event.bind(e)) + actions_container.add_child(button) + + if fix_focus: + add_button.grab_focus() + +func erase_event(e: InputEvent): + value.erase(e) + update(true) + changed.emit() + +func _input(e: InputEvent): + if listening: + if e is InputEventKey or e is InputEventMouseButton or e is InputEventJoypadButton or e is InputEventJoypadMotion: + # Check if key was already added + for e2 in value: + if events_equal(e, e2): return + + value.append(e) + _on_add_pressed() + update() + changed.emit() + +func events_equal(e1: InputEvent, e2: InputEvent) -> bool: + if e1 is InputEventKey and e2 is InputEventKey: + return e1.physical_keycode == e2.physical_keycode + if (e1 is InputEventMouseButton and e2 is InputEventMouseButton) or (e1 is InputEventJoypadButton and e2 is InputEventJoypadButton): + return e1.button_index == e2.button_index + if e1 is InputEventJoypadMotion and e2 is InputEventJoypadMotion: + return e1.axis == e2.axis + return false + +func _on_add_pressed() -> void: + listening = not listening + add_button.text = tr("c.settings.input.press_any_key") if listening else add_text diff --git a/client/gui/menus/settings/input/input_value_node.gd.uid b/client/gui/menus/settings/input/input_value_node.gd.uid new file mode 100644 index 00000000..3669b991 --- /dev/null +++ b/client/gui/menus/settings/input/input_value_node.gd.uid @@ -0,0 +1 @@ +uid://ckb78voiq05e3 diff --git a/client/gui/menus/settings/input/input_value_node.tscn b/client/gui/menus/settings/input/input_value_node.tscn new file mode 100644 index 00000000..1b2e89c4 --- /dev/null +++ b/client/gui/menus/settings/input/input_value_node.tscn @@ -0,0 +1,24 @@ +[gd_scene load_steps=3 format=3 uid="uid://c6r0nv5daq7wc"] + +[ext_resource type="Script" uid="uid://ckb78voiq05e3" path="res://gui/menus/settings/input/input_value_node.gd" id="1_snxax"] +[ext_resource type="Texture2D" uid="uid://cnfjbowd2i02r" path="res://gui/resources/icons/plus.svg" id="2_3vlvc"] + +[node name="InputValueNode" type="VBoxContainer"] +offset_right = 128.0 +offset_bottom = 31.0 +theme_override_constants/separation = 0 +script = ExtResource("1_snxax") + +[node name="ActionsContainer" type="VBoxContainer" parent="."] +layout_mode = 2 +theme_override_constants/separation = 0 + +[node name="Add" type="Button" parent="."] +custom_minimum_size = Vector2(128, 0) +layout_mode = 2 +size_flags_vertical = 3 +text = "c.settings.input.add" +icon = ExtResource("2_3vlvc") +expand_icon = true + +[connection signal="pressed" from="Add" to="." method="_on_add_pressed"] |