aboutsummaryrefslogtreecommitdiff
path: root/client/global.gd
blob: 4d1139d2fd26b9a30bddd897fb5af4d92e49540f (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Undercooked - a game about cooking
# Copyright 2024 metamuffin
# Copyright 2024 tpart
# Copyright 2024 nokoe
#
# 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

# Each setting contains a dictionary with the following keys:
# "type": The type of the setting. Can be "toggle", "line", "range", "dropdown", or "dropdown_preset"
# "value": The value of the setting. When using "dropdown", this contains an int
#	as the index for the respecitve element in the "options" array.
# (Optional: Only when type "dropdown_preset") "apply": A dictionary to override all other settings.
# (Optional: Only when type "range") "min" and "max": The min and max values.
# "description": The setting name displayed in the settings menu.
# (Optional: Only when type "dropdown") "options": An array which contains all
# 	possible values.

const DEFAULT_PROFILE := {
	"username": "Giovanni",
	"character": 0,
	"last_server_url": ""
}

var default_settings := {
	"interpolate_camera_rotation": {
		"type": "toggle",
		"value": true,
		"description": tr("Interpolate the camera rotation")
	},
	"server_binary": {
		"type": "line",
		"value": "",
		"description": tr("Server binary (leave empty to search PATH)")
	},
	"server_data": {
		"type": "line",
		"value": "",
		"description": tr("Server data directory (leave empty to auto-detect)")
	},
	"graphics_preset": {
		"type": "dropdown_preset",
		"options": [tr("Low"), tr("Medium"), tr("High"), tr("Ultra")],
		"value": 1,
		"description": tr("Graphics preset"),
		"apply": "GRAPHICS_PRESETS"
	},
	"voxel_gi": {
		"type": "toggle",
		"value": false,
		"description": tr("Use VoxelGI (Blocks the game on map update)")
	},
	"sdfgi": {
		"type": "toggle",
		"value": false,
		"description": tr("Use SDFGI (Doesn't block the game but is more resource-hungry)")
	},
	"grass_amount": {
		"type": "range",
		"value": 16,
		"min": 0,
		"max": 32,
		"description": tr("3D grass amount per grass tile")
	}
}

const GRAPHICS_PRESETS = [
	# Low:
	{
		"voxel_gi": false,
		"sdfgi": false,
		"grass_amount": 0
	},
	# Medium:
	{
		"voxel_gi": false,
		"sdfgi": false,
		"grass_amount": 16
	},
	# High:
	{
		"voxel_gi": false,
		"sdfgi": true,
		"grass_amount": 16
	},
	# Ultra:
	{
		"voxel_gi": true,
		"sdfgi": false,
		"grass_amount": 16
	}
]

var profile: Dictionary
var settings: Dictionary

var server_url = ""
var error_message = ""

var focus_auto_changed := false

var fade_next := false # Set true when transitioning from another scene (fade in requried)

func _init():
	profile = load_dict("user://profile", DEFAULT_PROFILE)
	settings = load_dict("user://settings", default_settings)
	print("DONE LOADING")

func save_profile():
	save_dict("user://profile", profile)

func save_settings():
	save_dict("user://settings", settings)

func save_dict(path: String, dict: Dictionary):
	var f = FileAccess.open(path, FileAccess.WRITE)
	f.store_var(dict.duplicate(true))

func load_dict(path: String, default: Dictionary) -> Dictionary:
	# TOCTOU here. Godot docs says its fine.
	if not FileAccess.file_exists(path):
		print("Skip profile load")
		return default
	var f = FileAccess.open(path, FileAccess.READ)
	var saved_dict = f.get_var()
	
	if saved_dict != null and saved_dict is Dictionary:
		add_missing_keys(saved_dict, default)

	print("Loaded dict: ", saved_dict)
	return saved_dict

func on_vulkan() -> bool:
	return ProjectSettings.get_setting("rendering/rendering_device/driver") == "vulkan"

func focus_first_button(node: Node) -> bool:
	focus_auto_changed = true
	
	if node is Button:
		node.grab_focus()
		print("Node %s (%s) was selected for focus" % [node.name, node])
		return true
	for c in node.get_children():
		if focus_first_button(c):
			return true
	return false

func connect_button_sounds(node: Node):
	if node is Button:
		node.pressed.connect(Sound.play_click)
	if node is Button or node is LineEdit or node is Slider:
		node.mouse_entered.connect(Sound.play_hover)
	for c in node.get_children():
		connect_button_sounds(c)

func add_missing_keys(dict: Dictionary, reference: Dictionary):
	for k in reference.keys():
		if !dict.has(k):
			dict[k] = reference[k]
		else:
			if dict[k] is Dictionary:
				add_missing_keys(dict[k], reference[k])