changeset 77:712915de47fd

Fixes #187: Error when adding equipment with minNum > unit size * Handle percentages greater than 100 * Remove value change listeners before setting min/max and re-add in new wrapper method * Hide value setting method and handle internally
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Oct 2009 19:58:23 +0000
parents f2006db02ad9
children 431cc3ace26c
files FrmEditUnitEquipment.cs UI/EquipmentAmountControl.cs
diffstat 2 files changed, 254 insertions(+), 234 deletions(-) [+]
line diff
     1.1 --- a/FrmEditUnitEquipment.cs	Sun Oct 04 14:24:52 2009 +0000
     1.2 +++ b/FrmEditUnitEquipment.cs	Sun Oct 04 19:58:23 2009 +0000
     1.3 @@ -39,7 +39,6 @@
     1.4  			this.Text = equipItem.Name + " for " + unit.Name;
     1.5  			equipmentAmount.SetUnit(unit);
     1.6  			equipmentAmount.SetUnitEquipmentItem(equipItem);
     1.7 -			equipmentAmount.SetUnitEquipmentItemAmount(unit.GetEquipmentAmount(equipItem), unit.GetEquipmentAmountIsRatio(equipItem));
     1.8  			bttnOkay.Enabled = false;
     1.9  		}
    1.10  
     2.1 --- a/UI/EquipmentAmountControl.cs	Sun Oct 04 14:24:52 2009 +0000
     2.2 +++ b/UI/EquipmentAmountControl.cs	Sun Oct 04 19:58:23 2009 +0000
     2.3 @@ -1,233 +1,254 @@
     2.4 -using System;
     2.5 -using System.Collections.Generic;
     2.6 -using System.ComponentModel;
     2.7 -using System.Drawing;
     2.8 -using System.Data;
     2.9 -using System.Text;
    2.10 -using System.Windows.Forms;
    2.11 -using IBBoard.Lang;
    2.12 -using IBBoard.WarFoundry.API;
    2.13 -using IBBoard.WarFoundry.API.Objects;
    2.14 -
    2.15 -namespace IBBoard.WarFoundry.GUI.WinForms.UI
    2.16 -{
    2.17 -	public partial class EquipmentAmountControl : UserControl
    2.18 -	{
    2.19 -		private Unit unit;
    2.20 -		private UnitEquipmentItem equip;
    2.21 -
    2.22 -		public EquipmentAmountControl()
    2.23 -		{
    2.24 -			InitializeComponent();
    2.25 -		}
    2.26 -
    2.27 -		public void SetUnit(Unit equipUnit)
    2.28 -		{
    2.29 -			unit = equipUnit;
    2.30 -		}
    2.31 -
    2.32 -		public void SetUnitEquipmentItem(UnitEquipmentItem unitEquipment)
    2.33 -		{
    2.34 -			equip = unitEquipment;
    2.35 -			SetWidgetValues();
    2.36 -		}
    2.37 -
    2.38 -		public event EventHandler ValueChanged;
    2.39 -
    2.40 -		private void OnValueChanged()
    2.41 -		{
    2.42 -			if (ValueChanged != null)
    2.43 -			{
    2.44 -				ValueChanged(this, new EventArgs());
    2.45 -			}
    2.46 -		}
    2.47 -
    2.48 -		private void SetWidgetValues()
    2.49 -		{
    2.50 -			if (equip != null)
    2.51 -			{
    2.52 -				if (equip.IsRatioLimit)
    2.53 -				{
    2.54 -					double minPercent = equip.MinPercentage;
    2.55 -					double maxPercent = equip.MaxPercentage;
    2.56 -					SetUpDownControlMinMaxAndValue(percentage, minPercent, maxPercent);
    2.57 -					SetUpDownControlMinMaxAndValue(numeric, CalculateNumericValueFromPercentage(minPercent), CalculateNumericValueFromPercentage(maxPercent));
    2.58 -					rbEquipAll.Enabled = maxPercent == 100;
    2.59 -
    2.60 -					if (minPercent == 100)
    2.61 -					{
    2.62 -						rbEquipAll.Checked = true;
    2.63 -						percentage.Enabled = false;
    2.64 -						rbPercentage.Enabled = false;
    2.65 -						numeric.Enabled = false;
    2.66 -						rbNumeric.Enabled = false;
    2.67 -					}
    2.68 -					else
    2.69 -					{
    2.70 -						rbPercentage.Checked = true;
    2.71 -						percentage.Enabled = true;
    2.72 -						rbPercentage.Enabled = true;
    2.73 -						numeric.Enabled = true;
    2.74 -						rbNumeric.Enabled = true;
    2.75 -					}
    2.76 -				}
    2.77 -				else
    2.78 -				{
    2.79 -					percentage.Enabled = false;
    2.80 -					rbPercentage.Enabled = false;
    2.81 -					int minNumber = equip.MinNumber;
    2.82 -					int maxNumber = equip.MaxNumber;
    2.83 -					SetUpDownControlMinMaxAndValue(numeric, (decimal) minNumber, (decimal) maxNumber);
    2.84 -					SetUpDownControlMinMaxAndValue(percentage, CalcualtePercentageValueFromNumber(minNumber), CalcualtePercentageValueFromNumber(maxNumber));
    2.85 -					rbEquipAll.Enabled = maxNumber == WarFoundryCore.INFINITY;
    2.86 -					
    2.87 -					if (minNumber == WarFoundryCore.INFINITY)
    2.88 -					{
    2.89 -						rbEquipAll.Checked = true;
    2.90 -						numeric.Enabled = false;
    2.91 -						rbNumeric.Enabled = false;
    2.92 -					}
    2.93 -					else
    2.94 -					{
    2.95 -						rbNumeric.Checked = true;
    2.96 -						numeric.Enabled = true;
    2.97 -						rbNumeric.Enabled = true;
    2.98 -					}
    2.99 -				}
   2.100 -			}
   2.101 -			else
   2.102 -			{
   2.103 -				Enabled = false;
   2.104 -			}
   2.105 -		}
   2.106 -
   2.107 -		private void SetUpDownControlMinMaxAndValue(NumericUpDown upDownControl, double min, double max)
   2.108 -		{
   2.109 -			SetUpDownControlMinMaxAndValue(upDownControl, (decimal)min, (decimal)max);
   2.110 -		}
   2.111 -
   2.112 -		private void SetUpDownControlMinMaxAndValue(NumericUpDown upDownControl, decimal min, decimal max)
   2.113 -		{
   2.114 -			upDownControl.Minimum = (min == WarFoundryCore.INFINITY ? unit.Size : min);
   2.115 -			upDownControl.Maximum = (max == WarFoundryCore.INFINITY ? unit.Size : max);
   2.116 -			upDownControl.Value = upDownControl.Value;
   2.117 -		}
   2.118 -
   2.119 -		private void rbEquipAll_CheckedChanged(object sender, EventArgs e)
   2.120 -		{
   2.121 -			bool equipAll = rbEquipAll.Checked;
   2.122 -			numeric.Enabled = !equipAll;
   2.123 -			percentage.Enabled = !equipAll;
   2.124 -
   2.125 -			if (equipAll)
   2.126 -			{
   2.127 -				numeric.Value = unit.Size;
   2.128 -				percentage.Value = 100;
   2.129 -			}
   2.130 -
   2.131 -			radioCheckedChanged(sender, e);
   2.132 -		}
   2.133 -
   2.134 -		private void percentage_ValueChanged(object sender, EventArgs e)
   2.135 -		{
   2.136 -			SetNumericValueFromPercentage();
   2.137 -			rbEquipAll.Checked = (percentage.Value == 100 && !rbNumeric.Checked);
   2.138 -			OnValueChanged();
   2.139 -		}
   2.140 -
   2.141 -		private void SetNumericValueFromPercentage()
   2.142 -		{
   2.143 -			double percent = (double)percentage.Value;
   2.144 -			numeric.Value = CalculateNumericValueFromPercentage(percent);
   2.145 -		}
   2.146 -
   2.147 -		private decimal CalculateNumericValueFromPercentage(double percent)
   2.148 -		{
   2.149 -			return (decimal) IBBMath.Round(unit.Size * (percent / 100.0), equip.RoundNumberUp);
   2.150 -		}
   2.151 -
   2.152 -		private void numeric_ValueChanged(object sender, EventArgs e)
   2.153 -		{
   2.154 -			SetPercentageValueFromNumeric();
   2.155 -			OnValueChanged();
   2.156 -		}
   2.157 -
   2.158 -		private void SetPercentageValueFromNumeric()
   2.159 -		{
   2.160 -			int number = (int)numeric.Value;
   2.161 -			percentage.Value = CalcualtePercentageValueFromNumber(number);
   2.162 -		}
   2.163 -
   2.164 -		private decimal CalcualtePercentageValueFromNumber(int number)
   2.165 -		{
   2.166 -			decimal percent = 0;
   2.167 -
   2.168 -			if (number > 0)
   2.169 -			{
   2.170 -				percent = (decimal) Math.Round((number / (unit.Size * 1.0)) * 100, 1);
   2.171 -			}
   2.172 -			else if (number == WarFoundryCore.INFINITY)
   2.173 -			{
   2.174 -				percent = 100;
   2.175 -			}
   2.176 -
   2.177 -			return percent;
   2.178 -		}
   2.179 -
   2.180 -		public double EquipmentAmount
   2.181 -		{
   2.182 -			get
   2.183 -			{
   2.184 -				double val = 0;
   2.185 -
   2.186 -				if (rbNumeric.Checked)
   2.187 -				{
   2.188 -					val = (double) numeric.Value;
   2.189 -				}
   2.190 -				else if (rbPercentage.Checked)
   2.191 -				{
   2.192 -					val = (double) percentage.Value;
   2.193 -				}
   2.194 -				else if (rbEquipAll.Checked)
   2.195 -				{
   2.196 -					val = WarFoundryCore.INFINITY;
   2.197 -				}
   2.198 -				else
   2.199 -				{
   2.200 -					val = 0;
   2.201 -				}
   2.202 -
   2.203 -				return val;
   2.204 -			}
   2.205 -		}
   2.206 -
   2.207 -		public bool IsRatioEquipmentAmount
   2.208 -		{
   2.209 -			get
   2.210 -			{
   2.211 -				return rbPercentage.Checked;
   2.212 -			}
   2.213 -		}
   2.214 -
   2.215 -		public void SetUnitEquipmentItemAmount(double equipAmountNum, bool isRatio)
   2.216 -		{
   2.217 -			if (isRatio)
   2.218 -			{
   2.219 -				percentage.Value = (decimal)equipAmountNum;
   2.220 -			}
   2.221 -			else if (equipAmountNum == WarFoundryCore.INFINITY)
   2.222 -			{
   2.223 -				rbEquipAll.Checked = true;
   2.224 -			}
   2.225 -			else
   2.226 -			{
   2.227 -				numeric.Value = (int)equipAmountNum;
   2.228 -			}
   2.229 -		}
   2.230 -
   2.231 -		private void radioCheckedChanged(object sender, EventArgs e)
   2.232 -		{
   2.233 -			OnValueChanged();
   2.234 -		}
   2.235 -	}
   2.236 -}
   2.237 +using System;
   2.238 +using System.Collections.Generic;
   2.239 +using System.ComponentModel;
   2.240 +using System.Drawing;
   2.241 +using System.Data;
   2.242 +using System.Text;
   2.243 +using System.Windows.Forms;
   2.244 +using IBBoard.Lang;
   2.245 +using IBBoard.WarFoundry.API;
   2.246 +using IBBoard.WarFoundry.API.Objects;
   2.247 +
   2.248 +namespace IBBoard.WarFoundry.GUI.WinForms.UI
   2.249 +{
   2.250 +	public partial class EquipmentAmountControl : UserControl
   2.251 +	{
   2.252 +		private Unit unit;
   2.253 +		private UnitEquipmentItem equip;
   2.254 +
   2.255 +		public EquipmentAmountControl()
   2.256 +		{
   2.257 +			InitializeComponent();
   2.258 +		}
   2.259 +
   2.260 +		public void SetUnit(Unit equipUnit)
   2.261 +		{
   2.262 +			unit = equipUnit;
   2.263 +		}
   2.264 +
   2.265 +		public void SetUnitEquipmentItem(UnitEquipmentItem unitEquipment)
   2.266 +		{
   2.267 +			equip = unitEquipment;
   2.268 +			SetWidgetValues();
   2.269 +			SetUnitEquipmentItemAmount();
   2.270 +		}
   2.271 +
   2.272 +		public event EventHandler ValueChanged;
   2.273 +
   2.274 +		private void OnValueChanged()
   2.275 +		{
   2.276 +			if (ValueChanged != null)
   2.277 +			{
   2.278 +				ValueChanged(this, new EventArgs());
   2.279 +			}
   2.280 +		}
   2.281 +
   2.282 +		private void SetWidgetValues()
   2.283 +		{
   2.284 +			if (equip != null)
   2.285 +			{
   2.286 +				if (equip.IsRatioLimit)
   2.287 +				{
   2.288 +					double minPercent = equip.MinPercentage;
   2.289 +					double maxPercent = equip.MaxPercentage;
   2.290 +					int minNumber = (int) CalculateNumericValueFromPercentage(minPercent);
   2.291 +					int maxNumber = (int) CalculateNumericValueFromPercentage(maxPercent);
   2.292 +					SetUpDownControlMinMaxes(minPercent, maxPercent, minNumber, maxNumber);
   2.293 +					percentage.Value = (decimal) minPercent;
   2.294 +					rbEquipAll.Enabled = maxPercent == 100;
   2.295 +
   2.296 +					if (minPercent == 100)
   2.297 +					{
   2.298 +						rbEquipAll.Checked = true;
   2.299 +						percentage.Enabled = false;
   2.300 +						rbPercentage.Enabled = false;
   2.301 +						numeric.Enabled = false;
   2.302 +						rbNumeric.Enabled = false;
   2.303 +					}
   2.304 +					else
   2.305 +					{
   2.306 +						rbPercentage.Checked = true;
   2.307 +						percentage.Enabled = true;
   2.308 +						rbPercentage.Enabled = true;
   2.309 +						numeric.Enabled = true;
   2.310 +						rbNumeric.Enabled = true;
   2.311 +					}
   2.312 +				}
   2.313 +				else
   2.314 +				{
   2.315 +					percentage.Enabled = false;
   2.316 +					rbPercentage.Enabled = false;
   2.317 +					int minNumber = equip.MinNumber;
   2.318 +					int maxNumber = equip.MaxNumber;
   2.319 +					double minPercent = (double) CalcualtePercentageValueFromNumber(minNumber);
   2.320 +					double maxPercent = (double) CalcualtePercentageValueFromNumber(maxNumber);
   2.321 +					SetUpDownControlMinMaxes(minPercent, maxPercent, minNumber, maxNumber);
   2.322 +					numeric.Value = (minNumber == WarFoundryCore.INFINITY ? unit.Size : minNumber);
   2.323 +					rbEquipAll.Enabled = maxNumber == WarFoundryCore.INFINITY;
   2.324 +
   2.325 +					if (minNumber == WarFoundryCore.INFINITY)
   2.326 +					{
   2.327 +						rbEquipAll.Checked = true;
   2.328 +						numeric.Enabled = false;
   2.329 +						rbNumeric.Enabled = false;
   2.330 +					}
   2.331 +					else
   2.332 +					{
   2.333 +						rbNumeric.Checked = true;
   2.334 +						numeric.Enabled = true;
   2.335 +						rbNumeric.Enabled = true;
   2.336 +					}
   2.337 +				}
   2.338 +			}
   2.339 +			else
   2.340 +			{
   2.341 +				Enabled = false;
   2.342 +			}
   2.343 +		}
   2.344 +
   2.345 +		private void SetUpDownControlMinMaxes(double minPercent, double maxPercent, int minNumber, int maxNumber)
   2.346 +		{
   2.347 +			percentage.ValueChanged -= percentage_ValueChanged;
   2.348 +			numeric.ValueChanged -= numeric_ValueChanged;
   2.349 +			SetUpDownControlMinMax(percentage, minPercent, maxPercent);
   2.350 +			SetUpDownControlMinMax(numeric, (decimal) minNumber, (decimal) maxNumber);
   2.351 +			percentage.ValueChanged += percentage_ValueChanged;
   2.352 +			numeric.ValueChanged += numeric_ValueChanged;
   2.353 +		}
   2.354 +
   2.355 +		private void SetUpDownControlMinMax(NumericUpDown upDownControl, double min, double max)
   2.356 +		{
   2.357 +			SetUpDownControlMinMax(upDownControl, (decimal)min, (decimal)max);
   2.358 +		}
   2.359 +
   2.360 +		private void SetUpDownControlMinMax(NumericUpDown upDownControl, decimal min, decimal max)
   2.361 +		{
   2.362 +			upDownControl.Minimum = (min == WarFoundryCore.INFINITY ? unit.Size : min);
   2.363 +			upDownControl.Maximum = (max == WarFoundryCore.INFINITY ? unit.Size : max);
   2.364 +		}
   2.365 +
   2.366 +		private void rbEquipAll_CheckedChanged(object sender, EventArgs e)
   2.367 +		{
   2.368 +			bool equipAll = rbEquipAll.Checked;
   2.369 +			numeric.Enabled = !equipAll;
   2.370 +			percentage.Enabled = !equipAll;
   2.371 +
   2.372 +			if (equipAll)
   2.373 +			{
   2.374 +				numeric.Value = unit.Size;
   2.375 +				percentage.Value = 100;
   2.376 +			}
   2.377 +
   2.378 +			radioCheckedChanged(sender, e);
   2.379 +		}
   2.380 +
   2.381 +		private void percentage_ValueChanged(object sender, EventArgs e)
   2.382 +		{
   2.383 +			SetNumericValueFromPercentage();
   2.384 +			rbEquipAll.Checked = (percentage.Value == 100 && !rbNumeric.Checked);
   2.385 +			OnValueChanged();
   2.386 +		}
   2.387 +
   2.388 +		private void SetNumericValueFromPercentage()
   2.389 +		{
   2.390 +			double percent = (double)percentage.Value;
   2.391 +			numeric.Value = CalculateNumericValueFromPercentage(percent);
   2.392 +		}
   2.393 +
   2.394 +		private decimal CalculateNumericValueFromPercentage(double percent)
   2.395 +		{
   2.396 +			return (decimal) IBBMath.Round(unit.Size * (percent / 100.0), equip.RoundNumberUp);
   2.397 +		}
   2.398 +
   2.399 +		private void numeric_ValueChanged(object sender, EventArgs e)
   2.400 +		{
   2.401 +			SetPercentageValueFromNumeric();
   2.402 +			OnValueChanged();
   2.403 +		}
   2.404 +
   2.405 +		private void SetPercentageValueFromNumeric()
   2.406 +		{
   2.407 +			int number = (int)numeric.Value;
   2.408 +			percentage.Value = CalcualtePercentageValueFromNumber(number);
   2.409 +		}
   2.410 +
   2.411 +		private decimal CalcualtePercentageValueFromNumber(int number)
   2.412 +		{
   2.413 +			decimal percent = 0;
   2.414 +
   2.415 +			if (number > 0)
   2.416 +			{
   2.417 +				percent = (decimal) Math.Round((number / (unit.Size * 1.0)) * 100, 1);
   2.418 +			}
   2.419 +			else if (number == WarFoundryCore.INFINITY)
   2.420 +			{
   2.421 +				percent = 100;
   2.422 +			}
   2.423 +
   2.424 +			return percent;
   2.425 +		}
   2.426 +
   2.427 +		public double EquipmentAmount
   2.428 +		{
   2.429 +			get
   2.430 +			{
   2.431 +				double val = 0;
   2.432 +
   2.433 +				if (rbNumeric.Checked)
   2.434 +				{
   2.435 +					val = (double) numeric.Value;
   2.436 +				}
   2.437 +				else if (rbPercentage.Checked)
   2.438 +				{
   2.439 +					val = (double) percentage.Value;
   2.440 +				}
   2.441 +				else if (rbEquipAll.Checked)
   2.442 +				{
   2.443 +					val = WarFoundryCore.INFINITY;
   2.444 +				}
   2.445 +				else
   2.446 +				{
   2.447 +					val = 0;
   2.448 +				}
   2.449 +
   2.450 +				return val;
   2.451 +			}
   2.452 +		}
   2.453 +
   2.454 +		public bool IsRatioEquipmentAmount
   2.455 +		{
   2.456 +			get
   2.457 +			{
   2.458 +				return rbPercentage.Checked;
   2.459 +			}
   2.460 +		}
   2.461 +
   2.462 +		private void SetUnitEquipmentItemAmount()
   2.463 +		{
   2.464 +			double equipAmountNum = unit.GetEquipmentAmount(equip);
   2.465 +
   2.466 +			if (equipAmountNum > 0)
   2.467 +			{
   2.468 +				bool isRatio = unit.GetEquipmentAmountIsRatio(equip);
   2.469 +
   2.470 +				if (isRatio)
   2.471 +				{
   2.472 +					percentage.Value = (decimal) equipAmountNum;
   2.473 +				}
   2.474 +				else if (equipAmountNum == WarFoundryCore.INFINITY)
   2.475 +				{
   2.476 +					rbEquipAll.Checked = true;
   2.477 +				}
   2.478 +				else
   2.479 +				{
   2.480 +					numeric.Value = (int) equipAmountNum;
   2.481 +				}
   2.482 +			}
   2.483 +		}
   2.484 +
   2.485 +		private void radioCheckedChanged(object sender, EventArgs e)
   2.486 +		{
   2.487 +			OnValueChanged();
   2.488 +		}
   2.489 +	}
   2.490 +}