diff options
| author | Jacob Palecki <[email protected]> | 2020-08-20 15:46:04 -0700 |
|---|---|---|
| committer | Jacob Palecki <[email protected]> | 2020-08-20 15:46:04 -0700 |
| commit | 55b739c50db217e6a61678c1eb1412e8884e3462 (patch) | |
| tree | a1924dce2e90d09dd3075c0ec1aca2283efc41d2 /grapher/Models/Serialized | |
| parent | Add accel type to active values and tweak color (diff) | |
| download | rawaccel-55b739c50db217e6a61678c1eb1412e8884e3462.tar.xz rawaccel-55b739c50db217e6a61678c1eb1412e8884e3462.zip | |
Serialization mostly working
Diffstat (limited to 'grapher/Models/Serialized')
| -rw-r--r-- | grapher/Models/Serialized/GUISettings.cs | 51 | ||||
| -rw-r--r-- | grapher/Models/Serialized/RawAccelSettings.cs | 81 |
2 files changed, 132 insertions, 0 deletions
diff --git a/grapher/Models/Serialized/GUISettings.cs b/grapher/Models/Serialized/GUISettings.cs new file mode 100644 index 0000000..8c182ae --- /dev/null +++ b/grapher/Models/Serialized/GUISettings.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher.Models.Serialized +{ + [Serializable] + public class GUISettings + { + public GUISettings( + Field dpiField, + Field pollRateField, + ToolStripMenuItem autoWriteMenuItem) + { + BindToGUI(dpiField, pollRateField, autoWriteMenuItem); + } + + public bool AutoWriteToDriverOnStartup { get; set; } + + public int DPI { get; set; } + + public int PollRate { get; set; } + + [field: NonSerialized] + private Field DpiField { get; set; } + + [field: NonSerialized] + private Field PollRateField { get; set; } + + [field: NonSerialized] + private ToolStripMenuItem AutoWriteMenuItem { get; set; } + + public void UpdateSettings() + { + DPI = (int)DpiField.Data; + PollRate = (int)PollRateField.Data; + AutoWriteToDriverOnStartup = AutoWriteMenuItem.Checked; + } + + public void BindToGUI(Field dpiField, Field pollRateField, ToolStripMenuItem autoWriteMenuItem) + { + DpiField = dpiField; + PollRateField = pollRateField; + AutoWriteMenuItem = autoWriteMenuItem; + } + } +} diff --git a/grapher/Models/Serialized/RawAccelSettings.cs b/grapher/Models/Serialized/RawAccelSettings.cs new file mode 100644 index 0000000..d8896b6 --- /dev/null +++ b/grapher/Models/Serialized/RawAccelSettings.cs @@ -0,0 +1,81 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher.Models.Serialized +{ + [Serializable] + public class RawAccelSettings + { + public const string DefaultSettingsFile = @".\settings.json"; + + public RawAccelSettings( + ManagedAccel managedAccel, + GUISettings guiSettings) + { + ManagedAccel = managedAccel; + GUISettings = guiSettings; + } + + public ManagedAccel ManagedAccel { get; set; } + + public GUISettings GUISettings { get; set; } + + public static RawAccelSettings Load() + { + return Load(DefaultSettingsFile); + } + + public static RawAccelSettings Load(string file) + { + if (!Exists(file)) + { + throw new Exception($"Settings file does not exist at {file}"); + } + + object deserializedObject; + try + { + deserializedObject = JsonConvert.DeserializeObject(File.ReadAllText(file)); + } + catch + { + throw new Exception($"Settings file at {file} does not contain valid JSON."); + } + + RawAccelSettings deserializedSettings = (RawAccelSettings)deserializedObject; + + if (deserializedSettings == null) + { + throw new Exception($"Settings file at {file} does not contain valid Raw Accel Settings."); + } + + return deserializedSettings; + } + + public static bool Exists() + { + return Exists(DefaultSettingsFile); + } + + public static bool Exists(string file) + { + return File.Exists(file); + } + + public void Save() + { + Save(DefaultSettingsFile); + } + + public void Save(string file) + { + File.WriteAllText(file, JsonConvert.SerializeObject(this, Formatting.Indented)); + } + } +} |