Skip to content

Commit 7ef60c2

Browse files
authored
Merge pull request #738 from voylin/GuiInputMappingPersistentKeyMapping4.0
[4.0-dev] GUI Input Mapping Demo Persistent Key Mapping- Fixes #629
2 parents bde67b4 + 471243f commit 7ef60c2

File tree

5 files changed

+56
-16
lines changed

5 files changed

+56
-16
lines changed

gui/input_mapping/ActionRemapButton.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ func _unhandled_key_input(event):
2525

2626

2727
func remap_action_to(event):
28+
# We first change the event in this game instance.
2829
InputMap.action_erase_events(action)
2930
InputMap.action_add_event(action, event)
31+
# And then save it to the keymaps file
32+
KeyPersistence.keymaps[action] = event
33+
KeyPersistence.save_keymap()
3034
text = "%s Key" % event.as_text()
3135

3236

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
[gd_scene load_steps=2 format=2]
1+
[gd_scene load_steps=2 format=3 uid="uid://cy6kmby6mupvv"]
22

3-
[ext_resource path="res://ActionRemapButton.gd" type="Script" id=1]
3+
[ext_resource type="Script" path="res://ActionRemapButton.gd" id="1"]
44

55
[node name="ActionRemapButton" type="Button"]
66
offset_right = 90.0
77
offset_bottom = 30.0
88
toggle_mode = true
9-
script = ExtResource( 1 )
10-
__meta__ = {
11-
"_edit_use_anchors_": false
12-
}
9+
script = ExtResource( "1" )

gui/input_mapping/InputRemapMenu.tscn

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[gd_scene load_steps=2 format=3 uid="uid://bjdgwg23lm1d4"]
22

3-
[ext_resource type="PackedScene" path="res://ActionRemapButton.tscn" id="1"]
3+
[ext_resource type="PackedScene" uid="uid://cy6kmby6mupvv" path="res://ActionRemapButton.tscn" id="1"]
44

55
[node name="InputRemapMenu" type="Control"]
66
anchor_right = 1.0
@@ -11,9 +11,6 @@ anchor_right = 1.0
1111
offset_top = 24.0
1212
offset_bottom = 55.0
1313
text = "Click on a button to reassign its action key."
14-
__meta__ = {
15-
"_edit_use_anchors_": false
16-
}
1714

1815
[node name="RemapButtonGroup" type="Button" parent="."]
1916
anchor_left = 0.5
@@ -26,9 +23,6 @@ offset_right = 160.0
2623
offset_bottom = 144.0
2724
disabled = true
2825
flat = true
29-
__meta__ = {
30-
"_edit_use_anchors_": false
31-
}
3226

3327
[node name="ActionsList" type="VBoxContainer" parent="RemapButtonGroup"]
3428
anchor_left = 0.5
@@ -39,9 +33,6 @@ offset_left = -160.0
3933
offset_top = -140.0
4034
offset_right = 160.0
4135
offset_bottom = 140.0
42-
__meta__ = {
43-
"_edit_use_anchors_": false
44-
}
4536

4637
[node name="ActionRemapRow" type="HBoxContainer" parent="RemapButtonGroup/ActionsList"]
4738
offset_right = 320.0
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# This is an autoload (singleton) which will save
2+
# the key maps in a simple way through a dictionary.
3+
extends Node
4+
5+
const keymaps_path = "user://keymaps.dat"
6+
var keymaps: Dictionary
7+
8+
9+
func _ready() -> void:
10+
# First we create the keymap dictionary on startup with all
11+
# the keymap actions we have.
12+
for action in InputMap.get_actions():
13+
if InputMap.action_get_events(action).size() != 0:
14+
keymaps[action] = InputMap.action_get_events(action)[0]
15+
load_keymap()
16+
17+
18+
func load_keymap() -> void:
19+
var file := File.new()
20+
if not file.file_exists(keymaps_path):
21+
save_keymap() # There is no save file yet, so let's create one.
22+
return
23+
file.open(keymaps_path, File.READ)
24+
var temp_keymap = file.get_var(true) as Dictionary
25+
file.close()
26+
# We don't just replace the keymaps dictionary, because if you
27+
# updated your game and removed/added keymaps, the data of this
28+
# save file may have invalid actions. So we check one by one to
29+
# make sure that the keymap dictionary really has all current actions.
30+
for action in keymaps.keys():
31+
if temp_keymap.has(action):
32+
keymaps[action] = temp_keymap[action]
33+
# Whilst setting the keymap dictionary, we also set the
34+
# correct InputMap event
35+
InputMap.action_erase_events(action)
36+
InputMap.action_add_event(action, keymaps[action])
37+
38+
39+
func save_keymap() -> void:
40+
# For saving the keymap, we just save the entire dictionary as a var.
41+
var file := File.new()
42+
file.open(keymaps_path, File.WRITE)
43+
file.store_var(keymaps, true)
44+
file.close()

gui/input_mapping/project.godot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ run/main_scene="res://InputRemapMenu.tscn"
2020
config/icon="res://icon.png"
2121
config/features=PackedStringArray("4.0")
2222

23+
[autoload]
24+
25+
KeyPersistence="*res://KeyPersistence.gd"
26+
2327
[display]
2428

2529
window/size/viewport_width=640

0 commit comments

Comments
 (0)