changeset 41:4fc87d6e6119

Re #117: Add percentage and number boxes to equipment item dialogs * Add events and value setting in EquipmentAmount widget * Hook up "value changed" event on new equipment form Also fixes adding side of re #114 (Enabling/disabling of "equip all" and number for equipment is wrong) by rebuilding everything
author IBBoard <dev@ibboard.co.uk>
date Sun, 06 Sep 2009 19:31:49 +0000
parents 740350673006
children d06c2e390a14
files FrmNewUnitEquipment.cs UI/EquipmentAmountControl.cs
diffstat 2 files changed, 137 insertions(+), 50 deletions(-) [+]
line diff
     1.1 --- a/FrmNewUnitEquipment.cs	Sun Sep 06 18:01:54 2009 +0000
     1.2 +++ b/FrmNewUnitEquipment.cs	Sun Sep 06 19:31:49 2009 +0000
     1.3 @@ -43,6 +43,7 @@
     1.4  			this.Text = "New equipment for " + unit.Name;
     1.5  			object[] items = Arrays.Subtract(unit.GetAllowedOptionalEquipment(), unit.GetEquipment());
     1.6  			listItems.Items.AddRange(items);
     1.7 +			equipmentAmount.SetUnit(unit);
     1.8  		}
     1.9  
    1.10  		/// <summary>
    1.11 @@ -129,6 +130,7 @@
    1.12  			this.equipmentAmount.Name = "equipmentAmount";
    1.13  			this.equipmentAmount.Size = new System.Drawing.Size(155, 77);
    1.14  			this.equipmentAmount.TabIndex = 8;
    1.15 +			this.equipmentAmount.ValueChanged += new System.EventHandler(this.equipmentAmount_ValueChanged);
    1.16  			// 
    1.17  			// FrmNewUnitEquipment
    1.18  			// 
    1.19 @@ -179,64 +181,28 @@
    1.20  			this.Close();
    1.21  		}
    1.22  
    1.23 -		private void cbEquipAll_CheckedChanged(object sender, System.EventArgs e)
    1.24 -		{
    1.25 -			//numEquipAmount.Enabled = ((UnitEquipmentItem) listItems.SelectedItem).MinNumber != WarFoundryCore.INFINITY && !rbEquipAll.Checked;
    1.26 -			setOkayButton();
    1.27 -		}
    1.28 -
    1.29  		private void listItems_SelectedIndexChanged(object sender, System.EventArgs e)
    1.30  		{
    1.31 -			refreshNumber();
    1.32 -			setOkayButton();
    1.33 -		}
    1.34 -
    1.35 -		private void refreshNumber()
    1.36 -		{
    1.37 -			/*
    1.38  			if (listItems.SelectedIndex > -1)
    1.39  			{
    1.40 -				UnitEquipmentItem equipItem = (UnitEquipmentItem)listItems.SelectedItem;
    1.41 -				required = equipItem.IsRequired;
    1.42 -
    1.43 -				if (equipItem.IsRatioLimit)
    1.44 -				{
    1.45 -					numEquipAmount.Minimum = (decimal) equipItem.MinPercentage;
    1.46 -					numEquipAmount.Maximum = (decimal) equipItem.MaxPercentage;
    1.47 -					numEquipAmount.Enabled = true;
    1.48 -					numEquipAmount.Value = numEquipAmount.Minimum;
    1.49 -					rbEquipAll.Enabled = false;
    1.50 -					lblPercent.Visible = true;
    1.51 -					numEquipAmount.Width = 120;
    1.52 -				}
    1.53 -				else
    1.54 -				{
    1.55 -					numEquipAmount.Minimum = (decimal)(equipItem.MinNumber != WarFoundryCore.INFINITY ? Math.Min(equipItem.MinNumber, unit.Size) : unit.Size);
    1.56 -					numEquipAmount.Maximum = (decimal)(equipItem.MaxNumber != WarFoundryCore.INFINITY ? Math.Min(equipItem.MaxNumber, unit.Size) : unit.Size);
    1.57 -					numEquipAmount.Value = numEquipAmount.Minimum;
    1.58 -					numEquipAmount.Enabled = equipItem.MinNumber != WarFoundryCore.INFINITY;
    1.59 -					rbEquipAll.Enabled = equipItem.MinNumber != WarFoundryCore.INFINITY && equipItem.MaxNumber == WarFoundryCore.INFINITY;
    1.60 -					rbEquipAll.Checked = equipItem.MinNumber == WarFoundryCore.INFINITY;
    1.61 -					lblPercent.Visible = false;
    1.62 -					numEquipAmount.Width = 144;
    1.63 -				}
    1.64 +				equipmentAmount.SetUnitEquipmentItem((UnitEquipmentItem)listItems.SelectedItem);
    1.65  			}
    1.66  			else
    1.67  			{
    1.68 -				numEquipAmount.Minimum = 0;
    1.69 -				numEquipAmount.Value = 0;
    1.70 -				numEquipAmount.Enabled = false;
    1.71 -				rbEquipAll.Enabled = false;
    1.72 -			}*/
    1.73 +				equipmentAmount.SetUnitEquipmentItem(null);
    1.74 +			}
    1.75 +
    1.76 +			setOkayButton();
    1.77  		}
    1.78  
    1.79  		private void setOkayButton()
    1.80  		{
    1.81 -			bttnOkay.Enabled = listItems.SelectedIndex > -1;// && (rbEquipAll.Checked || (numEquipAmount.Enabled && numEquipAmount.Value > 0));
    1.82 +			double equipAmount = equipmentAmount.EquipmentAmount;
    1.83 +			bttnOkay.Enabled = listItems.SelectedIndex > -1 && (equipAmount > 0 || equipAmount == WarFoundryCore.INFINITY);
    1.84  		}
    1.85  
    1.86 -		private void numEquipAmount_ValueChanged(object sender, System.EventArgs e)
    1.87 -		{			
    1.88 +		private void equipmentAmount_ValueChanged(object sender, EventArgs e)
    1.89 +		{
    1.90  			setOkayButton();
    1.91  		}
    1.92  	}
     2.1 --- a/UI/EquipmentAmountControl.cs	Sun Sep 06 18:01:54 2009 +0000
     2.2 +++ b/UI/EquipmentAmountControl.cs	Sun Sep 06 19:31:49 2009 +0000
     2.3 @@ -29,7 +29,88 @@
     2.4  		public void SetUnitEquipmentItem(UnitEquipmentItem unitEquipment)
     2.5  		{
     2.6  			equip = unitEquipment;
     2.7 -			//SetWidgetValues();
     2.8 +			SetWidgetValues();
     2.9 +		}
    2.10 +
    2.11 +		public event EventHandler ValueChanged;
    2.12 +
    2.13 +		private void OnValueChanged()
    2.14 +		{
    2.15 +			if (ValueChanged != null)
    2.16 +			{
    2.17 +				ValueChanged(this, new EventArgs());
    2.18 +			}
    2.19 +		}
    2.20 +
    2.21 +		private void SetWidgetValues()
    2.22 +		{
    2.23 +			if (equip != null)
    2.24 +			{
    2.25 +				if (equip.IsRatioLimit)
    2.26 +				{
    2.27 +					double minPercent = equip.MinPercentage;
    2.28 +					double maxPercent = equip.MaxPercentage;
    2.29 +					SetUpDownControlMinMaxAndValue(percentage, minPercent, maxPercent);
    2.30 +					SetUpDownControlMinMaxAndValue(numeric, CalculateNumericValueFromPercentage(minPercent), CalculateNumericValueFromPercentage(maxPercent));
    2.31 +					rbEquipAll.Enabled = maxPercent == 100;
    2.32 +
    2.33 +					if (minPercent == 100)
    2.34 +					{
    2.35 +						rbEquipAll.Checked = true;
    2.36 +						percentage.Enabled = false;
    2.37 +						rbPercentage.Enabled = false;
    2.38 +						numeric.Enabled = false;
    2.39 +						rbNumeric.Enabled = false;
    2.40 +					}
    2.41 +					else
    2.42 +					{
    2.43 +						rbPercentage.Checked = true;
    2.44 +						percentage.Enabled = true;
    2.45 +						rbPercentage.Enabled = true;
    2.46 +						numeric.Enabled = true;
    2.47 +						rbNumeric.Enabled = true;
    2.48 +					}
    2.49 +				}
    2.50 +				else
    2.51 +				{
    2.52 +					percentage.Enabled = false;
    2.53 +					rbPercentage.Enabled = false;
    2.54 +					int minNumber = equip.MinNumber;
    2.55 +					int maxNumber = equip.MaxNumber;
    2.56 +					SetUpDownControlMinMaxAndValue(numeric, (decimal) minNumber, (decimal) maxNumber);
    2.57 +					SetUpDownControlMinMaxAndValue(percentage, CalcualtePercentageValueFromNumber(minNumber), CalcualtePercentageValueFromNumber(maxNumber));
    2.58 +					rbEquipAll.Enabled = maxNumber == WarFoundryCore.INFINITY;
    2.59 +					
    2.60 +					if (minNumber == WarFoundryCore.INFINITY)
    2.61 +					{
    2.62 +						rbEquipAll.Checked = true;
    2.63 +						numeric.Enabled = false;
    2.64 +						rbNumeric.Enabled = false;
    2.65 +					}
    2.66 +					else
    2.67 +					{
    2.68 +						rbNumeric.Checked = true;
    2.69 +						numeric.Enabled = true;
    2.70 +						rbNumeric.Enabled = true;
    2.71 +					}
    2.72 +				}
    2.73 +			}
    2.74 +			else
    2.75 +			{
    2.76 +				Enabled = false;
    2.77 +			}
    2.78 +		}
    2.79 +
    2.80 +		private void SetUpDownControlMinMaxAndValue(NumericUpDown upDownControl, double min, double max)
    2.81 +		{
    2.82 +			SetUpDownControlMinMaxAndValue(upDownControl, (decimal)min, (decimal)max);
    2.83 +		}
    2.84 +
    2.85 +		private void SetUpDownControlMinMaxAndValue(NumericUpDown upDownControl, decimal min, decimal max)
    2.86 +		{
    2.87 +			upDownControl.Minimum = (min == WarFoundryCore.INFINITY ? unit.Size : min);
    2.88 +			upDownControl.Maximum = (max == WarFoundryCore.INFINITY ? unit.Size : max);
    2.89 +			upDownControl.Value = upDownControl.Value;
    2.90  		}
    2.91  
    2.92  		private void rbEquipAll_CheckedChanged(object sender, EventArgs e)
    2.93 @@ -43,18 +124,54 @@
    2.94  				numeric.Value = unit.Size;
    2.95  				percentage.Value = 100;
    2.96  			}
    2.97 +
    2.98 +			OnValueChanged();
    2.99  		}
   2.100  
   2.101  		private void percentage_ValueChanged(object sender, EventArgs e)
   2.102  		{
   2.103 -			double percent = (double) percentage.Value;
   2.104 -			numeric.Value = (decimal) IBBMath.Round(unit.Size * (percent / 100.0), equip.RoundNumberUp);
   2.105 +			SetNumericValueFromPercentage();
   2.106  			rbEquipAll.Checked = (percentage.Value == 100);
   2.107 +			OnValueChanged();
   2.108 +		}
   2.109 +
   2.110 +		private void SetNumericValueFromPercentage()
   2.111 +		{
   2.112 +			double percent = (double)percentage.Value;
   2.113 +			numeric.Value = CalculateNumericValueFromPercentage(percent);
   2.114 +		}
   2.115 +
   2.116 +		private decimal CalculateNumericValueFromPercentage(double percent)
   2.117 +		{
   2.118 +			return (decimal) IBBMath.Round(unit.Size * (percent / 100.0), equip.RoundNumberUp);
   2.119  		}
   2.120  
   2.121  		private void numeric_ValueChanged(object sender, EventArgs e)
   2.122  		{
   2.123 -			percentage.Value = (unit.Size / numeric.Value) * 100;
   2.124 +			SetPercentageValueFromNumeric();
   2.125 +			OnValueChanged();
   2.126 +		}
   2.127 +
   2.128 +		private void SetPercentageValueFromNumeric()
   2.129 +		{
   2.130 +			int number = (int)numeric.Value;
   2.131 +			percentage.Value = CalcualtePercentageValueFromNumber(number);
   2.132 +		}
   2.133 +
   2.134 +		private decimal CalcualtePercentageValueFromNumber(int number)
   2.135 +		{
   2.136 +			decimal percent = 0;
   2.137 +
   2.138 +			if (number > 0)
   2.139 +			{
   2.140 +				percent = (decimal) Math.Round((number / (unit.Size * 1.0)) * 100, 1);
   2.141 +			}
   2.142 +			else if (number == WarFoundryCore.INFINITY)
   2.143 +			{
   2.144 +				percent = 100;
   2.145 +			}
   2.146 +
   2.147 +			return percent;
   2.148  		}
   2.149  
   2.150  		public double EquipmentAmount
   2.151 @@ -71,9 +188,13 @@
   2.152  				{
   2.153  					val = (double) percentage.Value / 100.0;
   2.154  				}
   2.155 +				else if (rbEquipAll.Checked)
   2.156 +				{
   2.157 +					val = WarFoundryCore.INFINITY;
   2.158 +				}
   2.159  				else
   2.160  				{
   2.161 -					val = WarFoundryCore.INFINITY;
   2.162 +					val = 0;
   2.163  				}
   2.164  
   2.165  				return val;