Skip to content

Commit

Permalink
InputShifters save OnHold and OnLongRelease correctly (#1451)
Browse files Browse the repository at this point in the history
* InputShifters save OnHold and OnLongRelease correctly

* fixed small typo
  • Loading branch information
DocMoebiuz authored Nov 25, 2023
1 parent 3201bda commit 2fcb4a8
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 22 deletions.
25 changes: 16 additions & 9 deletions MobiFlight/InputConfig/ButtonInputConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,24 @@ public ButtonInputConfig()
RepeatTimer.Elapsed += RepeatTimer_Elapsed;
}

/// <summary>
/// Copy constructor, this allows to reuse the clone method in derived classes
/// </summary>
/// <param name="copyFrom"></param>
protected ButtonInputConfig(ButtonInputConfig copyFrom) : this()
{
this.onPress = (InputAction)copyFrom?.onPress?.Clone();
this.onRelease = (InputAction)copyFrom?.onRelease?.Clone();
this.onLongRelease = (InputAction)copyFrom?.onLongRelease?.Clone();
this.onHold = (InputAction)copyFrom?.onHold?.Clone();
this.RepeatDelay = copyFrom.RepeatDelay;
this.HoldDelay = copyFrom.HoldDelay;
this.LongReleaseDelay = copyFrom.LongReleaseDelay;
}

public object Clone()
{
ButtonInputConfig clone = new ButtonInputConfig();
if (onPress != null) clone.onPress = (InputAction)onPress.Clone();
if (onRelease != null) clone.onRelease = (InputAction)onRelease.Clone();
if (onLongRelease != null) clone.onLongRelease = (InputAction)onLongRelease.Clone();
if (onHold != null) clone.onHold = (InputAction)onHold.Clone();
clone.RepeatDelay = RepeatDelay;
clone.HoldDelay = HoldDelay;
clone.LongReleaseDelay = LongReleaseDelay;
return clone;
return new ButtonInputConfig(this);
}

public System.Xml.Schema.XmlSchema GetSchema()
Expand Down
22 changes: 17 additions & 5 deletions MobiFlight/InputConfig/InputMultiplexerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,25 @@ public class InputMultiplexerConfig : ButtonInputConfig
{
public int DataPin;

/// <summary>
/// Public constructor with no parameters
/// </summary>
public InputMultiplexerConfig() : base() { }

/// <summary>
/// Copy constructor, this allows to reuse the clone method in derived classes
/// </summary>
/// <param name="copyFrom"></param>
protected InputMultiplexerConfig(InputMultiplexerConfig copyFrom) : base(copyFrom) {
this.DataPin = copyFrom.DataPin;
}

/// <summary>
/// Clone method, uses the copy constuctor to respect inheritance and prevents code duplication
/// </summary>
public new object Clone()
{
InputMultiplexerConfig clone = new InputMultiplexerConfig();
if (onPress != null) clone.onPress = (InputAction)onPress.Clone();
if (onRelease != null) clone.onRelease = (InputAction)onRelease.Clone();
clone.DataPin = DataPin;
return clone;
return new InputMultiplexerConfig(this);
}

public new void ReadXml(System.Xml.XmlReader reader)
Expand Down
27 changes: 19 additions & 8 deletions MobiFlight/InputConfig/InputShiftRegisterConfig.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace MobiFlight.InputConfig
{
Expand All @@ -13,13 +10,27 @@ public class InputShiftRegisterConfig : ButtonInputConfig
{
public int ExtPin;

/// <summary>
/// Public constructor with no parameters
/// </summary>
public InputShiftRegisterConfig() : base() {
// we don't do anything in the public constructor
}

/// <summary>
/// Copy constructor, this allows to reuse the clone method in derived classes
/// </summary>
/// <param name="copyFrom"></param>
protected InputShiftRegisterConfig(InputShiftRegisterConfig copyFrom) : base(copyFrom) {
this.ExtPin = copyFrom.ExtPin;
}

/// <summary>
/// Clone method, uses the copy constuctor to respect inheritance and prevents code duplication
/// </summary>
public new object Clone()
{
InputShiftRegisterConfig clone = new InputShiftRegisterConfig();
if (onPress != null) clone.onPress = (InputAction)onPress.Clone();
if (onRelease != null) clone.onRelease = (InputAction)onRelease.Clone();
clone.ExtPin = ExtPin;
return clone;
return new InputShiftRegisterConfig(this);
}

public new void ReadXml(System.Xml.XmlReader reader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public void CloneTest()
Assert.AreEqual((o.onRelease as JeehellInputAction).EventId, (c.onRelease as JeehellInputAction).EventId, "OnRelease is not correct");
Assert.AreEqual((o.onLongRelease as MSFS2020CustomInputAction).PresetId, (c.onLongRelease as MSFS2020CustomInputAction).PresetId, "OnLongRelase is not correct");
Assert.AreEqual((o.onHold as XplaneInputAction).Path, (c.onHold as XplaneInputAction).Path, "onHold is not correct");
Assert.AreEqual(o.RepeatDelay, c.RepeatDelay, "RepeatDelay is not correct");
Assert.AreEqual(o.HoldDelay, c.HoldDelay, "HoldDelay is not correct");
Assert.AreEqual(o.LongReleaseDelay, c.LongReleaseDelay, "LongReleaseDelay is not correct");
}

private ButtonInputConfig generateTestObject()
Expand All @@ -31,6 +33,7 @@ private ButtonInputConfig generateTestObject()
o.onHold = new XplaneInputAction() { Expression = "", InputType = "Command", Path = "sim/autopilot/autothrottle_toggle" };
o.RepeatDelay = 1000;
o.HoldDelay = 2000;
o.LongReleaseDelay = 1234;
return o;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public void CloneTest()
Assert.AreNotSame(o, c, "Cloned object is the same");
Assert.AreEqual((o.onPress as EventIdInputAction).EventId, (c.onPress as EventIdInputAction).EventId, "OnPress is not correct");
Assert.AreEqual((o.onRelease as JeehellInputAction).EventId, (c.onRelease as JeehellInputAction).EventId, "OnRelase is not correct");
Assert.AreEqual((o.onHold as VariableInputAction).Variable.Name, (c.onHold as VariableInputAction).Variable.Name, "onHold is not correct");
Assert.AreEqual(o.HoldDelay, c.HoldDelay, "HoldDelay is not correct");
Assert.AreEqual(o.LongReleaseDelay, c.LongReleaseDelay, "LongReleaseDelay is not correct");
Assert.AreEqual(o.RepeatDelay, c.RepeatDelay, "RepeatDelay is not correct");
}

private InputMultiplexerConfig generateTestObject()
Expand All @@ -30,6 +34,11 @@ private InputMultiplexerConfig generateTestObject()
o.DataPin = 1;
o.onPress = new EventIdInputAction() { EventId = 12345 };
o.onRelease = new JeehellInputAction() { EventId = 127, Param = "123" };
o.onHold = new VariableInputAction() { Variable = new MobiFlightVariable() { Name = "onHold", Expression = "$+1" } };
o.onLongRelease = new VariableInputAction() { Variable = new MobiFlightVariable() { Name = "onLongRelease", Expression = "$+1" } };
o.HoldDelay = 1234;
o.LongReleaseDelay = 4321;
o.RepeatDelay = 111;
return o;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public void CloneTest()
Assert.AreNotSame(o, c, "Cloned object is the same");
Assert.AreEqual((o.onPress as EventIdInputAction).EventId, (c.onPress as EventIdInputAction).EventId, "OnPress is not correct");
Assert.AreEqual((o.onRelease as JeehellInputAction).EventId, (c.onRelease as JeehellInputAction).EventId, "OnRelase is not correct");
Assert.AreEqual((o.onHold as VariableInputAction).Variable.Name, (c.onHold as VariableInputAction).Variable.Name, "onHold is not correct");
Assert.AreEqual((o.onLongRelease as VariableInputAction).Variable.Name, (c.onLongRelease as VariableInputAction).Variable.Name, "onLongRelease is not correct");
}

private InputShiftRegisterConfig generateTestObject()
Expand All @@ -30,6 +32,11 @@ private InputShiftRegisterConfig generateTestObject()
o.ExtPin = 1;
o.onPress = new EventIdInputAction() { EventId = 12345 };
o.onRelease = new JeehellInputAction() { EventId = 127, Param = "123" };
o.onHold = new VariableInputAction() { Variable = new MobiFlightVariable() { Name = "onHold", Expression = "$+1" } };
o.onLongRelease = new VariableInputAction() { Variable = new MobiFlightVariable() { Name = "onLongRelease", Expression = "$+1" } };
o.HoldDelay = 1234;
o.LongReleaseDelay = 4321;
o.RepeatDelay = 111;
return o;
}

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 2fcb4a8

Please sign in to comment.