diff UIControl/AddEquipmentUIControl.cs @ 66:100626381159

Re #60: Add UI to add/remove/edit weapons in GTK * Rip apart UI controls and put all the common code in a base class * Add additional interfaces for common UI behaviour between equipment widgets Not tested, but should provide a base for Replace widget
author IBBoard <dev@ibboard.co.uk>
date Sat, 04 Sep 2010 20:11:05 +0000
parents 77448375d2f9
children 7028e24b67ec
line wrap: on
line diff
--- a/UIControl/AddEquipmentUIControl.cs	Sat Sep 04 10:25:39 2010 +0000
+++ b/UIControl/AddEquipmentUIControl.cs	Sat Sep 04 20:11:05 2010 +0000
@@ -11,113 +11,24 @@
 using IBBoard.Lang;
 namespace IBBoard.WarFoundry.GUI.GTK.UIControl
 {
-	public class AddEquipmentUIControl
-	{
-		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)
-		{
-			this.unit = unit;
-			this.commandStack = commandStack;
-			SetupUI();
-		}
-		
-		private void SetupUI()
+	public class AddEquipmentUIControl : AbstractBaseEquipmentUIControl<IAddEquipmentUI>
+	{		
+		public AddEquipmentUIControl(Unit unit, CommandStack commandStack) : base(unit, commandStack)
 		{
-			CreateEquipmentUI();
-			ui.SetUnitEquipmentLimitsEnabled(false);
-			UnitEquipmentItem[] items = Arrays.Subtract(UnitEquipmentUtil.GetAllowedEquipmentItems(unit), unit.GetEquipment());
-			ui.SetUnitEquipmentItems(items);
-			ui.SetOkayEnabledState(false);
-			ui.UnitEquipmentItemChoiceChanged += HandleUiUnitEquipmentItemChoiceChanged;
-			ui.UnitEquipmentAmountChanged += HandleUnitEquipmentAmountChanged;
-			ui.UnitEquipmentAmountTypeChanged += HandleUnitEquipmentAmountChanged;
-		}
-
-		private void HandleUnitEquipmentAmountChanged()
-		{
-			ui.SetOkayEnabledState(equipItem != null && HasNonZeroEquipmentAmount());
-			isRatioAmount = ui.IsRatioEquipmentAmount;
-			
-			if (isRatioAmount)
-			{
-				equipmentAmount = ui.EquipmentPercentageAmount;
-				SetEquipmentAmountsFromPercentage(equipmentAmount);
-			}
-			else
-			{
-				int equipmentIntAmount = ui.EquipmentNumericAmount;
-				equipmentAmount = equipmentIntAmount;
-				SetEquipmentAmountsFromNumber(equipmentIntAmount);
-			}
+			//Do nothing extra
 		}
 		
-		private void SetEquipmentAmountsFromPercentage(double equipAmount)
-		{			
-			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);
-				string percentageTooLargeTitle = Translation.GetTranslation("equipPercentageTooLargeTitle", "equipment percentage too large");
-				//				MessageBox.Show(ParentForm, percentageTooLarge, percentageTooLargeTitle);
-				equipAmount = maxPercentage;
-			}
-			else if (equipAmount < minPercentage)
-			{
-				string percentageTooSmall = Translation.GetTranslation("equipPercentageTooSmall", "the current percentage ({0}%) was smaller than the minimum for the equipment item ({1}%) - the minimum value will be used instead", equipAmount, minPercentage);
-				string percentageTooSmallTitle = Translation.GetTranslation("equipPercentageTooSmallTitle", "equipment percentage too small");
-		//				MessageBox.Show(ParentForm, percentageTooSmall, percentageTooSmallTitle);
-				equipAmount = minPercentage;
-			}
-			
-			ui.EquipmentNumericAmount = CalculateNumericValueFromPercentage(equipAmount);
-			ui.EquipmentPercentageAmount = equipAmount;
+		//TODO Make abstract
+		protected override IAddEquipmentUI CreateEquipmentUI()
+		{
+			return new FrmAddEquipment();
 		}
 		
-		private int CalculateNumericValueFromPercentage(double percent)
-		{
-			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)
+		protected override void CompleteUISetup()
 		{			
-			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);
-				string amountTooLargeTitle = Translation.GetTranslation("equipNumberTooLargeTitle", "equipment amount too large");
-				//MessageBox.Show(ParentForm, amountTooLarge, amountTooLargeTitle);
-				equipAmount = maxNumber;
-			}
-			else if (equipAmount < minNumber)
-			{
-				string amountTooSmall = Translation.GetTranslation("equipNumberTooSmall", "the current amount ({0}) was smaller than the minimum for the equipment item ({1}) - the minimum value will be used instead", equipAmount, minNumber);
-				string amountTooSmallTitle = Translation.GetTranslation("equipNumberTooSmallTitle", "equipment amount too small");
-				//MessageBox.Show(ParentForm, amountTooSmall, amountTooSmallTitle);
-				equipAmount = minNumber;
-			}
-			
-			ui.EquipmentPercentageAmount = CalcualtePercentageValueFromNumber(equipAmount);
-			ui.EquipmentNumericAmount = equipAmount;
-		}
-		
-		private double CalcualtePercentageValueFromNumber(int number)
-		{
-			double calcedAmount = RoundPercentage(CustomMath.IBBMath.Percentage(number, unit.Size));
-			return Math.Min(Math.Max(calcedAmount, minPercentage), maxPercentage);
-		}
-
-		//TODO Make abstract
-		protected void CreateEquipmentUI()
-		{
-			ui = new FrmAddEquipment();
+			UnitEquipmentItem[] items = Arrays.Subtract(UnitEquipmentUtil.GetAllowedEquipmentItems(unit), unit.GetEquipment());
+			ui.SetUnitEquipmentItems(items);
+			ui.UnitEquipmentItemChoiceChanged += HandleUiUnitEquipmentItemChoiceChanged;
 		}
 
 		private void HandleUiUnitEquipmentItemChoiceChanged(UnitEquipmentItem equip)
@@ -135,7 +46,7 @@
 				ui.SetUnitEquipmentLimits(equipIsRatioLimit, minPercentage, maxPercentage, minNumber, maxNumber);
 				ui.SetUnitEquipmentLimitsEnabled(true);
 				ui.SetOkayEnabledState(HasNonZeroEquipmentAmount());
-				SetEquipmentAmountControlEnabledStates(equipIsRatioLimit);
+				SetEquipmentAmountControlEnabledStates();
 			}
 			else
 			{
@@ -145,76 +56,18 @@
 				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));
-			return Math.Max(0, maxPercent);
-		}
-		
-		private double GetMinPercentageLimit(UnitEquipmentItem equip)
-		{
-			double minPercent = RoundPercentage(UnitEquipmentUtil.GetMinEquipmentPercentage(unit, equip));
-			return Math.Max(0, minPercent);
-		}
-		
-		private int GetMaxNumericLimit(UnitEquipmentItem equip)
-		{
-			int maxNumber = UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip);
-			return Math.Max(0, maxNumber);
-		}
-		private int GetMinNumericLimit(UnitEquipmentItem equip)
-		{
-			int minNumber = UnitEquipmentUtil.GetMinEquipmentCount(unit, equip);
-			return Math.Max(0, minNumber);
 		}
 
-		private bool HasNonZeroEquipmentAmount()
+		protected override void DoProcessing()
 		{
-			bool nonZero;
-			
 			if (isRatioAmount)
 			{
-				nonZero = (ui.EquipmentPercentageAmount > 0);
+				commandStack.Execute(new SetUnitEquipmentRatioAmountCommand(unit, equipItem, equipmentAmount));
 			}
-
 			else
 			{
-				nonZero = (ui.EquipmentNumericAmount > 0);
+				commandStack.Execute(new SetUnitEquipmentNumericAmountCommand(unit, equipItem, (int)equipmentAmount));
 			}
-			
-			return nonZero;
-		}
-
-		private double RoundPercentage(double percent)
-		{
-			return Math.Round(percent, 1);
-		}
-
-		public void Show()
-		{
-			bool okayed = ui.ShowControl();
-			
-			if (okayed)
-			{			
-				if (isRatioAmount)
-				{
-					commandStack.Execute(new SetUnitEquipmentRatioAmountCommand(unit, equipItem, equipmentAmount));
-				}
-				else
-				{
-					commandStack.Execute(new SetUnitEquipmentNumericAmountCommand(unit, equipItem, (int)equipmentAmount));
-				}
-			}
-			
-			ui.Dispose();
 		}
 	}
 }