changeset 82:9dc22147c2db

Re #204: Use new limits in WinForms UI * Convert equipment amount control over to new limits
author IBBoard <dev@ibboard.co.uk>
date Thu, 29 Oct 2009 20:49:44 +0000
parents e749b748e7ea
children 6d5cb8c7b6ed
files UI/EquipmentAmountControl.cs
diffstat 1 files changed, 22 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- a/UI/EquipmentAmountControl.cs	Mon Oct 26 20:56:09 2009 +0000
+++ b/UI/EquipmentAmountControl.cs	Thu Oct 29 20:49:44 2009 +0000
@@ -9,6 +9,7 @@
 using IBBoard.Limits;
 using IBBoard.WarFoundry.API;
 using IBBoard.WarFoundry.API.Objects;
+using IBBoard.WarFoundry.API.Util;
 
 namespace IBBoard.WarFoundry.GUI.WinForms.UI
 {
@@ -16,6 +17,7 @@
 	{
 		private Unit unit;
 		private UnitEquipmentItem equip;
+		public event EventHandler ValueChanged;
 
 		public EquipmentAmountControl()
 		{
@@ -34,8 +36,6 @@
 			SetUnitEquipmentItemAmount();
 		}
 
-		public event EventHandler ValueChanged;
-
 		private void OnValueChanged()
 		{
 			if (ValueChanged != null)
@@ -48,62 +48,23 @@
 		{
 			if (equip != null)
 			{
-				AbstractLimit limit = unit.UnitType.GetEquipmentSlotLimit(equip.SlotName);
+				bool equipIsRatioLimit = UnitEquipmentUtil.IsEquipmentRatioLimited(unit, equip);
+				double maxPercent = UnitEquipmentUtil.GetMaxEquipmentPercentage(unit, equip);
+				double minPercent = UnitEquipmentUtil.GetMinEquipmentPercentage(unit, equip);
+				int maxNumber = UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip);
+				int minNumber = UnitEquipmentUtil.GetMinEquipmentCount(unit, equip);
 
-				if (equip.IsRatioLimit && limit is IPercentageLimit)
-				{
-					double minPercent = ((IPercentageLimit)equip.MinLimit).Percentage;
-					double maxPercent = ((IPercentageLimit)equip.MaxLimit).Percentage;
-					int minNumber = (int) CalculateNumericValueFromPercentage(minPercent);
-					int maxNumber = (int) CalculateNumericValueFromPercentage(maxPercent);
-
-					rbEquipAll.Enabled = maxPercent == 100;
-					SetUpDownControlMinMaxes(minPercent, maxPercent, minNumber, maxNumber);
-					SetEquipmentAmountsFromPercentage(minPercent);
+				SetUpDownControlMinMaxes(minPercent, maxPercent, minNumber, maxNumber);
+				SetEquipmentAmountsFromPercentage(equipIsRatioLimit ? minPercent : minNumber);
 
-					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
-				{
-					int minNumber = equip.MinLimit.GetLimit(unit.Size);
-					int maxNumber = GetMaxNumber(limit);
-					double minPercent = (double) CalcualtePercentageValueFromNumber(minNumber);
-					double maxPercent = (double) CalcualtePercentageValueFromNumber(maxNumber);
-
-					percentage.Enabled = false;
-					rbPercentage.Enabled = false;
-					rbEquipAll.Enabled = maxNumber == WarFoundryCore.INFINITY;
-
-					SetUpDownControlMinMaxes(minPercent, maxPercent, minNumber, maxNumber);
-					SetEquipmentAmountsFromNumber(minNumber);
-
-					if (minNumber == WarFoundryCore.INFINITY)
-					{
-						numeric.Enabled = false;
-						rbNumeric.Enabled = false;
-					}
-					else
-					{
-						rbNumeric.Checked = true;
-						numeric.Enabled = true;
-						rbNumeric.Enabled = true;
-					}
-				}
+				rbEquipAll.Enabled = equipIsRatioLimit && maxPercent == 100;
+				rbEquipAll.Checked = equipIsRatioLimit && minPercent == 100;
+				percentage.Enabled = equipIsRatioLimit;
+				rbPercentage.Enabled = percentage.Enabled;
+				rbPercentage.Checked = equipIsRatioLimit && !rbEquipAll.Checked;
+				numeric.Enabled = !equipIsRatioLimit || minPercent != 100;
+				rbNumeric.Enabled = numeric.Enabled;
+				rbNumeric.Checked = !equipIsRatioLimit;
 			}
 			else
 			{
@@ -111,19 +72,6 @@
 			}
 		}
 
-		private int GetMaxNumber(AbstractLimit limit)
-		{
-			int maxNumber = equip.MaxLimit.GetLimit(unit.Size);
-
-			if (!(limit is UnlimitedLimit))
-			{
-				int slotMax = limit.GetLimit(unit.Size) - unit.GetEquipmentAmountInSlot(equip.SlotName);
-				maxNumber = Math.Min(slotMax, maxNumber);
-			}
-
-			return maxNumber;
-		}
-
 		private void SetUpDownControlMinMaxes(double minPercent, double maxPercent, int minNumber, int maxNumber)
 		{
 			percentage.ValueChanged -= percentage_ValueChanged;
@@ -141,8 +89,8 @@
 
 		private void SetUpDownControlMinMax(NumericUpDown upDownControl, decimal min, decimal max)
 		{
-			upDownControl.Minimum = (min == WarFoundryCore.INFINITY ? unit.Size : min);
-			upDownControl.Maximum = (max == WarFoundryCore.INFINITY ? unit.Size : max);
+			upDownControl.Minimum = min;
+			upDownControl.Maximum = max;
 		}
 
 		private void rbEquipAll_CheckedChanged(object sender, EventArgs e)
@@ -192,18 +140,7 @@
 
 		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;
+			return (decimal) Math.Round((number / (unit.Size * 1.0)) * 100, 1);
 		}
 
 		public double EquipmentAmount
@@ -222,7 +159,7 @@
 				}
 				else if (rbEquipAll.Checked)
 				{
-					val = WarFoundryCore.INFINITY;
+					val = 100;
 				}
 				else
 				{
@@ -237,7 +174,7 @@
 		{
 			get
 			{
-				return rbPercentage.Checked;
+				return !rbNumeric.Checked;
 			}
 		}