持久化设置

master
lightyears 2 years ago
parent 0bb16942bb
commit 6c6a4dce01
Signed by: lightyears
GPG Key ID: 98D80DDF26D4F2F9

@ -1,6 +1,63 @@
using Newtonsoft.Json;
namespace CMSGame namespace CMSGame
{ {
public partial class GameSettings : Node public partial class GameSettings : Node
{
public BattleSettings BattleSettings;
protected string BattleSettingsSavePath = new GodotPath("user://Settings/BattleSettings.json");
public override void _Ready()
{
MakeDirectories();
BattleSettings = GetSettings<BattleSettings>(BattleSettingsSavePath);
}
public override void _ExitTree()
{
SaveSettings(BattleSettingsSavePath);
}
private void MakeDirectories()
{
DirAccess.MakeDirRecursiveAbsolute("user://Settings/");
}
private SettingsType GetSettings<SettingsType>(string path) where SettingsType : new()
{
string settings_text = ReadFileAsString(path);
var settings = JsonConvert.DeserializeObject<SettingsType>(settings_text) ?? new SettingsType();
return settings;
}
private string ReadFileAsString(string path)
{
if (FileAccess.FileExists(path))
{
return FileAccess.Open(path, FileAccess.ModeFlags.Read).GetAsText();
}
return "null";
}
private void SaveSettings(string path)
{
string settings_text = JsonConvert.SerializeObject(BattleSettings);
var file = FileAccess.Open(path, FileAccess.ModeFlags.Write);
file.StoreString(settings_text);
file.Dispose();
}
}
public class SettingsBase
{
public override string ToString()
{
return JsonConvert.SerializeObject(this);
}
}
public class BattleSettings : SettingsBase
{ {
public bool PauseBattleWhenCharacterIsSelected = true; public bool PauseBattleWhenCharacterIsSelected = true;
} }

@ -1,19 +1,10 @@
namespace CMSGame namespace CMSGame
{ {
public partial class PauseMenu : Control public partial class PauseMenu : Popup
{ {
private GameSettings _settings; public void On_ButtonExit_Pressed()
public override void _Ready()
{ {
_settings = GetNode<GameSettings>("/root/GameSettings"); Hide();
GetNode<CheckBox>("%CheckBoxPauseBattleWhenCharacterIsSelected").ToggleMode = _settings.PauseBattleWhenCharacterIsSelected;
}
public void On_CheckBoxPauseBattleWhenCharacterIsSelected_Toggled()
{
_settings.PauseBattleWhenCharacterIsSelected = false;
} }
} }
} }

@ -1,37 +1,33 @@
[gd_scene load_steps=2 format=3 uid="uid://c1c87rr8eubhg"] [gd_scene load_steps=3 format=3 uid="uid://c1c87rr8eubhg"]
[ext_resource type="Script" path="res://Components/PauseMenu.cs" id="1_p1e8d"] [ext_resource type="Script" path="res://Components/PauseMenu.cs" id="1_p1e8d"]
[ext_resource type="PackedScene" uid="uid://blk2uswpo2a7k" path="res://Components/SettingsMenu.tscn" id="2_smral"]
[node name="PauseMenu" type="Control"] [node name="PauseMenu" type="Popup"]
layout_mode = 3 size = Vector2i(342, 289)
anchors_preset = 15 visible = true
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_p1e8d") script = ExtResource("1_p1e8d")
[node name="VBoxContainer" type="VBoxContainer" parent="."] [node name="VBoxContainer" type="VBoxContainer" parent="."]
layout_mode = 1
anchors_preset = 8 anchors_preset = 8
anchor_left = 0.5 anchor_left = 0.5
anchor_top = 0.5 anchor_top = 0.5
anchor_right = 0.5 anchor_right = 0.5
anchor_bottom = 0.5 anchor_bottom = 0.5
offset_left = -20.0 offset_left = -86.0
offset_top = -20.0 offset_top = -33.0
offset_right = 20.0 offset_right = 86.0
offset_bottom = 20.0 offset_bottom = 33.0
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
size_flags_horizontal = 4
size_flags_vertical = 4
[node name="CheckBoxPauseBattleWhenCharacterIsSelected" type="CheckBox" parent="VBoxContainer"] [node name="VBoxContainer" parent="VBoxContainer" instance=ExtResource("2_smral")]
unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
text = "选中角色时暂停战斗"
[node name="ButtonExit" type="Button" parent="VBoxContainer"] [node name="ButtonExit" type="Button" parent="VBoxContainer"]
layout_mode = 2 layout_mode = 2
text = "退出" text = "确认"
[connection signal="toggled" from="VBoxContainer/CheckBoxPauseBattleWhenCharacterIsSelected" to="." method="_On_CheckBoxPauseBattleWhenCharacterIsSelected_Toggled"] [connection signal="pressed" from="VBoxContainer/ButtonExit" to="." method="On_ButtonExit_Pressed"]

@ -0,0 +1,27 @@
namespace CMSGame
{
public partial class SettingsMenu : VBoxContainer
{
private GameSettings _settings;
private CheckBox _checkBoxPauseBattleWhenCharacterIsSelected;
public override void _Ready()
{
InitializeComponents();
_checkBoxPauseBattleWhenCharacterIsSelected.ButtonPressed = _settings.BattleSettings.PauseBattleWhenCharacterIsSelected;
}
private void InitializeComponents()
{
_settings = GetNode<GameSettings>("/root/GameSettings");
_checkBoxPauseBattleWhenCharacterIsSelected = GetNode<CheckBox>("%CheckBoxPauseBattleWhenCharacterIsSelected");
}
public void On_CheckBoxPauseBattleWhenCharacterIsSelected_Toggled(bool pressed)
{
_settings.BattleSettings.PauseBattleWhenCharacterIsSelected = pressed;
}
}
}

@ -0,0 +1,13 @@
[gd_scene load_steps=2 format=3 uid="uid://blk2uswpo2a7k"]
[ext_resource type="Script" path="res://Components/SettingsMenu.cs" id="1_b12ly"]
[node name="VBoxContainer" type="VBoxContainer"]
script = ExtResource("1_b12ly")
[node name="CheckBoxPauseBattleWhenCharacterIsSelected" type="CheckBox" parent="."]
unique_name_in_owner = true
layout_mode = 2
text = "选中角色时暂停战斗"
[connection signal="toggled" from="CheckBoxPauseBattleWhenCharacterIsSelected" to="." method="On_CheckBoxPauseBattleWhenCharacterIsSelected_Toggled"]

@ -0,0 +1,10 @@
namespace CMSGame
{
public static class FileHelper
{
public static string GodotPathToSystemPath(string godotPath)
{
return ProjectSettings.GlobalizePath(godotPath);
}
}
}

@ -0,0 +1,18 @@
using System.Runtime.CompilerServices;
namespace CMSGame
{
public class GodotPath
{
private readonly string _path = string.Empty;
public GodotPath(string godotPath)
{
_path = ProjectSettings.GlobalizePath(godotPath);
}
public override string ToString() => _path;
public static implicit operator string(GodotPath g) => g.ToString();
}
}

@ -2,19 +2,19 @@ namespace CMSGame
{ {
public partial class MainScene : Control public partial class MainScene : Control
{ {
public static void On_ButtonBattleDemo_Pressed() public void On_ButtonBattleDemo_Pressed()
{ {
GetTree().ChangeSceneToFile("res://Scenes/BattleScene.tscn");
} }
public static void On_ButtonSettings_Pressed() public void On_ButtonSettings_Pressed()
{ {
GetNode<Popup>("%PauseMenu").PopupCentered();
} }
public static void On_ButtonExit_Pressed() public void On_ButtonExit_Pressed()
{ {
GetTree().Quit();
} }
} }
} }

@ -1,6 +1,7 @@
[gd_scene load_steps=2 format=3 uid="uid://dxnmltew81ny0"] [gd_scene load_steps=3 format=3 uid="uid://dxnmltew81ny0"]
[ext_resource type="Script" path="res://Scenes/MainScene.cs" id="1_kso8c"] [ext_resource type="Script" path="res://Scenes/MainScene.cs" id="1_kso8c"]
[ext_resource type="PackedScene" uid="uid://c1c87rr8eubhg" path="res://Components/PauseMenu.tscn" id="2_txg46"]
[node name="MainScene" type="Control"] [node name="MainScene" type="Control"]
layout_mode = 3 layout_mode = 3
@ -11,10 +12,15 @@ grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
script = ExtResource("1_kso8c") script = ExtResource("1_kso8c")
[node name="Label" type="Label" parent="."] [node name="LabelGameTitle" type="Label" parent="."]
layout_mode = 0 layout_mode = 1
offset_right = 40.0 anchors_preset = 5
offset_bottom = 23.0 anchor_left = 0.5
anchor_right = 0.5
offset_left = -137.5
offset_right = 137.5
offset_bottom = 26.0
grow_horizontal = 2
text = "The Country of Mountains and Seas" text = "The Country of Mountains and Seas"
[node name="VBoxContainer" type="VBoxContainer" parent="."] [node name="VBoxContainer" type="VBoxContainer" parent="."]
@ -43,4 +49,10 @@ text = "游戏设置"
layout_mode = 2 layout_mode = 2
text = "退出" text = "退出"
[node name="PauseMenu" parent="." instance=ExtResource("2_txg46")]
unique_name_in_owner = true
visible = false
[connection signal="pressed" from="VBoxContainer/ButtonBattleDemo" to="." method="On_ButtonBattleDemo_Pressed"]
[connection signal="pressed" from="VBoxContainer/ButtonSettings" to="." method="On_ButtonSettings_Pressed"] [connection signal="pressed" from="VBoxContainer/ButtonSettings" to="." method="On_ButtonSettings_Pressed"]
[connection signal="pressed" from="VBoxContainer/ButtonExit" to="." method="On_ButtonExit_Pressed"]

Loading…
Cancel
Save