changeset 98:c7afc7824f50

Fixes #218: "Amount too high" warning can result in negative items * Restrict min/max values to a minimum of "0" to stop negatives * Ignore all events from widgets in EquimentAmountControl until we've finished setting it up for the new equipment item * Fire value modified events if the value has been limited * Remove disabling of OK button so that "Edit" dialog enables OK if amount has been limited
author IBBoard <dev@ibboard.co.uk>
date Sat, 21 Nov 2009 15:33:00 +0000
parents e356134d73c4
children acaea18ac0a1
files FrmEditUnitEquipment.cs UI/EquipmentAmountControl.cs
diffstat 2 files changed, 32 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/FrmEditUnitEquipment.cs	Mon Nov 16 20:56:54 2009 +0000
+++ b/FrmEditUnitEquipment.cs	Sat Nov 21 15:33:00 2009 +0000
@@ -43,7 +43,6 @@
 			this.Text = Translation.GetTranslation("FrmEditUnitEquipment", "edit {0} for {1}", equipItem.Name, unit.Name);
 			equipmentAmount.SetUnit(unit);
 			equipmentAmount.SetUnitEquipmentItem(equipItem);
-			bttnOkay.Enabled = false;
 		}
 
 		/// <summary>
@@ -187,15 +186,15 @@
 			this.Close();
 		}
 
-		private void setOkayButton()
+		private void SetOkayButton()
 		{
 			double equipAmount = equipmentAmount.EquipmentAmount;
-			bttnOkay.Enabled = equipAmount > 0 || equipAmount == WarFoundryCore.INFINITY || !equipItem.IsRequired;
+			bttnOkay.Enabled = equipAmount > 0 || !equipItem.IsRequired;
 		}
 
 		private void equipmentAmount_ValueChanged(object sender, EventArgs e)
 		{
-			setOkayButton();
+			SetOkayButton();
 		}
 	}
 }
--- a/UI/EquipmentAmountControl.cs	Mon Nov 16 20:56:54 2009 +0000
+++ b/UI/EquipmentAmountControl.cs	Sat Nov 21 15:33:00 2009 +0000
@@ -53,16 +53,22 @@
 		{
 			if (equip != null)
 			{
+				IgnoreWidgets();
+
 				log.Debug("Equipment update");
 				bool equipIsRatioLimit = UnitEquipmentUtil.IsEquipmentRatioLimited(unit, equip);
 				log.Debug("Equipment is ratio? " + (equipIsRatioLimit ? "yes" : "no"));
 				double maxPercent = RoundPercentage(UnitEquipmentUtil.GetMaxEquipmentPercentage(unit, equip));
+				maxPercent = Math.Max(0, maxPercent);
 				log.Debug("Equipment max percentage: " + maxPercent);
 				double minPercent = RoundPercentage(UnitEquipmentUtil.GetMinEquipmentPercentage(unit, equip));
+				minPercent = Math.Max(0, minPercent);
 				log.Debug("Equipment min percentage: " + minPercent);
 				int maxNumber = UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip);
+				maxNumber = Math.Max(0, maxNumber);
 				log.Debug("Equipment max count: " + maxNumber);
 				int minNumber = UnitEquipmentUtil.GetMinEquipmentCount(unit, equip);
+				minNumber = Math.Max(0, minNumber);
 				log.Debug("Equipment min count: " + minNumber);
 
 				SetUpDownControlMinMaxes(minPercent, maxPercent, minNumber, maxNumber);
@@ -88,6 +94,8 @@
 				rbNumeric.Checked = !equipIsRatioLimit;
 				log.Debug("rbNumeric: " + (rbNumeric.Enabled ? "enabled" : "disabled") + " " + (rbNumeric.Checked ? "checked" : "unchecked"));
 				SetUnitEquipmentItemAmount();
+
+				ListenToWidgets();
 			}
 			else
 			{
@@ -96,14 +104,28 @@
 			}
 		}
 
-		private void SetUpDownControlMinMaxes(double minPercent, double maxPercent, int minNumber, int maxNumber)
+		private void IgnoreWidgets()
 		{
 			percentage.ValueChanged -= percentage_ValueChanged;
 			numeric.ValueChanged -= numeric_ValueChanged;
+			rbEquipAll.CheckedChanged -= rbEquipAll_CheckedChanged;
+			rbNumeric.CheckedChanged -= radioCheckedChanged;
+			rbPercentage.CheckedChanged -= radioCheckedChanged;
+		}
+
+		private void ListenToWidgets()
+		{
+			percentage.ValueChanged += percentage_ValueChanged;
+			numeric.ValueChanged += numeric_ValueChanged;
+			rbEquipAll.CheckedChanged += rbEquipAll_CheckedChanged;
+			rbNumeric.CheckedChanged += radioCheckedChanged;
+			rbPercentage.CheckedChanged += radioCheckedChanged;
+		}
+
+		private void SetUpDownControlMinMaxes(double minPercent, double maxPercent, int minNumber, int maxNumber)
+		{
 			SetUpDownControlMinMax(percentage, minPercent, maxPercent);
 			SetUpDownControlMinMax(numeric, (decimal) minNumber, (decimal) maxNumber);
-			percentage.ValueChanged += percentage_ValueChanged;
-			numeric.ValueChanged += numeric_ValueChanged;
 		}
 
 		private void SetUpDownControlMinMax(NumericUpDown upDownControl, double min, double max)
@@ -259,6 +281,7 @@
 				MessageBox.Show(ParentForm, percentageTooLarge, percentageTooLargeTitle);
 				decEquipAmount = percentage.Maximum;
 				log.Debug("Limited equipment amount to " + decEquipAmount);
+				OnValueChanged();
 			}
 			else if (decEquipAmount < percentage.Minimum)
 			{
@@ -267,6 +290,7 @@
 				MessageBox.Show(ParentForm, percentageTooSmall, percentageTooSmallTitle);
 				decEquipAmount = percentage.Minimum;
 				log.Debug("Limited equipment amount to " + decEquipAmount);
+				OnValueChanged();
 			}
 
 			numeric.Value = CalculateNumericValueFromPercentage(decEquipAmount);
@@ -284,6 +308,7 @@
 				MessageBox.Show(ParentForm, amountTooLarge, amountTooLargeTitle);
 				equipAmount = (int)numeric.Maximum;
 				log.Debug("Limited equipment amount to " + equipAmount);
+				OnValueChanged();
 			}
 			else if (equipAmount < numeric.Minimum)
 			{
@@ -292,6 +317,7 @@
 				MessageBox.Show(ParentForm, amountTooSmall, amountTooSmallTitle);
 				equipAmount = (int) numeric.Minimum;
 				log.Debug("Limited equipment amount to " + equipAmount);
+				OnValueChanged();
 			}
 
 			percentage.Value = CalcualtePercentageValueFromNumber(equipAmount);