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 diff
     1.1 --- a/FrmAddEquipment.cs	Sun Aug 29 15:09:34 2010 +0000
     1.2 +++ b/FrmAddEquipment.cs	Sun Aug 29 15:44:20 2010 +0000
     1.3 @@ -188,6 +188,11 @@
     1.4  			{
     1.5  				return (int)numericAmount.Value;
     1.6  			}
     1.7 +			
     1.8 +			set
     1.9 +			{
    1.10 +				numericAmount.Value = value;
    1.11 +			}
    1.12  		}
    1.13  		
    1.14  		
    1.15 @@ -208,6 +213,11 @@
    1.16  				
    1.17  				return percent;
    1.18  			}
    1.19 +			
    1.20 +			set
    1.21 +			{
    1.22 +				percentageAmount.Value = value;
    1.23 +			}
    1.24  		}
    1.25  	}
    1.26  }
     2.1 --- a/UIControl/AddEquipmentUIControl.cs	Sun Aug 29 15:09:34 2010 +0000
     2.2 +++ b/UIControl/AddEquipmentUIControl.cs	Sun Aug 29 15:44:20 2010 +0000
     2.3 @@ -7,6 +7,8 @@
     2.4  using IBBoard.WarFoundry.API.Util;
     2.5  using IBBoard.WarFoundry.GUI.GTK.UIControl.Interfaces;
     2.6  using IBBoard.WarFoundry.API.Commands;
     2.7 +using CustomMath = IBBoard.CustomMath;
     2.8 +using IBBoard.Lang;
     2.9  namespace IBBoard.WarFoundry.GUI.GTK.UIControl
    2.10  {
    2.11  	public class AddEquipmentUIControl
    2.12 @@ -34,11 +36,83 @@
    2.13  			ui.UnitEquipmentAmountTypeChanged += HandleUnitEquipmentAmountChanged;
    2.14  		}
    2.15  
    2.16 -		private void HandleUnitEquipmentAmountChanged ()
    2.17 +		private void HandleUnitEquipmentAmountChanged()
    2.18  		{
    2.19  			ui.SetOkayEnabledState(ui.SelectedUnitEquipmentItem != null && HasNonZeroEquipmentAmount());
    2.20 +			
    2.21 +			if (ui.IsRatioEquipmentAmount)
    2.22 +			{
    2.23 +				SetEquipmentAmountsFromPercentage(ui.EquipmentPercentageAmount);
    2.24 +			}
    2.25 +			else
    2.26 +			{
    2.27 +				SetEquipmentAmountsFromNumber(ui.EquipmentNumericAmount);
    2.28 +			}
    2.29  		}
    2.30  		
    2.31 +		private void SetEquipmentAmountsFromPercentage(double equipAmount)
    2.32 +		{
    2.33 +			UnitEquipmentItem currEquipItem = ui.SelectedUnitEquipmentItem;
    2.34 +			
    2.35 +			double maxPercentage = GetMaxPercentageLimit(currEquipItem);
    2.36 +			double minPercentage = GetMinPercentageLimit(currEquipItem);
    2.37 +			
    2.38 +			if (equipAmount > maxPercentage)
    2.39 +			{
    2.40 +				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);
    2.41 +				string percentageTooLargeTitle = Translation.GetTranslation("equipPercentageTooLargeTitle", "equipment percentage too large");
    2.42 +				//				MessageBox.Show(ParentForm, percentageTooLarge, percentageTooLargeTitle);
    2.43 +				equipAmount = maxPercentage;
    2.44 +			}
    2.45 +			else if (equipAmount < minPercentage)
    2.46 +			{
    2.47 +				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);
    2.48 +				string percentageTooSmallTitle = Translation.GetTranslation("equipPercentageTooSmallTitle", "equipment percentage too small");
    2.49 +		//				MessageBox.Show(ParentForm, percentageTooSmall, percentageTooSmallTitle);
    2.50 +				equipAmount = minPercentage;
    2.51 +			}
    2.52 +			
    2.53 +			ui.EquipmentNumericAmount = CalculateNumericValueFromPercentage(equipAmount, currEquipItem);
    2.54 +			ui.EquipmentPercentageAmount = equipAmount;
    2.55 +		}
    2.56 +		
    2.57 +		private int CalculateNumericValueFromPercentage(double percent, UnitEquipmentItem equip)
    2.58 +		{
    2.59 +			int calcedAmount = (int)CustomMath.IBBMath.Round((double)(unit.Size * (percent / 100)), equip.RoundNumberUp);
    2.60 +			return Math.Min(Math.Max(calcedAmount, GetMinNumericLimit(equip)), GetMaxNumericLimit(equip));
    2.61 +		}
    2.62 +
    2.63 +		private void SetEquipmentAmountsFromNumber(int equipAmount)
    2.64 +		{
    2.65 +			UnitEquipmentItem currEquipItem = ui.SelectedUnitEquipmentItem;
    2.66 +			int maxNumber = GetMaxNumericLimit(currEquipItem);
    2.67 +			int minNumber = GetMinNumericLimit(currEquipItem);
    2.68 +			
    2.69 +			if (equipAmount > maxNumber)
    2.70 +			{
    2.71 +				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);
    2.72 +				string amountTooLargeTitle = Translation.GetTranslation("equipNumberTooLargeTitle", "equipment amount too large");
    2.73 +				//MessageBox.Show(ParentForm, amountTooLarge, amountTooLargeTitle);
    2.74 +				equipAmount = maxNumber;
    2.75 +			}
    2.76 +			else if (equipAmount < minNumber)
    2.77 +			{
    2.78 +				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);
    2.79 +				string amountTooSmallTitle = Translation.GetTranslation("equipNumberTooSmallTitle", "equipment amount too small");
    2.80 +				//MessageBox.Show(ParentForm, amountTooSmall, amountTooSmallTitle);
    2.81 +				equipAmount = minNumber;
    2.82 +			}
    2.83 +			
    2.84 +			ui.EquipmentPercentageAmount = CalcualtePercentageValueFromNumber(equipAmount, currEquipItem);
    2.85 +			ui.EquipmentNumericAmount = equipAmount;
    2.86 +		}
    2.87 +		
    2.88 +		private double CalcualtePercentageValueFromNumber(int number, UnitEquipmentItem equip)
    2.89 +		{
    2.90 +			double calcedAmount = RoundPercentage(CustomMath.IBBMath.Percentage(number, unit.Size));
    2.91 +			return Math.Min(Math.Max(calcedAmount, GetMinPercentageLimit(equip)), GetMaxPercentageLimit(equip));
    2.92 +		}
    2.93 +
    2.94  		//TODO Make abstract
    2.95  		protected void CreateEquipmentUI()
    2.96  		{
    2.97 @@ -50,14 +124,10 @@
    2.98  			if (equip != null)
    2.99  			{
   2.100  				bool equipIsRatioLimit = UnitEquipmentUtil.IsEquipmentRatioLimited(unit, equip);
   2.101 -				double maxPercent = RoundPercentage(UnitEquipmentUtil.GetMaxEquipmentPercentage(unit, equip));
   2.102 -				maxPercent = Math.Max(0, maxPercent);
   2.103 -				double minPercent = RoundPercentage(UnitEquipmentUtil.GetMinEquipmentPercentage(unit, equip));
   2.104 -				minPercent = Math.Max(0, minPercent);
   2.105 -				int maxNumber = UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip);
   2.106 -				maxNumber = Math.Max(0, maxNumber);
   2.107 -				int minNumber = UnitEquipmentUtil.GetMinEquipmentCount(unit, equip);
   2.108 -				minNumber = Math.Max(0, minNumber);
   2.109 +				double maxPercent = GetMaxPercentageLimit(equip);
   2.110 +				double minPercent = GetMinPercentageLimit(equip);
   2.111 +				int maxNumber = GetMaxNumericLimit(equip);
   2.112 +				int minNumber = GetMinNumericLimit(equip);
   2.113  			
   2.114  				ui.SetUnitEquipmentLimits(equipIsRatioLimit, minPercent, maxPercent, minNumber, maxNumber);
   2.115  				ui.SetUnitEquipmentLimitsEnabled(true);
   2.116 @@ -70,7 +140,29 @@
   2.117  				ui.SetOkayEnabledState(false);
   2.118  			}
   2.119  		}
   2.120 +		private double GetMaxPercentageLimit(UnitEquipmentItem equip)
   2.121 +		{
   2.122 +			double maxPercent = RoundPercentage(UnitEquipmentUtil.GetMaxEquipmentPercentage(unit, equip));
   2.123 +			return Math.Max(0, maxPercent);
   2.124 +		}
   2.125  		
   2.126 +		private double GetMinPercentageLimit(UnitEquipmentItem equip)
   2.127 +		{
   2.128 +			double minPercent = RoundPercentage(UnitEquipmentUtil.GetMinEquipmentPercentage(unit, equip));
   2.129 +			return Math.Max(0, minPercent);
   2.130 +		}
   2.131 +		
   2.132 +		private int GetMaxNumericLimit(UnitEquipmentItem equip)
   2.133 +		{
   2.134 +			int maxNumber = UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip);
   2.135 +			return Math.Max(0, maxNumber);
   2.136 +		}
   2.137 +		private int GetMinNumericLimit(UnitEquipmentItem equip)
   2.138 +		{
   2.139 +			int minNumber = UnitEquipmentUtil.GetMinEquipmentCount(unit, equip);
   2.140 +			return Math.Max(0, minNumber);
   2.141 +		}
   2.142 +
   2.143  		private bool HasNonZeroEquipmentAmount()
   2.144  		{
   2.145  			bool nonZero;
     3.1 --- a/UIControl/Interfaces/IAddEquipmentUI.cs	Sun Aug 29 15:09:34 2010 +0000
     3.2 +++ b/UIControl/Interfaces/IAddEquipmentUI.cs	Sun Aug 29 15:44:20 2010 +0000
     3.3 @@ -86,20 +86,20 @@
     3.4  		bool IsRatioEquipmentAmount { get; }
     3.5  		
     3.6  		/// <summary>
     3.7 -		/// Gets the numeric amount for the current equipment amount. This number is meaningless if <see cref="IsRatioEquipmentAmount"/> is <code>true</code>
     3.8 +		/// Gets and sets the numeric amount for the current equipment amount. This number is meaningless if <see cref="IsRatioEquipmentAmount"/> is <code>true</code>
     3.9  		/// </summary>
    3.10  		/// <value>
    3.11  		/// The absolue number of items taken.
    3.12  		/// </value>
    3.13 -		int EquipmentNumericAmount { get; }
    3.14 +		int EquipmentNumericAmount { get; set; }
    3.15  
    3.16  		/// <summary>
    3.17 -		/// Gets the percentage amount for the current equipment amount. This number is meaningless if <see cref="IsRatioEquipmentAmount"/> is <code>false</code>
    3.18 +		/// Gets and sets the percentage amount for the current equipment amount. This number is meaningless if <see cref="IsRatioEquipmentAmount"/> is <code>false</code>
    3.19  		/// </summary>
    3.20  		/// <value>
    3.21  		/// The number of items taken as a percentage of the unit size.
    3.22  		/// </value>
    3.23 -		double EquipmentPercentageAmount { get; }
    3.24 +		double EquipmentPercentageAmount { get; set; }
    3.25  	
    3.26  		/// <summary>
    3.27  		/// Sets the state of the Okay button.