Mercurial > repos > IBDev-IBBoard.WarFoundry.GUI.WinForms
comparison UI/EquipmentAmountControl.cs @ 97:e356134d73c4
Re #209: equipmentslot Value problem
* Add lots of debugging in the hope of tracking down Snowblizz's continued problems!
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 16 Nov 2009 20:56:54 +0000 |
parents | 057498981bde |
children | c7afc7824f50 |
comparison
equal
deleted
inserted
replaced
96:057498981bde | 97:e356134d73c4 |
---|---|
3 using System.ComponentModel; | 3 using System.ComponentModel; |
4 using System.Drawing; | 4 using System.Drawing; |
5 using System.Data; | 5 using System.Data; |
6 using System.Text; | 6 using System.Text; |
7 using System.Windows.Forms; | 7 using System.Windows.Forms; |
8 using log4net; | |
8 using IBBoard.CustomMath; | 9 using IBBoard.CustomMath; |
9 using IBBoard.Lang; | 10 using IBBoard.Lang; |
10 using IBBoard.Limits; | 11 using IBBoard.Limits; |
11 using IBBoard.WarFoundry.API; | 12 using IBBoard.WarFoundry.API; |
12 using IBBoard.WarFoundry.API.Objects; | 13 using IBBoard.WarFoundry.API.Objects; |
14 | 15 |
15 namespace IBBoard.WarFoundry.GUI.WinForms.UI | 16 namespace IBBoard.WarFoundry.GUI.WinForms.UI |
16 { | 17 { |
17 public partial class EquipmentAmountControl : UserControl | 18 public partial class EquipmentAmountControl : UserControl |
18 { | 19 { |
20 private ILog log = LogManager.GetLogger(typeof(EquipmentAmountControl)); | |
19 private Unit unit; | 21 private Unit unit; |
20 private UnitEquipmentItem equip; | 22 private UnitEquipmentItem equip; |
21 public event EventHandler ValueChanged; | 23 public event EventHandler ValueChanged; |
22 | 24 |
23 public EquipmentAmountControl() | 25 public EquipmentAmountControl() |
27 } | 29 } |
28 | 30 |
29 public void SetUnit(Unit equipUnit) | 31 public void SetUnit(Unit equipUnit) |
30 { | 32 { |
31 unit = equipUnit; | 33 unit = equipUnit; |
34 log.Debug("Set unit to: " + (unit == null ? "null" : unit.Name)); | |
32 } | 35 } |
33 | 36 |
34 public void SetUnitEquipmentItem(UnitEquipmentItem unitEquipment) | 37 public void SetUnitEquipmentItem(UnitEquipmentItem unitEquipment) |
35 { | 38 { |
36 equip = unitEquipment; | 39 equip = unitEquipment; |
40 log.Debug("Set equipment to: " + (equip == null ? "null" : equip.Name)); | |
37 SetWidgetValues(); | 41 SetWidgetValues(); |
38 } | 42 } |
39 | 43 |
40 private void OnValueChanged() | 44 private void OnValueChanged() |
41 { | 45 { |
47 | 51 |
48 private void SetWidgetValues() | 52 private void SetWidgetValues() |
49 { | 53 { |
50 if (equip != null) | 54 if (equip != null) |
51 { | 55 { |
56 log.Debug("Equipment update"); | |
52 bool equipIsRatioLimit = UnitEquipmentUtil.IsEquipmentRatioLimited(unit, equip); | 57 bool equipIsRatioLimit = UnitEquipmentUtil.IsEquipmentRatioLimited(unit, equip); |
58 log.Debug("Equipment is ratio? " + (equipIsRatioLimit ? "yes" : "no")); | |
53 double maxPercent = RoundPercentage(UnitEquipmentUtil.GetMaxEquipmentPercentage(unit, equip)); | 59 double maxPercent = RoundPercentage(UnitEquipmentUtil.GetMaxEquipmentPercentage(unit, equip)); |
60 log.Debug("Equipment max percentage: " + maxPercent); | |
54 double minPercent = RoundPercentage(UnitEquipmentUtil.GetMinEquipmentPercentage(unit, equip)); | 61 double minPercent = RoundPercentage(UnitEquipmentUtil.GetMinEquipmentPercentage(unit, equip)); |
62 log.Debug("Equipment min percentage: " + minPercent); | |
55 int maxNumber = UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip); | 63 int maxNumber = UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip); |
64 log.Debug("Equipment max count: " + maxNumber); | |
56 int minNumber = UnitEquipmentUtil.GetMinEquipmentCount(unit, equip); | 65 int minNumber = UnitEquipmentUtil.GetMinEquipmentCount(unit, equip); |
66 log.Debug("Equipment min count: " + minNumber); | |
57 | 67 |
58 SetUpDownControlMinMaxes(minPercent, maxPercent, minNumber, maxNumber); | 68 SetUpDownControlMinMaxes(minPercent, maxPercent, minNumber, maxNumber); |
59 | 69 |
60 if (equipIsRatioLimit) | 70 if (equipIsRatioLimit) |
61 { | 71 { |
66 SetEquipmentAmountsFromNumber(minNumber); | 76 SetEquipmentAmountsFromNumber(minNumber); |
67 } | 77 } |
68 | 78 |
69 rbEquipAll.Enabled = equipIsRatioLimit && maxPercent == 100; | 79 rbEquipAll.Enabled = equipIsRatioLimit && maxPercent == 100; |
70 rbEquipAll.Checked = equipIsRatioLimit && minPercent == 100; | 80 rbEquipAll.Checked = equipIsRatioLimit && minPercent == 100; |
81 log.Debug("rbEquipAll: " + (rbEquipAll.Enabled ? "enabled":"disabled") + " " + (rbEquipAll.Checked ? "checked" : "unchecked")); | |
71 percentage.Enabled = equipIsRatioLimit && minPercent != 100; | 82 percentage.Enabled = equipIsRatioLimit && minPercent != 100; |
72 rbPercentage.Enabled = percentage.Enabled; | 83 rbPercentage.Enabled = percentage.Enabled; |
73 rbPercentage.Checked = equipIsRatioLimit && !rbEquipAll.Checked; | 84 rbPercentage.Checked = equipIsRatioLimit && !rbEquipAll.Checked; |
85 log.Debug("rbPercentage: " + (rbPercentage.Enabled ? "enabled" : "disabled") + " " + (rbPercentage.Checked ? "checked" : "unchecked")); | |
74 numeric.Enabled = !equipIsRatioLimit || minPercent != 100; | 86 numeric.Enabled = !equipIsRatioLimit || minPercent != 100; |
75 rbNumeric.Enabled = numeric.Enabled; | 87 rbNumeric.Enabled = numeric.Enabled; |
76 rbNumeric.Checked = !equipIsRatioLimit; | 88 rbNumeric.Checked = !equipIsRatioLimit; |
77 | 89 log.Debug("rbNumeric: " + (rbNumeric.Enabled ? "enabled" : "disabled") + " " + (rbNumeric.Checked ? "checked" : "unchecked")); |
78 SetUnitEquipmentItemAmount(); | 90 SetUnitEquipmentItemAmount(); |
79 } | 91 } |
80 else | 92 else |
81 { | 93 { |
94 log.Debug("Null equipment - no widget update"); | |
82 Enabled = false; | 95 Enabled = false; |
83 } | 96 } |
84 } | 97 } |
85 | 98 |
86 private void SetUpDownControlMinMaxes(double minPercent, double maxPercent, int minNumber, int maxNumber) | 99 private void SetUpDownControlMinMaxes(double minPercent, double maxPercent, int minNumber, int maxNumber) |
98 SetUpDownControlMinMax(upDownControl, (decimal)min, (decimal)max); | 111 SetUpDownControlMinMax(upDownControl, (decimal)min, (decimal)max); |
99 } | 112 } |
100 | 113 |
101 private void SetUpDownControlMinMax(NumericUpDown upDownControl, decimal min, decimal max) | 114 private void SetUpDownControlMinMax(NumericUpDown upDownControl, decimal min, decimal max) |
102 { | 115 { |
116 log.Debug("Set "+upDownControl.Name+" min and max to: "+min+", "+max); | |
103 upDownControl.Minimum = min; | 117 upDownControl.Minimum = min; |
104 upDownControl.Maximum = max; | 118 upDownControl.Maximum = max; |
105 } | 119 } |
106 | 120 |
107 private void rbEquipAll_CheckedChanged(object sender, EventArgs e) | 121 private void rbEquipAll_CheckedChanged(object sender, EventArgs e) |
119 radioCheckedChanged(sender, e); | 133 radioCheckedChanged(sender, e); |
120 } | 134 } |
121 | 135 |
122 private void percentage_ValueChanged(object sender, EventArgs e) | 136 private void percentage_ValueChanged(object sender, EventArgs e) |
123 { | 137 { |
138 log.Debug("Percentage value changed"); | |
124 SetNumericValueFromPercentage(); | 139 SetNumericValueFromPercentage(); |
125 rbEquipAll.Checked = (percentage.Value == 100 && !rbNumeric.Checked); | 140 rbEquipAll.Checked = (percentage.Value == 100 && !rbNumeric.Checked); |
126 OnValueChanged(); | 141 OnValueChanged(); |
127 } | 142 } |
128 | 143 |
134 } | 149 } |
135 | 150 |
136 private decimal CalculateNumericValueFromPercentage(decimal percent) | 151 private decimal CalculateNumericValueFromPercentage(decimal percent) |
137 { | 152 { |
138 decimal calcedAmount = (decimal) IBBoard.CustomMath.IBBMath.Round((double)(unit.Size * (percent / 100)), equip.RoundNumberUp); | 153 decimal calcedAmount = (decimal) IBBoard.CustomMath.IBBMath.Round((double)(unit.Size * (percent / 100)), equip.RoundNumberUp); |
154 log.Debug("Numeric value calculated from percentage: "+percent+"% -> "+calcedAmount); | |
139 return Math.Min(Math.Max(calcedAmount, numeric.Minimum), numeric.Maximum); | 155 return Math.Min(Math.Max(calcedAmount, numeric.Minimum), numeric.Maximum); |
140 } | 156 } |
141 | 157 |
142 private void numeric_ValueChanged(object sender, EventArgs e) | 158 private void numeric_ValueChanged(object sender, EventArgs e) |
143 { | 159 { |
160 log.Debug("Numeric value changed"); | |
144 SetPercentageValueFromNumeric(); | 161 SetPercentageValueFromNumeric(); |
145 OnValueChanged(); | 162 OnValueChanged(); |
146 } | 163 } |
147 | 164 |
148 private void SetPercentageValueFromNumeric() | 165 private void SetPercentageValueFromNumeric() |
154 } | 171 } |
155 | 172 |
156 private decimal CalcualtePercentageValueFromNumber(int number) | 173 private decimal CalcualtePercentageValueFromNumber(int number) |
157 { | 174 { |
158 decimal calcedAmount = (decimal) RoundPercentage(IBBoard.CustomMath.IBBMath.Percentage(number, unit.Size)); | 175 decimal calcedAmount = (decimal) RoundPercentage(IBBoard.CustomMath.IBBMath.Percentage(number, unit.Size)); |
176 log.Debug("Numeric value calculated from percentage: " + number + " -> " + calcedAmount+"%"); | |
159 return Math.Min(Math.Max(calcedAmount, percentage.Minimum), percentage.Maximum); | 177 return Math.Min(Math.Max(calcedAmount, percentage.Minimum), percentage.Maximum); |
160 } | 178 } |
161 | 179 |
162 private double RoundPercentage(double percent) | 180 private double RoundPercentage(double percent) |
163 { | 181 { |
227 rbNumeric.Checked = true; | 245 rbNumeric.Checked = true; |
228 } | 246 } |
229 } | 247 } |
230 } | 248 } |
231 | 249 |
232 private void SetEquipmentAmountsFromPercentage(double equipAmountNum) | 250 private void SetEquipmentAmountsFromPercentage(double equipAmount) |
233 { | 251 { |
234 decimal decEquipAmount = (decimal) equipAmountNum; | 252 log.Debug("Set equipment amount from percentage: " + equipAmount); |
253 decimal decEquipAmount = (decimal) equipAmount; | |
235 | 254 |
236 if (decEquipAmount > percentage.Maximum) | 255 if (decEquipAmount > percentage.Maximum) |
237 { | 256 { |
238 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.", equipAmountNum, percentage.Maximum); | 257 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, percentage.Maximum); |
239 string percentageTooLargeTitle = Translation.GetTranslation("equipPercentageTooLargeTitle", "Equipment percentage too large"); | 258 string percentageTooLargeTitle = Translation.GetTranslation("equipPercentageTooLargeTitle", "Equipment percentage too large"); |
240 MessageBox.Show(ParentForm, percentageTooLarge, percentageTooLargeTitle); | 259 MessageBox.Show(ParentForm, percentageTooLarge, percentageTooLargeTitle); |
241 decEquipAmount = percentage.Maximum; | 260 decEquipAmount = percentage.Maximum; |
261 log.Debug("Limited equipment amount to " + decEquipAmount); | |
242 } | 262 } |
243 else if (decEquipAmount < percentage.Minimum) | 263 else if (decEquipAmount < percentage.Minimum) |
244 { | 264 { |
245 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.", equipAmountNum, percentage.Minimum); | 265 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, percentage.Minimum); |
246 string percentageTooSmallTitle = Translation.GetTranslation("equipPercentageTooSmallTitle", "Equipment percentage too small"); | 266 string percentageTooSmallTitle = Translation.GetTranslation("equipPercentageTooSmallTitle", "Equipment percentage too small"); |
247 MessageBox.Show(ParentForm, percentageTooSmall, percentageTooSmallTitle); | 267 MessageBox.Show(ParentForm, percentageTooSmall, percentageTooSmallTitle); |
248 decEquipAmount = percentage.Minimum; | 268 decEquipAmount = percentage.Minimum; |
269 log.Debug("Limited equipment amount to " + decEquipAmount); | |
249 } | 270 } |
250 | 271 |
251 numeric.Value = CalculateNumericValueFromPercentage(decEquipAmount); | 272 numeric.Value = CalculateNumericValueFromPercentage(decEquipAmount); |
252 percentage.Value = decEquipAmount; | 273 percentage.Value = decEquipAmount; |
253 } | 274 } |
254 | 275 |
255 private void SetEquipmentAmountsFromNumber(int equipAmount) | 276 private void SetEquipmentAmountsFromNumber(int equipAmount) |
256 { | 277 { |
278 log.Debug("Set equipment percentage from amount: " + equipAmount); | |
279 | |
257 if (equipAmount > numeric.Maximum) | 280 if (equipAmount > numeric.Maximum) |
258 { | 281 { |
259 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, numeric.Maximum); | 282 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, numeric.Maximum); |
260 string amountTooLargeTitle = Translation.GetTranslation("equipNumberTooLargeTitle", "Equipment amount too large"); | 283 string amountTooLargeTitle = Translation.GetTranslation("equipNumberTooLargeTitle", "Equipment amount too large"); |
261 MessageBox.Show(ParentForm, amountTooLarge, amountTooLargeTitle); | 284 MessageBox.Show(ParentForm, amountTooLarge, amountTooLargeTitle); |
262 equipAmount = (int)numeric.Maximum; | 285 equipAmount = (int)numeric.Maximum; |
286 log.Debug("Limited equipment amount to " + equipAmount); | |
263 } | 287 } |
264 else if (equipAmount < numeric.Minimum) | 288 else if (equipAmount < numeric.Minimum) |
265 { | 289 { |
266 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, numeric.Minimum); | 290 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, numeric.Minimum); |
267 string amountTooSmallTitle = Translation.GetTranslation("equipNumberTooSmallTitle", "Equipment amount too small"); | 291 string amountTooSmallTitle = Translation.GetTranslation("equipNumberTooSmallTitle", "Equipment amount too small"); |
268 MessageBox.Show(ParentForm, amountTooSmall, amountTooSmallTitle); | 292 MessageBox.Show(ParentForm, amountTooSmall, amountTooSmallTitle); |
269 equipAmount = (int) numeric.Minimum; | 293 equipAmount = (int) numeric.Minimum; |
294 log.Debug("Limited equipment amount to " + equipAmount); | |
270 } | 295 } |
271 | 296 |
272 percentage.Value = CalcualtePercentageValueFromNumber(equipAmount); | 297 percentage.Value = CalcualtePercentageValueFromNumber(equipAmount); |
273 numeric.Value = equipAmount; | 298 numeric.Value = equipAmount; |
274 } | 299 } |