From 37257dc3fd9023b9effe65bc0692202d2949f080 Mon Sep 17 00:00:00 2001 From: Rob Kelly Date: Tue, 10 Dec 2024 12:36:49 -0700 Subject: [PATCH] Turns out you can just save control bindings as regular project settings under `input/`. Who knew? --- project.godot | 1 - src/game/binding_loader.gd | 35 ------------------- .../control_binding/control_binding.gd | 10 ++++++ .../control_binding/control_binding.tscn | 2 +- src/ui/menus/settings_menu/settings_menu.gd | 10 +++++- src/util/binding_map.gd | 4 --- 6 files changed, 20 insertions(+), 42 deletions(-) delete mode 100644 src/game/binding_loader.gd delete mode 100644 src/util/binding_map.gd diff --git a/project.godot b/project.godot index 84bacef..c2e857a 100644 --- a/project.godot +++ b/project.godot @@ -21,7 +21,6 @@ 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 deleted file mode 100644 index 9489218..0000000 --- a/src/game/binding_loader.gd +++ /dev/null @@ -1,35 +0,0 @@ -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/control_binding/control_binding.gd b/src/ui/menus/settings_menu/control_binding/control_binding.gd index 53e60a5..6e6f175 100644 --- a/src/ui/menus/settings_menu/control_binding/control_binding.gd +++ b/src/ui/menus/settings_menu/control_binding/control_binding.gd @@ -19,6 +19,10 @@ var listening: bool = false: @onready var button: Button = %Button +func _get_settings_key() -> String: + return "input/%s" % key + + func _ready() -> void: # gdlint:ignore = private-method-call super._ready() @@ -99,6 +103,12 @@ func rebind(event: InputEvent) -> void: listening = false +func apply() -> void: + var setting: Dictionary = ProjectSettings.get_setting(_get_settings_key()) + setting["events"] = InputMap.action_get_events(key) + ProjectSettings.set_setting(_get_settings_key(), setting) + + static func create(_key: StringName) -> ControlBinding: var instance: ControlBinding = SCENE.instantiate() instance.key = _key diff --git a/src/ui/menus/settings_menu/control_binding/control_binding.tscn b/src/ui/menus/settings_menu/control_binding/control_binding.tscn index 2cb8744..aa2a96a 100644 --- a/src/ui/menus/settings_menu/control_binding/control_binding.tscn +++ b/src/ui/menus/settings_menu/control_binding/control_binding.tscn @@ -2,7 +2,7 @@ [ext_resource type="Script" path="res://src/ui/menus/settings_menu/control_binding/control_binding.gd" id="1_7mwhu"] -[node name="ControlBinding" type="PanelContainer"] +[node name="ControlBinding" type="PanelContainer" groups=["ControlBindings"]] theme_type_variation = &"CheckerContainerOdd" script = ExtResource("1_7mwhu") diff --git a/src/ui/menus/settings_menu/settings_menu.gd b/src/ui/menus/settings_menu/settings_menu.gd index 3b9dcc3..ec8e88c 100644 --- a/src/ui/menus/settings_menu/settings_menu.gd +++ b/src/ui/menus/settings_menu/settings_menu.gd @@ -3,6 +3,7 @@ extends MarginContainer const SETTINGS_GROUP := "Settings" const VOLUME_GROUP := "VolumeSliders" +const BINDINGS_GROUP := "ControlBindings" @onready var bus_mixer_list: HBoxContainer = %BusMixerList @onready var control_binding_list: VBoxContainer = %ControlBindingList @@ -25,6 +26,12 @@ func _get_volume_sliders() -> Array[VolumeSlider]: return elements +func _get_control_bindings() -> Array[ControlBinding]: + var elements: Array[ControlBinding] = [] + elements.assign(get_tree().get_nodes_in_group(BINDINGS_GROUP)) + return elements + + func populate_control_bindings() -> void: for action: StringName in InputMap.get_actions(): if not action.begins_with("ui_"): @@ -47,12 +54,13 @@ func apply() -> void: setting.apply() for mixer: VolumeSlider in _get_volume_sliders(): mixer.apply() + for binding: ControlBinding in _get_control_bindings(): + binding.apply() ## Write all applied settings to disk. func save_settings() -> void: Game.settings.write() - BindingLoader.write() ## Apply settings and close menu. diff --git a/src/util/binding_map.gd b/src/util/binding_map.gd deleted file mode 100644 index a738b59..0000000 --- a/src/util/binding_map.gd +++ /dev/null @@ -1,4 +0,0 @@ -class_name BindingMap extends Resource -## Serializable action input map. Used by `BindingLoader`. - -@export var bindings: Dictionary