From bbb01eecb61d463c289470341c85058717c96408 Mon Sep 17 00:00:00 2001 From: nokoe Date: Mon, 1 Jul 2024 14:19:37 +0200 Subject: object oriented settings --- client/menu/menu_manager.gd | 2 +- client/menu/settings/dropdown_setting.gd | 40 ++++++++++++++ client/menu/settings/game_setting.gd | 51 ++++++++++++++++++ client/menu/settings/range_setting.gd | 42 +++++++++++++++ client/menu/settings/settings_menu.gd | 43 +++++++++++++++ client/menu/settings/settings_row.gd | 36 +++++++++++++ client/menu/settings/settings_row.tscn | 39 ++++++++++++++ client/menu/settings/text_setting.gd | 38 +++++++++++++ client/menu/settings/toggle_setting.gd | 34 ++++++++++++ client/menu/settings_menu.gd | 59 --------------------- client/menu/settings_menu.tscn | 2 +- client/menu/settings_row.gd | 91 -------------------------------- client/menu/settings_row.tscn | 39 -------------- client/menu/setup_menu.gd | 2 +- 14 files changed, 326 insertions(+), 192 deletions(-) create mode 100644 client/menu/settings/dropdown_setting.gd create mode 100644 client/menu/settings/game_setting.gd create mode 100644 client/menu/settings/range_setting.gd create mode 100644 client/menu/settings/settings_menu.gd create mode 100644 client/menu/settings/settings_row.gd create mode 100644 client/menu/settings/settings_row.tscn create mode 100644 client/menu/settings/text_setting.gd create mode 100644 client/menu/settings/toggle_setting.gd delete mode 100644 client/menu/settings_menu.gd delete mode 100644 client/menu/settings_row.gd delete mode 100644 client/menu/settings_row.tscn (limited to 'client/menu') 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 . +# +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 . +# +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 . +# +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/settings_menu.gd b/client/menu/settings/settings_menu.gd new file mode 100644 index 00000000..bdced23b --- /dev/null +++ b/client/menu/settings/settings_menu.gd @@ -0,0 +1,43 @@ +# Undercooked - a game about cooking +# Copyright 2024 metamuffin +# +# 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 . +# +extends Control + +@onready var options: VBoxContainer = $OuterGap/Panel/InnerGap/VBoxContainer/ScrollContainer/Options +@onready var menu_manager: MenuManager = get_parent() + +func _on_back_pressed(): + Global.save_settings() + Global.update_language() + Global.update_fullscreen() + menu_manager.go_back() + +func _ready(): + update_rows() + +func show_graphis(): + menu_manager.goto("settings_graphics") + +func update_rows(fix_focus = false): + for c in options.get_children(): + c.queue_free() + + for k in Global.settings.keys(): + var row: SettingsRow = Global.settings[k].get_row() + options.add_child(row) + + if fix_focus: + await get_tree().process_frame + Global.focus_first_button(self) 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 . +# +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/settings_row.tscn b/client/menu/settings/settings_row.tscn new file mode 100644 index 00000000..d9c1f215 --- /dev/null +++ b/client/menu/settings/settings_row.tscn @@ -0,0 +1,39 @@ +[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_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_7k5da") + +[node name="SettingsRow" type="PanelContainer"] +offset_right = 105.0 +offset_bottom = 23.0 +size_flags_horizontal = 3 +theme = ExtResource("1_iij3k") +script = ExtResource("2_l8i7p") + +[node name="HBoxContainer" type="HBoxContainer" parent="."] +layout_mode = 2 + +[node name="Label" type="Label" parent="HBoxContainer"] +layout_mode = 2 +size_flags_horizontal = 3 +theme_override_styles/normal = SubResource("StyleBoxEmpty_pk3rs") + +[node name="BoxContainer" type="BoxContainer" parent="HBoxContainer"] +custom_minimum_size = Vector2(300, 50) +layout_mode = 2 +alignment = 2 + +[node name="Reset" type="Button" parent="HBoxContainer"] +layout_mode = 2 +theme_override_fonts/font = SubResource("FontVariation_o6i7s") +theme_override_font_sizes/font_size = 24 +text = "↺" + +[connection signal="pressed" from="HBoxContainer/Reset" to="." method="_on_reset_pressed"] 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 . +# +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 . +# +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.gd b/client/menu/settings_menu.gd deleted file mode 100644 index 9f033d40..00000000 --- a/client/menu/settings_menu.gd +++ /dev/null @@ -1,59 +0,0 @@ -# Undercooked - a game about cooking -# Copyright 2024 metamuffin -# -# 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 . -# -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() - menu_manager.go_back() - -func _ready(): - update_rows() - -func show_graphis(): - menu_manager.goto("settings_graphics") - -func update_rows(fix_focus = false): - for c in options.get_children(): - 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) - 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_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/settings_row.tscn b/client/menu/settings_row.tscn deleted file mode 100644 index d08963d6..00000000 --- a/client/menu/settings_row.tscn +++ /dev/null @@ -1,39 +0,0 @@ -[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"] - -[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_pk3rs"] -content_margin_left = 16.0 - -[sub_resource type="FontVariation" id="FontVariation_o6i7s"] -base_font = ExtResource("3_qh45r") - -[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") - -[node name="HBoxContainer" type="HBoxContainer" parent="."] -layout_mode = 2 - -[node name="Label" type="Label" parent="HBoxContainer"] -layout_mode = 2 -size_flags_horizontal = 3 -theme_override_styles/normal = SubResource("StyleBoxEmpty_pk3rs") - -[node name="BoxContainer" type="BoxContainer" parent="HBoxContainer"] -custom_minimum_size = Vector2(300, 50) -layout_mode = 2 -alignment = 2 - -[node name="Reset" type="Button" parent="HBoxContainer"] -layout_mode = 2 -theme_override_fonts/font = SubResource("FontVariation_o6i7s") -theme_override_font_sizes/font_size = 24 -text = "↺" - -[connection signal="pressed" from="HBoxContainer/Reset" to="." method="_on_reset_pressed"] 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") -- cgit v1.2.3-70-g09d2