diff options
author | tpart <tpart120@proton.me> | 2024-06-29 20:21:13 +0200 |
---|---|---|
committer | tpart <tpart120@proton.me> | 2024-06-29 20:21:20 +0200 |
commit | c865b535d9437d457162f1aad545383653deafc5 (patch) | |
tree | 05eb9d7f0eddcc79ea4588bbd0ffa7d554e88731 | |
parent | 0d59b04feb71b59ed742a61991768733a7e608f1 (diff) | |
download | hurrycurry-c865b535d9437d457162f1aad545383653deafc5.tar hurrycurry-c865b535d9437d457162f1aad545383653deafc5.tar.bz2 hurrycurry-c865b535d9437d457162f1aad545383653deafc5.tar.zst |
Re-write settings system
-rw-r--r-- | client/global.gd | 69 | ||||
-rw-r--r-- | client/menu/settings_menu.gd | 24 | ||||
-rw-r--r-- | client/menu/settings_menu.tscn | 23 | ||||
-rw-r--r-- | client/menu/settings_row.gd | 42 |
4 files changed, 130 insertions, 28 deletions
diff --git a/client/global.gd b/client/global.gd index f25845a6..35244fa7 100644 --- a/client/global.gd +++ b/client/global.gd @@ -17,6 +17,15 @@ # extends Node +# Each setting contains a dictionary with the following keys: +# "type": The type of the setting. Can be "toggle", "line", "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. +# "apply": Only for type "dropdown_preset": A dictionary to override all other settings. +# "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, @@ -25,27 +34,62 @@ const DEFAULT_PROFILE := { 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)") - }, + } } +const GRAPHICS_PRESETS = [ + # Low: + { + "voxel_gi": false, + "sdfgi": false + }, + # Medium: + { + "voxel_gi": false, + "sdfgi": false + }, + # High: + { + "voxel_gi": false, + "sdfgi": true + }, + # Ultra: + { + "voxel_gi": true, + "sdfgi": false + } +] + var profile: Dictionary var settings: Dictionary @@ -54,9 +98,10 @@ var error_message = "" var fade_next := false # Set true when transitioning from another scene (fade in requried) -func _ready(): +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) @@ -75,16 +120,12 @@ func load_dict(path: String, default: Dictionary) -> Dictionary: return default var f = FileAccess.open(path, FileAccess.READ) var saved_dict = f.get_var() - var res: Dictionary = {} + if saved_dict != null and saved_dict is Dictionary: - for i in default.keys(): - if saved_dict.has(i): - res[i] = saved_dict[i] - else: - res[i] = default[i] + add_missing_keys(saved_dict, default) - print("Loaded dict: ", res) - return res + print("Loaded dict: ", saved_dict) + return saved_dict func on_vulkan() -> bool: return ProjectSettings.get_setting("rendering/rendering_device/driver") == "vulkan" @@ -98,3 +139,11 @@ func focus_first_button(node: Node) -> bool: if focus_first_button(c): return true return false + +func add_missing_keys(dict: Dictionary, reference: Dictionary): + for k in reference.keys(): + if !dict.has(k): + dict[k] = reference + else: + if dict[k] is Dictionary: + add_missing_keys(dict[k], reference[k]) diff --git a/client/menu/settings_menu.gd b/client/menu/settings_menu.gd index 6b2a7f60..4c14a459 100644 --- a/client/menu/settings_menu.gd +++ b/client/menu/settings_menu.gd @@ -15,7 +15,7 @@ # extends Control -@onready var options: VBoxContainer = $outer_gap/panel/inner_gap/options +@onready var options: VBoxContainer = $OuterGap/Panel/InnerGap/VBoxContainer/ScrollContainer/Options @onready var menu_manager: MenuManager = get_parent() var settings: Dictionary @@ -26,11 +26,29 @@ func _on_back_pressed(): Global.save_settings() menu_manager.go_back() - func _ready(): + update_rows() + +func show_graphis(): + menu_manager.goto("settings_graphics") + +func update_rows(): + for c in options.get_children(): + c.queue_free() + for k in Global.settings.keys(): var v = Global.settings[k] var row: SettingsRow = preload("res://menu/settings_row.tscn").instantiate() - row.setup(v["description"], v["value"]) + row.setup(k, Global.settings, Global.default_settings) + row.connect("apply_preset", apply_preset) options.add_child(row) settings[k] = row + +func apply_preset(preset: Dictionary): + for k in settings.keys(): + Global.settings[k]["value"] = settings[k].get_value() + + for k in preset.keys(): + Global.settings[k]["value"] = preset[k] + + update_rows() diff --git a/client/menu/settings_menu.tscn b/client/menu/settings_menu.tscn index 84da46f3..9be24d86 100644 --- a/client/menu/settings_menu.tscn +++ b/client/menu/settings_menu.tscn @@ -14,7 +14,7 @@ grow_vertical = 2 theme = ExtResource("1_foq3a") script = ExtResource("2_3hgm8") -[node name="outer_gap" type="MarginContainer" parent="."] +[node name="OuterGap" type="MarginContainer" parent="."] layout_mode = 1 anchors_preset = 15 anchor_right = 1.0 @@ -22,11 +22,11 @@ anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 -[node name="panel" type="Panel" parent="outer_gap"] +[node name="Panel" type="Panel" parent="OuterGap"] material = ExtResource("3_8nykw") layout_mode = 2 -[node name="inner_gap" type="MarginContainer" parent="outer_gap/panel"] +[node name="InnerGap" type="MarginContainer" parent="OuterGap/Panel"] layout_mode = 1 anchors_preset = 15 anchor_right = 1.0 @@ -38,18 +38,27 @@ theme_override_constants/margin_top = 40 theme_override_constants/margin_right = 40 theme_override_constants/margin_bottom = 40 -[node name="options" type="VBoxContainer" parent="outer_gap/panel/inner_gap"] +[node name="VBoxContainer" type="VBoxContainer" parent="OuterGap/Panel/InnerGap"] layout_mode = 2 -[node name="title" type="Label" parent="outer_gap/panel/inner_gap/options"] +[node name="Title" type="Label" parent="OuterGap/Panel/InnerGap/VBoxContainer"] layout_mode = 2 size_flags_horizontal = 0 theme_override_font_sizes/font_size = 36 text = "Settings" -[node name="back" type="Button" parent="outer_gap/panel/inner_gap"] +[node name="ScrollContainer" type="ScrollContainer" parent="OuterGap/Panel/InnerGap/VBoxContainer"] +layout_mode = 2 +size_flags_vertical = 3 + +[node name="Options" type="VBoxContainer" parent="OuterGap/Panel/InnerGap/VBoxContainer/ScrollContainer"] +layout_mode = 2 +size_flags_horizontal = 3 +size_flags_vertical = 3 + +[node name="Back" type="Button" parent="OuterGap/Panel/InnerGap/VBoxContainer"] layout_mode = 2 size_flags_vertical = 8 text = "Back to Main Menu" -[connection signal="pressed" from="outer_gap/panel/inner_gap/back" to="." method="_on_back_pressed"] +[connection signal="pressed" from="OuterGap/Panel/InnerGap/VBoxContainer/Back" to="." method="_on_back_pressed"] diff --git a/client/menu/settings_row.gd b/client/menu/settings_row.gd index ee4b8cb9..4f1d326f 100644 --- a/client/menu/settings_row.gd +++ b/client/menu/settings_row.gd @@ -1,20 +1,40 @@ class_name SettingsRow extends PanelContainer +signal apply_preset + @onready var value_parent = $HBoxContainer/BoxContainer @onready var label = $HBoxContainer/Label var value_node var description = tr("no value was given to the row") -func setup(description_: String, value): - description = description_ - if value is bool: - value_node = CheckButton.new() - value_node.button_pressed = value - elif value is String: - value_node = LineEdit.new() - value_node.text = value +func setup(key: String, dict: Dictionary, defaults: Dictionary): + var setting = dict[key] + description = setting["description"] + var value = setting["value"] + var 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 + value_node.placeholder_text = default + "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(setting["apply"])) + _: + push_error("Could not set up SettingsRow: Unknown setting type \"%s\"" % setting["type"]) func _ready(): if value_node != null: @@ -24,13 +44,19 @@ func _ready(): 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 else: + push_error("get_value() failed for unknown node type: %s" % value_node) return null else: return null |