From 74520c3e040ca31a553916230c8413ee4b214ea3 Mon Sep 17 00:00:00 2001
From: msk <15199219+muskit@users.noreply.github.com>
Date: Mon, 18 Sep 2023 00:28:54 -0700
Subject: [PATCH] Add savable Config class
---
Scripts/Configuration/Config.cs | 77 +++++++++++++++++++++++++++
Scripts/Configuration/GameSettings.cs | 18 +++++++
Scripts/Configuration/PlaySettings.cs | 6 ++-
Scripts/Scenes/Play.cs | 2 +-
4 files changed, 100 insertions(+), 3 deletions(-)
create mode 100644 Scripts/Configuration/Config.cs
create mode 100644 Scripts/Configuration/GameSettings.cs
diff --git a/Scripts/Configuration/Config.cs b/Scripts/Configuration/Config.cs
new file mode 100644
index 0000000..e1c5eb1
--- /dev/null
+++ b/Scripts/Configuration/Config.cs
@@ -0,0 +1,77 @@
+using System.Windows.Markup;
+using System.Xml.Linq;
+using Godot;
+
+namespace WacK.Configuration
+{
+ ///
+ /// A configurable variable that saves to user://settings.cfg.
+ ///
+ public class Config<[MustBeVariant] T>
+ {
+ // --- STATICALLY-TRACKED CONFIG FILE ---
+ private static ConfigFile cfgFile = new();
+ public static readonly string cfgPath = "user://settings.cfg";
+
+ // Metadata
+ string section, name;
+
+ // Callback
+ public delegate void OnValueSet();
+ private OnValueSet callback;
+
+ // Stored values
+ public T _default { get; private set; }
+ private T _value;
+ public T Value
+ {
+ get => _value;
+ set
+ {
+ _value = value;
+ Save();
+ if (callback != null) callback();
+ }
+ }
+
+ static Config()
+ {
+ var err = cfgFile.Load("user://settings.cfg");
+ if (err != Error.Ok)
+ {
+ GD.Print("Creating new settings file...");
+ cfgFile.Save("user://settings.cfg");
+ }
+ }
+
+ ///
+ ///
+ ///
+ /// CANNOT have spaces.
+ /// CANNOT contain spaces.
+ /// The value to set if not found in the settings file.
+ public Config(string section, string name, T defaultValue, OnValueSet callback = null)
+ {
+ this.section = section;
+ this.name = name;
+ _default = defaultValue;
+ this.callback = callback;
+ Fetch();
+ }
+
+ private void Fetch()
+ {
+ if (!cfgFile.HasSectionKey(section, name))
+ {
+ cfgFile.SetValue(section, name, Variant.From(_default));
+ Save();
+ }
+ Value = cfgFile.GetValue(section, name).As();
+ }
+
+ private void Save()
+ {
+ cfgFile.Save(cfgPath);
+ }
+ }
+}
\ No newline at end of file
diff --git a/Scripts/Configuration/GameSettings.cs b/Scripts/Configuration/GameSettings.cs
new file mode 100644
index 0000000..2698b9a
--- /dev/null
+++ b/Scripts/Configuration/GameSettings.cs
@@ -0,0 +1,18 @@
+namespace WacK.Configuration
+{
+ ///
+ /// Configuration that affects the general application.
+ ///
+ public class GameSettings
+ {
+ /// --- GRAPHICS --- ///
+
+ ///
+ /// Value to multiply the canvas resolution by.
+ ///
+ public static Config canvasResolutionMult =
+ new("Graphics", "canvasResolutionMultiplier", 1f);
+ public static Config vsync =
+ new("Graphics", "vsync", true);
+ }
+}
\ No newline at end of file
diff --git a/Scripts/Configuration/PlaySettings.cs b/Scripts/Configuration/PlaySettings.cs
index af79000..bfaf193 100644
--- a/Scripts/Configuration/PlaySettings.cs
+++ b/Scripts/Configuration/PlaySettings.cs
@@ -9,11 +9,13 @@ namespace WacK.Configuration
///
/// Scroll speed multiplier.
///
- public static float playSpeedMultiplier = 2f;
+ public static Config playSpeedMultiplier =
+ new("PlaySettings", "playSpeedMultiplier", 2f);
///
/// How much to shift song audio by in seconds.
///
- public static float audioOffset = 0f;
+ public static Config audioOffset =
+ new("PlaySettings", "audioOffset", 0f);
}
}
\ No newline at end of file
diff --git a/Scripts/Scenes/Play.cs b/Scripts/Scenes/Play.cs
index 7c42445..b698a06 100644
--- a/Scripts/Scenes/Play.cs
+++ b/Scripts/Scenes/Play.cs
@@ -55,7 +55,7 @@ namespace WacK.Scenes
{
get
{
- return BASE_PIXELS_PER_SECOND * PlaySettings.playSpeedMultiplier;
+ return BASE_PIXELS_PER_SECOND * PlaySettings.playSpeedMultiplier.Value;
}
}