diff options
| author | a1xd <[email protected]> | 2020-08-22 22:33:45 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-08-22 22:33:45 -0400 |
| commit | 252637e53ca42353061dc3118e8625af6edc348f (patch) | |
| tree | 26ea73edae996242eaef559485309fb9c66f4d30 /grapher/Models | |
| parent | Merge pull request #15 from JacobPalecki/GUI (diff) | |
| parent | delete personal settings.json left in repo (diff) | |
| download | rawaccel-252637e53ca42353061dc3118e8625af6edc348f.tar.xz rawaccel-252637e53ca42353061dc3118e8625af6edc348f.zip | |
Merge pull request #16 from JacobPalecki/Misc
Gain Styles, Settings File, and other miscellaneous
Diffstat (limited to 'grapher/Models')
| -rw-r--r-- | grapher/Models/AccelGUI.cs | 61 | ||||
| -rw-r--r-- | grapher/Models/Calculations/AccelCalculator.cs | 70 | ||||
| -rw-r--r-- | grapher/Models/Options/AccelOptions.cs | 15 | ||||
| -rw-r--r-- | grapher/Models/Options/ActiveValueLabel.cs | 107 | ||||
| -rw-r--r-- | grapher/Models/Options/ActiveValueLabelXY.cs | 84 | ||||
| -rw-r--r-- | grapher/Models/Options/CapOptions.cs | 30 | ||||
| -rw-r--r-- | grapher/Models/Options/Option.cs | 50 | ||||
| -rw-r--r-- | grapher/Models/Options/OptionXY.cs | 22 | ||||
| -rw-r--r-- | grapher/Models/Serialized/GUISettings.cs | 33 | ||||
| -rw-r--r-- | grapher/Models/Serialized/ModifierArgs.cs | 75 | ||||
| -rw-r--r-- | grapher/Models/Serialized/RawAccelSettings.cs | 84 | ||||
| -rw-r--r-- | grapher/Models/Serialized/SettingsManager.cs | 123 |
12 files changed, 713 insertions, 41 deletions
diff --git a/grapher/Models/AccelGUI.cs b/grapher/Models/AccelGUI.cs index 8eb2226..e0dcc03 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; @@ -18,8 +19,9 @@ namespace grapher public AccelGUI( RawAcceleration accelForm, + AccelCalculator accelCalculator, AccelCharts accelCharts, - ManagedAccel managedAccel, + SettingsManager settings, AccelOptions accelOptions, OptionXY sensitivity, Option rotation, @@ -30,11 +32,13 @@ namespace grapher Option limtOrExp, Option midpoint, Button writeButton, - Label mouseMoveLabel) + Label mouseMoveLabel, + ToolStripMenuItem scaleMenuItem, + ToolStripMenuItem autoWriteMenuItem) { AccelForm = accelForm; + AccelCalculator = accelCalculator; AccelCharts = accelCharts; - ManagedAcceleration = managedAccel; AccelerationOptions = accelOptions; Sensitivity = sensitivity; Rotation = rotation; @@ -45,11 +49,14 @@ namespace grapher LimitOrExponent = limtOrExp; Midpoint = midpoint; WriteButton = writeButton; - - ManagedAcceleration.ReadFromDriver(); + ScaleMenuItem = scaleMenuItem; + Settings = settings; + Settings.Startup(); UpdateGraph(); MouseWatcher = new MouseWatcher(AccelForm, mouseMoveLabel, AccelCharts); + + ScaleMenuItem.Click += new System.EventHandler(OnScaleMenuItemClick); } #endregion constructors @@ -58,9 +65,11 @@ namespace grapher public RawAcceleration AccelForm { get; } + public AccelCalculator AccelCalculator { get; } + public AccelCharts AccelCharts { get; } - public ManagedAccel ManagedAcceleration { get; } + public SettingsManager Settings { get; } public AccelOptions AccelerationOptions { get; } @@ -84,17 +93,55 @@ namespace grapher public MouseWatcher MouseWatcher { get; } + public ToolStripMenuItem ScaleMenuItem { get; } + #endregion properties #region methods + public void UpdateActiveSettingsFromFields() + { + Settings.UpdateActiveSettings( + AccelerationOptions.AccelerationIndex, + Rotation.Field.Data, + Sensitivity.Fields.X, + Sensitivity.Fields.Y, + Weight.Fields.X, + Weight.Fields.Y, + Cap.SensitivityCapX, + Cap.SensitivityCapY, + Offset.Field.Data, + Acceleration.Field.Data, + LimitOrExponent.Field.Data, + Midpoint.Field.Data, + Cap.VelocityGainCap); + UpdateGraph(); + } public void UpdateGraph() { - AccelCalculator.Calculate(AccelCharts.AccelData, ManagedAcceleration); + AccelCalculator.Calculate(AccelCharts.AccelData, Settings.ActiveAccel); AccelCharts.Bind(); + UpdateActiveValueLabels(); } + public void UpdateActiveValueLabels() + { + Sensitivity.SetActiveValues(Settings.ActiveAccel.SensitivityX, Settings.ActiveAccel.SensitivityY); + Rotation.SetActiveValue(Settings.ActiveAccel.Rotation); + AccelerationOptions.SetActiveValue(Settings.ActiveAccel.Type); + Offset.SetActiveValue(Settings.ActiveAccel.Offset); + Acceleration.SetActiveValue(Settings.ActiveAccel.Acceleration); + Cap.SetActiveValues(Settings.ActiveAccel.GainCap, Settings.ActiveAccel.CapX, Settings.ActiveAccel.CapY, Settings.ActiveAccel.GainCapEnabled); + Weight.SetActiveValues(Settings.ActiveAccel.WeightX, Settings.ActiveAccel.WeightY); + LimitOrExponent.SetActiveValue(Settings.ActiveAccel.LimitExp); + Midpoint.SetActiveValue(Settings.ActiveAccel.Midpoint); + } + + private void OnScaleMenuItemClick(object sender, EventArgs e) + { + UpdateGraph(); + } #endregion methods } diff --git a/grapher/Models/Calculations/AccelCalculator.cs b/grapher/Models/Calculations/AccelCalculator.cs index bba3c32..63ed281 100644 --- a/grapher/Models/Calculations/AccelCalculator.cs +++ b/grapher/Models/Calculations/AccelCalculator.cs @@ -4,13 +4,17 @@ using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows.Forms; namespace grapher.Models.Calculations { - public static class AccelCalculator + public class AccelCalculator { - public const int MaxCombined = 100; - public const int MaxXY = 150; + public const int DefaultDPI = 1200; + public const int DefaultPollRate = 1000; + public const int Resolution = 100; + public const double MaxMultiplier = 85; + public const double XYToCombinedRatio = 1.3; public struct MagnitudeData { @@ -19,17 +23,38 @@ namespace grapher.Models.Calculations public int y; } - public static ReadOnlyCollection<MagnitudeData> MagnitudesCombined = GetMagnitudes(); - public static ReadOnlyCollection<MagnitudeData> MagnitudesX = GetMagnitudesX(); - public static ReadOnlyCollection<MagnitudeData> MagnitudesY = GetMagnitudesY(); - public static void Calculate(AccelData data, ManagedAccel accel) + public AccelCalculator(Field dpi, Field pollRate) { + DPI = dpi; + PollRate = pollRate; + } + + public ReadOnlyCollection<MagnitudeData> MagnitudesCombined { get; private set; } + + public ReadOnlyCollection<MagnitudeData> MagnitudesX { get; private set; } + + public ReadOnlyCollection<MagnitudeData> MagnitudesY { get; private set; } + + public Field DPI { get; private set; } + + public Field PollRate { get; private set; } + + private double CombinedMaxVelocity { get; set; } + + private double XYMaxVelocity { get; set; } + + private int Increment { get; set; } + + public void Calculate(AccelData data, ManagedAccel accel) + { + ScaleByMouseSettings(); + data.Clear(); - Calculate(data.Combined, accel, accel.GetSensitivityX(), MagnitudesCombined); - Calculate(data.X, accel, accel.GetSensitivityX(), MagnitudesX); - Calculate(data.Y, accel, accel.GetSensitivityY(), MagnitudesY); + Calculate(data.Combined, accel, accel.SensitivityX, MagnitudesCombined); + Calculate(data.X, accel, accel.SensitivityX, MagnitudesX); + Calculate(data.Y, accel, accel.SensitivityY, MagnitudesY); } public static void Calculate(AccelChartData data, ManagedAccel accel, double starter, ICollection<MagnitudeData> magnitudeData) @@ -70,12 +95,12 @@ namespace grapher.Models.Calculations data.OrderedVelocityPointsList.AddRange(data.VelocityPoints.Values.ToList()); } - public static ReadOnlyCollection<MagnitudeData> GetMagnitudes() + public ReadOnlyCollection<MagnitudeData> GetMagnitudes() { var magnitudes = new List<MagnitudeData>(); - for (int i = 0; i < MaxCombined; i++) + for (int i = 0; i < CombinedMaxVelocity; i+=Increment) { - for (int j = 0; j <= i; j++) + for (int j = 0; j <= i; j+=Increment) { MagnitudeData magnitudeData; magnitudeData.magnitude = Magnitude(i, j); @@ -90,11 +115,11 @@ namespace grapher.Models.Calculations return magnitudes.AsReadOnly(); } - public static ReadOnlyCollection<MagnitudeData> GetMagnitudesX() + public ReadOnlyCollection<MagnitudeData> GetMagnitudesX() { var magnitudes = new List<MagnitudeData>(); - for (int i = 0; i < MaxXY; i++) + for (int i = 0; i < XYMaxVelocity; i+=Increment) { MagnitudeData magnitudeData; magnitudeData.magnitude = i; @@ -106,11 +131,11 @@ namespace grapher.Models.Calculations return magnitudes.AsReadOnly(); } - public static ReadOnlyCollection<MagnitudeData> GetMagnitudesY() + public ReadOnlyCollection<MagnitudeData> GetMagnitudesY() { var magnitudes = new List<MagnitudeData>(); - for (int i = 0; i < MaxXY; i++) + for (int i = 0; i < XYMaxVelocity; i+=Increment) { MagnitudeData magnitudeData; magnitudeData.magnitude = i; @@ -141,5 +166,16 @@ namespace grapher.Models.Calculations { return Magnitude(x, y) / time; } + + public void ScaleByMouseSettings() + { + var dpiPollFactor = DPI.Data / PollRate.Data; + CombinedMaxVelocity = dpiPollFactor * MaxMultiplier; + Increment = (int) Math.Floor(CombinedMaxVelocity / Resolution); + XYMaxVelocity = CombinedMaxVelocity * 1.5; + MagnitudesCombined = GetMagnitudes(); + MagnitudesX = GetMagnitudesX(); + MagnitudesY = GetMagnitudesY(); + } } } diff --git a/grapher/Models/Options/AccelOptions.cs b/grapher/Models/Options/AccelOptions.cs index b233552..cd7c4e5 100644 --- a/grapher/Models/Options/AccelOptions.cs +++ b/grapher/Models/Options/AccelOptions.cs @@ -1,4 +1,5 @@ using grapher.Layouts; +using grapher.Models.Options; using System; using System.Collections.Generic; using System.Linq; @@ -22,6 +23,8 @@ namespace grapher new LogLayout(), new SigmoidLayout(), new PowerLayout(), + new NaturalGainLayout(), + new SigmoidGainLayout(), new OffLayout() }.ToDictionary(k => k.Name); @@ -29,7 +32,8 @@ namespace grapher ComboBox accelDropdown, Option[] options, OptionXY[] optionsXY, - Button writeButton) + Button writeButton, + ActiveValueLabel activeValueLabel) { AccelDropdown = accelDropdown; AccelDropdown.Items.Clear(); @@ -49,6 +53,7 @@ namespace grapher Options = options; OptionsXY = optionsXY; WriteButton = writeButton; + ActiveValueLabel = activeValueLabel; Layout("Default"); } @@ -59,10 +64,18 @@ namespace grapher public int AccelerationIndex { get; private set; } + public ActiveValueLabel ActiveValueLabel { get; } + public Option[] Options { get; } public OptionXY[] OptionsXY { get; } + public void SetActiveValue(int index) + { + var name = AccelerationTypes.Where(t => t.Value.Index == index).FirstOrDefault().Value.Name; + ActiveValueLabel.SetValue(name); + } + private void OnIndexChanged(object sender, EventArgs e) { var accelerationTypeString = AccelDropdown.SelectedItem.ToString(); diff --git a/grapher/Models/Options/ActiveValueLabel.cs b/grapher/Models/Options/ActiveValueLabel.cs new file mode 100644 index 0000000..138775a --- /dev/null +++ b/grapher/Models/Options/ActiveValueLabel.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher.Models.Options +{ + public class ActiveValueLabel + { + public const string DefaultFormatString = "0.######"; + public static readonly Color ActiveValueFontColor = Color.FromArgb(255, 65, 65, 65); + + private string _prefix; + private string _value; + + public ActiveValueLabel(Label valueLabel, Label centeringLabel) + { + ValueLabel = valueLabel; + ValueLabel.ForeColor = ActiveValueFontColor; + Left = centeringLabel.Left; + Width = centeringLabel.Width; + ValueLabel.AutoSize = false; + ValueLabel.TextAlign = ContentAlignment.MiddleCenter; + + FormatString = DefaultFormatString; + Prefix = string.Empty; + } + + public Label ValueLabel { get; } + + public string FormatString { get; set; } + + public string Prefix + { + get { return _prefix; } + set + { + _prefix = value; + RefreshText(); + } + } + + private string Value + { + get { return _value; } + set + { + _value = value; + RefreshText(); + } + } + + public int Left + { + get + { + return ValueLabel.Left; + } + + set + { + ValueLabel.Left = value; + } + } + + public int Width + { + get + { + return ValueLabel.Width; + } + + set + { + ValueLabel.Width = value; + } + } + + public void Hide() + { + ValueLabel.Hide(); + } + + public void Show() + { + ValueLabel.Show(); + } + + public void SetValue(double value) + { + SetValue(value.ToString(FormatString)); + } + + public void SetValue(string value) + { + Value = value; + } + + public void RefreshText() + { + ValueLabel.Text = string.IsNullOrWhiteSpace(Prefix) ? Value: $"{Prefix}: {Value}"; + } + } +} diff --git a/grapher/Models/Options/ActiveValueLabelXY.cs b/grapher/Models/Options/ActiveValueLabelXY.cs new file mode 100644 index 0000000..12506e9 --- /dev/null +++ b/grapher/Models/Options/ActiveValueLabelXY.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace grapher.Models.Options +{ + public class ActiveValueLabelXY + { + public const int ActiveLabelXYSeparation = 2; + public const string ShortenedFormatString = "0.###"; + + public ActiveValueLabelXY( + ActiveValueLabel x, + ActiveValueLabel y) + { + X = x; + Y = y; + + FullWidth = x.Width; + ShortenedWidth = (FullWidth - ActiveLabelXYSeparation) / 2; + + Y.Left = X.Left + ShortenedWidth + ActiveLabelXYSeparation; + Y.Width = ShortenedWidth; + Y.FormatString = ShortenedFormatString; + + Combined = false; + SetCombined(); + } + + public ActiveValueLabel X { get; } + + public ActiveValueLabel Y { get; } + + public bool Combined { get; private set; } + + private int FullWidth { get; } + + private int ShortenedWidth { get; } + + public void SetValues(double x, double y) + { + X.SetValue(x); + Y.SetValue(y); + + if (x == y) + { + SetCombined(); + } + else + { + SetSeparate(); + } + } + + public void SetCombined() + { + if (!Combined) + { + X.FormatString = ActiveValueLabel.DefaultFormatString; + X.Width = FullWidth; + X.Prefix = string.Empty; + Y.Hide(); + } + + Combined = true; + } + + public void SetSeparate() + { + if (Combined) + { + X.FormatString = ShortenedFormatString; + X.Width = ShortenedWidth; + X.Prefix = "X"; + Y.Prefix = "Y"; + Y.Show(); + } + + Combined = false; + } + } +} diff --git a/grapher/Models/Options/CapOptions.cs b/grapher/Models/Options/CapOptions.cs index 2ee7f6b..493561a 100644 --- a/grapher/Models/Options/CapOptions.cs +++ b/grapher/Models/Options/CapOptions.cs @@ -1,4 +1,5 @@ -using System; +using grapher.Models.Options; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -9,6 +10,9 @@ namespace grapher { public class CapOptions { + + public const string GainCapFormatString = "0.##"; + public CapOptions( ToolStripMenuItem sensitivityCapCheck, ToolStripMenuItem velocityGainCapCheck, @@ -30,13 +34,13 @@ namespace grapher EnableSensitivityCap(); } - ToolStripMenuItem SensitivityCapCheck { get; } + public ToolStripMenuItem SensitivityCapCheck { get; } - ToolStripMenuItem VelocityGainCapCheck { get; } + public ToolStripMenuItem VelocityGainCapCheck { get; } - OptionXY CapOption { get; } + public OptionXY CapOption { get; } - OptionXY WeightOption { get; } + public OptionXY WeightOption { get; } public double SensitivityCapX { get @@ -82,6 +86,22 @@ namespace grapher public bool IsSensitivityGain { get; private set; } + public void SetActiveValues(double gainCap, double sensCapX, double sensCapY, bool capGainEnabled) + { + if (capGainEnabled) + { + CapOption.ActiveValueLabels.X.FormatString = GainCapFormatString; + CapOption.ActiveValueLabels.X.Prefix = "Gain"; + CapOption.SetActiveValues(gainCap, gainCap); + } + else + { + CapOption.ActiveValueLabels.X.FormatString = ActiveValueLabel.DefaultFormatString; + CapOption.ActiveValueLabels.X.Prefix = string.Empty; + CapOption.SetActiveValues(sensCapX, sensCapY); + } + } + void OnSensitivityCapCheckClick(object sender, EventArgs e) { if (!SensitivityCapCheck.Checked) diff --git a/grapher/Models/Options/Option.cs b/grapher/Models/Options/Option.cs index eb5105e..bacd760 100644 --- a/grapher/Models/Options/Option.cs +++ b/grapher/Models/Options/Option.cs @@ -1,4 +1,5 @@ -using System; +using grapher.Models.Options; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -9,19 +10,42 @@ namespace grapher { public class Option { - public Option(Field field, Label label) + public Option( + Field field, + Label label, + ActiveValueLabel activeValueLabel) { Field = field; Label = label; + ActiveValueLabel = activeValueLabel; } - public Option(TextBox box, Form containingForm, double defaultData, Label label) - : this(new Field(box, containingForm, defaultData), label) + public Option( + TextBox box, + Form containingForm, + double defaultData, + Label label, + ActiveValueLabel activeValueLabel) + : this( + new Field(box, containingForm, defaultData), + label, + activeValueLabel) { } - public Option(TextBox box, Form containingForm, double defaultData, Label label, string startingName) - : this(box, containingForm, defaultData, label) + public Option( + TextBox box, + Form containingForm, + double defaultData, + Label label, + ActiveValueLabel activeValueLabel, + string startingName) + : this( + box, + containingForm, + defaultData, + label, + activeValueLabel) { SetName(startingName); } @@ -30,22 +54,36 @@ namespace grapher public Label Label { get; } + public ActiveValueLabel ActiveValueLabel { get; } + public void SetName(string name) { Label.Text = name; Label.Left = Convert.ToInt32((Field.Box.Left / 2.0) - (Label.Width / 2.0)); } + public void SetActiveValue(double value) + { + ActiveValueLabel.SetValue(value); + } + public void Hide() { Field.Box.Hide(); Label.Hide(); + ActiveValueLabel.Hide(); } public void Show() { Field.Box.Show(); Label.Show(); + ActiveValueLabel.Show(); + } + + public void UpdateActiveValue(double value) + { + ActiveValueLabel.SetValue(value); } public void Show(string name) diff --git a/grapher/Models/Options/OptionXY.cs b/grapher/Models/Options/OptionXY.cs index 90a46d7..b22bb78 100644 --- a/grapher/Models/Options/OptionXY.cs +++ b/grapher/Models/Options/OptionXY.cs @@ -1,4 +1,5 @@ -using System; +using grapher.Models.Options; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -9,10 +10,11 @@ namespace grapher { public class OptionXY { - public OptionXY(FieldXY fields, Label label) + public OptionXY(FieldXY fields, Label label, ActiveValueLabelXY activeValueLabels) { Fields = fields; Label = label; + ActiveValueLabels = activeValueLabels; } public OptionXY( @@ -22,8 +24,9 @@ namespace grapher Form containingForm, double defaultData, Label label, - AccelCharts accelCharts) - : this(new FieldXY(xBox, yBox, lockCheckBox, containingForm, defaultData, accelCharts), label) + AccelCharts accelCharts, + ActiveValueLabelXY activeValueLabels) + : this(new FieldXY(xBox, yBox, lockCheckBox, containingForm, defaultData, accelCharts), label, activeValueLabels) { } @@ -34,6 +37,7 @@ namespace grapher Form containingForm, double defaultData, Label label, + ActiveValueLabelXY activeValueLabels, string startingName, AccelCharts accelCharts): this( @@ -43,7 +47,8 @@ namespace grapher containingForm, defaultData, label, - accelCharts) + accelCharts, + activeValueLabels) { SetName(startingName); } @@ -52,12 +57,19 @@ namespace grapher public Label Label { get; } + public ActiveValueLabelXY ActiveValueLabels { get; } + public void SetName(string name) { Label.Text = name; Label.Left = Convert.ToInt32((Fields.XField.Box.Left / 2.0) - (Label.Width / 2.0)); } + public void SetActiveValues(double x, double y) + { + ActiveValueLabels.SetValues(x, y); + } + public void Hide() { Fields.Hide(); diff --git a/grapher/Models/Serialized/GUISettings.cs b/grapher/Models/Serialized/GUISettings.cs new file mode 100644 index 0000000..7c8e9a4 --- /dev/null +++ b/grapher/Models/Serialized/GUISettings.cs @@ -0,0 +1,33 @@ +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 GUISettings + { + public GUISettings() {} + + public GUISettings(bool autoWrite, int dpi, int pollRate) + { + AutoWriteToDriverOnStartup = autoWrite; + DPI = dpi; + PollRate = pollRate; + } + + [JsonProperty(Order = 1)] + public bool AutoWriteToDriverOnStartup { get; set; } + + [JsonProperty(Order = 2)] + public int DPI { get; set; } + + [JsonProperty(Order = 3)] + public int PollRate { get; set; } + } +} diff --git a/grapher/Models/Serialized/ModifierArgs.cs b/grapher/Models/Serialized/ModifierArgs.cs new file mode 100644 index 0000000..206a3c9 --- /dev/null +++ b/grapher/Models/Serialized/ModifierArgs.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace grapher.Models.Serialized +{ + public enum accel_mode + { + linear=1, classic, natural, logarithmic, sigmoid, power, naturalgain, sigmoidgain, noaccel + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + [Serializable] + public struct vec2d + { + public double x; + public double y; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + [Serializable] + public struct accel_args + { + public double offset; + public double accel; + public double limit; + public double exponent; + public double midpoint; + public double power_scale; + public double gain_cap; + public vec2d weight; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + [Serializable] + public struct accel_fn_args + { + public accel_args acc_args; + public int accel_mode; + public double time_min; + public vec2d cap; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + [Serializable] + public struct modifier_args + { + public double degrees; + public vec2d sens; + public accel_fn_args acc_fn_args; + + public modifier_args(ManagedAccel managedAccel) + { + degrees = managedAccel.Rotation; + sens.x = managedAccel.SensitivityX; + sens.y = managedAccel.SensitivityY; + acc_fn_args.accel_mode = managedAccel.Type; + acc_fn_args.time_min = managedAccel.MinimumTime; + acc_fn_args.cap.x = managedAccel.CapX; + acc_fn_args.cap.y = managedAccel.CapY; + acc_fn_args.acc_args.accel = managedAccel.Acceleration; + acc_fn_args.acc_args.exponent = managedAccel.LimitExp; + acc_fn_args.acc_args.gain_cap = managedAccel.GainCap; + acc_fn_args.acc_args.limit = managedAccel.LimitExp; + acc_fn_args.acc_args.midpoint = managedAccel.Midpoint; + acc_fn_args.acc_args.offset = managedAccel.Offset; + acc_fn_args.acc_args.power_scale = managedAccel.PowerScale; + acc_fn_args.acc_args.weight.x = managedAccel.WeightX; + acc_fn_args.acc_args.weight.y = managedAccel.WeightY; + } + } +} diff --git a/grapher/Models/Serialized/RawAccelSettings.cs b/grapher/Models/Serialized/RawAccelSettings.cs new file mode 100644 index 0000000..21a7f0c --- /dev/null +++ b/grapher/Models/Serialized/RawAccelSettings.cs @@ -0,0 +1,84 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher.Models.Serialized +{ + [Serializable] + public class RawAccelSettings + { + public const string DefaultSettingsFileName = @"settings.json"; + public static readonly string ExecutingDirectory = AppDomain.CurrentDomain.BaseDirectory; + public static readonly string DefaultSettingsFile = Path.Combine(ExecutingDirectory, DefaultSettingsFileName); + public static readonly JsonSerializerSettings SerializerSettings = new JsonSerializerSettings + { + MissingMemberHandling = MissingMemberHandling.Error, + }; + + public RawAccelSettings() { } + + public RawAccelSettings( + ManagedAccel managedAccel, + GUISettings guiSettings) + { + AccelerationSettings = new modifier_args(managedAccel); + GUISettings = guiSettings; + } + + + public GUISettings GUISettings { get; set; } + + public modifier_args AccelerationSettings { 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}"); + } + + RawAccelSettings deserializedSettings; + try + { + deserializedSettings = JsonConvert.DeserializeObject<RawAccelSettings>(File.ReadAllText(file), SerializerSettings); + } + catch(Exception e) + { + throw new Exception($"Settings file at {file} does not contain valid Raw Accel Settings.", e); + } + + 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)); + } + } +} diff --git a/grapher/Models/Serialized/SettingsManager.cs b/grapher/Models/Serialized/SettingsManager.cs new file mode 100644 index 0000000..848606d --- /dev/null +++ b/grapher/Models/Serialized/SettingsManager.cs @@ -0,0 +1,123 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher.Models.Serialized +{ + public class SettingsManager + { + public SettingsManager( + ManagedAccel activeAccel, + Field dpiField, + Field pollRateField, + ToolStripMenuItem autoWrite) + { + ActiveAccel = activeAccel; + DpiField = dpiField; + PollRateField = pollRateField; + AutoWriteMenuItem = autoWrite; + } + + public ManagedAccel ActiveAccel { get; } + + public RawAccelSettings RawAccelSettings { get; private set; } + + private Field DpiField { get; set; } + + private Field PollRateField { get; set; } + + private ToolStripMenuItem AutoWriteMenuItem { get; set; } + + public void UpdateActiveSettings( + int mode, + double degrees, + double sensitivityX, + double sensitivityY, + double weightX, + double weightY, + double capX, + double capY, + double offset, + double accel, + double limitOrExp, + double midpoint, + double gainCap) + { + ActiveAccel.UpdateAccel( + mode, + degrees, + sensitivityX, + sensitivityY, + weightX, + weightY, + capX, + capY, + offset, + accel, + limitOrExp, + midpoint, + gainCap); + + RawAccelSettings.AccelerationSettings = new modifier_args(ActiveAccel); + RawAccelSettings.GUISettings = new GUISettings + { + AutoWriteToDriverOnStartup = AutoWriteMenuItem.Checked, + DPI = (int)DpiField.Data, + PollRate = (int)PollRateField.Data + }; + + RawAccelSettings.Save(); + } + + public void UpdateActiveAccelFromFileSettings() + { + ActiveAccel.UpdateAccel( + RawAccelSettings.AccelerationSettings.acc_fn_args.accel_mode, + RawAccelSettings.AccelerationSettings.degrees, + RawAccelSettings.AccelerationSettings.sens.x, + RawAccelSettings.AccelerationSettings.sens.y, + RawAccelSettings.AccelerationSettings.acc_fn_args.acc_args.weight.x, + RawAccelSettings.AccelerationSettings.acc_fn_args.acc_args.weight.y, + RawAccelSettings.AccelerationSettings.acc_fn_args.cap.x, + RawAccelSettings.AccelerationSettings.acc_fn_args.cap.y, + RawAccelSettings.AccelerationSettings.acc_fn_args.acc_args.offset, + RawAccelSettings.AccelerationSettings.acc_fn_args.acc_args.accel, + RawAccelSettings.AccelerationSettings.acc_fn_args.acc_args.exponent, + RawAccelSettings.AccelerationSettings.acc_fn_args.acc_args.midpoint, + RawAccelSettings.AccelerationSettings.acc_fn_args.acc_args.gain_cap); + DpiField.SetToEntered(RawAccelSettings.GUISettings.DPI); + PollRateField.SetToEntered(RawAccelSettings.GUISettings.PollRate); + AutoWriteMenuItem.Checked = RawAccelSettings.GUISettings.AutoWriteToDriverOnStartup; + } + + public void Startup() + { + ActiveAccel.ReadFromDriver(); + + if(RawAccelSettings.Exists()) + { + RawAccelSettings = RawAccelSettings.Load(); + if (RawAccelSettings.GUISettings.AutoWriteToDriverOnStartup) + { + UpdateActiveAccelFromFileSettings(); + } + } + else + { + RawAccelSettings = new RawAccelSettings( + ActiveAccel, + new GUISettings + { + AutoWriteToDriverOnStartup = AutoWriteMenuItem.Checked, + DPI = (int)DpiField.Data, + PollRate = (int)PollRateField.Data + }); + RawAccelSettings.Save(); + } + } + } +} |