Mercurial > repos > IBDev-IBBoard.WarFoundry.GUI.WinForms
changeset 78:431cc3ace26c
Fixes #187: Error when adding equipment with minNum > unit size
* Separate out methods to ensure that the percentages and numbers are kept correct and that they're not messed up when editing percentage equipment items
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 06 Oct 2009 13:39:50 +0000 |
parents | 712915de47fd |
children | e3488dd61a21 |
files | UI/EquipmentAmountControl.Designer.cs UI/EquipmentAmountControl.cs |
diffstat | 2 files changed, 275 insertions(+), 258 deletions(-) [+] |
line wrap: on
line diff
--- a/UI/EquipmentAmountControl.Designer.cs Sun Oct 04 19:58:23 2009 +0000 +++ b/UI/EquipmentAmountControl.Designer.cs Tue Oct 06 13:39:50 2009 +0000 @@ -34,8 +34,8 @@ this.numeric = new System.Windows.Forms.NumericUpDown(); this.percentage = new System.Windows.Forms.NumericUpDown(); this.lblPercentSign = new System.Windows.Forms.Label(); - ((System.ComponentModel.ISupportInitialize)(this.numeric)).BeginInit(); - ((System.ComponentModel.ISupportInitialize)(this.percentage)).BeginInit(); + ((System.ComponentModel.ISupportInitialize) (this.numeric)).BeginInit(); + ((System.ComponentModel.ISupportInitialize) (this.percentage)).BeginInit(); this.SuspendLayout(); // // rbNumeric @@ -115,8 +115,8 @@ this.Controls.Add(this.rbNumeric); this.Name = "EquipmentAmountControl"; this.Size = new System.Drawing.Size(155, 77); - ((System.ComponentModel.ISupportInitialize)(this.numeric)).EndInit(); - ((System.ComponentModel.ISupportInitialize)(this.percentage)).EndInit(); + ((System.ComponentModel.ISupportInitialize) (this.numeric)).EndInit(); + ((System.ComponentModel.ISupportInitialize) (this.percentage)).EndInit(); this.ResumeLayout(false); this.PerformLayout();
--- a/UI/EquipmentAmountControl.cs Sun Oct 04 19:58:23 2009 +0000 +++ b/UI/EquipmentAmountControl.cs Tue Oct 06 13:39:50 2009 +0000 @@ -1,254 +1,271 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Drawing; -using System.Data; -using System.Text; -using System.Windows.Forms; -using IBBoard.Lang; -using IBBoard.WarFoundry.API; -using IBBoard.WarFoundry.API.Objects; - -namespace IBBoard.WarFoundry.GUI.WinForms.UI -{ - public partial class EquipmentAmountControl : UserControl - { - private Unit unit; - private UnitEquipmentItem equip; - - public EquipmentAmountControl() - { - InitializeComponent(); - } - - public void SetUnit(Unit equipUnit) - { - unit = equipUnit; - } - - public void SetUnitEquipmentItem(UnitEquipmentItem unitEquipment) - { - equip = unitEquipment; - SetWidgetValues(); - SetUnitEquipmentItemAmount(); - } - - public event EventHandler ValueChanged; - - private void OnValueChanged() - { - if (ValueChanged != null) - { - ValueChanged(this, new EventArgs()); - } - } - - private void SetWidgetValues() - { - if (equip != null) - { - if (equip.IsRatioLimit) - { - double minPercent = equip.MinPercentage; - double maxPercent = equip.MaxPercentage; - int minNumber = (int) CalculateNumericValueFromPercentage(minPercent); - int maxNumber = (int) CalculateNumericValueFromPercentage(maxPercent); - SetUpDownControlMinMaxes(minPercent, maxPercent, minNumber, maxNumber); - percentage.Value = (decimal) minPercent; - rbEquipAll.Enabled = maxPercent == 100; - - if (minPercent == 100) - { - rbEquipAll.Checked = true; - percentage.Enabled = false; - rbPercentage.Enabled = false; - numeric.Enabled = false; - rbNumeric.Enabled = false; - } - else - { - rbPercentage.Checked = true; - percentage.Enabled = true; - rbPercentage.Enabled = true; - numeric.Enabled = true; - rbNumeric.Enabled = true; - } - } - else - { - percentage.Enabled = false; - rbPercentage.Enabled = false; - int minNumber = equip.MinNumber; - int maxNumber = equip.MaxNumber; - double minPercent = (double) CalcualtePercentageValueFromNumber(minNumber); - double maxPercent = (double) CalcualtePercentageValueFromNumber(maxNumber); - SetUpDownControlMinMaxes(minPercent, maxPercent, minNumber, maxNumber); - numeric.Value = (minNumber == WarFoundryCore.INFINITY ? unit.Size : minNumber); - rbEquipAll.Enabled = maxNumber == WarFoundryCore.INFINITY; - - if (minNumber == WarFoundryCore.INFINITY) - { - rbEquipAll.Checked = true; - numeric.Enabled = false; - rbNumeric.Enabled = false; - } - else - { - rbNumeric.Checked = true; - numeric.Enabled = true; - rbNumeric.Enabled = true; - } - } - } - else - { - Enabled = false; - } - } - - private void SetUpDownControlMinMaxes(double minPercent, double maxPercent, int minNumber, int maxNumber) - { - percentage.ValueChanged -= percentage_ValueChanged; - numeric.ValueChanged -= numeric_ValueChanged; - SetUpDownControlMinMax(percentage, minPercent, maxPercent); - SetUpDownControlMinMax(numeric, (decimal) minNumber, (decimal) maxNumber); - percentage.ValueChanged += percentage_ValueChanged; - numeric.ValueChanged += numeric_ValueChanged; - } - - private void SetUpDownControlMinMax(NumericUpDown upDownControl, double min, double max) - { - SetUpDownControlMinMax(upDownControl, (decimal)min, (decimal)max); - } - - private void SetUpDownControlMinMax(NumericUpDown upDownControl, decimal min, decimal max) - { - upDownControl.Minimum = (min == WarFoundryCore.INFINITY ? unit.Size : min); - upDownControl.Maximum = (max == WarFoundryCore.INFINITY ? unit.Size : max); - } - - private void rbEquipAll_CheckedChanged(object sender, EventArgs e) - { - bool equipAll = rbEquipAll.Checked; - numeric.Enabled = !equipAll; - percentage.Enabled = !equipAll; - - if (equipAll) - { - numeric.Value = unit.Size; - percentage.Value = 100; - } - - radioCheckedChanged(sender, e); - } - - private void percentage_ValueChanged(object sender, EventArgs e) - { - SetNumericValueFromPercentage(); - rbEquipAll.Checked = (percentage.Value == 100 && !rbNumeric.Checked); - OnValueChanged(); - } - - private void SetNumericValueFromPercentage() - { - double percent = (double)percentage.Value; - numeric.Value = CalculateNumericValueFromPercentage(percent); - } - - private decimal CalculateNumericValueFromPercentage(double percent) - { - return (decimal) IBBMath.Round(unit.Size * (percent / 100.0), equip.RoundNumberUp); - } - - private void numeric_ValueChanged(object sender, EventArgs e) - { - SetPercentageValueFromNumeric(); - OnValueChanged(); - } - - private void SetPercentageValueFromNumeric() - { - int number = (int)numeric.Value; - percentage.Value = CalcualtePercentageValueFromNumber(number); - } - - private decimal CalcualtePercentageValueFromNumber(int number) - { - decimal percent = 0; - - if (number > 0) - { - percent = (decimal) Math.Round((number / (unit.Size * 1.0)) * 100, 1); - } - else if (number == WarFoundryCore.INFINITY) - { - percent = 100; - } - - return percent; - } - - public double EquipmentAmount - { - get - { - double val = 0; - - if (rbNumeric.Checked) - { - val = (double) numeric.Value; - } - else if (rbPercentage.Checked) - { - val = (double) percentage.Value; - } - else if (rbEquipAll.Checked) - { - val = WarFoundryCore.INFINITY; - } - else - { - val = 0; - } - - return val; - } - } - - public bool IsRatioEquipmentAmount - { - get - { - return rbPercentage.Checked; - } - } - - private void SetUnitEquipmentItemAmount() - { - double equipAmountNum = unit.GetEquipmentAmount(equip); - - if (equipAmountNum > 0) - { - bool isRatio = unit.GetEquipmentAmountIsRatio(equip); - - if (isRatio) - { - percentage.Value = (decimal) equipAmountNum; - } - else if (equipAmountNum == WarFoundryCore.INFINITY) - { - rbEquipAll.Checked = true; - } - else - { - numeric.Value = (int) equipAmountNum; - } - } - } - - private void radioCheckedChanged(object sender, EventArgs e) - { - OnValueChanged(); - } - } -} +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Drawing; +using System.Data; +using System.Text; +using System.Windows.Forms; +using IBBoard.Lang; +using IBBoard.WarFoundry.API; +using IBBoard.WarFoundry.API.Objects; + +namespace IBBoard.WarFoundry.GUI.WinForms.UI +{ + public partial class EquipmentAmountControl : UserControl + { + private Unit unit; + private UnitEquipmentItem equip; + + public EquipmentAmountControl() + { + InitializeComponent(); + } + + public void SetUnit(Unit equipUnit) + { + unit = equipUnit; + } + + public void SetUnitEquipmentItem(UnitEquipmentItem unitEquipment) + { + equip = unitEquipment; + SetWidgetValues(); + SetUnitEquipmentItemAmount(); + } + + public event EventHandler ValueChanged; + + private void OnValueChanged() + { + if (ValueChanged != null) + { + ValueChanged(this, new EventArgs()); + } + } + + private void SetWidgetValues() + { + if (equip != null) + { + if (equip.IsRatioLimit) + { + double minPercent = equip.MinPercentage; + double maxPercent = equip.MaxPercentage; + int minNumber = (int) CalculateNumericValueFromPercentage(minPercent); + int maxNumber = (int) CalculateNumericValueFromPercentage(maxPercent); + + rbEquipAll.Enabled = maxPercent == 100; + SetUpDownControlMinMaxes(minPercent, maxPercent, minNumber, maxNumber); + SetEquipmentAmountsFromPercentage(minPercent); + + if (minPercent == 100) + { + rbEquipAll.Checked = true; + percentage.Enabled = false; + rbPercentage.Enabled = false; + numeric.Enabled = false; + rbNumeric.Enabled = false; + } + else + { + rbPercentage.Checked = true; + percentage.Enabled = true; + rbPercentage.Enabled = true; + numeric.Enabled = true; + rbNumeric.Enabled = true; + } + } + else + { + int minNumber = equip.MinNumber; + int maxNumber = equip.MaxNumber; + double minPercent = (double) CalcualtePercentageValueFromNumber(minNumber); + double maxPercent = (double) CalcualtePercentageValueFromNumber(maxNumber); + + percentage.Enabled = false; + rbPercentage.Enabled = false; + rbEquipAll.Enabled = maxNumber == WarFoundryCore.INFINITY; + + SetUpDownControlMinMaxes(minPercent, maxPercent, minNumber, maxNumber); + SetEquipmentAmountsFromNumber(minNumber); + + if (minNumber == WarFoundryCore.INFINITY) + { + numeric.Enabled = false; + rbNumeric.Enabled = false; + } + else + { + rbNumeric.Checked = true; + numeric.Enabled = true; + rbNumeric.Enabled = true; + } + } + } + else + { + Enabled = false; + } + } + + private void SetUpDownControlMinMaxes(double minPercent, double maxPercent, int minNumber, int maxNumber) + { + percentage.ValueChanged -= percentage_ValueChanged; + numeric.ValueChanged -= numeric_ValueChanged; + SetUpDownControlMinMax(percentage, minPercent, maxPercent); + SetUpDownControlMinMax(numeric, (decimal) minNumber, (decimal) maxNumber); + percentage.ValueChanged += percentage_ValueChanged; + numeric.ValueChanged += numeric_ValueChanged; + } + + private void SetUpDownControlMinMax(NumericUpDown upDownControl, double min, double max) + { + SetUpDownControlMinMax(upDownControl, (decimal)min, (decimal)max); + } + + private void SetUpDownControlMinMax(NumericUpDown upDownControl, decimal min, decimal max) + { + upDownControl.Minimum = (min == WarFoundryCore.INFINITY ? unit.Size : min); + upDownControl.Maximum = (max == WarFoundryCore.INFINITY ? unit.Size : max); + } + + private void rbEquipAll_CheckedChanged(object sender, EventArgs e) + { + bool equipAll = rbEquipAll.Checked; + numeric.Enabled = !equipAll; + percentage.Enabled = !equipAll; + + if (equipAll) + { + numeric.Value = unit.Size; + percentage.Value = 100; + } + + radioCheckedChanged(sender, e); + } + + private void percentage_ValueChanged(object sender, EventArgs e) + { + SetNumericValueFromPercentage(); + rbEquipAll.Checked = (percentage.Value == 100 && !rbNumeric.Checked); + OnValueChanged(); + } + + private void SetNumericValueFromPercentage() + { + double percent = (double)percentage.Value; + numeric.Value = CalculateNumericValueFromPercentage(percent); + } + + private decimal CalculateNumericValueFromPercentage(double percent) + { + return (decimal) IBBMath.Round(unit.Size * (percent / 100.0), equip.RoundNumberUp); + } + + private void numeric_ValueChanged(object sender, EventArgs e) + { + SetPercentageValueFromNumeric(); + OnValueChanged(); + } + + private void SetPercentageValueFromNumeric() + { + int number = (int)numeric.Value; + percentage.Value = CalcualtePercentageValueFromNumber(number); + } + + private decimal CalcualtePercentageValueFromNumber(int number) + { + decimal percent = 0; + + if (number > 0) + { + percent = (decimal) Math.Round((number / (unit.Size * 1.0)) * 100, 1); + } + else if (number == WarFoundryCore.INFINITY) + { + percent = 100; + } + + return percent; + } + + public double EquipmentAmount + { + get + { + double val = 0; + + if (rbNumeric.Checked) + { + val = (double) numeric.Value; + } + else if (rbPercentage.Checked) + { + val = (double) percentage.Value; + } + else if (rbEquipAll.Checked) + { + val = WarFoundryCore.INFINITY; + } + else + { + val = 0; + } + + return val; + } + } + + public bool IsRatioEquipmentAmount + { + get + { + return rbPercentage.Checked; + } + } + + private void SetUnitEquipmentItemAmount() + { + double equipAmountNum = unit.GetEquipmentAmount(equip); + + if (equipAmountNum > 0) + { + bool isRatio = unit.GetEquipmentAmountIsRatio(equip); + + if (isRatio) + { + SetEquipmentAmountsFromPercentage(equipAmountNum); + } + else if (equipAmountNum == WarFoundryCore.INFINITY) + { + rbEquipAll.Checked = true; + } + else + { + int equipAmount = (int) equipAmountNum; + SetEquipmentAmountsFromNumber(equipAmount); + } + } + } + + private void SetEquipmentAmountsFromPercentage(double equipAmountNum) + { + numeric.Value = CalculateNumericValueFromPercentage(equipAmountNum); + percentage.Value = (decimal) equipAmountNum; + } + + private void SetEquipmentAmountsFromNumber(int equipAmount) + { + rbEquipAll.Checked = (equipAmount == WarFoundryCore.INFINITY); + equipAmount = (equipAmount == WarFoundryCore.INFINITY ? unit.Size : equipAmount); + percentage.Value = CalcualtePercentageValueFromNumber(equipAmount); + numeric.Value = equipAmount; + } + + private void radioCheckedChanged(object sender, EventArgs e) + { + OnValueChanged(); + } + } +}