changeset 41:4fc87d6e6119

Re #117: Add percentage and number boxes to equipment item dialogs * Add events and value setting in EquipmentAmount widget * Hook up "value changed" event on new equipment form Also fixes adding side of re #114 (Enabling/disabling of "equip all" and number for equipment is wrong) by rebuilding everything
author IBBoard <dev@ibboard.co.uk>
date Sun, 06 Sep 2009 19:31:49 +0000
parents 740350673006
children d06c2e390a14
files FrmNewUnitEquipment.cs UI/EquipmentAmountControl.cs
diffstat 2 files changed, 137 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/FrmNewUnitEquipment.cs	Sun Sep 06 18:01:54 2009 +0000
+++ b/FrmNewUnitEquipment.cs	Sun Sep 06 19:31:49 2009 +0000
@@ -43,6 +43,7 @@
 			this.Text = "New equipment for " + unit.Name;
 			object[] items = Arrays.Subtract(unit.GetAllowedOptionalEquipment(), unit.GetEquipment());
 			listItems.Items.AddRange(items);
+			equipmentAmount.SetUnit(unit);
 		}
 
 		/// <summary>
@@ -129,6 +130,7 @@
 			this.equipmentAmount.Name = "equipmentAmount";
 			this.equipmentAmount.Size = new System.Drawing.Size(155, 77);
 			this.equipmentAmount.TabIndex = 8;
+			this.equipmentAmount.ValueChanged += new System.EventHandler(this.equipmentAmount_ValueChanged);
 			// 
 			// FrmNewUnitEquipment
 			// 
@@ -179,64 +181,28 @@
 			this.Close();
 		}
 
-		private void cbEquipAll_CheckedChanged(object sender, System.EventArgs e)
-		{
-			//numEquipAmount.Enabled = ((UnitEquipmentItem) listItems.SelectedItem).MinNumber != WarFoundryCore.INFINITY && !rbEquipAll.Checked;
-			setOkayButton();
-		}
-
 		private void listItems_SelectedIndexChanged(object sender, System.EventArgs e)
 		{
-			refreshNumber();
-			setOkayButton();
-		}
-
-		private void refreshNumber()
-		{
-			/*
 			if (listItems.SelectedIndex > -1)
 			{
-				UnitEquipmentItem equipItem = (UnitEquipmentItem)listItems.SelectedItem;
-				required = equipItem.IsRequired;
-
-				if (equipItem.IsRatioLimit)
-				{
-					numEquipAmount.Minimum = (decimal) equipItem.MinPercentage;
-					numEquipAmount.Maximum = (decimal) equipItem.MaxPercentage;
-					numEquipAmount.Enabled = true;
-					numEquipAmount.Value = numEquipAmount.Minimum;
-					rbEquipAll.Enabled = false;
-					lblPercent.Visible = true;
-					numEquipAmount.Width = 120;
-				}
-				else
-				{
-					numEquipAmount.Minimum = (decimal)(equipItem.MinNumber != WarFoundryCore.INFINITY ? Math.Min(equipItem.MinNumber, unit.Size) : unit.Size);
-					numEquipAmount.Maximum = (decimal)(equipItem.MaxNumber != WarFoundryCore.INFINITY ? Math.Min(equipItem.MaxNumber, unit.Size) : unit.Size);
-					numEquipAmount.Value = numEquipAmount.Minimum;
-					numEquipAmount.Enabled = equipItem.MinNumber != WarFoundryCore.INFINITY;
-					rbEquipAll.Enabled = equipItem.MinNumber != WarFoundryCore.INFINITY && equipItem.MaxNumber == WarFoundryCore.INFINITY;
-					rbEquipAll.Checked = equipItem.MinNumber == WarFoundryCore.INFINITY;
-					lblPercent.Visible = false;
-					numEquipAmount.Width = 144;
-				}
+				equipmentAmount.SetUnitEquipmentItem((UnitEquipmentItem)listItems.SelectedItem);
 			}
 			else
 			{
-				numEquipAmount.Minimum = 0;
-				numEquipAmount.Value = 0;
-				numEquipAmount.Enabled = false;
-				rbEquipAll.Enabled = false;
-			}*/
+				equipmentAmount.SetUnitEquipmentItem(null);
+			}
+
+			setOkayButton();
 		}
 
 		private void setOkayButton()
 		{
-			bttnOkay.Enabled = listItems.SelectedIndex > -1;// && (rbEquipAll.Checked || (numEquipAmount.Enabled && numEquipAmount.Value > 0));
+			double equipAmount = equipmentAmount.EquipmentAmount;
+			bttnOkay.Enabled = listItems.SelectedIndex > -1 && (equipAmount > 0 || equipAmount == WarFoundryCore.INFINITY);
 		}
 
-		private void numEquipAmount_ValueChanged(object sender, System.EventArgs e)
-		{			
+		private void equipmentAmount_ValueChanged(object sender, EventArgs e)
+		{
 			setOkayButton();
 		}
 	}
--- a/UI/EquipmentAmountControl.cs	Sun Sep 06 18:01:54 2009 +0000
+++ b/UI/EquipmentAmountControl.cs	Sun Sep 06 19:31:49 2009 +0000
@@ -29,7 +29,88 @@
 		public void SetUnitEquipmentItem(UnitEquipmentItem unitEquipment)
 		{
 			equip = unitEquipment;
-			//SetWidgetValues();
+			SetWidgetValues();
+		}
+
+		public event EventHandler ValueChanged;
+
+		private void OnValueChanged()
+		{
+			if (ValueChanged != null)
+			{
+				ValueChanged(this, new EventArgs());
+			}
+		}
+
+		private void SetWidgetValues()
+		{
+			if (equip != null)
+			{
+				if (equip.IsRatioLimit)
+				{
+					double minPercent = equip.MinPercentage;
+					double maxPercent = equip.MaxPercentage;
+					SetUpDownControlMinMaxAndValue(percentage, minPercent, maxPercent);
+					SetUpDownControlMinMaxAndValue(numeric, CalculateNumericValueFromPercentage(minPercent), CalculateNumericValueFromPercentage(maxPercent));
+					rbEquipAll.Enabled = maxPercent == 100;
+
+					if (minPercent == 100)
+					{
+						rbEquipAll.Checked = true;
+						percentage.Enabled = false;
+						rbPercentage.Enabled = false;
+						numeric.Enabled = false;
+						rbNumeric.Enabled = false;
+					}
+					else
+					{
+						rbPercentage.Checked = true;
+						percentage.Enabled = true;
+						rbPercentage.Enabled = true;
+						numeric.Enabled = true;
+						rbNumeric.Enabled = true;
+					}
+				}
+				else
+				{
+					percentage.Enabled = false;
+					rbPercentage.Enabled = false;
+					int minNumber = equip.MinNumber;
+					int maxNumber = equip.MaxNumber;
+					SetUpDownControlMinMaxAndValue(numeric, (decimal) minNumber, (decimal) maxNumber);
+					SetUpDownControlMinMaxAndValue(percentage, CalcualtePercentageValueFromNumber(minNumber), CalcualtePercentageValueFromNumber(maxNumber));
+					rbEquipAll.Enabled = maxNumber == WarFoundryCore.INFINITY;
+					
+					if (minNumber == WarFoundryCore.INFINITY)
+					{
+						rbEquipAll.Checked = true;
+						numeric.Enabled = false;
+						rbNumeric.Enabled = false;
+					}
+					else
+					{
+						rbNumeric.Checked = true;
+						numeric.Enabled = true;
+						rbNumeric.Enabled = true;
+					}
+				}
+			}
+			else
+			{
+				Enabled = false;
+			}
+		}
+
+		private void SetUpDownControlMinMaxAndValue(NumericUpDown upDownControl, double min, double max)
+		{
+			SetUpDownControlMinMaxAndValue(upDownControl, (decimal)min, (decimal)max);
+		}
+
+		private void SetUpDownControlMinMaxAndValue(NumericUpDown upDownControl, decimal min, decimal max)
+		{
+			upDownControl.Minimum = (min == WarFoundryCore.INFINITY ? unit.Size : min);
+			upDownControl.Maximum = (max == WarFoundryCore.INFINITY ? unit.Size : max);
+			upDownControl.Value = upDownControl.Value;
 		}
 
 		private void rbEquipAll_CheckedChanged(object sender, EventArgs e)
@@ -43,18 +124,54 @@
 				numeric.Value = unit.Size;
 				percentage.Value = 100;
 			}
+
+			OnValueChanged();
 		}
 
 		private void percentage_ValueChanged(object sender, EventArgs e)
 		{
-			double percent = (double) percentage.Value;
-			numeric.Value = (decimal) IBBMath.Round(unit.Size * (percent / 100.0), equip.RoundNumberUp);
+			SetNumericValueFromPercentage();
 			rbEquipAll.Checked = (percentage.Value == 100);
+			OnValueChanged();
+		}
+
+		private void SetNumericValueFromPercentage()
+		{
+			double percent = (double)percentage.Value;
+			numeric.Value = CalculateNumericValueFromPercentage(percent);
+		}
+
+		private decimal CalculateNumericValueFromPercentage(double percent)
+		{
+			return (decimal) IBBMath.Round(unit.Size * (percent / 100.0), equip.RoundNumberUp);
 		}
 
 		private void numeric_ValueChanged(object sender, EventArgs e)
 		{
-			percentage.Value = (unit.Size / numeric.Value) * 100;
+			SetPercentageValueFromNumeric();
+			OnValueChanged();
+		}
+
+		private void SetPercentageValueFromNumeric()
+		{
+			int number = (int)numeric.Value;
+			percentage.Value = CalcualtePercentageValueFromNumber(number);
+		}
+
+		private decimal CalcualtePercentageValueFromNumber(int number)
+		{
+			decimal percent = 0;
+
+			if (number > 0)
+			{
+				percent = (decimal) Math.Round((number / (unit.Size * 1.0)) * 100, 1);
+			}
+			else if (number == WarFoundryCore.INFINITY)
+			{
+				percent = 100;
+			}
+
+			return percent;
 		}
 
 		public double EquipmentAmount
@@ -71,9 +188,13 @@
 				{
 					val = (double) percentage.Value / 100.0;
 				}
+				else if (rbEquipAll.Checked)
+				{
+					val = WarFoundryCore.INFINITY;
+				}
 				else
 				{
-					val = WarFoundryCore.INFINITY;
+					val = 0;
 				}
 
 				return val;