changeset 62:f733073967a2

Re #60: Add UI to add/remove/edit weapons in GTK * Add initial code to keep numeric and percentage in sync TODO: Allow users to change the non-selected one (e.g. select percentage but set the percentage by incrementing the number)
author IBBoard <dev@ibboard.co.uk>
date Sun, 29 Aug 2010 15:44:20 +0000
parents e7ad676a7344
children c2d79b4209e3
files FrmAddEquipment.cs UIControl/AddEquipmentUIControl.cs UIControl/Interfaces/IAddEquipmentUI.cs
diffstat 3 files changed, 115 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/FrmAddEquipment.cs	Sun Aug 29 15:09:34 2010 +0000
+++ b/FrmAddEquipment.cs	Sun Aug 29 15:44:20 2010 +0000
@@ -188,6 +188,11 @@
 			{
 				return (int)numericAmount.Value;
 			}
+			
+			set
+			{
+				numericAmount.Value = value;
+			}
 		}
 		
 		
@@ -208,6 +213,11 @@
 				
 				return percent;
 			}
+			
+			set
+			{
+				percentageAmount.Value = value;
+			}
 		}
 	}
 }
--- a/UIControl/AddEquipmentUIControl.cs	Sun Aug 29 15:09:34 2010 +0000
+++ b/UIControl/AddEquipmentUIControl.cs	Sun Aug 29 15:44:20 2010 +0000
@@ -7,6 +7,8 @@
 using IBBoard.WarFoundry.API.Util;
 using IBBoard.WarFoundry.GUI.GTK.UIControl.Interfaces;
 using IBBoard.WarFoundry.API.Commands;
+using CustomMath = IBBoard.CustomMath;
+using IBBoard.Lang;
 namespace IBBoard.WarFoundry.GUI.GTK.UIControl
 {
 	public class AddEquipmentUIControl
@@ -34,11 +36,83 @@
 			ui.UnitEquipmentAmountTypeChanged += HandleUnitEquipmentAmountChanged;
 		}
 
-		private void HandleUnitEquipmentAmountChanged ()
+		private void HandleUnitEquipmentAmountChanged()
 		{
 			ui.SetOkayEnabledState(ui.SelectedUnitEquipmentItem != null && HasNonZeroEquipmentAmount());
+			
+			if (ui.IsRatioEquipmentAmount)
+			{
+				SetEquipmentAmountsFromPercentage(ui.EquipmentPercentageAmount);
+			}
+			else
+			{
+				SetEquipmentAmountsFromNumber(ui.EquipmentNumericAmount);
+			}
+		}
+		
+		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);
+				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, currEquipItem);
+			ui.EquipmentPercentageAmount = equipAmount;
 		}
 		
+		private int CalculateNumericValueFromPercentage(double percent, UnitEquipmentItem equip)
+		{
+			int calcedAmount = (int)CustomMath.IBBMath.Round((double)(unit.Size * (percent / 100)), equip.RoundNumberUp);
+			return Math.Min(Math.Max(calcedAmount, GetMinNumericLimit(equip)), GetMaxNumericLimit(equip));
+		}
+
+		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);
+				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, currEquipItem);
+			ui.EquipmentNumericAmount = equipAmount;
+		}
+		
+		private double CalcualtePercentageValueFromNumber(int number, UnitEquipmentItem equip)
+		{
+			double calcedAmount = RoundPercentage(CustomMath.IBBMath.Percentage(number, unit.Size));
+			return Math.Min(Math.Max(calcedAmount, GetMinPercentageLimit(equip)), GetMaxPercentageLimit(equip));
+		}
+
 		//TODO Make abstract
 		protected void CreateEquipmentUI()
 		{
@@ -50,14 +124,10 @@
 			if (equip != null)
 			{
 				bool equipIsRatioLimit = UnitEquipmentUtil.IsEquipmentRatioLimited(unit, equip);
-				double maxPercent = RoundPercentage(UnitEquipmentUtil.GetMaxEquipmentPercentage(unit, equip));
-				maxPercent = Math.Max(0, maxPercent);
-				double minPercent = RoundPercentage(UnitEquipmentUtil.GetMinEquipmentPercentage(unit, equip));
-				minPercent = Math.Max(0, minPercent);
-				int maxNumber = UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip);
-				maxNumber = Math.Max(0, maxNumber);
-				int minNumber = UnitEquipmentUtil.GetMinEquipmentCount(unit, equip);
-				minNumber = Math.Max(0, minNumber);
+				double maxPercent = GetMaxPercentageLimit(equip);
+				double minPercent = GetMinPercentageLimit(equip);
+				int maxNumber = GetMaxNumericLimit(equip);
+				int minNumber = GetMinNumericLimit(equip);
 			
 				ui.SetUnitEquipmentLimits(equipIsRatioLimit, minPercent, maxPercent, minNumber, maxNumber);
 				ui.SetUnitEquipmentLimitsEnabled(true);
@@ -70,7 +140,29 @@
 				ui.SetOkayEnabledState(false);
 			}
 		}
+		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()
 		{
 			bool nonZero;
--- a/UIControl/Interfaces/IAddEquipmentUI.cs	Sun Aug 29 15:09:34 2010 +0000
+++ b/UIControl/Interfaces/IAddEquipmentUI.cs	Sun Aug 29 15:44:20 2010 +0000
@@ -86,20 +86,20 @@
 		bool IsRatioEquipmentAmount { get; }
 		
 		/// <summary>
-		/// Gets the numeric amount for the current equipment amount. This number is meaningless if <see cref="IsRatioEquipmentAmount"/> is <code>true</code>
+		/// Gets and sets the numeric amount for the current equipment amount. This number is meaningless if <see cref="IsRatioEquipmentAmount"/> is <code>true</code>
 		/// </summary>
 		/// <value>
 		/// The absolue number of items taken.
 		/// </value>
-		int EquipmentNumericAmount { get; }
+		int EquipmentNumericAmount { get; set; }
 
 		/// <summary>
-		/// Gets the percentage amount for the current equipment amount. This number is meaningless if <see cref="IsRatioEquipmentAmount"/> is <code>false</code>
+		/// Gets and sets the percentage amount for the current equipment amount. This number is meaningless if <see cref="IsRatioEquipmentAmount"/> is <code>false</code>
 		/// </summary>
 		/// <value>
 		/// The number of items taken as a percentage of the unit size.
 		/// </value>
-		double EquipmentPercentageAmount { get; }
+		double EquipmentPercentageAmount { get; set; }
 	
 		/// <summary>
 		/// Sets the state of the Okay button.