Mercurial > repos > IBDev-IBBoard.WarFoundry.GUI.GTK
comparison UIControl/AbstractBaseEquipmentUIControl.cs @ 66:100626381159
Re #60: Add UI to add/remove/edit weapons in GTK
* Rip apart UI controls and put all the common code in a base class
* Add additional interfaces for common UI behaviour between equipment widgets
Not tested, but should provide a base for Replace widget
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 04 Sep 2010 20:11:05 +0000 |
parents | |
children | 3b4a646b4054 |
comparison
equal
deleted
inserted
replaced
65:77448375d2f9 | 66:100626381159 |
---|---|
1 // This file (AbstractBaseEquipmentUIControl.cs) is a part of the IBBoard.WarFoundry.GUI.GTK project and is copyright 2010 IBBoard | |
2 // | |
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. | |
4 using System; | |
5 using IBBoard.Commands; | |
6 using IBBoard.WarFoundry.API.Objects; | |
7 using IBBoard.WarFoundry.GUI.GTK.UIControl.Interfaces; | |
8 using IBBoard.Lang; | |
9 using IBBoard.WarFoundry.API.Util; | |
10 namespace IBBoard.WarFoundry.GUI.GTK.UIControl | |
11 { | |
12 public abstract class AbstractBaseEquipmentUIControl<UI_TYPE> where UI_TYPE : IBaseEquipmentUI | |
13 { | |
14 protected CommandStack commandStack; | |
15 protected Unit unit; | |
16 protected UI_TYPE ui; | |
17 protected UnitEquipmentItem equipItem; | |
18 protected double minPercentage, maxPercentage; | |
19 protected int minNumber, maxNumber; | |
20 protected bool isRatioAmount; | |
21 protected double equipmentAmount; | |
22 | |
23 public AbstractBaseEquipmentUIControl(Unit unit, CommandStack commandStack) | |
24 { | |
25 this.unit = unit; | |
26 this.commandStack = commandStack; | |
27 SetupUI(); | |
28 } | |
29 | |
30 private void SetupUI() | |
31 { | |
32 ui = CreateEquipmentUI(); | |
33 ui.SetUnitEquipmentLimitsEnabled(false); | |
34 ui.SetOkayEnabledState(false); | |
35 ui.UnitEquipmentAmountChanged += SetUnitEquipmentValues; | |
36 ui.UnitEquipmentAmountTypeChanged += SetUnitEquipmentValues; | |
37 CompleteUISetup(); | |
38 } | |
39 | |
40 /// <summary> | |
41 /// Creates the UI component that will be displayed to the user and returns it | |
42 /// </summary> | |
43 /// <returns> | |
44 /// the UI component to display to the user | |
45 /// </returns> | |
46 protected abstract UI_TYPE CreateEquipmentUI(); | |
47 | |
48 /// <summary> | |
49 /// Completes any additional user interface setup. | |
50 /// </summary> | |
51 protected virtual void CompleteUISetup() | |
52 { | |
53 //Do nothing | |
54 } | |
55 | |
56 protected void HandleUnitEquipmentAmountChanged() | |
57 { | |
58 SetUnitEquipmentValues(); | |
59 } | |
60 | |
61 /// <summary> | |
62 /// Sets the unit equipment values on the UI | |
63 /// </summary> | |
64 protected void SetUnitEquipmentValues() | |
65 { | |
66 ui.SetOkayEnabledState(HasNonZeroEquipmentAmount()); | |
67 isRatioAmount = ui.IsRatioEquipmentAmount; | |
68 | |
69 if (isRatioAmount) | |
70 { | |
71 equipmentAmount = ui.EquipmentPercentageAmount; | |
72 SetEquipmentAmountsFromPercentage(equipmentAmount); | |
73 } | |
74 | |
75 else | |
76 { | |
77 int equipmentIntAmount = ui.EquipmentNumericAmount; | |
78 equipmentAmount = equipmentIntAmount; | |
79 SetEquipmentAmountsFromNumber(equipmentIntAmount); | |
80 } | |
81 } | |
82 | |
83 private void SetEquipmentAmountsFromPercentage(double equipAmount) | |
84 { | |
85 if (equipAmount > maxPercentage) | |
86 { | |
87 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); | |
88 string percentageTooLargeTitle = Translation.GetTranslation("equipPercentageTooLargeTitle", "equipment percentage too large"); | |
89 // MessageBox.Show(ParentForm, percentageTooLarge, percentageTooLargeTitle); | |
90 equipAmount = maxPercentage; | |
91 } | |
92 | |
93 else if (equipAmount < minPercentage) | |
94 { | |
95 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, minPercentage); | |
96 string percentageTooSmallTitle = Translation.GetTranslation("equipPercentageTooSmallTitle", "equipment percentage too small"); | |
97 // MessageBox.Show(ParentForm, percentageTooSmall, percentageTooSmallTitle); | |
98 equipAmount = minPercentage; | |
99 } | |
100 | |
101 ui.EquipmentNumericAmount = CalculateNumericValueFromPercentage(equipAmount); | |
102 ui.EquipmentPercentageAmount = equipAmount; | |
103 } | |
104 | |
105 private int CalculateNumericValueFromPercentage(double percent) | |
106 { | |
107 int calcedAmount = (int)CustomMath.IBBMath.Round((unit.Size * (percent / 100.0)), equipItem.RoundNumberUp); | |
108 return Math.Min(Math.Max(calcedAmount, minNumber), maxNumber); | |
109 } | |
110 | |
111 private void SetEquipmentAmountsFromNumber(int equipAmount) | |
112 { | |
113 if (equipAmount > maxNumber) | |
114 { | |
115 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); | |
116 string amountTooLargeTitle = Translation.GetTranslation("equipNumberTooLargeTitle", "equipment amount too large"); | |
117 //MessageBox.Show(ParentForm, amountTooLarge, amountTooLargeTitle); | |
118 equipAmount = maxNumber; | |
119 } | |
120 | |
121 else if (equipAmount < minNumber) | |
122 { | |
123 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, minNumber); | |
124 string amountTooSmallTitle = Translation.GetTranslation("equipNumberTooSmallTitle", "equipment amount too small"); | |
125 //MessageBox.Show(ParentForm, amountTooSmall, amountTooSmallTitle); | |
126 equipAmount = minNumber; | |
127 } | |
128 | |
129 ui.EquipmentPercentageAmount = CalcualtePercentageValueFromNumber(equipAmount); | |
130 ui.EquipmentNumericAmount = equipAmount; | |
131 } | |
132 | |
133 private double CalcualtePercentageValueFromNumber(int number) | |
134 { | |
135 double calcedAmount = RoundPercentage(CustomMath.IBBMath.Percentage(number, unit.Size)); | |
136 return Math.Min(Math.Max(calcedAmount, minPercentage), maxPercentage); | |
137 } | |
138 | |
139 protected void SetEquipmentAmountControlEnabledStates() | |
140 { | |
141 ui.SetNumericAmountEnabledState(!isRatioAmount); | |
142 ui.SetPercentageAmountEnabledState(true); | |
143 } | |
144 | |
145 protected double GetMaxPercentageLimit(UnitEquipmentItem equip) | |
146 { | |
147 double maxPercent = RoundPercentage(UnitEquipmentUtil.GetMaxEquipmentPercentage(unit, equip)); | |
148 return Math.Max(0, maxPercent); | |
149 } | |
150 | |
151 protected double GetMinPercentageLimit(UnitEquipmentItem equip) | |
152 { | |
153 double minPercent = RoundPercentage(UnitEquipmentUtil.GetMinEquipmentPercentage(unit, equip)); | |
154 return Math.Max(0, minPercent); | |
155 } | |
156 | |
157 protected int GetMaxNumericLimit(UnitEquipmentItem equip) | |
158 { | |
159 int maxNumber = UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip); | |
160 return Math.Max(0, maxNumber); | |
161 } | |
162 | |
163 protected int GetMinNumericLimit(UnitEquipmentItem equip) | |
164 { | |
165 int minNumber = UnitEquipmentUtil.GetMinEquipmentCount(unit, equip); | |
166 return Math.Max(0, minNumber); | |
167 } | |
168 | |
169 protected bool HasNonZeroEquipmentAmount() | |
170 { | |
171 bool nonZero; | |
172 | |
173 if (isRatioAmount) | |
174 { | |
175 nonZero = (ui.EquipmentPercentageAmount > 0); | |
176 } | |
177 | |
178 | |
179 else | |
180 { | |
181 nonZero = (ui.EquipmentNumericAmount > 0); | |
182 } | |
183 | |
184 return nonZero; | |
185 } | |
186 | |
187 private double RoundPercentage(double percent) | |
188 { | |
189 return Math.Round(percent, 1); | |
190 } | |
191 | |
192 public void Show() | |
193 { | |
194 bool okayed = ui.ShowControl(); | |
195 | |
196 if (okayed) | |
197 { | |
198 DoProcessing(); | |
199 } | |
200 | |
201 ui.Dispose(); | |
202 } | |
203 | |
204 /// <summary> | |
205 /// Does the processing required for the control when the "OK" button was clicked | |
206 /// </summary> | |
207 protected abstract void DoProcessing(); | |
208 } | |
209 } | |
210 |