diff options
-rw-r--r-- | client/game.gd | 2 | ||||
-rw-r--r-- | client/global.gd | 51 | ||||
-rw-r--r-- | client/menu/character_menu.gd | 14 | ||||
-rw-r--r-- | client/menu/main_menu.gd | 4 | ||||
-rw-r--r-- | client/menu/settings_menu.gd | 15 | ||||
-rw-r--r-- | client/menu/settings_row.gd | 36 | ||||
-rw-r--r-- | client/menu/settings_row.tscn | 24 |
7 files changed, 120 insertions, 26 deletions
diff --git a/client/game.gd b/client/game.gd index 3ee960f7..ab2cdfbd 100644 --- a/client/game.gd +++ b/client/game.gd @@ -154,7 +154,7 @@ func _ready(): p.bubble.set_text(item_names[item]) ) - mp.send_join(Global.settings["username"], Global.settings["character"]) + mp.send_join(Global.profile["username"], Global.profile["character"]) func _process(delta): marker.position = lerp(marker.position, marker_target, delta * 40.0) diff --git a/client/global.gd b/client/global.gd index 83517b2d..6e12d0cf 100644 --- a/client/global.gd +++ b/client/global.gd @@ -17,39 +17,58 @@ # extends Node -const DEFAULT_SETTINGS := { +const DEFAULT_PROFILE := { "username": "Giovanni", "character": 0, "last_server_url": "" } -var settings := DEFAULT_SETTINGS.duplicate(true) +var default_settings := { + "interpolate_camera_rotation": { + "value": true, + "description": tr("Interpolate the camera rotation") + }, + "test": { + "value": "hehe", + "description": tr("Just a test value") + }, +} + +var profile: Dictionary +var settings: Dictionary var server_url = "" var error_message = "" func _ready(): - load_profile() + profile = load_dict("user://profile", DEFAULT_PROFILE) + settings = load_dict("user://settings", default_settings) func save_profile(): - var f = FileAccess.open("user://profile", FileAccess.WRITE) - f.store_var(settings.duplicate(true)) + save_dict("user://profile", profile) + +func save_settings(): + save_dict("user://settings", settings) - print("Saved settings: ", settings) +func save_dict(path: String, dict: Dictionary): + var f = FileAccess.open(path, FileAccess.WRITE) + f.store_var(dict.duplicate(true)) -func load_profile(): +func load_dict(path: String, default: Dictionary) -> Dictionary: # TOCTOU here. Godot docs says its fine. - if not FileAccess.file_exists("user://profile"): + if not FileAccess.file_exists(path): print("Skip profile load") - return - var f = FileAccess.open("user://profile", FileAccess.READ) - var saved_settings = f.get_var() - if saved_settings != null and saved_settings is Dictionary: - for i in DEFAULT_SETTINGS.keys(): - if saved_settings.has(i): - settings[i] = saved_settings[i] + 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] - print("Loaded settings: ", settings) + print("Loaded dict: ", res) + return res func focus_first_button(node: Node) -> bool: if node is Button: diff --git a/client/menu/character_menu.gd b/client/menu/character_menu.gd index f0c17f18..3e535e71 100644 --- a/client/menu/character_menu.gd +++ b/client/menu/character_menu.gd @@ -19,22 +19,22 @@ extends Control @onready var num_hairstyles := character.hairstyles.keys().size() func _ready(): - $VBoxContainer/top_panel/a/username.text = Global.settings["username"] - character.select_hairstyle(Global.settings["character"]) + $VBoxContainer/top_panel/a/username.text = Global.profile["username"] + character.select_hairstyle(Global.profile["character"]) Global.focus_first_button(self) func _on_back_pressed(): $SceneTransition.transition_to("res://menu/menu_manager.tscn") func _on_username_text_changed(new_text): - Global.settings["username"] = new_text + Global.profile["username"] = new_text func _on_character_back_pressed(): - Global.settings["character"] = (Global.settings["character"] - 1) % num_hairstyles - character.select_hairstyle(Global.settings["character"]) + Global.profile["character"] = (Global.profile["character"] - 1) % num_hairstyles + character.select_hairstyle(Global.profile["character"]) Global.save_profile() func _on_character_forward_pressed(): - Global.settings["character"] = (Global.settings["character"] + 1) % num_hairstyles - character.select_hairstyle(Global.settings["character"]) + Global.profile["character"] = (Global.profile["character"] + 1) % num_hairstyles + character.select_hairstyle(Global.profile["character"]) Global.save_profile() diff --git a/client/menu/main_menu.gd b/client/menu/main_menu.gd index 5e66646b..28629e0d 100644 --- a/client/menu/main_menu.gd +++ b/client/menu/main_menu.gd @@ -24,7 +24,7 @@ extends Control func _ready(): if OS.has_feature("web"): quit_button.hide() - connect_uri.text = Global.settings["last_server_url"] + connect_uri.text = Global.profile["last_server_url"] func _on_quit_pressed(): menu_manager.transition.quit() @@ -34,7 +34,7 @@ func _on_credits_pressed(): func _on_connect_pressed(): var url = connect_uri.text - Global.settings["last_server_url"] = url + Global.profile["last_server_url"] = url Global.save_profile() connect_to(url) diff --git a/client/menu/settings_menu.gd b/client/menu/settings_menu.gd index c92a64f3..47aa90be 100644 --- a/client/menu/settings_menu.gd +++ b/client/menu/settings_menu.gd @@ -15,7 +15,22 @@ # extends Control +@onready var options: VBoxContainer = $outer_gap/panel/inner_gap/options @onready var menu_manager: MenuManager = get_parent() +var settings: Dictionary + func _on_back_pressed(): + for k in settings.keys(): + Global.settings[k]["value"] = settings[k].get_value() + Global.save_settings() menu_manager.goBack() + + +func _ready(): + 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"]) + options.add_child(row) + settings[k] = row diff --git a/client/menu/settings_row.gd b/client/menu/settings_row.gd new file mode 100644 index 00000000..ee4b8cb9 --- /dev/null +++ b/client/menu/settings_row.gd @@ -0,0 +1,36 @@ +class_name SettingsRow +extends PanelContainer + +@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 _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 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 + else: + return null + else: + return null diff --git a/client/menu/settings_row.tscn b/client/menu/settings_row.tscn new file mode 100644 index 00000000..b083d291 --- /dev/null +++ b/client/menu/settings_row.tscn @@ -0,0 +1,24 @@ +[gd_scene load_steps=2 format=3 uid="uid://o5e5vpem8w0k"] + +[ext_resource type="Script" path="res://menu/settings_row.gd" id="1_lxjnc"] + +[node name="SettingsRow" type="PanelContainer"] +offset_right = 105.0 +offset_bottom = 23.0 +size_flags_horizontal = 3 +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 + +[node name="BoxContainer" type="BoxContainer" parent="HBoxContainer"] +custom_minimum_size = Vector2(300, 50) +layout_mode = 2 +alignment = 2 + +[node name="Button" type="Button" parent="HBoxContainer"] +layout_mode = 2 |