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 | |
| 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')
| -rw-r--r-- | grapher/Models/AccelGUI.cs | 27 | ||||
| -rw-r--r-- | grapher/Models/Serialized/GUISettings.cs | 51 | ||||
| -rw-r--r-- | grapher/Models/Serialized/RawAccelSettings.cs | 81 |
3 files changed, 158 insertions, 1 deletions
diff --git a/grapher/Models/AccelGUI.cs b/grapher/Models/AccelGUI.cs index e345743..a0a76c8 100644 --- a/grapher/Models/AccelGUI.cs +++ b/grapher/Models/AccelGUI.cs @@ -1,5 +1,6 @@ using grapher.Models.Calculations; using grapher.Models.Mouse; +using grapher.Models.Serialized; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -32,7 +33,8 @@ namespace grapher Option midpoint, Button writeButton, Label mouseMoveLabel, - ToolStripMenuItem scaleMenuItem) + ToolStripMenuItem scaleMenuItem, + ToolStripMenuItem autoWriteMenuItem) { AccelForm = accelForm; AccelCalculator = accelCalculator; @@ -51,6 +53,7 @@ namespace grapher ScaleMenuItem = scaleMenuItem; ManagedAcceleration.ReadFromDriver(); + SavedSettings = StartupLoad(AccelCalculator.DPI, AccelCalculator.PollRate, autoWriteMenuItem); UpdateGraph(); MouseWatcher = new MouseWatcher(AccelForm, mouseMoveLabel, AccelCharts); @@ -64,6 +67,8 @@ namespace grapher public RawAcceleration AccelForm { get; } + public RawAccelSettings SavedSettings { get; } + public AccelCalculator AccelCalculator { get; } public AccelCharts AccelCharts { get; } @@ -103,6 +108,7 @@ namespace grapher AccelCalculator.Calculate(AccelCharts.AccelData, ManagedAcceleration); AccelCharts.Bind(); UpdateActiveValueLabels(); + SavedSettings.Save(); } public void UpdateActiveValueLabels() @@ -118,6 +124,25 @@ namespace grapher Midpoint.SetActiveValue(ManagedAcceleration.Midpoint); } + private RawAccelSettings StartupLoad(Field dpiField, Field pollRateField, ToolStripMenuItem autoWriteMenuItem) + { + if (RawAccelSettings.Exists()) + { + var settings = RawAccelSettings.Load(); + settings.GUISettings.BindToGUI(dpiField, pollRateField, autoWriteMenuItem); + return settings; + } + else + { + return new RawAccelSettings( + ManagedAcceleration, + new GUISettings( + AccelCalculator.DPI, + AccelCalculator.PollRate, + autoWriteMenuItem)); + } + } + private void OnScaleMenuItemClick(object sender, EventArgs e) { UpdateGraph(); 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)); + } + } +} |