Mercurial > repos > IBBoard.WarFoundry.GUI.GTK
comparison Widgets/UnitDisplayWidget.cs @ 68:7028e24b67ec
Re #60: Add UI to add/remove/edit weapons in GTK
* Add "Replace" dialog
Note: Dialog null refs because of a bad assumption in the base class - base constructor calls SetupUI before Replace constructor has set all of its values
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 03 Nov 2010 21:02:54 +0000 |
parents | 77448375d2f9 |
children | 91354245218a |
comparison
equal
deleted
inserted
replaced
67:a7306b5a229e | 68:7028e24b67ec |
---|---|
20 [System.ComponentModel.Category("WarFoundry GTK# GUI")] | 20 [System.ComponentModel.Category("WarFoundry GTK# GUI")] |
21 [System.ComponentModel.ToolboxItem(true)] | 21 [System.ComponentModel.ToolboxItem(true)] |
22 public partial class UnitDisplayWidget : Gtk.Bin | 22 public partial class UnitDisplayWidget : Gtk.Bin |
23 { | 23 { |
24 private static ILog log = LogManager.GetLogger(typeof(UnitDisplayWidget)); | 24 private static ILog log = LogManager.GetLogger(typeof(UnitDisplayWidget)); |
25 | |
26 private WFObjects.Unit unit; | 25 private WFObjects.Unit unit; |
27 private CommandStack stack; | 26 private CommandStack stack; |
28 | 27 |
29 public UnitDisplayWidget(WFObjects.Unit sourceUnit, CommandStack commandStack) | 28 public UnitDisplayWidget(WFObjects.Unit sourceUnit, CommandStack commandStack) |
30 { | 29 { |
44 equipmentList.Selection.Changed += HandleEquipmentListSelectionChanged; | 43 equipmentList.Selection.Changed += HandleEquipmentListSelectionChanged; |
45 SetStats(); | 44 SetStats(); |
46 SetWeapons(); | 45 SetWeapons(); |
47 } | 46 } |
48 | 47 |
49 private void HandleEquipmentListSelectionChanged (object sender, EventArgs e) | 48 private void HandleEquipmentListSelectionChanged(object sender, EventArgs e) |
50 { | 49 { |
51 SetButtonsEnabledState(); | 50 SetButtonsEnabledState(); |
52 } | 51 } |
52 | |
53 private void SetButtonsEnabledState() | 53 private void SetButtonsEnabledState() |
54 { | 54 { |
55 UnitEquipmentItem equipItem = GetSelectedEquipmentItem(); | 55 UnitEquipmentItem equipItem = GetSelectedEquipmentItem(); |
56 bttnReplaceEquipment.Sensitive = (equipItem != null && equipItem.HasAlternatives()); | 56 bttnReplaceEquipment.Sensitive = (equipItem != null && equipItem.HasAlternatives()); |
57 bttnEditEquipment.Sensitive = (UnitEquipmentUtil.CanEditEquipmentAmount(unit, equipItem)); | 57 bttnEditEquipment.Sensitive = (UnitEquipmentUtil.CanEditEquipmentAmount(unit, equipItem)); |
58 bttnRemoveEquipment.Sensitive = (equipItem != null && !equipItem.IsRequired); | 58 bttnRemoveEquipment.Sensitive = (equipItem != null && !equipItem.IsRequired); |
59 } | 59 } |
60 | |
60 private UnitEquipmentItem GetSelectedEquipmentItem() | 61 private UnitEquipmentItem GetSelectedEquipmentItem() |
61 { | 62 { |
62 return (UnitEquipmentItem)TreeUtils.GetSelectedItem(equipmentList); | 63 return (UnitEquipmentItem)TreeUtils.GetSelectedItem(equipmentList); |
63 } | 64 } |
64 | 65 |
65 private void SetStats() | 66 private void SetStats() |
66 { | 67 { |
67 CellRendererText renderer = new CellRendererText(); | 68 CellRendererText renderer = new CellRendererText(); |
68 unitStats.AppendColumn(Translation.GetTranslation("UnitNameColumn", "Unit Type", null), renderer, new TreeCellDataFunc(RenderUnitName)); | 69 unitStats.AppendColumn(Translation.GetTranslation("UnitNameColumn", "Unit Type", null), renderer, new TreeCellDataFunc(RenderUnitName)); |
69 | 70 |
70 TreeCellDataFunc statFunc = new TreeCellDataFunc(RenderUnitStat); | 71 TreeCellDataFunc statFunc = new TreeCellDataFunc(RenderUnitStat); |
71 Stat[] stats = unit.UnitStatsArray; | 72 Stat[] stats = unit.UnitStatsArray; |
72 | 73 |
73 int length = stats.Length; | 74 int length = stats.Length; |
74 | 75 |
75 for (int i = 0; i < length; i++) | 76 for (int i = 0; i < length; i++) |
76 { | 77 { |
77 unitStats.AppendColumn(stats[i].ParentSlotName, renderer, statFunc); | 78 unitStats.AppendColumn(stats[i].ParentSlotName, renderer, statFunc); |
78 } | 79 } |
79 | 80 |
80 TreeStore model = new TreeStore(typeof(WFObjects.Unit)); | 81 TreeStore model = new TreeStore(typeof(WFObjects.Unit)); |
81 model.AppendValues(unit); | 82 model.AppendValues(unit); |
82 unitStats.Model = model; | 83 unitStats.Model = model; |
158 | 159 |
159 if (amount == 100) | 160 if (amount == 100) |
160 { | 161 { |
161 amountString = Translation.GetTranslation("equipmentChoiceAmountAll", "all ({1})", amount, number); | 162 amountString = Translation.GetTranslation("equipmentChoiceAmountAll", "all ({1})", amount, number); |
162 } | 163 } |
163 | |
164 else | 164 else |
165 { | 165 { |
166 amountString = Translation.GetTranslation("equipmentChoiceAmountPercentage", "{0}% ({1})", amount, number); | 166 amountString = Translation.GetTranslation("equipmentChoiceAmountPercentage", "{0}% ({1})", amount, number); |
167 } | 167 } |
168 } | 168 } |
193 { | 193 { |
194 if (oldValue == 0) | 194 if (oldValue == 0) |
195 { | 195 { |
196 ((ListStore)equipmentList.Model).AppendValues(obj); | 196 ((ListStore)equipmentList.Model).AppendValues(obj); |
197 } | 197 } |
198 else if (newValue == 0) | 198 else |
199 { | 199 { |
200 TreeIter treeIter = TreeUtils.GetItemIter(equipmentList, obj); | 200 if (newValue == 0) |
201 ((ListStore)equipmentList.Model).Remove(ref treeIter); | 201 { |
202 } | 202 TreeIter treeIter = TreeUtils.GetItemIter(equipmentList, obj); |
203 | 203 ((ListStore)equipmentList.Model).Remove(ref treeIter); |
204 } | |
205 | |
206 } | |
204 equipmentList.QueueDraw(); | 207 equipmentList.QueueDraw(); |
205 } | 208 } |
206 | 209 |
207 protected virtual void OnUnitSizeFocusOut (object o, Gtk.FocusOutEventArgs args) | 210 protected virtual void OnUnitSizeFocusOut(object o, Gtk.FocusOutEventArgs args) |
208 { | 211 { |
209 SetNewUnitSize(); | 212 SetNewUnitSize(); |
210 } | 213 } |
211 | 214 |
212 [GLib.ConnectBefore ()] | 215 [GLib.ConnectBefore ()] |
213 protected virtual void OnUnitSizeKeyPress (object o, Gtk.KeyPressEventArgs args) | 216 |
217 protected virtual void OnUnitSizeKeyPress(object o, Gtk.KeyPressEventArgs args) | |
214 { | 218 { |
215 if (args.Event.Key == Gdk.Key.Return || args.Event.Key == Gdk.Key.KP_Enter) | 219 if (args.Event.Key == Gdk.Key.Return || args.Event.Key == Gdk.Key.KP_Enter) |
216 { | 220 { |
217 SetNewUnitSize(); | 221 SetNewUnitSize(); |
218 } | 222 } |
219 } | 223 } |
220 | 224 |
221 private void SetNewUnitSize() | 225 private void SetNewUnitSize() |
222 { | 226 { |
223 if (unitSize.Value!=unit.Size) | 227 if (unitSize.Value != unit.Size) |
224 { | 228 { |
225 SetUnitSizeCommand cmd = new SetUnitSizeCommand(unit, (int)Math.Round(unitSize.Value)); | 229 SetUnitSizeCommand cmd = new SetUnitSizeCommand(unit, (int)Math.Round(unitSize.Value)); |
226 stack.Execute(cmd); | 230 stack.Execute(cmd); |
227 } | 231 } |
228 } | 232 } |
229 | 233 |
230 protected virtual void OnUnitNameFocusOut (object o, Gtk.FocusOutEventArgs args) | 234 protected virtual void OnUnitNameFocusOut(object o, Gtk.FocusOutEventArgs args) |
231 { | 235 { |
232 SetNewUnitName(); | 236 SetNewUnitName(); |
233 } | 237 } |
234 | 238 |
235 [GLib.ConnectBefore ()] | 239 [GLib.ConnectBefore ()] |
236 protected virtual void OnUnitNameKeyPress (object o, Gtk.KeyPressEventArgs args) | 240 |
241 protected virtual void OnUnitNameKeyPress(object o, Gtk.KeyPressEventArgs args) | |
237 { | 242 { |
238 if (args.Event.Key == Gdk.Key.Return || args.Event.Key == Gdk.Key.KP_Enter) | 243 if (args.Event.Key == Gdk.Key.Return || args.Event.Key == Gdk.Key.KP_Enter) |
239 { | 244 { |
240 SetNewUnitName(); | 245 SetNewUnitName(); |
241 } | 246 } |
262 } | 267 } |
263 | 268 |
264 protected virtual void HandleRemoveButtonActivated(object sender, System.EventArgs e) | 269 protected virtual void HandleRemoveButtonActivated(object sender, System.EventArgs e) |
265 { | 270 { |
266 UnitEquipmentItem item = GetSelectedEquipmentItem(); | 271 UnitEquipmentItem item = GetSelectedEquipmentItem(); |
267 log.Debug("Remove "+item); | 272 log.Debug("Remove " + item); |
268 | 273 |
269 if (item != null) | 274 if (item != null) |
270 { | 275 { |
271 SetUnitEquipmentNumericAmountCommand cmd = new SetUnitEquipmentNumericAmountCommand(unit, item, 0); | 276 SetUnitEquipmentNumericAmountCommand cmd = new SetUnitEquipmentNumericAmountCommand(unit, item, 0); |
272 stack.Execute(cmd); | 277 stack.Execute(cmd); |
283 EditEquipmentUIControl editEquipment = new EditEquipmentUIControl(unit, item, stack); | 288 EditEquipmentUIControl editEquipment = new EditEquipmentUIControl(unit, item, stack); |
284 editEquipment.Show(); | 289 editEquipment.Show(); |
285 } | 290 } |
286 } | 291 } |
287 | 292 |
293 protected virtual void HandleReplaceButtonClicked(object sender, System.EventArgs e) | |
294 { | |
295 UnitEquipmentItem item = GetSelectedEquipmentItem(); | |
296 log.Debug("Replace " + item); | |
297 | |
298 if (item != null) | |
299 { | |
300 ReplaceEquipmentUIControl addEquipment = new ReplaceEquipmentUIControl(unit, item, stack); | |
301 addEquipment.Show(); | |
302 } | |
303 } | |
304 | |
305 | |
288 | 306 |
289 } | 307 } |
290 } | 308 } |