aboutsummaryrefslogtreecommitdiff
path: root/client/menu/settings/input/input_manager.gd
blob: 96cdca09c23c20eb078e256e7b2c5f6b83e44c79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Hurry Curry! - a game about cooking
# Copyright 2024 tpart
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
extends Node

var action_descriptions = {
	"forwards": tr("Move forwards"),
	"backwards": tr("Move backwards"),
	"left": tr("Move left"),
	"right": tr("Move right"),
	"rotate_left": tr("Rotate camera to the left"),
	"rotate_right": tr("Rotate camera to the right"),
	"rotate_up": tr("Rotate camera upwards"),
	"rotate_down": tr("Rotate camera downwards"),
	"interact": tr("Interact", "Interacting with items, etc."),
	"boost": tr("Boost movement"),
	"zoom_in": tr("Zoom in"),
	"zoom_out": tr("Zoom out"),
	"chat": tr("Toggle chat", "Toggle chat on or off"),
	"reset": tr("Reset camera view"),
	"fullscreen": tr("Toggle fullscreen"),
	"previous": tr("Previous"),
	"next": tr("Next"),
	"start_game": tr("Start game"),
	"join_spectate": tr("Join / Spectate"),
	"zoom_in_discrete": tr("Zoom in (discrete)"),
	"zoom_out_discrete": tr("Zoom out (discrete)"),
	"scroll_down": tr("Scroll down"),
	"scroll_up": tr("Scroll up"),
}

var default_input_map = {}
var input_map

func _init():
	default_input_map = get_input_map()
	input_map = default_input_map.duplicate(true)

func get_input_map() -> Dictionary:
	var actions = InputMap.get_actions().filter(func isBuiltIn(k: String): return !k.begins_with("ui_"))
	var kb = {}
	for a in actions:
		var input_events: Array[InputEvent] = InputMap.action_get_events(a).duplicate(true)
		kb[a] = input_events
	return kb

func input_map_to_settings_dictionary(map: Dictionary) -> Dictionary:
	var settings_dict := {}
	for k in map.keys():
		var events = map[k]
		settings_dict[k] = InputSetting.new(action_descriptions[k] if action_descriptions.has(k) else k, events)
	return settings_dict

func settings_dictionary_to_input_map(settings: Dictionary) -> Dictionary:
	var map := {}
	for k in settings.keys():
		var setting: InputSetting = settings[k]
		map[k] = setting.get_value()
	return map

func change_input_map_action(action_name: String, events: Array, save: bool = true):
	if !InputMap.has_action(action_name):
		push_error("Action %s does not exist" % action_name, false)
		return
	# Erase previous keybindings
	InputMap.action_erase_events(action_name)
	# Add new keybindings
	for e in events:
		InputMap.action_add_event(action_name, e)
	
	if save:
		# Update input map dictionary
		input_map = get_input_map()
		# Save settings
		Global.set_setting("input_map", input_map.duplicate(true))

func apply_input_map(new_input_map: Dictionary):
	# Load into input map dictionary
	for k in new_input_map.keys():
		input_map[k] = []
		for a in new_input_map[k]:
			input_map[k].append(a)
	
	# Apply keybindings
	for k in input_map.keys():
		change_input_map_action(k, input_map[k], false)

func reset_input_map():
	Global.set_setting("input_map", default_input_map.duplicate())
	apply_input_map(Global.get_setting("input_map"))