diff options
-rw-r--r-- | client/global.gd | 142 | ||||
-rw-r--r-- | client/menu/menu_manager.gd | 2 | ||||
-rw-r--r-- | client/menu/settings/dropdown_setting.gd | 40 | ||||
-rw-r--r-- | client/menu/settings/game_setting.gd | 51 | ||||
-rw-r--r-- | client/menu/settings/range_setting.gd | 42 | ||||
-rw-r--r-- | client/menu/settings/settings_menu.gd (renamed from client/menu/settings_menu.gd) | 18 | ||||
-rw-r--r-- | client/menu/settings/settings_row.gd | 36 | ||||
-rw-r--r-- | client/menu/settings/settings_row.tscn (renamed from client/menu/settings_row.tscn) | 12 | ||||
-rw-r--r-- | client/menu/settings/text_setting.gd | 38 | ||||
-rw-r--r-- | client/menu/settings/toggle_setting.gd | 34 | ||||
-rw-r--r-- | client/menu/settings_menu.tscn | 2 | ||||
-rw-r--r-- | client/menu/settings_row.gd | 91 | ||||
-rw-r--r-- | client/menu/setup_menu.gd | 2 |
13 files changed, 295 insertions, 215 deletions
diff --git a/client/global.gd b/client/global.gd index e756fcfe..e5ec91ee 100644 --- a/client/global.gd +++ b/client/global.gd @@ -33,96 +33,21 @@ var default_profile := { "last_server_url": "" } +var languages := [tr("System default"), "en", "de"] + var default_settings := { - "language": { - "type": "dropdown", - "options": [tr("System default"), "en", "de"], - "value": 0, - "description": tr("Language"), - }, - "fullscreen": { - "type": "toggle", - "value": false, - "description": tr("Fullscreen") - }, - "interpolate_camera_rotation": { - "type": "toggle", - "value": true, - "description": tr("Interpolate the camera rotation") - }, - "invert_camera": { - "type": "toggle", - "value": false, - "description": tr("Invert camera movement") - }, - "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, looks good)") - }, - "sdfgi": { - "type": "toggle", - "value": false, - "description": tr("Use SDFGI (Doesn't block the game, looks worse)") - }, - "grass_amount": { - "type": "range", - "value": 16, - "min": 0, - "max": 32, - "description": tr("3D grass amount per grass tile") - }, - "setup_complete": { - "type": "toggle", - "value": false, - "description": tr("Initial setup complete. (Uncheck and restart to reenter)") - } + "language": DropdownSetting.new(tr("Language"), 0, languages), + "fullscreen": ToggleSetting.new(tr("Fullscreen"), false), + "interpolate_camera_rotation": ToggleSetting.new(tr("Interpolate the camera rotation"), true), + "invert_camera": ToggleSetting.new(tr("Invert camera movement"), false), + "server_binary": TextSetting.new(tr("Server binary (leave empty to search PATH)"), ""), + "server_data": TextSetting.new(tr("Server data directory (leave empty to auto-detect)"), ""), + "voxel_gi": ToggleSetting.new(tr("Use VoxelGI (Blocks the game on map update but is more accurate)"), false), + "sdfgi": ToggleSetting.new(tr("Use SDFGI (Doesn't block the game but produces more artifacts)"), false), + "grass_amount": RangeSetting.new(tr("3D grass amount per grass tile"), 16, 0, 32), + "setup_complete": ToggleSetting.new(tr("Initial setup complete. (Uncheck and restart to reenter)"), false) } -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 @@ -135,20 +60,20 @@ var fade_next := false # Set true when transitioning from another scene (fade in func _init(): profile = load_dict("user://profile", default_profile) - settings = load_dict("user://settings", default_settings) + load_settings("user://settings") update_fullscreen() update_language() -func _input(event): +func _input(_event): if Input.is_action_just_pressed("fullscreen"): Global.set_setting("fullscreen", not Global.get_setting("fullscreen")) save_settings() update_fullscreen() func update_language(): - var lang_idx = get_setting("language") - var lang = settings["language"]["options"][lang_idx] - + var lang_idx: int = get_setting("language") + var lang = languages[lang_idx] + if lang_idx != 0: # 0 is system language TranslationServer.set_locale(lang) else: @@ -164,7 +89,13 @@ func save_profile(): save_dict("user://profile", profile) func save_settings(): - save_dict("user://settings", settings) + for v in settings.values(): + v.fetch_setting() + var dict := {} + for k in settings.keys(): + var setting: GameSetting = settings[k] + dict[k] = setting.get_value() + save_dict("user://settings", dict) func save_dict(path: String, dict: Dictionary): var f = FileAccess.open(path, FileAccess.WRITE) @@ -177,19 +108,34 @@ func load_dict(path: String, default: Dictionary) -> Dictionary: 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 load_settings(path: String): + settings = default_settings + if not FileAccess.file_exists(path): + print("Skip settings load") + return + var f = FileAccess.open(path, FileAccess.READ) + var saved_dict = f.get_var() + + if saved_dict != null and saved_dict is Dictionary: + for k in default_settings.keys(): + var setting: GameSetting = default_settings[k] + if saved_dict.has(k): + setting.set_value(saved_dict[k]) + settings[k] = setting + 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]) @@ -217,7 +163,7 @@ func add_missing_keys(dict: Dictionary, reference: Dictionary): func get_setting(key: String): if settings.has(key): - return settings[key]["value"] + return settings[key].get_value() else: push_error("Tried to access setting \"%s\", which does not exist (missing key)" % key) return null @@ -225,5 +171,5 @@ func get_setting(key: String): func set_setting(key: String, value): if !settings.has(key): push_error("Tried to set setting \"%s\", which does not yet exist (missing key)" % key) - settings[key] = {} - settings[key]["value"] = value + return + settings[key].set_value(value) diff --git a/client/menu/menu_manager.gd b/client/menu/menu_manager.gd index bba074f6..85b574f7 100644 --- a/client/menu/menu_manager.gd +++ b/client/menu/menu_manager.gd @@ -27,7 +27,7 @@ class_name MenuManager var menu_stack = ["main"] func _ready(): - if not Global.settings["setup_complete"]["value"]: return transition.instant_to("res://menu/setup_menu.tscn") + if not Global.get_setting("setup_complete"): return transition.instant_to("res://menu/setup_menu.tscn") get_viewport().gui_focus_changed.connect(Sound.play_hover_maybe) Global.focus_first_button(menus[menu_stack.back()]) diff --git a/client/menu/settings/dropdown_setting.gd b/client/menu/settings/dropdown_setting.gd new file mode 100644 index 00000000..47f7355b --- /dev/null +++ b/client/menu/settings/dropdown_setting.gd @@ -0,0 +1,40 @@ +# Undercooked - a game about cooking +# 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/>. +# +class_name DropdownSetting +extends GameSetting + +var options: Array + +func _init(new_description: String, new_preset: int, new_options: Array): + assert(new_preset < new_options.size()) + super(new_description, new_preset) + options = new_options + +func _update_row(): + super() + row.value_node = OptionButton.new() + for i in options: + row.value_node.add_item(i) + row.value_node.select(_value) + +func fetch_setting(): + if row != null: + _value = row.value_node.selected + +func set_value(v): + super(v) + if row != null: + row.value_node.selected = _value diff --git a/client/menu/settings/game_setting.gd b/client/menu/settings/game_setting.gd new file mode 100644 index 00000000..67dca232 --- /dev/null +++ b/client/menu/settings/game_setting.gd @@ -0,0 +1,51 @@ +# Undercooked - a game about cooking +# 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/>. +# +class_name GameSetting +extends Object + +var preset +var _value +var description: String + +var row: SettingsRow + +func _init(new_description: String, new_preset): + _value = new_preset + preset = new_preset + description = new_description + +func reset(): + set_value(preset) + +func get_row() -> SettingsRow: + _update_row() + return row + +func _update_row(): + if row != null: + row.queue_free() + row = preload("res://menu/settings/settings_row.tscn").instantiate() + row.description = description + row.reset.connect(reset) + +func fetch_setting(): + pass + +func get_value(): + return _value + +func set_value(v): + _value = v diff --git a/client/menu/settings/range_setting.gd b/client/menu/settings/range_setting.gd new file mode 100644 index 00000000..97229db9 --- /dev/null +++ b/client/menu/settings/range_setting.gd @@ -0,0 +1,42 @@ +# Undercooked - a game about cooking +# 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/>. +# +class_name RangeSetting +extends GameSetting + +var min_value: float +var max_value: float + +func _init(new_description: String, new_preset: float, new_min_value: float, new_max_value: float): + super(new_description, new_preset) + min_value = new_min_value + max_value = new_max_value + +func _update_row(): + super() + row.value_node = HSlider.new() + row.value_node.min_value = min_value + row.value_node.max_value = max_value + row.value_node.tick_count = abs(max_value - min_value) + row.value_node.value = _value + +func fetch_setting(): + if row != null: + _value = row.value_node.value + +func set_value(v): + super(v) + if row != null: + row.value_node.value = _value diff --git a/client/menu/settings_menu.gd b/client/menu/settings/settings_menu.gd index 9f033d40..bdced23b 100644 --- a/client/menu/settings_menu.gd +++ b/client/menu/settings/settings_menu.gd @@ -18,11 +18,7 @@ extends Control @onready var options: VBoxContainer = $OuterGap/Panel/InnerGap/VBoxContainer/ScrollContainer/Options @onready var menu_manager: MenuManager = get_parent() -var settings: Dictionary - func _on_back_pressed(): - for k in settings.keys(): - Global.set_setting(k, settings[k].get_value()) Global.save_settings() Global.update_language() Global.update_fullscreen() @@ -39,21 +35,9 @@ func update_rows(fix_focus = false): c.queue_free() for k in Global.settings.keys(): - var row: SettingsRow = preload("res://menu/settings_row.tscn").instantiate() - row.setup(k, Global.settings, Global.default_settings) - row.connect("apply_preset", apply_preset) + var row: SettingsRow = Global.settings[k].get_row() options.add_child(row) - settings[k] = row if fix_focus: await get_tree().process_frame Global.focus_first_button(self) - -func apply_preset(preset: Dictionary): - for k in settings.keys(): - Global.set_setting(k, settings[k].get_value()) - - for k in preset.keys(): - Global.set_setting(k, preset[k]) - - update_rows(true) diff --git a/client/menu/settings/settings_row.gd b/client/menu/settings/settings_row.gd new file mode 100644 index 00000000..8c71a8c5 --- /dev/null +++ b/client/menu/settings/settings_row.gd @@ -0,0 +1,36 @@ +# Undercooked - a game about cooking +# 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/>. +# +class_name SettingsRow +extends PanelContainer + +signal reset() + +@onready var value_parent = $HBoxContainer/BoxContainer +@onready var label = $HBoxContainer/Label + +var value_node: Node +var description = tr("no value was given to the row") + +func _ready(): + if value_node != null: + var c: Control = value_node + c.size_flags_vertical = Control.SIZE_EXPAND_FILL + c.size_flags_horizontal = Control.SIZE_EXPAND_FILL + label.text = description + value_parent.add_child(c) + +func _on_reset_pressed(): + reset.emit() diff --git a/client/menu/settings_row.tscn b/client/menu/settings/settings_row.tscn index d08963d6..d9c1f215 100644 --- a/client/menu/settings_row.tscn +++ b/client/menu/settings/settings_row.tscn @@ -1,21 +1,21 @@ [gd_scene load_steps=6 format=3 uid="uid://o5e5vpem8w0k"] -[ext_resource type="Theme" uid="uid://b0qmvo504e457" path="res://menu/theme/theme.tres" id="1_fk0r2"] -[ext_resource type="Script" path="res://menu/settings_row.gd" id="1_lxjnc"] -[ext_resource type="FontFile" uid="uid://5ixo6b3bd3km" path="res://menu/theme/font-josefin-sans.woff2" id="3_qh45r"] +[ext_resource type="Theme" uid="uid://b0qmvo504e457" path="res://menu/theme/theme.tres" id="1_iij3k"] +[ext_resource type="Script" path="res://menu/settings/settings_row.gd" id="2_l8i7p"] +[ext_resource type="FontFile" uid="uid://5ixo6b3bd3km" path="res://menu/theme/font-josefin-sans.woff2" id="3_7k5da"] [sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_pk3rs"] content_margin_left = 16.0 [sub_resource type="FontVariation" id="FontVariation_o6i7s"] -base_font = ExtResource("3_qh45r") +base_font = ExtResource("3_7k5da") [node name="SettingsRow" type="PanelContainer"] offset_right = 105.0 offset_bottom = 23.0 size_flags_horizontal = 3 -theme = ExtResource("1_fk0r2") -script = ExtResource("1_lxjnc") +theme = ExtResource("1_iij3k") +script = ExtResource("2_l8i7p") [node name="HBoxContainer" type="HBoxContainer" parent="."] layout_mode = 2 diff --git a/client/menu/settings/text_setting.gd b/client/menu/settings/text_setting.gd new file mode 100644 index 00000000..c8aa49d4 --- /dev/null +++ b/client/menu/settings/text_setting.gd @@ -0,0 +1,38 @@ +# Undercooked - a game about cooking +# 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/>. +# +class_name TextSetting +extends GameSetting + +var placeholder: String + +func _init(new_description: String, new_preset: String, new_placeholder: String = ""): + super(new_description, new_preset) + placeholder = new_placeholder + +func _update_row(): + super() + row.value_node = LineEdit.new() + row.value_node.text = _value + row.value_node.placeholder_text = placeholder + +func fetch_setting(): + if row != null: + _value = row.value_node.text + +func set_value(v): + super(v) + if row != null: + row.value_node.text = _value diff --git a/client/menu/settings/toggle_setting.gd b/client/menu/settings/toggle_setting.gd new file mode 100644 index 00000000..90e42264 --- /dev/null +++ b/client/menu/settings/toggle_setting.gd @@ -0,0 +1,34 @@ +# Undercooked - a game about cooking +# 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/>. +# +class_name ToggleSetting +extends GameSetting + +func _init(new_description: String, new_preset: bool): + super(new_description, new_preset) + +func fetch_setting(): + if row != null: + _value = row.value_node.button_pressed + +func _update_row(): + super() + row.value_node = CheckButton.new() + row.value_node.button_pressed = _value + +func set_value(v): + super(v) + if row != null: + row.value_node.button_pressed = _value diff --git a/client/menu/settings_menu.tscn b/client/menu/settings_menu.tscn index ecaea17c..24d5aab4 100644 --- a/client/menu/settings_menu.tscn +++ b/client/menu/settings_menu.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=4 format=3 uid="uid://8ic77jmadadj"] [ext_resource type="Theme" uid="uid://b0qmvo504e457" path="res://menu/theme/theme.tres" id="1_foq3a"] -[ext_resource type="Script" path="res://menu/settings_menu.gd" id="2_3hgm8"] +[ext_resource type="Script" path="res://menu/settings/settings_menu.gd" id="2_3hgm8"] [ext_resource type="Material" uid="uid://beea1pc5nt67r" path="res://menu/theme/dark_blur_material.tres" id="3_8nykw"] [node name="SettingsMenu" type="Control"] diff --git a/client/menu/settings_row.gd b/client/menu/settings_row.gd deleted file mode 100644 index d57bbee0..00000000 --- a/client/menu/settings_row.gd +++ /dev/null @@ -1,91 +0,0 @@ -class_name SettingsRow -extends PanelContainer - -signal apply_preset - -@onready var value_parent = $HBoxContainer/BoxContainer -@onready var label = $HBoxContainer/Label - -var setting -var default -var value_node -var description = tr("no value was given to the row") - -func setup(key: String, dict: Dictionary, defaults: Dictionary): - setting = dict[key] - description = setting["description"] - var value = setting["value"] - default = defaults[key]["value"] - match setting["type"]: - "toggle": - value_node = CheckButton.new() - value_node.button_pressed = value - "line": - value_node = LineEdit.new() - value_node.text = value - if default != "": - value_node.placeholder_text = default - else: - value_node.placeholder_text = description - "dropdown": - value_node = OptionButton.new() - for i in setting["options"]: - value_node.add_item(i) - value_node.select(value) - "dropdown_preset": - value_node = OptionButton.new() - for i in setting["options"]: - value_node.add_item(i) - value_node.select(value) - value_node.connect("item_selected", apply.bind(Global.get(setting["apply"]))) - "range": - value_node = HSlider.new() - value_node.min_value = setting["min"] - value_node.max_value = setting["max"] - value_node.tick_count = abs(setting["max"] - setting["min"]) - value_node.value = value - _: - push_error("Could not set up SettingsRow: Unknown setting type \"%s\"" % setting["type"]) - -func _ready(): - if value_node != null: - var c: Control = value_node - c.size_flags_vertical = Control.SIZE_EXPAND_FILL - c.size_flags_horizontal = Control.SIZE_EXPAND_FILL - label.text = description - value_parent.add_child(c) - -func apply(idx: int, apply: Array): - emit_signal("apply_preset", apply[idx]) - -func get_value(): - if value_node != null: - if value_node is CheckButton: - return value_node.button_pressed - elif value_node is LineEdit: - return value_node.text - elif value_node is OptionButton: - return value_node.selected - elif value_node is HSlider: - return value_node.value - else: - push_error("get_value() failed for unknown node type: %s" % value_node) - return null - else: - return null - -func _on_reset_pressed(): - match setting["type"]: - "toggle": - value_node.button_pressed = default - "line": - value_node.text = default - "dropdown": - value_node.select(default) - "dropdown_preset": - value_node.select(default) - value_node.emit_signal("item_selected", value_node.selected) - "range": - value_node.value = default - _: - push_error("Could not reset setting: Unknown setting type \"%s\"" % setting["type"]) diff --git a/client/menu/setup_menu.gd b/client/menu/setup_menu.gd index 565753d8..c5080920 100644 --- a/client/menu/setup_menu.gd +++ b/client/menu/setup_menu.gd @@ -43,7 +43,7 @@ func _on_sign_pressed(): Global.profile["username"] = username.text Global.profile["character"] = character - Global.settings["setup_complete"]["value"] = true + Global.set_setting("setup_complete", true) Global.save_profile() Global.save_settings() $SceneTransition.transition_to("res://menu/menu_manager.tscn") |