generated from krampus/template-godot4
Keybinds are persisted to user config
This commit is contained in:
parent
b780bb5a2c
commit
dff8065f95
|
@ -20,6 +20,7 @@ run/max_fps=60
|
||||||
|
|
||||||
ClubCatalog="*res://src/equipment/clubs/club_catalog.tscn"
|
ClubCatalog="*res://src/equipment/clubs/club_catalog.tscn"
|
||||||
GameSettings="*res://src/game/game_settings.gd"
|
GameSettings="*res://src/game/game_settings.gd"
|
||||||
|
BindingLoader="*res://src/game/binding_loader.gd"
|
||||||
|
|
||||||
[debug]
|
[debug]
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
class_name BindingLoaderType extends Node
|
||||||
|
## Handles persisting action input bindings.
|
||||||
|
|
||||||
|
const BINDINGS_FILE := "user://bindings.tres"
|
||||||
|
|
||||||
|
|
||||||
|
func _ready() -> void:
|
||||||
|
# Map may not be defined if no keybinds have been written
|
||||||
|
if FileAccess.file_exists(BINDINGS_FILE):
|
||||||
|
print_debug("Reading keybinds from ", BINDINGS_FILE)
|
||||||
|
var map: BindingMap = load(BINDINGS_FILE) as BindingMap
|
||||||
|
|
||||||
|
# Overwrite InputMap with loaded bindings
|
||||||
|
for action: StringName in map.bindings.keys():
|
||||||
|
# Clear existing bindings
|
||||||
|
InputMap.action_erase_events(action)
|
||||||
|
# Apply loaded binding
|
||||||
|
var event: InputEvent = map.bindings[action]
|
||||||
|
if event:
|
||||||
|
InputMap.action_add_event(action, event)
|
||||||
|
|
||||||
|
|
||||||
|
func write() -> void:
|
||||||
|
# Build map from input actions
|
||||||
|
var map: BindingMap = BindingMap.new()
|
||||||
|
for action: StringName in InputMap.get_actions():
|
||||||
|
var events := InputMap.action_get_events(action)
|
||||||
|
if events:
|
||||||
|
map.bindings[action] = events[0]
|
||||||
|
else:
|
||||||
|
map.bindings[action] = null
|
||||||
|
|
||||||
|
# Write to disk
|
||||||
|
print_debug("Writing keybinds to ", BINDINGS_FILE)
|
||||||
|
ResourceSaver.save(map, BINDINGS_FILE)
|
|
@ -18,7 +18,6 @@ func _get_settings_elements() -> Array[Setting]:
|
||||||
|
|
||||||
|
|
||||||
func populate_control_bindings() -> void:
|
func populate_control_bindings() -> void:
|
||||||
InputMap.get
|
|
||||||
for action: StringName in InputMap.get_actions():
|
for action: StringName in InputMap.get_actions():
|
||||||
if not action.begins_with("ui_"):
|
if not action.begins_with("ui_"):
|
||||||
control_binding_list.add_child(ControlBinding.create(action))
|
control_binding_list.add_child(ControlBinding.create(action))
|
||||||
|
@ -38,6 +37,7 @@ func apply() -> void:
|
||||||
## Write all applied settings to disk.
|
## Write all applied settings to disk.
|
||||||
func save_settings() -> void:
|
func save_settings() -> void:
|
||||||
ProjectSettings.save_custom(SETTINGS_FILE)
|
ProjectSettings.save_custom(SETTINGS_FILE)
|
||||||
|
BindingLoader.write()
|
||||||
|
|
||||||
|
|
||||||
## Apply settings and close menu.
|
## Apply settings and close menu.
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
class_name BindingMap extends Resource
|
||||||
|
## Serializable action input map. Used by `BindingLoader`.
|
||||||
|
|
||||||
|
@export var bindings: Dictionary
|
Loading…
Reference in New Issue