Mercurial > repos > IBDev-IBBoard.WarFoundry.GUI.GTK
comparison UIControl/AddEquipmentUIControl.cs @ 63:c2d79b4209e3
* FrmAddEquipment.cs: Re #60: Add UI to add/remove/edit weapons in
GTK
* Cache more values in the controller so that we don't rely on the UI
as a the model/data store
* Move enable/disable of amount controls to controller (assumes
minimum numeric and percentage, so implementation has its own custom
implementation to handle "Equip All")
* AddEquipmentUIControl.cs:
* IAddEquipmentUI.cs:
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 30 Aug 2010 19:44:35 +0000 |
parents | f733073967a2 |
children | e3fe48c4d794 |
comparison
equal
deleted
inserted
replaced
62:f733073967a2 | 63:c2d79b4209e3 |
---|---|
11 using IBBoard.Lang; | 11 using IBBoard.Lang; |
12 namespace IBBoard.WarFoundry.GUI.GTK.UIControl | 12 namespace IBBoard.WarFoundry.GUI.GTK.UIControl |
13 { | 13 { |
14 public class AddEquipmentUIControl | 14 public class AddEquipmentUIControl |
15 { | 15 { |
16 private Unit unit; | |
17 private CommandStack commandStack; | 16 private CommandStack commandStack; |
18 private IAddEquipmentUI ui; | 17 private IAddEquipmentUI ui; |
18 private Unit unit; | |
19 private UnitEquipmentItem equipItem; | |
20 private double minPercentage, maxPercentage; | |
21 private int minNumber, maxNumber; | |
22 private bool isRatioAmount; | |
23 private double equipmentAmount; | |
24 | |
19 | 25 |
20 public AddEquipmentUIControl(Unit unit, CommandStack commandStack) | 26 public AddEquipmentUIControl(Unit unit, CommandStack commandStack) |
21 { | 27 { |
22 this.unit = unit; | 28 this.unit = unit; |
23 this.commandStack = commandStack; | 29 this.commandStack = commandStack; |
36 ui.UnitEquipmentAmountTypeChanged += HandleUnitEquipmentAmountChanged; | 42 ui.UnitEquipmentAmountTypeChanged += HandleUnitEquipmentAmountChanged; |
37 } | 43 } |
38 | 44 |
39 private void HandleUnitEquipmentAmountChanged() | 45 private void HandleUnitEquipmentAmountChanged() |
40 { | 46 { |
41 ui.SetOkayEnabledState(ui.SelectedUnitEquipmentItem != null && HasNonZeroEquipmentAmount()); | 47 ui.SetOkayEnabledState(equipItem != null && HasNonZeroEquipmentAmount()); |
42 | 48 isRatioAmount = ui.IsRatioEquipmentAmount; |
43 if (ui.IsRatioEquipmentAmount) | 49 |
44 { | 50 if (isRatioAmount) |
45 SetEquipmentAmountsFromPercentage(ui.EquipmentPercentageAmount); | 51 { |
52 double equipmentAmount = ui.EquipmentPercentageAmount; | |
53 SetEquipmentAmountsFromPercentage(equipmentAmount); | |
46 } | 54 } |
47 else | 55 else |
48 { | 56 { |
49 SetEquipmentAmountsFromNumber(ui.EquipmentNumericAmount); | 57 int equipmentIntAmount = ui.EquipmentNumericAmount; |
58 equipmentAmount = equipmentIntAmount; | |
59 SetEquipmentAmountsFromNumber(equipmentIntAmount); | |
50 } | 60 } |
51 } | 61 } |
52 | 62 |
53 private void SetEquipmentAmountsFromPercentage(double equipAmount) | 63 private void SetEquipmentAmountsFromPercentage(double equipAmount) |
54 { | 64 { |
55 UnitEquipmentItem currEquipItem = ui.SelectedUnitEquipmentItem; | |
56 | |
57 double maxPercentage = GetMaxPercentageLimit(currEquipItem); | |
58 double minPercentage = GetMinPercentageLimit(currEquipItem); | |
59 | |
60 if (equipAmount > maxPercentage) | 65 if (equipAmount > maxPercentage) |
61 { | 66 { |
62 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, maxPercentage); | 67 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, maxPercentage); |
63 string percentageTooLargeTitle = Translation.GetTranslation("equipPercentageTooLargeTitle", "equipment percentage too large"); | 68 string percentageTooLargeTitle = Translation.GetTranslation("equipPercentageTooLargeTitle", "equipment percentage too large"); |
64 // MessageBox.Show(ParentForm, percentageTooLarge, percentageTooLargeTitle); | 69 // MessageBox.Show(ParentForm, percentageTooLarge, percentageTooLargeTitle); |
70 string percentageTooSmallTitle = Translation.GetTranslation("equipPercentageTooSmallTitle", "equipment percentage too small"); | 75 string percentageTooSmallTitle = Translation.GetTranslation("equipPercentageTooSmallTitle", "equipment percentage too small"); |
71 // MessageBox.Show(ParentForm, percentageTooSmall, percentageTooSmallTitle); | 76 // MessageBox.Show(ParentForm, percentageTooSmall, percentageTooSmallTitle); |
72 equipAmount = minPercentage; | 77 equipAmount = minPercentage; |
73 } | 78 } |
74 | 79 |
75 ui.EquipmentNumericAmount = CalculateNumericValueFromPercentage(equipAmount, currEquipItem); | 80 ui.EquipmentNumericAmount = CalculateNumericValueFromPercentage(equipAmount); |
76 ui.EquipmentPercentageAmount = equipAmount; | 81 ui.EquipmentPercentageAmount = equipAmount; |
77 } | 82 } |
78 | 83 |
79 private int CalculateNumericValueFromPercentage(double percent, UnitEquipmentItem equip) | 84 private int CalculateNumericValueFromPercentage(double percent) |
80 { | 85 { |
81 int calcedAmount = (int)CustomMath.IBBMath.Round((double)(unit.Size * (percent / 100)), equip.RoundNumberUp); | 86 int calcedAmount = (int)CustomMath.IBBMath.Round((unit.Size * (percent / 100.0)), equipItem.RoundNumberUp); |
82 return Math.Min(Math.Max(calcedAmount, GetMinNumericLimit(equip)), GetMaxNumericLimit(equip)); | 87 return Math.Min(Math.Max(calcedAmount, minNumber), maxNumber); |
83 } | 88 } |
84 | 89 |
85 private void SetEquipmentAmountsFromNumber(int equipAmount) | 90 private void SetEquipmentAmountsFromNumber(int equipAmount) |
86 { | 91 { |
87 UnitEquipmentItem currEquipItem = ui.SelectedUnitEquipmentItem; | |
88 int maxNumber = GetMaxNumericLimit(currEquipItem); | |
89 int minNumber = GetMinNumericLimit(currEquipItem); | |
90 | |
91 if (equipAmount > maxNumber) | 92 if (equipAmount > maxNumber) |
92 { | 93 { |
93 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, maxNumber); | 94 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, maxNumber); |
94 string amountTooLargeTitle = Translation.GetTranslation("equipNumberTooLargeTitle", "equipment amount too large"); | 95 string amountTooLargeTitle = Translation.GetTranslation("equipNumberTooLargeTitle", "equipment amount too large"); |
95 //MessageBox.Show(ParentForm, amountTooLarge, amountTooLargeTitle); | 96 //MessageBox.Show(ParentForm, amountTooLarge, amountTooLargeTitle); |
101 string amountTooSmallTitle = Translation.GetTranslation("equipNumberTooSmallTitle", "equipment amount too small"); | 102 string amountTooSmallTitle = Translation.GetTranslation("equipNumberTooSmallTitle", "equipment amount too small"); |
102 //MessageBox.Show(ParentForm, amountTooSmall, amountTooSmallTitle); | 103 //MessageBox.Show(ParentForm, amountTooSmall, amountTooSmallTitle); |
103 equipAmount = minNumber; | 104 equipAmount = minNumber; |
104 } | 105 } |
105 | 106 |
106 ui.EquipmentPercentageAmount = CalcualtePercentageValueFromNumber(equipAmount, currEquipItem); | 107 ui.EquipmentPercentageAmount = CalcualtePercentageValueFromNumber(equipAmount); |
107 ui.EquipmentNumericAmount = equipAmount; | 108 ui.EquipmentNumericAmount = equipAmount; |
108 } | 109 } |
109 | 110 |
110 private double CalcualtePercentageValueFromNumber(int number, UnitEquipmentItem equip) | 111 private double CalcualtePercentageValueFromNumber(int number) |
111 { | 112 { |
112 double calcedAmount = RoundPercentage(CustomMath.IBBMath.Percentage(number, unit.Size)); | 113 double calcedAmount = RoundPercentage(CustomMath.IBBMath.Percentage(number, unit.Size)); |
113 return Math.Min(Math.Max(calcedAmount, GetMinPercentageLimit(equip)), GetMaxPercentageLimit(equip)); | 114 return Math.Min(Math.Max(calcedAmount, minPercentage), maxPercentage); |
114 } | 115 } |
115 | 116 |
116 //TODO Make abstract | 117 //TODO Make abstract |
117 protected void CreateEquipmentUI() | 118 protected void CreateEquipmentUI() |
118 { | 119 { |
119 ui = new FrmAddEquipment(); | 120 ui = new FrmAddEquipment(); |
120 } | 121 } |
121 | 122 |
122 private void HandleUiUnitEquipmentItemChoiceChanged(UnitEquipmentItem equip) | 123 private void HandleUiUnitEquipmentItemChoiceChanged(UnitEquipmentItem equip) |
123 { | 124 { |
125 equipItem = equip; | |
126 | |
124 if (equip != null) | 127 if (equip != null) |
125 { | 128 { |
126 bool equipIsRatioLimit = UnitEquipmentUtil.IsEquipmentRatioLimited(unit, equip); | 129 bool equipIsRatioLimit = UnitEquipmentUtil.IsEquipmentRatioLimited(unit, equip); |
127 double maxPercent = GetMaxPercentageLimit(equip); | 130 maxPercentage = GetMaxPercentageLimit(equip); |
128 double minPercent = GetMinPercentageLimit(equip); | 131 minPercentage = GetMinPercentageLimit(equip); |
129 int maxNumber = GetMaxNumericLimit(equip); | 132 maxNumber = GetMaxNumericLimit(equip); |
130 int minNumber = GetMinNumericLimit(equip); | 133 minNumber = GetMinNumericLimit(equip); |
131 | 134 |
132 ui.SetUnitEquipmentLimits(equipIsRatioLimit, minPercent, maxPercent, minNumber, maxNumber); | 135 ui.SetUnitEquipmentLimits(equipIsRatioLimit, minPercentage, maxPercentage, minNumber, maxNumber); |
133 ui.SetUnitEquipmentLimitsEnabled(true); | 136 ui.SetUnitEquipmentLimitsEnabled(true); |
134 ui.SetOkayEnabledState(HasNonZeroEquipmentAmount()); | 137 ui.SetOkayEnabledState(HasNonZeroEquipmentAmount()); |
138 SetEquipmentAmountControlEnabledStates(equipIsRatioLimit); | |
135 } | 139 } |
136 else | 140 else |
137 { | 141 { |
138 ui.SetUnitEquipmentLimits(true, 0, 0, 0, 0); | 142 maxPercentage = minPercentage = 0; |
143 maxNumber = minNumber = 0; | |
144 ui.SetUnitEquipmentLimits(false, minPercentage, maxPercentage, minNumber, maxNumber); | |
139 ui.SetUnitEquipmentLimitsEnabled(false); | 145 ui.SetUnitEquipmentLimitsEnabled(false); |
140 ui.SetOkayEnabledState(false); | 146 ui.SetOkayEnabledState(false); |
141 } | 147 } |
142 } | 148 } |
149 | |
150 private void SetEquipmentAmountControlEnabledStates(bool ratioLimited) | |
151 { | |
152 ui.SetNumericAmountEnabledState(!ratioLimited); | |
153 ui.SetPercentageAmountEnabledState(true); | |
154 } | |
155 | |
143 private double GetMaxPercentageLimit(UnitEquipmentItem equip) | 156 private double GetMaxPercentageLimit(UnitEquipmentItem equip) |
144 { | 157 { |
145 double maxPercent = RoundPercentage(UnitEquipmentUtil.GetMaxEquipmentPercentage(unit, equip)); | 158 double maxPercent = RoundPercentage(UnitEquipmentUtil.GetMaxEquipmentPercentage(unit, equip)); |
146 return Math.Max(0, maxPercent); | 159 return Math.Max(0, maxPercent); |
147 } | 160 } |
165 | 178 |
166 private bool HasNonZeroEquipmentAmount() | 179 private bool HasNonZeroEquipmentAmount() |
167 { | 180 { |
168 bool nonZero; | 181 bool nonZero; |
169 | 182 |
170 if (ui.IsRatioEquipmentAmount) | 183 if (isRatioAmount) |
171 { | 184 { |
172 nonZero = (ui.EquipmentPercentageAmount > 0); | 185 nonZero = (ui.EquipmentPercentageAmount > 0); |
173 } | 186 } |
174 | 187 |
175 else | 188 else |
188 public void Show() | 201 public void Show() |
189 { | 202 { |
190 bool okayed = ui.ShowControl(); | 203 bool okayed = ui.ShowControl(); |
191 | 204 |
192 if (okayed) | 205 if (okayed) |
193 { | 206 { |
194 UnitEquipmentItem equipItem = ui.SelectedUnitEquipmentItem; | 207 if (isRatioAmount) |
195 | |
196 if (ui.IsRatioEquipmentAmount) | |
197 { | 208 { |
198 commandStack.Execute(new SetUnitEquipmentRatioAmountCommand(unit, equipItem, ui.EquipmentPercentageAmount)); | 209 commandStack.Execute(new SetUnitEquipmentRatioAmountCommand(unit, equipItem, equipmentAmount)); |
199 } | 210 } |
200 else | 211 else |
201 { | 212 { |
202 commandStack.Execute(new SetUnitEquipmentNumericAmountCommand(unit, equipItem, ui.EquipmentNumericAmount)); | 213 commandStack.Execute(new SetUnitEquipmentNumericAmountCommand(unit, equipItem, (int)equipmentAmount)); |
203 } | 214 } |
204 } | 215 } |
205 | 216 |
206 ui.Dispose(); | 217 ui.Dispose(); |
207 } | 218 } |