From 6024c4ad2287f206f92d6fac9686dd10ca28162e Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Tue, 13 Jun 2023 18:14:34 +0200 Subject: [PATCH] Added SettingsManager.cs --- PCK-Studio/Internals/ApplicationScope.cs | 5 +- PCK-Studio/Internals/SettingsManager.cs | 65 ++++++++++++++++++++++++ PCK-Studio/MainForm.Designer.cs | 4 +- PCK-Studio/MainForm.cs | 22 +++++--- PCK-Studio/PckStudio.csproj | 5 +- PCK-Studio/Program.cs | 1 - 6 files changed, 88 insertions(+), 14 deletions(-) create mode 100644 PCK-Studio/Internals/SettingsManager.cs diff --git a/PCK-Studio/Internals/ApplicationScope.cs b/PCK-Studio/Internals/ApplicationScope.cs index 6030f7f1..51af5313 100644 --- a/PCK-Studio/Internals/ApplicationScope.cs +++ b/PCK-Studio/Internals/ApplicationScope.cs @@ -10,6 +10,8 @@ using PckStudio.Forms.Utilities; using PckStudio.Properties; using PckStudio.Extensions; using System.Globalization; +using System.ComponentModel; +using PckStudio.Internals; namespace PckStudio { @@ -29,10 +31,11 @@ namespace PckStudio _ = AnimationResources.JsonTileData; _ = AnimationResources.ItemList; _ = AnimationResources.BlockList; + SettingsManager.Initialize(); + CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; } stopwatch.Stop(); Debug.WriteLine($"{nameof(ApplicationScope.Initialize)} took {stopwatch.ElapsedMilliseconds}ms"); - CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; } } } \ No newline at end of file diff --git a/PCK-Studio/Internals/SettingsManager.cs b/PCK-Studio/Internals/SettingsManager.cs new file mode 100644 index 00000000..f5a9cfb7 --- /dev/null +++ b/PCK-Studio/Internals/SettingsManager.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Configuration; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PckStudio.Properties; + +namespace PckStudio.Internals +{ + internal static class SettingsManager + { + private static Dictionary> _onSettingChanged = new Dictionary>(); + + private static object _newValue = null; + + private static void PropertyChangedHandler(object sender, PropertyChangedEventArgs e) + { + if (_onSettingChanged.ContainsKey(e.PropertyName)) + { + _onSettingChanged[e.PropertyName]?.Invoke(_newValue); + } + } + + private static void SettingChangingHandler(object sender, SettingChangingEventArgs e) + { + if (_onSettingChanged.ContainsKey(e.SettingName)) + { + _newValue = e.NewValue; + } + } + + internal static void Initialize() + { + Settings.Default.PropertyChanged += PropertyChangedHandler; + Settings.Default.SettingChanging += SettingChangingHandler; + } + + internal static bool RegisterPropertyChangedCallback(string propertyName, Action callback) + { + Type propertyType = Settings.Default[propertyName].GetType(); + if (!propertyType.Equals(typeof(TSettingsType))) + { + return false; + } + return RegisterPropertyChangedCallback(propertyName, delegate (object obj) { callback((TSettingsType)obj); }); + } + + internal static bool RegisterPropertyChangedCallback(string propertyName, Action callback) + { + return RegisterPropertyChangedCallback(propertyName, delegate (object _) { callback(); }); + } + + private static bool RegisterPropertyChangedCallback(string propertyName, Action callback) + { + if (_onSettingChanged.ContainsKey(propertyName)) + return false; + _onSettingChanged.Add(propertyName, callback); + return true; + } + + } +} diff --git a/PCK-Studio/MainForm.Designer.cs b/PCK-Studio/MainForm.Designer.cs index 7334b025..42a20ff1 100644 --- a/PCK-Studio/MainForm.Designer.cs +++ b/PCK-Studio/MainForm.Designer.cs @@ -1115,8 +1115,8 @@ this.ShadowType = MetroFramework.Forms.MetroFormShadowType.DropShadow; this.Style = MetroFramework.MetroColorStyle.Black; this.Theme = MetroFramework.MetroThemeStyle.Dark; - this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FormMain_FormClosing); - this.Load += new System.EventHandler(this.Form1_Load); + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing); + this.Load += new System.EventHandler(this.MainForm_Load); this.contextMenuPCKEntries.ResumeLayout(false); this.menuStrip.ResumeLayout(false); this.menuStrip.PerformLayout(); diff --git a/PCK-Studio/MainForm.cs b/PCK-Studio/MainForm.cs index 8f1d34a8..1bb966a9 100644 --- a/PCK-Studio/MainForm.cs +++ b/PCK-Studio/MainForm.cs @@ -33,6 +33,7 @@ using PckStudio.Extensions; using PckStudio.Popups; using PckStudio.API.Miles; using PckStudio.Classes.Utils; +using PckStudio.Internals; namespace PckStudio { @@ -122,8 +123,20 @@ namespace PckStudio LoadEditorTab(); } - private void Form1_Load(object sender, EventArgs e) + private void MainForm_Load(object sender, EventArgs e) { + SettingsManager.RegisterPropertyChangedCallback(nameof(Settings.Default.UseLittleEndianAsDefault), state => + { + LittleEndianCheckBox.Checked = state; + }); + SettingsManager.RegisterPropertyChangedCallback(nameof(Settings.Default.LoadSubPcks), () => + { + if (currentPCK is not null) + { + BuildMainTreeView(); + } + }); + UpdateRPC(); skinToolStripMenuItem1.Click += (sender, e) => setFileType_Click(sender, e, PckFile.FileData.FileType.SkinFile); @@ -140,12 +153,7 @@ namespace PckStudio entityMaterialsFileBINToolStripMenuItem.Click += (sender, e) => setFileType_Click(sender, e, PckFile.FileData.FileType.MaterialFile); } - public void LoadUserSettings() - { - LittleEndianCheckBox.Checked = Settings.Default.UseLittleEndianAsDefault; - } - - private void FormMain_FormClosing(object sender, FormClosingEventArgs e) + private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { checkSaveState(); } diff --git a/PCK-Studio/PckStudio.csproj b/PCK-Studio/PckStudio.csproj index d7aee94d..9480438f 100644 --- a/PCK-Studio/PckStudio.csproj +++ b/PCK-Studio/PckStudio.csproj @@ -215,6 +215,7 @@ + @@ -781,9 +782,7 @@ SharpMss32 - - - +