diff options
Diffstat (limited to 'grapher/Models/Serialized')
| -rw-r--r-- | grapher/Models/Serialized/GUISettings.cs | 47 | ||||
| -rw-r--r-- | grapher/Models/Serialized/RawAccelSettings.cs | 41 | ||||
| -rw-r--r-- | grapher/Models/Serialized/SettingsManager.cs | 52 |
3 files changed, 94 insertions, 46 deletions
diff --git a/grapher/Models/Serialized/GUISettings.cs b/grapher/Models/Serialized/GUISettings.cs index 84e681b..c8f87ae 100644 --- a/grapher/Models/Serialized/GUISettings.cs +++ b/grapher/Models/Serialized/GUISettings.cs @@ -10,32 +10,55 @@ namespace grapher.Models.Serialized public GUISettings() {} - public GUISettings(bool autoWrite, int dpi, int pollRate) - { - AutoWriteToDriverOnStartup = autoWrite; - DPI = dpi; - PollRate = pollRate; - } - #endregion Constructors #region Properties - [JsonProperty(Order = 1)] - public bool AutoWriteToDriverOnStartup { get; set; } - [JsonProperty(Order = 2)] + [JsonProperty(Order = 1)] public int DPI { get; set; } - [JsonProperty(Order = 3)] + [JsonProperty(Order = 2)] public int PollRate { get; set; } - [JsonProperty(Order = 4)] + [JsonProperty(Order = 3)] public bool ShowLastMouseMove { get; set; } [JsonProperty(Order = 4)] public bool ShowVelocityAndGain { get; set; } #endregion Properties + + #region Methods + + public override bool Equals(object obj) + { + var other = obj as GUISettings; + + if (other == null) + { + return false; + } + + return Equals(other); + } + + public bool Equals(GUISettings other) + { + return DPI == other.DPI && + PollRate == other.PollRate && + ShowLastMouseMove == other.ShowLastMouseMove && + ShowVelocityAndGain == other.ShowVelocityAndGain; + } + + public override int GetHashCode() + { + return DPI.GetHashCode() ^ + PollRate.GetHashCode() ^ + ShowLastMouseMove.GetHashCode() ^ + ShowVelocityAndGain.GetHashCode(); + } + + #endregion Methods } } diff --git a/grapher/Models/Serialized/RawAccelSettings.cs b/grapher/Models/Serialized/RawAccelSettings.cs index 570a6c8..818bfb6 100644 --- a/grapher/Models/Serialized/RawAccelSettings.cs +++ b/grapher/Models/Serialized/RawAccelSettings.cs @@ -44,17 +44,36 @@ namespace grapher.Models.Serialized #region Methods - public static RawAccelSettings Load() + public static RawAccelSettings Load(Func<GUISettings> DefaultGUISettingsSupplier) { - return Load(DefaultSettingsFile); + return Load(DefaultSettingsFile, DefaultGUISettingsSupplier); } - public static RawAccelSettings Load(string file) + public static RawAccelSettings Load(string file, Func<GUISettings> DefaultGUISettingsSupplier) { try { - var settings = JsonConvert.DeserializeObject<RawAccelSettings>(File.ReadAllText(file), SerializerSettings); - if (settings is null) throw new JsonException($"{file} contains invalid JSON"); + RawAccelSettings settings = null; + + JObject settingsJObject = JObject.Parse(File.ReadAllText(file)); + if (settingsJObject.ContainsKey(DriverSettings.Key)) + { + settings = settingsJObject.ToObject<RawAccelSettings>(JsonSerializer.Create(SerializerSettings)); + } + else + { + settings = new RawAccelSettings + { + AccelerationSettings = settingsJObject.ToObject<DriverSettings>(), + GUISettings = DefaultGUISettingsSupplier() + }; + } + + if (settings is null || settings.AccelerationSettings is null) + { + throw new JsonException($"{file} contains invalid JSON"); + } + return settings; } catch (FileNotFoundException e) @@ -96,6 +115,18 @@ namespace grapher.Models.Serialized .AddFirst(new JProperty("### Mode Types ###", modes)); } + public bool IsDefaultEquivalent() + { + bool wholeOrNoY = AccelerationSettings.combineMagnitudes || + AccelerationSettings.modes.y == AccelMode.noaccel; + + return AccelerationSettings.sensitivity.x == 1 && + AccelerationSettings.sensitivity.y == 1 && + AccelerationSettings.rotation == 0 && + AccelerationSettings.modes.x == AccelMode.noaccel && + wholeOrNoY; + } + #endregion Methods } } diff --git a/grapher/Models/Serialized/SettingsManager.cs b/grapher/Models/Serialized/SettingsManager.cs index 416823e..8712c87 100644 --- a/grapher/Models/Serialized/SettingsManager.cs +++ b/grapher/Models/Serialized/SettingsManager.cs @@ -14,14 +14,12 @@ namespace grapher.Models.Serialized ManagedAccel activeAccel, Field dpiField, Field pollRateField, - ToolStripMenuItem autoWrite, ToolStripMenuItem showLastMouseMove, ToolStripMenuItem showVelocityAndGain) { ActiveAccel = activeAccel; DpiField = dpiField; PollRateField = pollRateField; - AutoWriteMenuItem = autoWrite; ShowLastMouseMoveMenuItem = showLastMouseMove; ShowVelocityAndGainMoveMenuItem = showVelocityAndGain; } @@ -38,8 +36,6 @@ namespace grapher.Models.Serialized private Field PollRateField { get; set; } - private ToolStripMenuItem AutoWriteMenuItem { get; set; } - private ToolStripMenuItem ShowLastMouseMoveMenuItem { get; set; } private ToolStripMenuItem ShowVelocityAndGainMoveMenuItem { get; set; } @@ -83,32 +79,27 @@ namespace grapher.Models.Serialized if (errors.Empty()) { RawAccelSettings.AccelerationSettings = settings; - RawAccelSettings.GUISettings = new GUISettings - { - AutoWriteToDriverOnStartup = AutoWriteMenuItem.Checked, - DPI = (int)DpiField.Data, - PollRate = (int)PollRateField.Data, - ShowLastMouseMove = ShowLastMouseMoveMenuItem.Checked, - ShowVelocityAndGain = ShowVelocityAndGainMoveMenuItem.Checked, - }; - + RawAccelSettings.GUISettings = MakeGUISettingsFromFields(); RawAccelSettings.Save(); } return errors; } - public void UpdateActiveAccelFromFileSettings(DriverSettings settings) + public void UpdateFieldsFromGUISettings() { - TryUpdateAccel(settings); - DpiField.SetToEntered(RawAccelSettings.GUISettings.DPI); PollRateField.SetToEntered(RawAccelSettings.GUISettings.PollRate); - AutoWriteMenuItem.Checked = RawAccelSettings.GUISettings.AutoWriteToDriverOnStartup; ShowLastMouseMoveMenuItem.Checked = RawAccelSettings.GUISettings.ShowLastMouseMove; ShowVelocityAndGainMoveMenuItem.Checked = RawAccelSettings.GUISettings.ShowVelocityAndGain; } + public void UpdateActiveAccelFromFileSettings(DriverSettings settings) + { + TryUpdateAccel(settings); + UpdateFieldsFromGUISettings(); + } + public SettingsErrors TryUpdateAccel(DriverSettings settings) { var errors = SendToDriverSafe(settings); @@ -128,17 +119,26 @@ namespace grapher.Models.Serialized return errors; } + public GUISettings MakeGUISettingsFromFields() + { + return new GUISettings + { + DPI = (int)DpiField.Data, + PollRate = (int)PollRateField.Data, + ShowLastMouseMove = ShowLastMouseMoveMenuItem.Checked, + ShowVelocityAndGain = ShowVelocityAndGainMoveMenuItem.Checked + }; + } + public void Startup() { if (RawAccelSettings.Exists()) { try { - RawAccelSettings = RawAccelSettings.Load(); - if (RawAccelSettings.GUISettings.AutoWriteToDriverOnStartup) - { - UpdateActiveAccelFromFileSettings(RawAccelSettings.AccelerationSettings); - } + RawAccelSettings = RawAccelSettings.Load(() => MakeGUISettingsFromFields()); + UpdateFieldsFromGUISettings(); + UpdateActiveAccelFromFileSettings(RawAccelSettings.AccelerationSettings); return; } catch (JsonException e) @@ -149,13 +149,7 @@ namespace grapher.Models.Serialized RawAccelSettings = new RawAccelSettings( DriverInterop.GetActiveSettings(), - new GUISettings - { - AutoWriteToDriverOnStartup = AutoWriteMenuItem.Checked, - DPI = (int)DpiField.Data, - PollRate = (int)PollRateField.Data, - ShowLastMouseMove = ShowLastMouseMoveMenuItem.Checked, - }); + MakeGUISettingsFromFields()); RawAccelSettings.Save(); } |