Added SettingsManager.cs

This commit is contained in:
miku-666
2023-06-13 18:14:34 +02:00
parent 8ba073092e
commit 6024c4ad22
6 changed files with 88 additions and 14 deletions

View File

@@ -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;
}
}
}

View File

@@ -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<string, Action<object>> _onSettingChanged = new Dictionary<string, Action<object>>();
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<TSettingsType>(string propertyName, Action<TSettingsType> 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<object> callback)
{
if (_onSettingChanged.ContainsKey(propertyName))
return false;
_onSettingChanged.Add(propertyName, callback);
return true;
}
}
}

View File

@@ -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();

View File

@@ -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<bool>(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();
}

View File

@@ -215,6 +215,7 @@
<Compile Include="Classes\Misc\OpenFolderDialog.cs" />
<Compile Include="Classes\Models\DefaultModels\Steve64x64Model.cs" />
<Compile Include="Internals\ApplicationScope.cs" />
<Compile Include="Internals\SettingsManager.cs" />
<Compile Include="Internals\SkinBOX.cs" />
<Compile Include="Extensions\ImageExtensions.cs" />
<Compile Include="Internals\SkinANIM.cs" />
@@ -781,9 +782,7 @@
<Name>SharpMss32</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Classes\Utils\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View File

@@ -25,7 +25,6 @@ namespace PckStudio
ApplicationScope.Initialize();
RPC.Initialize();
MainInstance = new MainForm();
MainInstance.LoadUserSettings();
if (args.Length > 0 && File.Exists(args[0]) && args[0].EndsWith(".pck"))
MainInstance.LoadPckFromFile(args[0]);
Application.Run(MainInstance);