From 69758f25235b89bab679552e4211ea6751b5c274 Mon Sep 17 00:00:00 2001 From: Voylin <0voylin0@gmail.com> Date: Sun, 8 May 2022 06:55:30 +0900 Subject: [PATCH] Fixes #629 Update gui/input_mapping/KeyPersistence.gd Co-authored-by: Aaron Franke Update gui/input_mapping/KeyPersistence.gd Co-authored-by: Aaron Franke Update gui/input_mapping/KeyPersistence.gd Co-authored-by: Aaron Franke --- gui/input_mapping/ActionRemapButton.gd | 4 +++ gui/input_mapping/KeyPersistence.gd | 45 ++++++++++++++++++++++++++ gui/input_mapping/project.godot | 4 +++ 3 files changed, 53 insertions(+) create mode 100644 gui/input_mapping/KeyPersistence.gd diff --git a/gui/input_mapping/ActionRemapButton.gd b/gui/input_mapping/ActionRemapButton.gd index 94b6491d63..6aad90e57f 100644 --- a/gui/input_mapping/ActionRemapButton.gd +++ b/gui/input_mapping/ActionRemapButton.gd @@ -25,8 +25,12 @@ func _unhandled_key_input(event): func remap_action_to(event): + # We first change the event in this game instance. InputMap.action_erase_events(action) InputMap.action_add_event(action, event) + # And then save it to the keymaps file + KeyPersistence.keymaps[action] = event + KeyPersistence.save_keymap() text = "%s Key" % event.as_text() diff --git a/gui/input_mapping/KeyPersistence.gd b/gui/input_mapping/KeyPersistence.gd new file mode 100644 index 0000000000..15ad58504c --- /dev/null +++ b/gui/input_mapping/KeyPersistence.gd @@ -0,0 +1,45 @@ +# This is an autoload (singleton) which will save +# the key maps in a simple way through a dictionary. +extends Node + +const keymaps_path = "user://keymaps.dat" +var keymaps: Dictionary + + +func _ready() -> void: + # First we create the keymap dictionary on startup with all + # the keymap actions we have. + for action in InputMap.get_actions(): + keymaps[action] = InputMap.get_action_list(action)[0] + load_keymap() + + +func load_keymap() -> void: + var file := File.new() + if not file.file_exists(keymaps_path): + save_keymap() # There is no save file yet, so let's create one. + return + #warning-ignore:return_value_discarded + file.open(keymaps_path, File.READ) + var temp_keymap: Dictionary = file.get_var(true) + file.close() + # We don't just replace the keymaps dictionary, because if you + # updated your game and removed/added keymaps, the data of this + # save file may have invalid actions. So we check one by one to + # make sure that the keymap dictionary really has all current actions. + for action in keymaps.keys(): + if temp_keymap.has(action): + keymaps[action] = temp_keymap[action] + # Whilst setting the keymap dictionary, we also set the + # correct InputMap event + InputMap.action_erase_events(action) + InputMap.action_add_event(action, keymaps[action]) + + +func save_keymap() -> void: + # For saving the keymap, we just save the entire dictionary as a var. + var file := File.new() + #warning-ignore:return_value_discarded + file.open(keymaps_path, File.WRITE) + file.store_var(keymaps, true) + file.close() diff --git a/gui/input_mapping/project.godot b/gui/input_mapping/project.godot index 723f6a357b..1250daf359 100644 --- a/gui/input_mapping/project.godot +++ b/gui/input_mapping/project.godot @@ -19,6 +19,10 @@ config/description="A demo showing how to build an input key remapping screen. run/main_scene="res://InputRemapMenu.tscn" config/icon="res://icon.png" +[autoload] + +KeyPersistence="*res://KeyPersistence.gd" + [display] window/size/width=640