Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions gui/input_mapping/ActionRemapButton.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
9 changes: 3 additions & 6 deletions gui/input_mapping/ActionRemapButton.tscn
Original file line number Diff line number Diff line change
@@ -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" )
11 changes: 1 addition & 10 deletions gui/input_mapping/InputRemapMenu.tscn
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
44 changes: 44 additions & 0 deletions gui/input_mapping/KeyPersistence.gd
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 4 additions & 0 deletions gui/input_mapping/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down