From 7adeecbf0ac35c2fab0b49a1de4b24d7ffd09207 Mon Sep 17 00:00:00 2001 From: Marcus Brummer Date: Sun, 18 Aug 2024 19:32:13 +0200 Subject: [PATCH] Keybindings persistence --- lorien/Misc/KeybindingsManager.gd | 10 +++++++++- lorien/Misc/Settings.gd | 4 ++-- lorien/UI/Dialogs/SettingsDialog.gd | 11 +++++------ lorien/project.godot | 2 +- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lorien/Misc/KeybindingsManager.gd b/lorien/Misc/KeybindingsManager.gd index 9456a753..23c12adb 100644 --- a/lorien/Misc/KeybindingsManager.gd +++ b/lorien/Misc/KeybindingsManager.gd @@ -6,6 +6,12 @@ class Action: var display_name: String var event: InputEventKey +# ------------------------------------------------------------------------------------------------- +func _ready() -> void: + for action in get_actions(): + var event := Settings.get_keybind_value(action.name, action.event) + rebind_action(action, event) + # ------------------------------------------------------------------------------------------------- func get_actions() -> Array[Action]: var actions: Array[Action] @@ -13,8 +19,10 @@ func get_actions() -> Array[Action]: if !action_name.begins_with("ui_") && !action_name.begins_with("player_"): var events := InputMap.action_get_events(action_name) if events.size() > 0: + var event := events[0] + event = Settings.get_keybind_value(action_name, event) var action := Action.new() - action.event = events[0] + action.event = event action.name = action_name action.display_name = tr("ACTION_" + action_name) actions.append(action) diff --git a/lorien/Misc/Settings.gd b/lorien/Misc/Settings.gd index 00e78ff5..278e9e77 100644 --- a/lorien/Misc/Settings.gd +++ b/lorien/Misc/Settings.gd @@ -97,8 +97,8 @@ func set_rendering_value(key: String, value = null): _save_settings() # ------------------------------------------------------------------------------------------------- -func get_keybind_value(action_name: String) -> InputEventKey: - return _config_file.get_value(KEYBINDINGS_SECTION, action_name, null) +func get_keybind_value(action_name: String, default_value = null) -> InputEventKey: + return _config_file.get_value(KEYBINDINGS_SECTION, action_name, default_value) # ------------------------------------------------------------------------------------------------- func set_keybind_value(action_name: String, event: InputEventKey) -> void: diff --git a/lorien/UI/Dialogs/SettingsDialog.gd b/lorien/UI/Dialogs/SettingsDialog.gd index 8babb09e..2d4dc24f 100644 --- a/lorien/UI/Dialogs/SettingsDialog.gd +++ b/lorien/UI/Dialogs/SettingsDialog.gd @@ -99,8 +99,6 @@ func _set_values() -> void: var foreground_fps = Settings.get_rendering_value(Settings.RENDERING_FOREGROUND_FPS, Config.DEFAULT_FOREGROUND_FPS) var background_fps = Settings.get_rendering_value(Settings.RENDERING_BACKGROUND_FPS, Config.DEFAULT_BACKGROUND_FPS) - _constant_pressure.button_pressed = constant_pressure - match ui_theme: Types.UITheme.DARK: _theme.selected = THEME_DARK_INDEX Types.UITheme.LIGHT: _theme.selected = THEME_LIGHT_INDEX @@ -114,9 +112,10 @@ func _set_values() -> void: _set_languages(locale) _set_rounding() - _set_input_actions() + _set_keybindings() _set_ui_scale_range() + _constant_pressure.button_pressed = constant_pressure _pressure_sensitivity.value = pressure_sensitivity _brush_size.value = brush_size _tool_pressure.value = tool_pressure @@ -130,7 +129,7 @@ func _set_values() -> void: _foreground_fps.value = foreground_fps _background_fps.value = background_fps _ui_scale.value = ui_scale - + # ------------------------------------------------------------------------------------------------- func _set_rounding(): _brush_rounding.selected = Settings.get_rendering_value( @@ -174,7 +173,7 @@ func _set_languages(current_locale: String) -> void: _language.selected = _language.get_item_index(id) #-------------------------------------------------------------------------------------------------- -func _set_input_actions() -> void: +func _set_keybindings() -> void: for action: KeybindingsManager.Action in KeybindingsManager.get_actions(): var item: KeybindItem = KEYBIND_ITEM.instantiate() item.action_rebind_requested.connect(_on_action_keybinding_changed) @@ -304,5 +303,5 @@ func _on_constant_pressure_toggled(button_pressed: bool): # ------------------------------------------------------------------------------------------------- func _on_action_keybinding_changed(action: KeybindingsManager.Action, event: InputEventKey) -> void: KeybindingsManager.rebind_action(action, event) - # TODO: serialize to settings + Settings.set_keybind_value(action.name, action.event) print("Rebind done") diff --git a/lorien/project.godot b/lorien/project.godot index 8cf16c58..99d635e2 100644 --- a/lorien/project.godot +++ b/lorien/project.godot @@ -24,8 +24,8 @@ config/icon="res://Assets/icon.png" BrushStrokeTexture="*res://BrushStroke/BrushStrokeTexture.gd" Utils="*res://Misc/Utils.gd" -ProjectManager="*res://ProjectManager/ProjectManager.gd" Settings="*res://Misc/Settings.gd" +ProjectManager="*res://ProjectManager/ProjectManager.gd" ProjectMetadata="*res://ProjectManager/ProjectMetadata.gd" PaletteManager="*res://Palette/PaletteManager.gd" StatePersistence="*res://Misc/StatePersistence.gd"