Skip to content

Commit

Permalink
Keybindings persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrlabs committed Aug 18, 2024
1 parent 8690fe3 commit 7adeecb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
10 changes: 9 additions & 1 deletion lorien/Misc/KeybindingsManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@ 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]
for action_name: String in InputMap.get_actions():
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)
Expand Down
4 changes: 2 additions & 2 deletions lorien/Misc/Settings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 5 additions & 6 deletions lorien/UI/Dialogs/SettingsDialog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
2 changes: 1 addition & 1 deletion lorien/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 7adeecb

Please sign in to comment.