summaryrefslogtreecommitdiff
path: root/grapher/Models
diff options
context:
space:
mode:
authorJacob Palecki <[email protected]>2020-09-27 21:13:03 -0700
committerJacob Palecki <[email protected]>2020-09-27 21:13:03 -0700
commit5d8700a462b4798c02f4a73bc66d2a69a4920ae1 (patch)
tree3a2b71991a6efce65be9af0c303ec2b59bdebff4 /grapher/Models
parentSet tab order (diff)
parentMerge pull request #26 from a1xd/argcheck (diff)
downloadrawaccel-5d8700a462b4798c02f4a73bc66d2a69a4920ae1.tar.xz
rawaccel-5d8700a462b4798c02f4a73bc66d2a69a4920ae1.zip
Merge and fix write button
Diffstat (limited to 'grapher/Models')
-rw-r--r--grapher/Models/AccelGUI.cs22
-rw-r--r--grapher/Models/AccelGUIFactory.cs64
-rw-r--r--grapher/Models/Options/AccelOptionSet.cs8
-rw-r--r--grapher/Models/Options/AccelTypeOptions.cs56
-rw-r--r--grapher/Models/Options/ApplyOptions.cs6
-rw-r--r--grapher/Models/Options/OffsetOptions.cs11
-rw-r--r--grapher/Models/Serialized/RawAccelSettings.cs27
-rw-r--r--grapher/Models/Serialized/SettingsManager.cs51
8 files changed, 167 insertions, 78 deletions
diff --git a/grapher/Models/AccelGUI.cs b/grapher/Models/AccelGUI.cs
index 1fff4c3..c9c4ed0 100644
--- a/grapher/Models/AccelGUI.cs
+++ b/grapher/Models/AccelGUI.cs
@@ -3,6 +3,7 @@ using grapher.Models.Mouse;
using grapher.Models.Options;
using grapher.Models.Serialized;
using System;
+using System.Drawing;
using System.Windows.Forms;
namespace grapher
@@ -72,6 +73,8 @@ namespace grapher
public void UpdateActiveSettingsFromFields()
{
+ var driverSettings = Settings.RawAccelSettings.AccelerationSettings;
+
var settings = new DriverSettings
{
rotation = ApplyOptions.Rotation.Field.Data,
@@ -82,13 +85,20 @@ namespace grapher
},
combineMagnitudes = ApplyOptions.IsWhole,
modes = ApplyOptions.GetModes(),
- args = ApplyOptions.GetArgs(),
- minimumTime = .4
+ args = ApplyOptions.GetUpdatedArgs(ref driverSettings.args),
+ minimumTime = driverSettings.minimumTime
};
WriteButtonDelay();
- Settings.UpdateActiveSettings(settings);
- RefreshOnRead();
+ SettingsErrors errors = Settings.TryUpdateActiveSettings(settings);
+ if (errors.Empty())
+ {
+ RefreshOnRead();
+ }
+ else
+ {
+ WriteButton.Text = "bad args";
+ }
}
public void RefreshOnRead()
@@ -126,7 +136,7 @@ namespace grapher
{
Timer buttonTimer = new Timer();
buttonTimer.Enabled = true;
- buttonTimer.Interval = Convert.ToInt32(ManagedAccel.WriteDelay);
+ buttonTimer.Interval = Convert.ToInt32(DriverInterop.WriteDelayMs);
buttonTimer.Tick += new System.EventHandler(OnButtonTimerTick);
return buttonTimer;
}
@@ -141,12 +151,14 @@ namespace grapher
{
WriteButton.Text = Constants.WriteButtonDefaultText;
WriteButton.Enabled = true;
+ WriteButton.Update();
}
private void SetWriteButtonDelay()
{
WriteButton.Enabled = false;
WriteButton.Text = $"{Constants.WriteButtonDelayText} : {ButtonTimer.Interval} ms";
+ WriteButton.Update();
}
private void OnScaleMenuItemClick(object sender, EventArgs e)
diff --git a/grapher/Models/AccelGUIFactory.cs b/grapher/Models/AccelGUIFactory.cs
index 469fcf7..9f557f3 100644
--- a/grapher/Models/AccelGUIFactory.cs
+++ b/grapher/Models/AccelGUIFactory.cs
@@ -46,8 +46,12 @@ namespace grapher.Models
TextBox offsetBoxY,
TextBox accelerationBoxX,
TextBox accelerationBoxY,
+ TextBox scaleBoxX,
+ TextBox scaleBoxY,
TextBox limitBoxX,
TextBox limitBoxY,
+ TextBox expBoxX,
+ TextBox expBoxY,
TextBox midpointBoxX,
TextBox midpointBoxY,
CheckBox sensXYLock,
@@ -63,8 +67,12 @@ namespace grapher.Models
Label offsetLabelY,
Label constantOneLabelX,
Label constantOneLabelY,
- Label constantTwoLabelX,
- Label constantTwoLabelY,
+ Label scaleLabelX,
+ Label scaleLabelY,
+ Label limitLabelX,
+ Label limitLabelY,
+ Label expLabelX,
+ Label expLabelY,
Label constantThreeLabelX,
Label constantThreeLabelY,
Label activeValueTitleX,
@@ -80,8 +88,12 @@ namespace grapher.Models
Label offsetActiveLabelY,
Label accelerationActiveLabelX,
Label accelerationActiveLabelY,
- Label limitExpActiveLabelX,
- Label limitExpActiveLabelY,
+ Label scaleActiveLabelX,
+ Label scaleActiveLabelY,
+ Label limitActiveLabelX,
+ Label limitActiveLabelY,
+ Label expActiveLabelX,
+ Label expActiveLabelY,
Label midpointActiveLabelX,
Label midpointActiveLabelY,
Label accelTypeActiveLabelX,
@@ -203,16 +215,40 @@ namespace grapher.Models
new ActiveValueLabel(accelerationActiveLabelY, activeValueTitleY),
optionSetYLeft);
- var limitOrExponentX = new Option(
+ var scaleX = new Option(
+ new Field(scaleBoxX, form, 0),
+ scaleLabelX,
+ new ActiveValueLabel(scaleActiveLabelX, activeValueTitleX),
+ 0);
+
+ var scaleY = new Option(
+ new Field(scaleBoxY, form, 0),
+ scaleLabelY,
+ new ActiveValueLabel(scaleActiveLabelY, activeValueTitleY),
+ optionSetYLeft);
+
+ var limitX = new Option(
new Field(limitBoxX, form, 2),
- constantTwoLabelX,
- new ActiveValueLabel(limitExpActiveLabelX, activeValueTitleX),
+ limitLabelX,
+ new ActiveValueLabel(limitActiveLabelX, activeValueTitleX),
0);
- var limitOrExponentY = new Option(
+ var limitY = new Option(
new Field(limitBoxY, form, 2),
- constantTwoLabelY,
- new ActiveValueLabel(limitExpActiveLabelY, activeValueTitleY),
+ limitLabelY,
+ new ActiveValueLabel(limitActiveLabelY, activeValueTitleY),
+ optionSetYLeft);
+
+ var exponentX = new Option(
+ new Field(expBoxX, form, 2),
+ expLabelX,
+ new ActiveValueLabel(expActiveLabelX, activeValueTitleX),
+ 0);
+
+ var exponentY = new Option(
+ new Field(expBoxY, form, 2),
+ expLabelY,
+ new ActiveValueLabel(expActiveLabelY, activeValueTitleY),
optionSetYLeft);
var midpointX = new Option(
@@ -240,10 +276,12 @@ namespace grapher.Models
var accelerationOptionsX = new AccelTypeOptions(
accelTypeDropX,
accelerationX,
+ scaleX,
capOptionsX,
weightX,
offsetOptionsX,
- limitOrExponentX,
+ limitX,
+ exponentX,
midpointX,
writeButton,
new ActiveValueLabel(accelTypeActiveLabelX, activeValueTitleX));
@@ -251,10 +289,12 @@ namespace grapher.Models
var accelerationOptionsY = new AccelTypeOptions(
accelTypeDropY,
accelerationY,
+ scaleY,
capOptionsY,
weightY,
offsetOptionsY,
- limitOrExponentY,
+ limitY,
+ exponentY,
midpointY,
writeButton,
new ActiveValueLabel(accelTypeActiveLabelY, activeValueTitleY));
diff --git a/grapher/Models/Options/AccelOptionSet.cs b/grapher/Models/Options/AccelOptionSet.cs
index 11a7f10..53c39af 100644
--- a/grapher/Models/Options/AccelOptionSet.cs
+++ b/grapher/Models/Options/AccelOptionSet.cs
@@ -98,14 +98,14 @@ namespace grapher.Models.Options
Options.Top = TopAnchor;
}
- public void SetArgs(ref AccelArgs args)
+ public void SetArgs(ref AccelArgs args, ref /*readonly*/ AccelArgs last)
{
- Options.SetArgs(ref args);
+ Options.SetArgs(ref args, ref last);
}
- public AccelArgs GenerateArgs()
+ public AccelArgs GenerateArgs(ref /*readonly*/ AccelArgs last)
{
- return Options.GenerateArgs();
+ return Options.GenerateArgs(ref last);
}
public void SetActiveValues(int mode, AccelArgs args)
diff --git a/grapher/Models/Options/AccelTypeOptions.cs b/grapher/Models/Options/AccelTypeOptions.cs
index 573e9b9..9a4ab7c 100644
--- a/grapher/Models/Options/AccelTypeOptions.cs
+++ b/grapher/Models/Options/AccelTypeOptions.cs
@@ -30,10 +30,12 @@ namespace grapher
public AccelTypeOptions(
ComboBox accelDropdown,
Option acceleration,
+ Option scale,
CapOptions cap,
Option weight,
OffsetOptions offset,
- Option limitOrExponent,
+ Option limit,
+ Option exponent,
Option midpoint,
Button writeButton,
ActiveValueLabel accelTypeActiveValue)
@@ -44,10 +46,12 @@ namespace grapher
AccelDropdown.SelectedIndexChanged += new System.EventHandler(OnIndexChanged);
Acceleration = acceleration;
+ Scale = scale;
Cap = cap;
Weight = weight;
Offset = offset;
- LimitOrExponent = limitOrExponent;
+ Limit = limit;
+ Exponent = exponent;
Midpoint = midpoint;
WriteButton = writeButton;
AccelTypeActiveValue = accelTypeActiveValue;
@@ -82,13 +86,17 @@ namespace grapher
public Option Acceleration { get; }
+ public Option Scale { get; }
+
public CapOptions Cap { get; }
public Option Weight { get; }
public OffsetOptions Offset { get; }
- public Option LimitOrExponent { get; }
+ public Option Limit { get; }
+
+ public Option Exponent { get; }
public Option Midpoint { get; }
@@ -158,10 +166,12 @@ namespace grapher
AccelTypeActiveValue.Hide();
Acceleration.Hide();
+ Scale.Hide();
Cap.Hide();
Weight.Hide();
Offset.Hide();
- LimitOrExponent.Hide();
+ Limit.Hide();
+ Exponent.Hide();
Midpoint.Hide();
}
@@ -185,9 +195,11 @@ namespace grapher
Weight.SetActiveValue(args.weight);
Cap.SetActiveValues(args.gainCap, args.scaleCap, args.gainCap > 0 || args.scaleCap <= 0);
- Offset.SetActiveValue(args.offset, args.legacy_offset);
- Acceleration.SetActiveValue(args.accel);
- LimitOrExponent.SetActiveValue(args.exponent);
+ Offset.SetActiveValue(args.offset, args.legacyOffset);
+ Acceleration.SetActiveValue(args.acceleration);
+ Scale.SetActiveValue(args.scale);
+ Limit.SetActiveValue(args.limit);
+ Exponent.SetActiveValue(args.exponent);
Midpoint.SetActiveValue(args.midpoint);
}
@@ -213,26 +225,24 @@ namespace grapher
Width = Acceleration.Field.Width;
}
- public void SetArgs(ref AccelArgs args)
+ public void SetArgs(ref AccelArgs args, ref /*readonly*/ AccelArgs last)
{
- args.accel = Acceleration.Field.Data;
- args.rate = Acceleration.Field.Data;
- args.powerScale = Acceleration.Field.Data;
+ args.acceleration = Acceleration.Visible ? Acceleration.Field.Data : last.acceleration;
+ args.scale = Scale.Visible ? Scale.Field.Data : last.scale;
args.gainCap = Cap.VelocityGainCap;
args.scaleCap = Cap.SensitivityCap;
- args.limit = LimitOrExponent.Field.Data;
- args.exponent = LimitOrExponent.Field.Data;
- args.powerExponent = LimitOrExponent.Field.Data;
+ args.limit = Limit.Visible ? Limit.Field.Data : last.limit;
+ args.exponent = Exponent.Visible ? Exponent.Field.Data : last.exponent;
args.offset = Offset.Offset;
- args.legacy_offset = Offset.LegacyOffset;
- args.midpoint = Midpoint.Field.Data;
- args.weight = Weight.Field.Data;
+ args.legacyOffset = Offset.IsLegacy;
+ args.midpoint = Midpoint.Visible ? Midpoint.Field.Data : last.midpoint;
+ args.weight = Weight.Visible ? Weight.Field.Data : last.weight;
}
- public AccelArgs GenerateArgs()
+ public AccelArgs GenerateArgs(ref /*readonly*/ AccelArgs last)
{
AccelArgs args = new AccelArgs();
- SetArgs(ref args);
+ SetArgs(ref args, ref last);
return args;
}
@@ -240,10 +250,12 @@ namespace grapher
{
AccelTypeActiveValue.Align();
Acceleration.AlignActiveValues();
+ Scale.AlignActiveValues();
Cap.AlignActiveValues();
Offset.AlignActiveValues();
Weight.AlignActiveValues();
- LimitOrExponent.AlignActiveValues();
+ Limit.AlignActiveValues();
+ Exponent.AlignActiveValues();
Midpoint.AlignActiveValues();
}
@@ -269,10 +281,12 @@ namespace grapher
AccelerationType.Layout(
Acceleration,
+ Scale,
Cap,
Weight,
Offset,
- LimitOrExponent,
+ Limit,
+ Exponent,
Midpoint,
WriteButton,
top);
diff --git a/grapher/Models/Options/ApplyOptions.cs b/grapher/Models/Options/ApplyOptions.cs
index c2ed498..51c80ea 100644
--- a/grapher/Models/Options/ApplyOptions.cs
+++ b/grapher/Models/Options/ApplyOptions.cs
@@ -89,14 +89,14 @@ namespace grapher.Models.Options
};
}
- public Vec2<AccelArgs> GetArgs()
+ public Vec2<AccelArgs> GetUpdatedArgs(ref /*readonly*/ Vec2<AccelArgs> last)
{
- var xArgs = OptionSetX.GenerateArgs();
+ var xArgs = OptionSetX.GenerateArgs(ref last.x);
return new Vec2<AccelArgs>
{
x = xArgs,
- y = ByComponentVectorXYLock.Checked ? xArgs : OptionSetY.GenerateArgs()
+ y = ByComponentVectorXYLock.Checked ? xArgs : OptionSetY.GenerateArgs(ref last.y)
};
}
diff --git a/grapher/Models/Options/OffsetOptions.cs b/grapher/Models/Options/OffsetOptions.cs
index b351ab5..c6bee75 100644
--- a/grapher/Models/Options/OffsetOptions.cs
+++ b/grapher/Models/Options/OffsetOptions.cs
@@ -128,16 +128,9 @@ namespace grapher.Models.Options
OffsetOption.Show(name);
}
- public void SetActiveValue(double offset, double legacyOffset)
+ public void SetActiveValue(double offset, bool legacy)
{
- if (offset > 0)
- {
- OffsetOption.SetActiveValue(offset);
- }
- else
- {
- OffsetOption.SetActiveValue(legacyOffset);
- }
+ OffsetOption.SetActiveValue(offset);
}
public override void AlignActiveValues()
diff --git a/grapher/Models/Serialized/RawAccelSettings.cs b/grapher/Models/Serialized/RawAccelSettings.cs
index 3f5aebc..570a6c8 100644
--- a/grapher/Models/Serialized/RawAccelSettings.cs
+++ b/grapher/Models/Serialized/RawAccelSettings.cs
@@ -1,6 +1,8 @@
using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
using System;
using System.IO;
+using System.Linq;
namespace grapher.Models.Serialized
{
@@ -13,9 +15,8 @@ namespace grapher.Models.Serialized
public static readonly string DefaultSettingsFile = Path.Combine(ExecutingDirectory, Constants.DefaultSettingsFileName);
public static readonly JsonSerializerSettings SerializerSettings = new JsonSerializerSettings
{
- MissingMemberHandling = MissingMemberHandling.Error,
+ MissingMemberHandling = MissingMemberHandling.Ignore,
};
-
#endregion Fields
#region Constructors
@@ -33,9 +34,10 @@ namespace grapher.Models.Serialized
#endregion Constructors
#region Properties
-
+ [JsonProperty(Required = Required.Always)]
public GUISettings GUISettings { get; set; }
+ [JsonProperty(DriverSettings.Key, Required = Required.Always)]
public DriverSettings AccelerationSettings { get; set; }
#endregion Properties
@@ -51,15 +53,17 @@ namespace grapher.Models.Serialized
{
try
{
- return JsonConvert.DeserializeObject<RawAccelSettings>(File.ReadAllText(file), SerializerSettings);
+ var settings = JsonConvert.DeserializeObject<RawAccelSettings>(File.ReadAllText(file), SerializerSettings);
+ if (settings is null) throw new JsonException($"{file} contains invalid JSON");
+ return settings;
}
catch (FileNotFoundException e)
{
throw new FileNotFoundException($"Settings file does not exist at {file}", e);
}
- catch (JsonSerializationException e)
+ catch (JsonException e)
{
- throw new JsonSerializationException($"Settings file at {file} does not contain valid Raw Accel Settings.", e);
+ throw new JsonException($"Settings file at {file} does not contain valid Raw Accel Settings.", e);
}
}
@@ -80,7 +84,16 @@ namespace grapher.Models.Serialized
public void Save(string file)
{
- File.WriteAllText(file, JsonConvert.SerializeObject(this, Formatting.Indented));
+ JObject thisJO = JObject.FromObject(this);
+ AddComments(thisJO);
+ File.WriteAllText(file, thisJO.ToString(Formatting.Indented));
+ }
+
+ private void AddComments(JObject thisJO)
+ {
+ string modes = string.Join(" | ", Enum.GetNames(typeof(AccelMode)));
+ ((JObject)thisJO[DriverSettings.Key])
+ .AddFirst(new JProperty("### Mode Types ###", modes));
}
#endregion Methods
diff --git a/grapher/Models/Serialized/SettingsManager.cs b/grapher/Models/Serialized/SettingsManager.cs
index 26160f5..93cf42b 100644
--- a/grapher/Models/Serialized/SettingsManager.cs
+++ b/grapher/Models/Serialized/SettingsManager.cs
@@ -47,28 +47,31 @@ namespace grapher.Models.Serialized
#region Methods
- public void UpdateActiveSettings(DriverSettings settings)
+ public SettingsErrors TryUpdateActiveSettings(DriverSettings settings)
{
- ActiveAccel.UpdateFromSettings(settings);
- SendToDriver(settings);
+ var errors = TryUpdateAccel(settings);
- RawAccelSettings.AccelerationSettings = settings;
- RawAccelSettings.GUISettings = new GUISettings
+ if (errors.Empty())
{
- AutoWriteToDriverOnStartup = AutoWriteMenuItem.Checked,
- DPI = (int)DpiField.Data,
- PollRate = (int)PollRateField.Data,
- ShowLastMouseMove = ShowLastMouseMoveMenuItem.Checked,
- ShowVelocityAndGain = ShowVelocityAndGainMoveMenuItem.Checked,
- };
+ 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.Save();
+ RawAccelSettings.Save();
+ }
+
+ return errors;
}
public void UpdateActiveAccelFromFileSettings(DriverSettings settings)
- {
- ActiveAccel.UpdateFromSettings(settings);
- SendToDriver(settings);
+ {
+ TryUpdateAccel(settings);
DpiField.SetToEntered(RawAccelSettings.GUISettings.DPI);
PollRateField.SetToEntered(RawAccelSettings.GUISettings.PollRate);
@@ -77,9 +80,23 @@ namespace grapher.Models.Serialized
ShowVelocityAndGainMoveMenuItem.Checked = RawAccelSettings.GUISettings.ShowVelocityAndGain;
}
+ public SettingsErrors TryUpdateAccel(DriverSettings settings)
+ {
+ var errors = SendToDriverSafe(settings);
+ if (errors.Empty()) ActiveAccel.UpdateFromSettings(settings);
+ return errors;
+ }
+
public static void SendToDriver(DriverSettings settings)
{
- new Thread(() => DriverInterop.SetActiveSettings(settings)).Start();
+ new Thread(() => DriverInterop.Write(settings)).Start();
+ }
+
+ public static SettingsErrors SendToDriverSafe(DriverSettings settings)
+ {
+ var errors = DriverInterop.GetSettingsErrors(settings);
+ if (errors.Empty()) SendToDriver(settings);
+ return errors;
}
public void Startup()
@@ -95,7 +112,7 @@ namespace grapher.Models.Serialized
}
return;
}
- catch (JsonSerializationException e)
+ catch (JsonException e)
{
Console.WriteLine($"bad settings: {e}");
}