From dff8065f9579994a6cd41c3bd5f53d4d52de6445 Mon Sep 17 00:00:00 2001 From: Rob Kelly Date: Sun, 8 Dec 2024 21:39:50 -0700 Subject: [PATCH] Keybinds are persisted to user config --- project.godot | 1 + src/game/binding_loader.gd | 35 +++++++++++++++++++++ src/ui/menus/settings_menu/settings_menu.gd | 2 +- src/util/binding_map.gd | 4 +++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/game/binding_loader.gd create mode 100644 src/util/binding_map.gd diff --git a/project.godot b/project.godot index 36ba6ec..c8bc3c3 100644 --- a/project.godot +++ b/project.godot @@ -20,6 +20,7 @@ run/max_fps=60 ClubCatalog="*res://src/equipment/clubs/club_catalog.tscn" GameSettings="*res://src/game/game_settings.gd" +BindingLoader="*res://src/game/binding_loader.gd" [debug] diff --git a/src/game/binding_loader.gd b/src/game/binding_loader.gd new file mode 100644 index 0000000..9489218 --- /dev/null +++ b/src/game/binding_loader.gd @@ -0,0 +1,35 @@ +class_name BindingLoaderType extends Node +## Handles persisting action input bindings. + +const BINDINGS_FILE := "user://bindings.tres" + + +func _ready() -> void: + # Map may not be defined if no keybinds have been written + if FileAccess.file_exists(BINDINGS_FILE): + print_debug("Reading keybinds from ", BINDINGS_FILE) + var map: BindingMap = load(BINDINGS_FILE) as BindingMap + + # Overwrite InputMap with loaded bindings + for action: StringName in map.bindings.keys(): + # Clear existing bindings + InputMap.action_erase_events(action) + # Apply loaded binding + var event: InputEvent = map.bindings[action] + if event: + InputMap.action_add_event(action, event) + + +func write() -> void: + # Build map from input actions + var map: BindingMap = BindingMap.new() + for action: StringName in InputMap.get_actions(): + var events := InputMap.action_get_events(action) + if events: + map.bindings[action] = events[0] + else: + map.bindings[action] = null + + # Write to disk + print_debug("Writing keybinds to ", BINDINGS_FILE) + ResourceSaver.save(map, BINDINGS_FILE) diff --git a/src/ui/menus/settings_menu/settings_menu.gd b/src/ui/menus/settings_menu/settings_menu.gd index 7c21c27..e3d68aa 100644 --- a/src/ui/menus/settings_menu/settings_menu.gd +++ b/src/ui/menus/settings_menu/settings_menu.gd @@ -18,7 +18,6 @@ func _get_settings_elements() -> Array[Setting]: func populate_control_bindings() -> void: - InputMap.get for action: StringName in InputMap.get_actions(): if not action.begins_with("ui_"): control_binding_list.add_child(ControlBinding.create(action)) @@ -38,6 +37,7 @@ func apply() -> void: ## Write all applied settings to disk. func save_settings() -> void: ProjectSettings.save_custom(SETTINGS_FILE) + BindingLoader.write() ## Apply settings and close menu. diff --git a/src/util/binding_map.gd b/src/util/binding_map.gd new file mode 100644 index 0000000..a738b59 --- /dev/null +++ b/src/util/binding_map.gd @@ -0,0 +1,4 @@ +class_name BindingMap extends Resource +## Serializable action input map. Used by `BindingLoader`. + +@export var bindings: Dictionary