清理代码

master
lightyears 2 years ago
parent f8294dff94
commit f8f281f2f7
Signed by: lightyears
GPG Key ID: 98D80DDF26D4F2F9

@ -1,7 +1,6 @@
using CMSGame.Models;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace CMSGame.Autoloads namespace CMSGame
{ {
/// <summary> /// <summary>
/// 游戏设置持久化节点 /// 游戏设置持久化节点
@ -27,6 +26,7 @@ namespace CMSGame.Autoloads
RegisterAllSettings(); RegisterAllSettings();
MakeDirectories(); MakeDirectories();
LoadAllSettings(); LoadAllSettings();
ApplyVideoSettings();
} }
protected void RegisterAllSettings() protected void RegisterAllSettings()
@ -62,12 +62,12 @@ namespace CMSGame.Autoloads
} }
} }
private static void MakeDirectories() protected static void MakeDirectories()
{ {
DirAccess.MakeDirRecursiveAbsolute("user://Settings/"); DirAccess.MakeDirRecursiveAbsolute("user://Settings/");
} }
private void LoadSettings(Type settingsType) protected void LoadSettings(Type settingsType)
{ {
string settingsText = ReadFileAsString(SettingsPaths[settingsType]); string settingsText = ReadFileAsString(SettingsPaths[settingsType]);
var settings = JsonConvert.DeserializeObject(settingsText, settingsType); var settings = JsonConvert.DeserializeObject(settingsText, settingsType);
@ -83,7 +83,7 @@ namespace CMSGame.Autoloads
return (TSettings)CurrentSettings[typeof(TSettings)]; return (TSettings)CurrentSettings[typeof(TSettings)];
} }
private static string ReadFileAsString(string path) protected static string ReadFileAsString(string path)
{ {
if (FileAccess.FileExists(path)) if (FileAccess.FileExists(path))
{ {
@ -93,11 +93,16 @@ namespace CMSGame.Autoloads
return "null"; return "null";
} }
private void SaveSettings(Type settingsType) protected void SaveSettings(Type settingsType)
{ {
string settingsText = JsonConvert.SerializeObject(CurrentSettings[settingsType]); string settingsText = JsonConvert.SerializeObject(CurrentSettings[settingsType]);
using var file = FileAccess.Open(SettingsPaths[settingsType], FileAccess.ModeFlags.Write); using var file = FileAccess.Open(SettingsPaths[settingsType], FileAccess.ModeFlags.Write);
file.StoreString(settingsText); file.StoreString(settingsText);
} }
protected void ApplyVideoSettings()
{
DisplayServerHelper.ApplyResolutionSettings(VideoSettings.UseFullScreen);
}
} }
} }

@ -6,7 +6,7 @@ namespace CMSGame
{ {
} }
public void On_OpenUserDataDirButton_Pressed() public static void On_OpenUserDataDirButton_Pressed()
{ {
OS.ShellOpen(new GodotPath("user://")); OS.ShellOpen(new GodotPath("user://"));
} }

@ -1,5 +1,3 @@
using CMSGame.Autoloads;
namespace CMSGame namespace CMSGame
{ {
public partial class SettingsMenu : TabContainer public partial class SettingsMenu : TabContainer

@ -1,5 +1,3 @@
using CMSGame.Models;
namespace CMSGame namespace CMSGame
{ {
public partial class VideoSettingsMenu : Control public partial class VideoSettingsMenu : Control
@ -16,20 +14,13 @@ namespace CMSGame
FullScreenCheckButton!.Toggled += FullScreenCheckButton_Toggled; FullScreenCheckButton!.Toggled += FullScreenCheckButton_Toggled;
FullScreenCheckButton.ButtonPressed = Settings.UseFullScreen; FullScreenCheckButton.SetPressedNoSignal(Settings.UseFullScreen);
} }
private void FullScreenCheckButton_Toggled(bool buttonPressed) private void FullScreenCheckButton_Toggled(bool buttonPressed)
{ {
Settings!.UseFullScreen = buttonPressed; Settings!.UseFullScreen = buttonPressed;
if (buttonPressed) DisplayServerHelper.ApplyResolutionSettings(buttonPressed);
{
DisplayServer.WindowSetMode(DisplayServer.WindowMode.Fullscreen);
}
else
{
DisplayServer.WindowSetMode(DisplayServer.WindowMode.Windowed);
}
} }
} }
} }

@ -1,5 +1,4 @@
global using System; global using System;
global using System.Collections;
global using System.Collections.Generic; global using System.Collections.Generic;
global using System.Linq; global using System.Linq;
global using Godot; global using Godot;

@ -1,8 +1,8 @@
namespace CMSGame.Helpers namespace CMSGame
{ {
internal static class DisplayServerHelper internal static class DisplayServerHelper
{ {
public static void ApplyAndPersistResolutionSettings(bool useFullScreen) public static void ApplyResolutionSettings(bool useFullScreen)
{ {
if (useFullScreen) if (useFullScreen)
{ {

@ -1,6 +1,6 @@
using System.Text; using System.Text;
namespace CMSGame.Models namespace CMSGame
{ {
internal record class Changelog internal record class Changelog
{ {

@ -1,4 +1,4 @@
namespace CMSGame.Models namespace CMSGame
{ {
public record class GameSettings public record class GameSettings
{ {

@ -1,4 +1,3 @@
using CMSGame.Models;
using System.Text; using System.Text;
namespace CMSGame namespace CMSGame

Loading…
Cancel
Save