changeset 63:c2d79b4209e3

* FrmAddEquipment.cs: Re #60: Add UI to add/remove/edit weapons in GTK * Cache more values in the controller so that we don't rely on the UI as a the model/data store * Move enable/disable of amount controls to controller (assumes minimum numeric and percentage, so implementation has its own custom implementation to handle "Equip All") * AddEquipmentUIControl.cs: * IAddEquipmentUI.cs:
author IBBoard <dev@ibboard.co.uk>
date Mon, 30 Aug 2010 19:44:35 +0000
parents f733073967a2
children e3fe48c4d794
files FrmAddEquipment.cs UIControl/AddEquipmentUIControl.cs UIControl/Interfaces/IAddEquipmentUI.cs
diffstat 3 files changed, 107 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/FrmAddEquipment.cs	Sun Aug 29 15:44:20 2010 +0000
+++ b/FrmAddEquipment.cs	Mon Aug 30 19:44:35 2010 +0000
@@ -92,40 +92,12 @@
 			{
 				rbEquipNumeric.Active = true;
 			}
-			
-			SetEnabledState();
 		}
 
 		public void SetUnitEquipmentLimitsEnabled(bool isEnabled)
 		{
-			limitsEnabled = isEnabled;
-			SetEnabledState();
-		}
-		
-		private void SetEnabledState()
-		{
-			rbEquipNumeric.Sensitive = (limitsEnabled && !ratioLimited);
-			numericAmount.Sensitive = (limitsEnabled && !ratioLimited);
-			double minPercentage = GetMinPercentage();
-			rbEquipPercent.Sensitive = limitsEnabled && minPercentage != 100;
-			percentageAmount.Sensitive = limitsEnabled && minPercentage != 100;
-			double maxPercentage = GetMaxPercentage();
-			rbEquipAll.Sensitive = limitsEnabled && ratioLimited && maxPercentage == 100;
-			lblEquipAll.Sensitive = limitsEnabled && ratioLimited && maxPercentage == 100;
-		}
-		
-		private double GetMaxPercentage()
-		{
-			double min, max;
-			percentageAmount.GetRange(out min, out max);
-			return max;
-		}
-
-		private double GetMinPercentage()
-		{
-			double min, max;
-			percentageAmount.GetRange(out min, out max);
-			return min;
+			SetNumericAmountEnabledState(isEnabled);
+			SetPercentageAmountEnabledState(isEnabled);
 		}
 
 		public bool ShowControl()
@@ -163,6 +135,45 @@
 			OnUnitEquipmentAmountTypeChanged();
 		}
 		
+		public void SetNumericAmountEnabledState (bool enabled)
+		{
+			rbEquipNumeric.Sensitive = enabled;
+			numericAmount.Sensitive = enabled;
+		}		
+		
+		public void SetPercentageAmountEnabledState(bool enabled)
+		{
+			if (enabled)
+			{
+				double minPercentage = GetMinPercentage();
+				rbEquipPercent.Sensitive = minPercentage != 100;
+				percentageAmount.Sensitive = minPercentage != 100;
+				double maxPercentage = GetMaxPercentage();
+				rbEquipAll.Sensitive = ratioLimited && maxPercentage == 100;
+				lblEquipAll.Sensitive = ratioLimited && maxPercentage == 100;
+			}
+			else
+			{
+				rbEquipPercent.Sensitive = false;
+				percentageAmount.Sensitive = false;
+				rbEquipAll.Sensitive = false;
+				lblEquipAll.Sensitive = false;
+			}
+		}
+
+		private double GetMaxPercentage()
+		{
+			double min, max;
+			percentageAmount.GetRange(out min, out max);
+			return max;
+		}
+
+		private double GetMinPercentage()
+		{
+			double min, max;
+			percentageAmount.GetRange(out min, out max);
+			return min;
+		}
 		
 		public UnitEquipmentItem SelectedUnitEquipmentItem
 		{
--- a/UIControl/AddEquipmentUIControl.cs	Sun Aug 29 15:44:20 2010 +0000
+++ b/UIControl/AddEquipmentUIControl.cs	Mon Aug 30 19:44:35 2010 +0000
@@ -13,9 +13,15 @@
 {
 	public class AddEquipmentUIControl
 	{
-		private Unit unit;
 		private CommandStack commandStack;
 		private IAddEquipmentUI ui;
+		private Unit unit;
+		private UnitEquipmentItem equipItem;
+		private double minPercentage, maxPercentage;
+		private int minNumber, maxNumber;
+		private bool isRatioAmount;
+		private double equipmentAmount;
+		
 		
 		public AddEquipmentUIControl(Unit unit, CommandStack commandStack)
 		{
@@ -38,25 +44,24 @@
 
 		private void HandleUnitEquipmentAmountChanged()
 		{
-			ui.SetOkayEnabledState(ui.SelectedUnitEquipmentItem != null && HasNonZeroEquipmentAmount());
+			ui.SetOkayEnabledState(equipItem != null && HasNonZeroEquipmentAmount());
+			isRatioAmount = ui.IsRatioEquipmentAmount;
 			
-			if (ui.IsRatioEquipmentAmount)
+			if (isRatioAmount)
 			{
-				SetEquipmentAmountsFromPercentage(ui.EquipmentPercentageAmount);
+				double equipmentAmount = ui.EquipmentPercentageAmount;
+				SetEquipmentAmountsFromPercentage(equipmentAmount);
 			}
 			else
 			{
-				SetEquipmentAmountsFromNumber(ui.EquipmentNumericAmount);
+				int equipmentIntAmount = ui.EquipmentNumericAmount;
+				equipmentAmount = equipmentIntAmount;
+				SetEquipmentAmountsFromNumber(equipmentIntAmount);
 			}
 		}
 		
 		private void SetEquipmentAmountsFromPercentage(double equipAmount)
-		{
-			UnitEquipmentItem currEquipItem = ui.SelectedUnitEquipmentItem;
-			
-			double maxPercentage = GetMaxPercentageLimit(currEquipItem);
-			double minPercentage = GetMinPercentageLimit(currEquipItem);
-			
+		{			
 			if (equipAmount > maxPercentage)
 			{
 				string percentageTooLarge = Translation.GetTranslation("equipPercentageTooLarge", "the current percentage ({0}%) was larger than the maximum for the equipment item ({1}%) - the maximum value will be used instead", equipAmount, maxPercentage);
@@ -72,22 +77,18 @@
 				equipAmount = minPercentage;
 			}
 			
-			ui.EquipmentNumericAmount = CalculateNumericValueFromPercentage(equipAmount, currEquipItem);
+			ui.EquipmentNumericAmount = CalculateNumericValueFromPercentage(equipAmount);
 			ui.EquipmentPercentageAmount = equipAmount;
 		}
 		
-		private int CalculateNumericValueFromPercentage(double percent, UnitEquipmentItem equip)
+		private int CalculateNumericValueFromPercentage(double percent)
 		{
-			int calcedAmount = (int)CustomMath.IBBMath.Round((double)(unit.Size * (percent / 100)), equip.RoundNumberUp);
-			return Math.Min(Math.Max(calcedAmount, GetMinNumericLimit(equip)), GetMaxNumericLimit(equip));
+			int calcedAmount = (int)CustomMath.IBBMath.Round((unit.Size * (percent / 100.0)), equipItem.RoundNumberUp);
+			return Math.Min(Math.Max(calcedAmount, minNumber), maxNumber);
 		}
 
 		private void SetEquipmentAmountsFromNumber(int equipAmount)
-		{
-			UnitEquipmentItem currEquipItem = ui.SelectedUnitEquipmentItem;
-			int maxNumber = GetMaxNumericLimit(currEquipItem);
-			int minNumber = GetMinNumericLimit(currEquipItem);
-			
+		{			
 			if (equipAmount > maxNumber)
 			{
 				string amountTooLarge = Translation.GetTranslation("equipNumberTooLarge", "the current amount ({0}) was larger than the maximum for the equipment item ({1}) - the maximum value will be used instead", equipAmount, maxNumber);
@@ -103,14 +104,14 @@
 				equipAmount = minNumber;
 			}
 			
-			ui.EquipmentPercentageAmount = CalcualtePercentageValueFromNumber(equipAmount, currEquipItem);
+			ui.EquipmentPercentageAmount = CalcualtePercentageValueFromNumber(equipAmount);
 			ui.EquipmentNumericAmount = equipAmount;
 		}
 		
-		private double CalcualtePercentageValueFromNumber(int number, UnitEquipmentItem equip)
+		private double CalcualtePercentageValueFromNumber(int number)
 		{
 			double calcedAmount = RoundPercentage(CustomMath.IBBMath.Percentage(number, unit.Size));
-			return Math.Min(Math.Max(calcedAmount, GetMinPercentageLimit(equip)), GetMaxPercentageLimit(equip));
+			return Math.Min(Math.Max(calcedAmount, minPercentage), maxPercentage);
 		}
 
 		//TODO Make abstract
@@ -121,25 +122,37 @@
 
 		private void HandleUiUnitEquipmentItemChoiceChanged(UnitEquipmentItem equip)
 		{
+			equipItem = equip;
+			
 			if (equip != null)
 			{
 				bool equipIsRatioLimit = UnitEquipmentUtil.IsEquipmentRatioLimited(unit, equip);
-				double maxPercent = GetMaxPercentageLimit(equip);
-				double minPercent = GetMinPercentageLimit(equip);
-				int maxNumber = GetMaxNumericLimit(equip);
-				int minNumber = GetMinNumericLimit(equip);
+				maxPercentage = GetMaxPercentageLimit(equip);
+				minPercentage = GetMinPercentageLimit(equip);
+				maxNumber = GetMaxNumericLimit(equip);
+				minNumber = GetMinNumericLimit(equip);
 			
-				ui.SetUnitEquipmentLimits(equipIsRatioLimit, minPercent, maxPercent, minNumber, maxNumber);
+				ui.SetUnitEquipmentLimits(equipIsRatioLimit, minPercentage, maxPercentage, minNumber, maxNumber);
 				ui.SetUnitEquipmentLimitsEnabled(true);
 				ui.SetOkayEnabledState(HasNonZeroEquipmentAmount());
+				SetEquipmentAmountControlEnabledStates(equipIsRatioLimit);
 			}
 			else
 			{
-				ui.SetUnitEquipmentLimits(true, 0, 0, 0, 0);
+				maxPercentage = minPercentage = 0;
+				maxNumber = minNumber = 0;
+				ui.SetUnitEquipmentLimits(false, minPercentage, maxPercentage, minNumber, maxNumber);
 				ui.SetUnitEquipmentLimitsEnabled(false);
 				ui.SetOkayEnabledState(false);
 			}
+		}		
+
+		private void SetEquipmentAmountControlEnabledStates(bool ratioLimited)
+		{
+			ui.SetNumericAmountEnabledState(!ratioLimited);
+			ui.SetPercentageAmountEnabledState(true);
 		}
+
 		private double GetMaxPercentageLimit(UnitEquipmentItem equip)
 		{
 			double maxPercent = RoundPercentage(UnitEquipmentUtil.GetMaxEquipmentPercentage(unit, equip));
@@ -167,7 +180,7 @@
 		{
 			bool nonZero;
 			
-			if (ui.IsRatioEquipmentAmount)
+			if (isRatioAmount)
 			{
 				nonZero = (ui.EquipmentPercentageAmount > 0);
 			}
@@ -190,16 +203,14 @@
 			bool okayed = ui.ShowControl();
 			
 			if (okayed)
-			{
-				UnitEquipmentItem equipItem = ui.SelectedUnitEquipmentItem;
-			
-				if (ui.IsRatioEquipmentAmount)
+			{			
+				if (isRatioAmount)
 				{
-					commandStack.Execute(new SetUnitEquipmentRatioAmountCommand(unit, equipItem, ui.EquipmentPercentageAmount));
+					commandStack.Execute(new SetUnitEquipmentRatioAmountCommand(unit, equipItem, equipmentAmount));
 				}
 				else
 				{
-					commandStack.Execute(new SetUnitEquipmentNumericAmountCommand(unit, equipItem, ui.EquipmentNumericAmount));
+					commandStack.Execute(new SetUnitEquipmentNumericAmountCommand(unit, equipItem, (int)equipmentAmount));
 				}
 			}
 			
--- a/UIControl/Interfaces/IAddEquipmentUI.cs	Sun Aug 29 15:44:20 2010 +0000
+++ b/UIControl/Interfaces/IAddEquipmentUI.cs	Mon Aug 30 19:44:35 2010 +0000
@@ -54,7 +54,9 @@
 		void SetUnitEquipmentLimits(bool isRatioLimit, double minPercent, double maxPercent, int minNumber, int maxNumber);
 		
 		/// <summary>
-		/// Sets whether the unit equipment limit UI components should be enabled and able to accept input.
+		/// Sets whether the unit equipment limit UI components should be enabled and able to accept input. This will 
+		/// generally pass the values on to the <see cref="SetNumericAmountEnabledState(bool)"/> and
+		/// <see cref="SetPercentageAmountEnabledState(bool)"/> methods and is included for convenience
 		/// </summary>
 		/// <param name='isEnabled'>
 		/// <code>True</code> if the UI components should accept input, else <code>false</code>
@@ -108,6 +110,22 @@
 		/// <code>true</code> to enable the button, else <code>false</code>
 		/// </param>
 		void SetOkayEnabledState(bool enabled);
+		
+		/// <summary>
+		/// Sets the state of the numeric equipment amount control.
+		/// </summary>
+		/// <param name='enabled'>
+		/// <code>true</code> to enable the control, else <code>false</code>
+		/// </param>
+		void SetNumericAmountEnabledState(bool enabled);
+		
+		/// <summary>
+		/// Sets the state of the percentage equipment amount control.
+		/// </summary>
+		/// <param name='enabled'>
+		/// <code>true</code> to enable the control, else <code>false</code>
+		/// </param>
+		void SetPercentageAmountEnabledState(bool enabled);
 	}
 }