comparison UI/EquipmentAmountControl.cs @ 82:9dc22147c2db

Re #204: Use new limits in WinForms UI * Convert equipment amount control over to new limits
author IBBoard <dev@ibboard.co.uk>
date Thu, 29 Oct 2009 20:49:44 +0000
parents e749b748e7ea
children 6d5cb8c7b6ed
comparison
equal deleted inserted replaced
81:e749b748e7ea 82:9dc22147c2db
7 using System.Windows.Forms; 7 using System.Windows.Forms;
8 using IBBoard.CustomMath; 8 using IBBoard.CustomMath;
9 using IBBoard.Limits; 9 using IBBoard.Limits;
10 using IBBoard.WarFoundry.API; 10 using IBBoard.WarFoundry.API;
11 using IBBoard.WarFoundry.API.Objects; 11 using IBBoard.WarFoundry.API.Objects;
12 using IBBoard.WarFoundry.API.Util;
12 13
13 namespace IBBoard.WarFoundry.GUI.WinForms.UI 14 namespace IBBoard.WarFoundry.GUI.WinForms.UI
14 { 15 {
15 public partial class EquipmentAmountControl : UserControl 16 public partial class EquipmentAmountControl : UserControl
16 { 17 {
17 private Unit unit; 18 private Unit unit;
18 private UnitEquipmentItem equip; 19 private UnitEquipmentItem equip;
20 public event EventHandler ValueChanged;
19 21
20 public EquipmentAmountControl() 22 public EquipmentAmountControl()
21 { 23 {
22 InitializeComponent(); 24 InitializeComponent();
23 } 25 }
32 equip = unitEquipment; 34 equip = unitEquipment;
33 SetWidgetValues(); 35 SetWidgetValues();
34 SetUnitEquipmentItemAmount(); 36 SetUnitEquipmentItemAmount();
35 } 37 }
36 38
37 public event EventHandler ValueChanged;
38
39 private void OnValueChanged() 39 private void OnValueChanged()
40 { 40 {
41 if (ValueChanged != null) 41 if (ValueChanged != null)
42 { 42 {
43 ValueChanged(this, new EventArgs()); 43 ValueChanged(this, new EventArgs());
46 46
47 private void SetWidgetValues() 47 private void SetWidgetValues()
48 { 48 {
49 if (equip != null) 49 if (equip != null)
50 { 50 {
51 AbstractLimit limit = unit.UnitType.GetEquipmentSlotLimit(equip.SlotName); 51 bool equipIsRatioLimit = UnitEquipmentUtil.IsEquipmentRatioLimited(unit, equip);
52 52 double maxPercent = UnitEquipmentUtil.GetMaxEquipmentPercentage(unit, equip);
53 if (equip.IsRatioLimit && limit is IPercentageLimit) 53 double minPercent = UnitEquipmentUtil.GetMinEquipmentPercentage(unit, equip);
54 { 54 int maxNumber = UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip);
55 double minPercent = ((IPercentageLimit)equip.MinLimit).Percentage; 55 int minNumber = UnitEquipmentUtil.GetMinEquipmentCount(unit, equip);
56 double maxPercent = ((IPercentageLimit)equip.MaxLimit).Percentage; 56
57 int minNumber = (int) CalculateNumericValueFromPercentage(minPercent); 57 SetUpDownControlMinMaxes(minPercent, maxPercent, minNumber, maxNumber);
58 int maxNumber = (int) CalculateNumericValueFromPercentage(maxPercent); 58 SetEquipmentAmountsFromPercentage(equipIsRatioLimit ? minPercent : minNumber);
59 59
60 rbEquipAll.Enabled = maxPercent == 100; 60 rbEquipAll.Enabled = equipIsRatioLimit && maxPercent == 100;
61 SetUpDownControlMinMaxes(minPercent, maxPercent, minNumber, maxNumber); 61 rbEquipAll.Checked = equipIsRatioLimit && minPercent == 100;
62 SetEquipmentAmountsFromPercentage(minPercent); 62 percentage.Enabled = equipIsRatioLimit;
63 63 rbPercentage.Enabled = percentage.Enabled;
64 if (minPercent == 100) 64 rbPercentage.Checked = equipIsRatioLimit && !rbEquipAll.Checked;
65 { 65 numeric.Enabled = !equipIsRatioLimit || minPercent != 100;
66 rbEquipAll.Checked = true; 66 rbNumeric.Enabled = numeric.Enabled;
67 percentage.Enabled = false; 67 rbNumeric.Checked = !equipIsRatioLimit;
68 rbPercentage.Enabled = false;
69 numeric.Enabled = false;
70 rbNumeric.Enabled = false;
71 }
72 else
73 {
74 rbPercentage.Checked = true;
75 percentage.Enabled = true;
76 rbPercentage.Enabled = true;
77 numeric.Enabled = true;
78 rbNumeric.Enabled = true;
79 }
80 }
81 else
82 {
83 int minNumber = equip.MinLimit.GetLimit(unit.Size);
84 int maxNumber = GetMaxNumber(limit);
85 double minPercent = (double) CalcualtePercentageValueFromNumber(minNumber);
86 double maxPercent = (double) CalcualtePercentageValueFromNumber(maxNumber);
87
88 percentage.Enabled = false;
89 rbPercentage.Enabled = false;
90 rbEquipAll.Enabled = maxNumber == WarFoundryCore.INFINITY;
91
92 SetUpDownControlMinMaxes(minPercent, maxPercent, minNumber, maxNumber);
93 SetEquipmentAmountsFromNumber(minNumber);
94
95 if (minNumber == WarFoundryCore.INFINITY)
96 {
97 numeric.Enabled = false;
98 rbNumeric.Enabled = false;
99 }
100 else
101 {
102 rbNumeric.Checked = true;
103 numeric.Enabled = true;
104 rbNumeric.Enabled = true;
105 }
106 }
107 } 68 }
108 else 69 else
109 { 70 {
110 Enabled = false; 71 Enabled = false;
111 } 72 }
112 }
113
114 private int GetMaxNumber(AbstractLimit limit)
115 {
116 int maxNumber = equip.MaxLimit.GetLimit(unit.Size);
117
118 if (!(limit is UnlimitedLimit))
119 {
120 int slotMax = limit.GetLimit(unit.Size) - unit.GetEquipmentAmountInSlot(equip.SlotName);
121 maxNumber = Math.Min(slotMax, maxNumber);
122 }
123
124 return maxNumber;
125 } 73 }
126 74
127 private void SetUpDownControlMinMaxes(double minPercent, double maxPercent, int minNumber, int maxNumber) 75 private void SetUpDownControlMinMaxes(double minPercent, double maxPercent, int minNumber, int maxNumber)
128 { 76 {
129 percentage.ValueChanged -= percentage_ValueChanged; 77 percentage.ValueChanged -= percentage_ValueChanged;
139 SetUpDownControlMinMax(upDownControl, (decimal)min, (decimal)max); 87 SetUpDownControlMinMax(upDownControl, (decimal)min, (decimal)max);
140 } 88 }
141 89
142 private void SetUpDownControlMinMax(NumericUpDown upDownControl, decimal min, decimal max) 90 private void SetUpDownControlMinMax(NumericUpDown upDownControl, decimal min, decimal max)
143 { 91 {
144 upDownControl.Minimum = (min == WarFoundryCore.INFINITY ? unit.Size : min); 92 upDownControl.Minimum = min;
145 upDownControl.Maximum = (max == WarFoundryCore.INFINITY ? unit.Size : max); 93 upDownControl.Maximum = max;
146 } 94 }
147 95
148 private void rbEquipAll_CheckedChanged(object sender, EventArgs e) 96 private void rbEquipAll_CheckedChanged(object sender, EventArgs e)
149 { 97 {
150 bool equipAll = rbEquipAll.Checked; 98 bool equipAll = rbEquipAll.Checked;
190 percentage.Value = CalcualtePercentageValueFromNumber(number); 138 percentage.Value = CalcualtePercentageValueFromNumber(number);
191 } 139 }
192 140
193 private decimal CalcualtePercentageValueFromNumber(int number) 141 private decimal CalcualtePercentageValueFromNumber(int number)
194 { 142 {
195 decimal percent = 0; 143 return (decimal) Math.Round((number / (unit.Size * 1.0)) * 100, 1);
196
197 if (number > 0)
198 {
199 percent = (decimal) Math.Round((number / (unit.Size * 1.0)) * 100, 1);
200 }
201 else if (number == WarFoundryCore.INFINITY)
202 {
203 percent = 100;
204 }
205
206 return percent;
207 } 144 }
208 145
209 public double EquipmentAmount 146 public double EquipmentAmount
210 { 147 {
211 get 148 get
220 { 157 {
221 val = (double) percentage.Value; 158 val = (double) percentage.Value;
222 } 159 }
223 else if (rbEquipAll.Checked) 160 else if (rbEquipAll.Checked)
224 { 161 {
225 val = WarFoundryCore.INFINITY; 162 val = 100;
226 } 163 }
227 else 164 else
228 { 165 {
229 val = 0; 166 val = 0;
230 } 167 }
235 172
236 public bool IsRatioEquipmentAmount 173 public bool IsRatioEquipmentAmount
237 { 174 {
238 get 175 get
239 { 176 {
240 return rbPercentage.Checked; 177 return !rbNumeric.Checked;
241 } 178 }
242 } 179 }
243 180
244 private void SetUnitEquipmentItemAmount() 181 private void SetUnitEquipmentItemAmount()
245 { 182 {