Skip to content

Commit

Permalink
Keybindings persistence (mbrlabs#172)
Browse files Browse the repository at this point in the history
* Refactor settings-shortcuts a bit and add saving of shortcuts

* Forwards compatiblity for new actions
  • Loading branch information
MrApplejuice authored Jul 6, 2022
1 parent 8f13ad4 commit e65b641
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
54 changes: 33 additions & 21 deletions lorien/Misc/Settings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extends Node

# -------------------------------------------------------------------------------------------------
const DEFAULT_SECTION := "settings"
const SHORTCUTS_SECTION := "shortcuts"
const GENERAL_PRESSURE_SENSITIVITY := "general_pressure_sensitvity"
const GENERAL_DEFAULT_CANVAS_COLOR := "general_default_canvas_color"
const GENERAL_DEFAULT_BRUSH_SIZE := "general_default_brush_size"
Expand All @@ -26,9 +27,9 @@ var language_names: PoolStringArray
func _ready():
_config_file = ConfigFile.new()
_load_settings()
reload_locales()
_setup_shortcuts()
_load_shortcuts()
_setup_default_shortcuts()
reload_locales()

# -------------------------------------------------------------------------------------------------
func reload_locales():
Expand Down Expand Up @@ -67,31 +68,42 @@ func set_value(key: String, value = null):
_save_settings()

# -------------------------------------------------------------------------------------------------
func _setup_shortcuts(override = false) -> void:
for action_name in InputMap.get_actions():
if !override && _config_file.has_section_key("shortcuts", action_name):
continue

set_default_shortcut(action_name)
_save_settings()

# -------------------------------------------------------------------------------------------------
func set_default_shortcut(action_name: String, save = false) -> void:
var shortcuts = []
for event in InputMap.get_action_list(action_name):
shortcuts.append(event)
func _setup_default_shortcuts() -> void:
var new_actions := []
for action_name in Utils.bindable_actions():
if ! _config_file.has_section_key(SHORTCUTS_SECTION, action_name):
new_actions.append(action_name)

if len(new_actions) > 0:
var old_actions := []
for action_name in Utils.bindable_actions():
if ! action_name in new_actions:
old_actions.append(action_name)

_config_file.set_value("shortcuts", action_name, shortcuts)
if save:
for new_action in new_actions:
var event_list := InputMap.get_action_list(new_action)
for event in event_list:
for old_action in old_actions:
if InputMap.action_has_event(old_action, event):
event_list.erase(event)
break
_config_file.set_value(SHORTCUTS_SECTION, new_action, event_list)
_save_settings()
_load_shortcuts()

# -------------------------------------------------------------------------------------------------
func _load_shortcuts() -> void:
for action_name in InputMap.get_actions():
if !_config_file.has_section_key("shortcuts", action_name):
for action_name in Utils.bindable_actions():
if !_config_file.has_section_key(SHORTCUTS_SECTION, action_name):
continue
var shortcuts = _config_file.get_value("shortcuts", action_name)
InputMap.action_erase_events(action_name)

var shortcuts = _config_file.get_value(SHORTCUTS_SECTION, action_name)
InputMap.action_erase_events(action_name)
for shortcut in shortcuts:
InputMap.action_add_event(action_name, shortcut)

# -------------------------------------------------------------------------------------------------
func store_shortcuts() -> void:
for action_name in Utils.bindable_actions():
_config_file.set_value(SHORTCUTS_SECTION, action_name, InputMap.get_action_list(action_name))
_save_settings()
2 changes: 2 additions & 0 deletions lorien/UI/Dialogs/KeyBindingsList.gd
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func _modify_keybinding(bindings_data: Dictionary, action_name: String) -> void:
for e in bindings_data["events"]:
InputMap.action_add_event(action_name, e)
_populate_input_list()
Settings.store_shortcuts()
Settings.reload_locales()

# -------------------------------------------------------------------------------------------------
Expand All @@ -63,4 +64,5 @@ func _bind_new_key(action_name: String) -> void:
# -------------------------------------------------------------------------------------------------
func _bind_key_dialog_hidden() -> void:
_populate_input_list()
Settings.store_shortcuts()
Settings.reload_locales()

0 comments on commit e65b641

Please sign in to comment.