diff --git a/gui/input_mapping/ActionRemapButton.gd b/gui/input_mapping/ActionRemapButton.gd index c95e9a87..f50ca9d4 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/ActionRemapButton.tscn b/gui/input_mapping/ActionRemapButton.tscn index 8b100a5b..6cda12a4 100644 --- a/gui/input_mapping/ActionRemapButton.tscn +++ b/gui/input_mapping/ActionRemapButton.tscn @@ -1,12 +1,9 @@ -[gd_scene load_steps=2 format=2] +[gd_scene load_steps=2 format=3 uid="uid://cy6kmby6mupvv"] -[ext_resource path="res://ActionRemapButton.gd" type="Script" id=1] +[ext_resource type="Script" path="res://ActionRemapButton.gd" id="1"] [node name="ActionRemapButton" type="Button"] offset_right = 90.0 offset_bottom = 30.0 toggle_mode = true -script = ExtResource( 1 ) -__meta__ = { -"_edit_use_anchors_": false -} +script = ExtResource( "1" ) diff --git a/gui/input_mapping/InputRemapMenu.tscn b/gui/input_mapping/InputRemapMenu.tscn index 15b942ac..640b05c5 100644 --- a/gui/input_mapping/InputRemapMenu.tscn +++ b/gui/input_mapping/InputRemapMenu.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=2 format=3 uid="uid://bjdgwg23lm1d4"] -[ext_resource type="PackedScene" path="res://ActionRemapButton.tscn" id="1"] +[ext_resource type="PackedScene" uid="uid://cy6kmby6mupvv" path="res://ActionRemapButton.tscn" id="1"] [node name="InputRemapMenu" type="Control"] anchor_right = 1.0 @@ -11,9 +11,6 @@ anchor_right = 1.0 offset_top = 24.0 offset_bottom = 55.0 text = "Click on a button to reassign its action key." -__meta__ = { -"_edit_use_anchors_": false -} [node name="RemapButtonGroup" type="Button" parent="."] anchor_left = 0.5 @@ -26,9 +23,6 @@ offset_right = 160.0 offset_bottom = 144.0 disabled = true flat = true -__meta__ = { -"_edit_use_anchors_": false -} [node name="ActionsList" type="VBoxContainer" parent="RemapButtonGroup"] anchor_left = 0.5 @@ -39,9 +33,6 @@ offset_left = -160.0 offset_top = -140.0 offset_right = 160.0 offset_bottom = 140.0 -__meta__ = { -"_edit_use_anchors_": false -} [node name="ActionRemapRow" type="HBoxContainer" parent="RemapButtonGroup/ActionsList"] offset_right = 320.0 diff --git a/gui/input_mapping/KeyPersistence.gd b/gui/input_mapping/KeyPersistence.gd new file mode 100644 index 00000000..74da9778 --- /dev/null +++ b/gui/input_mapping/KeyPersistence.gd @@ -0,0 +1,44 @@ +# 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(): + if InputMap.action_get_events(action).size() != 0: + keymaps[action] = InputMap.action_get_events(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 + file.open(keymaps_path, File.READ) + var temp_keymap = file.get_var(true) as Dictionary + 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() + 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 cfb8ee9f..330d9caf 100644 --- a/gui/input_mapping/project.godot +++ b/gui/input_mapping/project.godot @@ -20,6 +20,10 @@ run/main_scene="res://InputRemapMenu.tscn" config/icon="res://icon.png" config/features=PackedStringArray("4.0") +[autoload] + +KeyPersistence="*res://KeyPersistence.gd" + [display] window/size/viewport_width=640