comparison Widgets/UnitDisplayWidget.cs @ 113:4a33b3012100 WarFoundry_v0.1RC1

* Tag v0.1RC1 release no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Mon, 17 Jan 2011 19:43:47 +0000
parents a35c8be46006
children
comparison
equal deleted inserted replaced
39:146b7a893a01 113:4a33b3012100
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. 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 4
5 using System; 5 using System;
6 using Gtk; 6 using Gtk;
7 using IBBoard.Commands; 7 using IBBoard.Commands;
8 using IBBoard.GtkSharp;
8 using IBBoard.Lang; 9 using IBBoard.Lang;
9 using IBBoard.WarFoundry.API; 10 using IBBoard.WarFoundry.API;
11 using IBBoard.WarFoundry.API.Commands;
10 using IBBoard.WarFoundry.API.Objects; 12 using IBBoard.WarFoundry.API.Objects;
11 using IBBoard.WarFoundry.API.Commands; 13 using IBBoard.WarFoundry.API.Util;
12 14 using IBBoard.WarFoundry.GUI.GTK.UIControl;
13 namespace IBBoard.WarFoundry.GTK.Widgets 15 using log4net;
16 using WFObjects = IBBoard.WarFoundry.API.Objects;
17 using System.Collections.Generic;
18 using IBBoard.GtkSharp.Translatable;
19
20 namespace IBBoard.WarFoundry.GUI.GTK.Widgets
14 { 21 {
15 [System.ComponentModel.Category("WarFoundry GTK# GUI")] 22 [System.ComponentModel.Category("WarFoundry GTK# GUI")]
16 [System.ComponentModel.ToolboxItem(true)] 23 [System.ComponentModel.ToolboxItem(true)]
17 public partial class UnitDisplayWidget : Gtk.Bin 24 public partial class UnitDisplayWidget : Gtk.Bin
18 { 25 {
19 private IBBoard.WarFoundry.API.Objects.Unit unit; 26 private static ILog log = LogManager.GetLogger(typeof(UnitDisplayWidget));
27 private WFObjects.Unit unit;
20 private CommandStack stack; 28 private CommandStack stack;
21 29 private Dictionary<string, NodeView> statsViews = new Dictionary<string, NodeView>();
22 public UnitDisplayWidget(IBBoard.WarFoundry.API.Objects.Unit sourceUnit, CommandStack commandStack) 30
31 public UnitDisplayWidget(WFObjects.Unit sourceUnit, CommandStack commandStack)
23 { 32 {
24 this.Build(); 33 this.Build();
25 stack = commandStack; 34 stack = commandStack;
26 unit = sourceUnit; 35 unit = sourceUnit;
27 unitName.Text = unit.Name; 36 unitName.Text = unit.Name;
28 unitSize.Value = unit.Size; 37 unitSize.Value = unit.Size;
29 double max = unit.UnitType.MaxSize; 38 int maxSize = (unit.UnitType.MaxSize == WarFoundryCore.INFINITY ? int.MaxValue : unit.UnitType.MaxSize);
30 39 int minSize = unit.UnitType.MinSize;
31 if (max == -1) 40 unitSize.SetRange(minSize, maxSize);
32 { 41 unitSize.Sensitive = (maxSize != minSize);
33 max = double.MaxValue; 42 notesView.Buffer.Text = unit.UnitType.Notes;
34 } 43 unit.NameChanged += UnitNameChanged;
35 44 unit.UnitSizeChanged += UnitSizeChanged;
36 unitSize.SetRange(unit.UnitType.MinSize, max); 45 unit.UnitEquipmentAmountChanged += HandleUnitUnitEquipmentAmountChanged;
37 unit.NameChanged+= new StringValChangedDelegate(UnitNameChanged); 46 equipmentList.Selection.Changed += HandleEquipmentListSelectionChanged;
38 unit.UnitSizeChanged+= new IntValChangedDelegate(UnitSizeChanged); 47 SetAbilities();
39 SetStats(); 48 SetStats();
49 SetWeapons();
50 SetAddButtonEnabledState();
51 ControlTranslator.TranslateWidget(this);
52 }
53
54 private void HandleEquipmentListSelectionChanged(object sender, EventArgs e)
55 {
56 SetButtonsEnabledState();
57 }
58
59 private void SetButtonsEnabledState()
60 {
61 UnitEquipmentItem equipItem = GetSelectedEquipmentItem();
62 bttnReplaceWeapon.Sensitive = (equipItem != null && equipItem.HasAlternatives());
63 bttnEditWeapon.Sensitive = (UnitEquipmentUtil.CanEditEquipmentAmount(unit, equipItem));
64 bttnRemoveWeapon.Sensitive = (equipItem != null && !equipItem.IsRequired);
65 }
66
67 private void SetAddButtonEnabledState()
68 {
69 bttnAddWeapon.Sensitive = AddEquipmentUIControl.HasEquipmentToAdd(unit);
70 }
71
72 private UnitEquipmentItem GetSelectedEquipmentItem()
73 {
74 return (UnitEquipmentItem)TreeUtils.GetSelectedItem(equipmentList);
75 }
76
77 private void SetAbilities()
78 {
79 CellRendererText renderer = new CellRendererText();
80 abilitiesList.AppendColumn("", renderer, new TreeCellDataFunc(RenderAbility));
81
82 ListStore model = new ListStore(typeof(Ability));
83
84 foreach (Ability ability in unit.UnitType.GetRequiredAbilities())
85 {
86 model.AppendValues(ability);
87 }
88
89 abilitiesList.Model = model;
90 }
91
92 public void RenderAbility(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
93 {
94 object o = model.GetValue(iter, 0);
95
96 if (o is Ability)
97 {
98 Ability ability = (Ability)o;
99 (cell as CellRendererText).Text = ability.Name;
100 }
101
40 } 102 }
41 103
42 private void SetStats() 104 private void SetStats()
43 { 105 {
44 //GameSystem system = unit.Army.GameSystem; 106 Stat[][] stats = unit.UnitStatsArraysWithName;
45 //SystemStats stats = system.StandardSystemStats; 107 string[] statsIDs = unit.UnitStatsArrayIDs;
46 CellRendererText renderer = new CellRendererText(); 108 int statsCount = stats.Length;
47 unitStats.AppendColumn(Translation.GetTranslation("UnitNameColumn", "Unit Type", null), renderer, new TreeCellDataFunc(RenderUnitName)); 109 log.DebugFormat("Unit {0} has {1} stats arrays", unit.UnitType.Name, statsCount);
48 110
49 TreeCellDataFunc statFunc = new TreeCellDataFunc(RenderUnitStat); 111 for (int i = 0; i < statsCount; i++)
50 Stat[] stats = unit.UnitStatsArray; 112 {
51 113 NodeView statsGrid = GetStatsView(statsIDs[i]);
52 int length = stats.Length; 114 TreeStore model = (TreeStore)statsGrid.Model;
53 115 log.DebugFormat("Adding row to data table for {0}", statsIDs[i]);
54 for (int i = 0; i < length; i++) 116 log.DebugFormat("TreeStore supports {0} columns", model.NColumns);
55 { 117 model.AppendValues((object)stats[i]);
56 unitStats.AppendColumn(stats[i].ParentSlotName, renderer, statFunc); 118 }
57 } 119 }
58 120
59 TreeStore model = new TreeStore(typeof(IBBoard.WarFoundry.API.Objects.Unit)); 121 private NodeView GetStatsView(string statsID)
60 model.AppendValues(unit); 122 {
61 unitStats.Model = model; 123 NodeView statsView;
62 } 124
63 125 if (statsViews.ContainsKey(statsID))
64 private void RenderUnitName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) 126 {
127 statsView = DictionaryUtils.GetValue(statsViews, statsID);
128 }
129 else
130 {
131 statsView = CreateStatsView(statsID);
132 statsViews[statsID] = statsView;
133 }
134
135 return statsView;
136 }
137
138 private NodeView CreateStatsView(string statsID)
139 {
140 log.DebugFormat("Create NodeView for stats ID {0}", statsID);
141 SystemStats sysStats = unit.Race.GameSystem.GetSystemStatsForID(statsID);
142 StatSlot[] sysStatSlots = sysStats.StatSlots;
143 int statsCount = sysStatSlots.Length;
144 NodeView statsGrid = CreateNodeView();
145 CellRendererText renderer = new CellRendererText();
146 statsGrid.AppendColumn(Translation.GetTranslation("UnitNameColumn", "Unit Type", null), renderer, RenderUnitStat);
147
148 for (int i = 0; i < statsCount; i++)
149 {
150 StatSlot stat = sysStatSlots[i];
151 string slotName = stat.Name;
152 statsGrid.AppendColumn(slotName, renderer, RenderUnitStat);
153 }
154
155 statsGrid.Model = new TreeStore(typeof(Stat[]));
156 return statsGrid;
157 }
158
159 private NodeView CreateNodeView()
160 {
161 NodeView nodeView = new NodeView();
162 statsRepeatBox.Add(nodeView);
163 return nodeView;
164 }
165
166 private void RenderUnitStat(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
65 { 167 {
66 object o = model.GetValue(iter, 0); 168 object o = model.GetValue(iter, 0);
67 169
68 if (o is IBBoard.WarFoundry.API.Objects.Unit) 170 if (o is Stat[])
69 { 171 {
70 IBBoard.WarFoundry.API.Objects.Unit u = (IBBoard.WarFoundry.API.Objects.Unit)o; 172 Stat[] stats = (Stat[])o;
71 (cell as CellRendererText).Text = u.UnitType.Name; 173 (cell as CellRendererText).Text = stats[GetStatColumnIndex(column)].SlotValueString;
72 } 174 }
73 } 175 }
74 176
75 private void RenderUnitStat(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) 177 private int GetStatColumnIndex(TreeViewColumn column)
178 {
179 int idx = -1;
180 TreeViewColumn[] cols = ((TreeView)column.TreeView).Columns;
181 int colCount = cols.Length;
182
183 for (int i = 0; i < colCount; i++)
184 {
185 if (cols[i] == column)
186 {
187 idx = i;
188 break;
189 }
190 }
191
192 return idx;
193 }
194
195 private void SetWeapons()
196 {
197 CellRendererText renderer = new CellRendererText();
198 equipmentList.AppendColumn("", renderer, new TreeCellDataFunc(RenderEquipmentLine));
199
200 ListStore model = new ListStore(typeof(UnitEquipmentItem));
201
202 foreach (UnitEquipmentItem item in unit.GetEquipment())
203 {
204 model.AppendValues(item);
205 }
206
207 equipmentList.Model = model;
208 }
209
210 public void RenderEquipmentLine(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
76 { 211 {
77 object o = model.GetValue(iter, 0); 212 object o = model.GetValue(iter, 0);
78 213
79 if (o is IBBoard.WarFoundry.API.Objects.Unit) 214 if (o is UnitEquipmentItem)
80 { 215 {
81 IBBoard.WarFoundry.API.Objects.Unit u = (IBBoard.WarFoundry.API.Objects.Unit)o; 216 UnitEquipmentItem item = (UnitEquipmentItem)o;
82 (cell as CellRendererText).Text = u.GetStatValue(column.Title); 217 (cell as CellRendererText).Text = GetUnitEquipmentText(item);
83 } 218 }
84 } 219
85 220 }
86 public IBBoard.WarFoundry.API.Objects.Unit Unit 221
222 private string GetUnitEquipmentText(UnitEquipmentItem item)
223 {
224 string translation = "";
225
226 if (item.Cost == 0)
227 {
228 translation = Translation.GetTranslation("equipmentAmountWithZeroCost", "{0} ({1} - free)", item.Name, GetAmountString(item));
229 }
230 else
231 {
232 translation = Translation.GetTranslation("equipmentAmountWithCost", "{0} ({1} at {2}pts each)", item.Name, GetAmountString(item), item.Cost);
233 }
234
235 return translation;
236 }
237
238 private string GetAmountString(UnitEquipmentItem item)
239 {
240 double amount = UnitEquipmentUtil.GetEquipmentAmount(unit, item);
241 string amountString = "";
242
243 if (UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, item))
244 {
245 int number = UnitEquipmentUtil.GetEquipmentAmountTaken(unit, item);
246
247 if (amount == 100)
248 {
249 amountString = Translation.GetTranslation("equipmentChoiceAmountAll", "all ({1})", amount, number);
250 }
251 else
252 {
253 amountString = Translation.GetTranslation("equipmentChoiceAmountPercentage", "{0}% ({1})", amount, number);
254 }
255 }
256 else
257 {
258 amountString = Translation.GetTranslation("equipmentChoiceAmountNumber", "{0}", amount);
259 }
260
261 return amountString;
262 }
263
264 public WFObjects.Unit Unit
87 { 265 {
88 get { return unit; } 266 get { return unit; }
89 } 267 }
90 268
91 private void UnitNameChanged(WarFoundryObject obj, string oldValue, string newValue) 269 private void UnitNameChanged(WarFoundryObject obj, string oldValue, string newValue)
92 { 270 {
93 unitName.Text = newValue; 271 unitName.Text = newValue;
94 } 272 }
95 273
96 private void UnitSizeChanged(WarFoundryObject obj, int oldValue, int newValue) 274 private void UnitSizeChanged(WarFoundryObject obj, int oldValue, int newValue)
97 { 275 {
98 unitSize.Value = newValue; 276 unitSize.Value = newValue;
99 } 277 }
100 278
101 protected virtual void OnUnitSizeFocusOut (object o, Gtk.FocusOutEventArgs args) 279 private void HandleUnitUnitEquipmentAmountChanged(WarFoundryObject obj, double oldValue, double newValue)
280 {
281 if (oldValue == 0)
282 {
283 ((ListStore)equipmentList.Model).AppendValues(obj);
284 }
285 else
286 {
287 if (newValue == 0)
288 {
289 TreeIter treeIter = TreeUtils.GetItemIter(equipmentList, obj);
290 ((ListStore)equipmentList.Model).Remove(ref treeIter);
291 }
292 }
293
294 SetAddButtonEnabledState();
295 equipmentList.QueueDraw();
296 }
297
298 protected virtual void OnUnitSizeFocusOut(object o, Gtk.FocusOutEventArgs args)
102 { 299 {
103 SetNewUnitSize(); 300 SetNewUnitSize();
104 } 301 }
105 302
106 [GLib.ConnectBefore ()] 303 [GLib.ConnectBefore ()]
107 protected virtual void OnUnitSizeKeyPress (object o, Gtk.KeyPressEventArgs args) 304
108 { 305 protected virtual void OnUnitSizeKeyPress(object o, Gtk.KeyPressEventArgs args)
109 if (args.Event.Key == Gdk.Key.Return) 306 {
307 if (args.Event.Key == Gdk.Key.Return || args.Event.Key == Gdk.Key.KP_Enter)
110 { 308 {
111 SetNewUnitSize(); 309 SetNewUnitSize();
112 } 310 }
113 } 311 }
114 312
115 private void SetNewUnitSize() 313 private void SetNewUnitSize()
116 { 314 {
117 if (unitSize.Value!=unit.Size) 315 if (unitSize.Value != unit.Size)
118 { 316 {
119 SetUnitSizeCommand cmd = new SetUnitSizeCommand(unit, (int)Math.Round(unitSize.Value)); 317 SetUnitSizeCommand cmd = new SetUnitSizeCommand(unit, (int)Math.Round(unitSize.Value));
120 stack.Execute(cmd); 318 stack.Execute(cmd);
121 } 319 }
122 } 320 }
123 321
124 protected virtual void OnUnitNameFocusOut (object o, Gtk.FocusOutEventArgs args) 322 protected virtual void OnUnitNameFocusOut(object o, Gtk.FocusOutEventArgs args)
125 { 323 {
126 SetNewUnitName(); 324 SetNewUnitName();
127 } 325 }
128 326
129 [GLib.ConnectBefore ()] 327 [GLib.ConnectBefore ()]
130 protected virtual void OnUnitNameKeyPress (object o, Gtk.KeyPressEventArgs args) 328
131 { 329 protected virtual void OnUnitNameKeyPress(object o, Gtk.KeyPressEventArgs args)
132 if (args.Event.Key == Gdk.Key.Return) 330 {
331 if (args.Event.Key == Gdk.Key.Return || args.Event.Key == Gdk.Key.KP_Enter)
133 { 332 {
134 SetNewUnitName(); 333 SetNewUnitName();
135 } 334 }
136 } 335 }
137 336
138 private void SetNewUnitName() 337 private void SetNewUnitName()
139 { 338 {
140 if (unitName.Text!=unit.Name) 339 if (unitName.Text != unit.Name)
141 { 340 {
142 SetNameCommand cmd = new SetNameCommand(unit, unitName.Text); 341 SetNameCommand cmd = new SetNameCommand(unit, unitName.Text);
143 stack.Execute(cmd); 342 stack.Execute(cmd);
144 } 343 }
145 } 344 }
345
346 private void OnBttnAddEquipmentClicked(object sender, System.EventArgs e)
347 {
348 AddEquipment();
349 }
350
351 private void AddEquipment()
352 {
353 AddEquipmentUIControl addEquipment = new AddEquipmentUIControl(unit, stack);
354 addEquipment.Show();
355 }
356
357 protected virtual void HandleRemoveButtonActivated(object sender, System.EventArgs e)
358 {
359 UnitEquipmentItem item = GetSelectedEquipmentItem();
360 log.Debug("Remove " + item);
361
362 if (item != null)
363 {
364 SetUnitEquipmentNumericAmountCommand cmd = new SetUnitEquipmentNumericAmountCommand(unit, item, 0);
365 stack.Execute(cmd);
366 }
367 }
368
369 protected virtual void HandleEditButtonClicked(object sender, System.EventArgs e)
370 {
371 UnitEquipmentItem item = GetSelectedEquipmentItem();
372 log.Debug("Edit " + item);
373
374 if (item != null)
375 {
376 EditEquipmentUIControl editEquipment = new EditEquipmentUIControl(unit, item, stack);
377 editEquipment.Show();
378 }
379 }
380
381 protected virtual void HandleReplaceButtonClicked(object sender, System.EventArgs e)
382 {
383 UnitEquipmentItem item = GetSelectedEquipmentItem();
384 log.Debug("Replace " + item);
385
386 if (item != null)
387 {
388 ReplaceEquipmentUIControl addEquipment = new ReplaceEquipmentUIControl(unit, item, stack);
389 addEquipment.Show();
390 }
391 }
146 } 392 }
147 } 393 }