Skip to content

Commit

Permalink
Fix for #643-Incorrect update of pin selection combo boxes (#645)
Browse files Browse the repository at this point in the history
* Free pins now update even without having to change a device in between
* Changed pin selection boxes to disallow direct user input
  • Loading branch information
GioCC committed Feb 26, 2022
1 parent a84c1d3 commit 320c917
Show file tree
Hide file tree
Showing 29 changed files with 855 additions and 1,152 deletions.
40 changes: 38 additions & 2 deletions Base/ComboBoxHelper.cs
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MobiFlight;

Expand Down Expand Up @@ -61,7 +59,15 @@ static public bool SetSelectedItemByValue(ComboBox comboBox, string value)

static public bool BindMobiFlightFreePins(ComboBox comboBox, List<MobiFlightPin> Pins, String CurrentPin, bool analogOnly = false)
{
// This function assigns to a ComboBox the supplied list of all currently free pins,
// plus the specified one marked as free.
// Required because, in a selection list for a device signal, the already assigned pin
// must be in the list in order to be selectable.

if (Pins == null) return false;
// Deep-clone list as 'Used' list
List<MobiFlightPin> UsablePins = Pins.ConvertAll(pin => new MobiFlightPin(pin));
// Mark current pin as free
if (UsablePins.Exists(x => x.Pin == byte.Parse(CurrentPin)))
UsablePins.Find(x => x.Pin == byte.Parse(CurrentPin)).Used = false;

Expand All @@ -70,11 +76,41 @@ static public bool BindMobiFlightFreePins(ComboBox comboBox, List<MobiFlightPin>
UsablePins = UsablePins.FindAll(x => x.isAnalog == true);
}

// Assign the all-free list to the combobox
comboBox.DataSource = UsablePins.FindAll(x => x.Used == false);
comboBox.DisplayMember = "Name";
comboBox.ValueMember = "Pin";

// Restore the original item selection
comboBox.SelectedValue = byte.Parse(CurrentPin);

return false;
}
static public void reassignPin(ComboBox comboBox, List<MobiFlightPin> pinList, ref string signalPin)
{
// This function updates the config data (signalPin) with the new value read from the ComboBox.
// At the same time:
// - the assignment flags in the "base" pin list are accordingly updated (the current pin no. is marked as free
// and the new one as used)
// - an updated pin list is associated to the ComboBox
string after = comboBox.SelectedItem.ToString();
byte nBefore = byte.Parse(signalPin);
byte nAfter = byte.Parse(after);
try {
if (signalPin != after) {
pinList.Find(x => x.Pin == nBefore).Used = false;
pinList.Find(x => x.Pin == nAfter).Used = true;
}
}
catch (Exception e) {
Log.Instance.log($"Pin reassignment from {signalPin} to {after} went wrong", LogSeverity.Debug);
}
// now confirm assignment of the new value in the configuration data
signalPin = after;

//ComboBoxHelper.BindMobiFlightFreePins(comboBox, pinList, after);
// the function above has rebuilt its datasource, therefore the ComboBox selection must be restored:
//comboBox.SelectedValue = nAfter;
}
}
}
1 change: 1 addition & 0 deletions MobiFlight/MobiFlightModule.cs
Expand Up @@ -1036,6 +1036,7 @@ public List<MobiFlightPin> GetFreePins()
return GetPins(true);
}

// Returns a List<> of the pins used by the module
public List<MobiFlightPin> GetPins(bool FreeOnly = false)
{
List<MobiFlightPin> ResultPins = new List<MobiFlightPin>();
Expand Down
53 changes: 27 additions & 26 deletions UI/Panels/Device/MFAnalogPanel.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 6 additions & 21 deletions UI/Panels/Device/MFAnalogPanel.cs
Expand Up @@ -17,14 +17,10 @@ public partial class MFAnalogPanel : UserControl
static int MIN_SENSITIVITY = 1;
static int MAX_SENSITIVITY = 20;
static String DEFAULT_SENSITIVITY_STRING = DEFAULT_SENSITIVITY.ToString();
private MobiFlight.Config.AnalogInput analog;
private bool initialized = false;


/// <summary>
/// Gets raised whenever config object has changed
/// </summary>
public event EventHandler Changed;
private MobiFlight.Config.AnalogInput analog;
bool initialized = false;

public MFAnalogPanel()
{
Expand All @@ -40,33 +36,23 @@ private void SensitivityTrackBar_ValueChanged(object sender, EventArgs e)
value_Changed(sender, e);
}

public MFAnalogPanel(MobiFlight.Config.AnalogInput analogDevice, List<MobiFlightPin> FreePins)
: this()
public MFAnalogPanel(MobiFlight.Config.AnalogInput analogDevice, List<MobiFlightPin> FreePins): this()
{
//var list = FreePins.Where(s => s.isAnalog == true);
ComboBoxHelper.BindMobiFlightFreePins(mfPinComboBox, FreePins, analogDevice.Pin, true);

if (mfPinComboBox.Items.Count > 0)
{
mfPinComboBox.SelectedIndex = 0;
}

this.analog = analogDevice;
mfPinComboBox.SelectedValue = byte.Parse(analog.Pin);
SensitivityTrackBar.Value = byte.Parse(analog.Sensitivity);

mfPinComboBox.SelectedValue = byte.Parse(analog.Pin);
textBox1.Text = analog.Name;
setValues();

SensitivityTrackBar.Value = byte.Parse(analog.Sensitivity);
initialized = true;
}

private void value_Changed(object sender, EventArgs e)
{
if (!initialized) return;

setValues();

if (Changed != null)
Changed(analog, new EventArgs());
}
Expand All @@ -75,7 +61,6 @@ private void setValues()
{
analog.Pin = mfPinComboBox.SelectedValue.ToString();
analog.Name = textBox1.Text;

analog.Sensitivity = SensitivityTrackBar.Value.ToString();
}
}
Expand Down
112 changes: 38 additions & 74 deletions UI/Panels/Device/MFAnalogPanel.resx
Expand Up @@ -117,6 +117,20 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="mfPinLabel.Location" type="System.Drawing.Point, System.Drawing">
<value>61, 22</value>
</data>
<data name="mfPinLabel.Size" type="System.Drawing.Size, System.Drawing">
<value>75, 18</value>
</data>
<assembly alias="mscorlib" name="mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="mfPinLabel.TabIndex" type="System.Int32, mscorlib">
<value>14</value>
</data>
<data name="mfPinLabel.Text" xml:space="preserve">
<value>Pin</value>
</data>
<data name="&gt;&gt;mfPinLabel.Name" xml:space="preserve">
<value>mfPinLabel</value>
</data>
Expand All @@ -129,6 +143,18 @@
<data name="&gt;&gt;mfPinLabel.ZOrder" xml:space="preserve">
<value>0</value>
</data>
<data name="mfPinComboBox.Location" type="System.Drawing.Point, System.Drawing">
<value>18, 19</value>
</data>
<data name="mfPinComboBox.MaxLength" type="System.Int32, mscorlib">
<value>2</value>
</data>
<data name="mfPinComboBox.Size" type="System.Drawing.Size, System.Drawing">
<value>37, 21</value>
</data>
<data name="mfPinComboBox.TabIndex" type="System.Int32, mscorlib">
<value>13</value>
</data>
<data name="&gt;&gt;mfPinComboBox.Name" xml:space="preserve">
<value>mfPinComboBox</value>
</data>
Expand All @@ -145,14 +171,12 @@
<data name="groupBox1.Dock" type="System.Windows.Forms.DockStyle, System.Windows.Forms">
<value>Top</value>
</data>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="groupBox1.Location" type="System.Drawing.Point, System.Drawing">
<value>0, 0</value>
</data>
<data name="groupBox1.Size" type="System.Drawing.Size, System.Drawing">
<value>205, 54</value>
</data>
<assembly alias="mscorlib" name="mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="groupBox1.TabIndex" type="System.Int32, mscorlib">
<value>1</value>
</data>
Expand All @@ -171,54 +195,6 @@
<data name="&gt;&gt;groupBox1.ZOrder" xml:space="preserve">
<value>2</value>
</data>
<data name="mfPinLabel.Location" type="System.Drawing.Point, System.Drawing">
<value>61, 22</value>
</data>
<data name="mfPinLabel.Size" type="System.Drawing.Size, System.Drawing">
<value>75, 18</value>
</data>
<data name="mfPinLabel.TabIndex" type="System.Int32, mscorlib">
<value>14</value>
</data>
<data name="mfPinLabel.Text" xml:space="preserve">
<value>Pin</value>
</data>
<data name="&gt;&gt;mfPinLabel.Name" xml:space="preserve">
<value>mfPinLabel</value>
</data>
<data name="&gt;&gt;mfPinLabel.Type" xml:space="preserve">
<value>System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;mfPinLabel.Parent" xml:space="preserve">
<value>groupBox1</value>
</data>
<data name="&gt;&gt;mfPinLabel.ZOrder" xml:space="preserve">
<value>0</value>
</data>
<data name="mfPinComboBox.Location" type="System.Drawing.Point, System.Drawing">
<value>18, 19</value>
</data>
<data name="mfPinComboBox.MaxLength" type="System.Int32, mscorlib">
<value>2</value>
</data>
<data name="mfPinComboBox.Size" type="System.Drawing.Size, System.Drawing">
<value>37, 21</value>
</data>
<data name="mfPinComboBox.TabIndex" type="System.Int32, mscorlib">
<value>13</value>
</data>
<data name="&gt;&gt;mfPinComboBox.Name" xml:space="preserve">
<value>mfPinComboBox</value>
</data>
<data name="&gt;&gt;mfPinComboBox.Type" xml:space="preserve">
<value>System.Windows.Forms.ComboBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;mfPinComboBox.Parent" xml:space="preserve">
<value>groupBox1</value>
</data>
<data name="&gt;&gt;mfPinComboBox.ZOrder" xml:space="preserve">
<value>1</value>
</data>
<data name="SensitivityValueLabel.Anchor" type="System.Windows.Forms.AnchorStyles, System.Windows.Forms">
<value>Top, Left, Right</value>
</data>
Expand Down Expand Up @@ -372,6 +348,18 @@
<data name="&gt;&gt;groupBox2.ZOrder" xml:space="preserve">
<value>1</value>
</data>
<data name="textBox1.Anchor" type="System.Windows.Forms.AnchorStyles, System.Windows.Forms">
<value>Top, Left, Right</value>
</data>
<data name="textBox1.Location" type="System.Drawing.Point, System.Drawing">
<value>18, 19</value>
</data>
<data name="textBox1.Size" type="System.Drawing.Size, System.Drawing">
<value>169, 20</value>
</data>
<data name="textBox1.TabIndex" type="System.Int32, mscorlib">
<value>0</value>
</data>
<data name="&gt;&gt;textBox1.Name" xml:space="preserve">
<value>textBox1</value>
</data>
Expand Down Expand Up @@ -411,30 +399,6 @@
<data name="&gt;&gt;groupBox3.ZOrder" xml:space="preserve">
<value>0</value>
</data>
<data name="textBox1.Anchor" type="System.Windows.Forms.AnchorStyles, System.Windows.Forms">
<value>Top, Left, Right</value>
</data>
<data name="textBox1.Location" type="System.Drawing.Point, System.Drawing">
<value>18, 19</value>
</data>
<data name="textBox1.Size" type="System.Drawing.Size, System.Drawing">
<value>169, 20</value>
</data>
<data name="textBox1.TabIndex" type="System.Int32, mscorlib">
<value>0</value>
</data>
<data name="&gt;&gt;textBox1.Name" xml:space="preserve">
<value>textBox1</value>
</data>
<data name="&gt;&gt;textBox1.Type" xml:space="preserve">
<value>System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;textBox1.Parent" xml:space="preserve">
<value>groupBox3</value>
</data>
<data name="&gt;&gt;textBox1.ZOrder" xml:space="preserve">
<value>0</value>
</data>
<metadata name="$this.Localizable" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
Expand Down

0 comments on commit 320c917

Please sign in to comment.