190
|
1 // This file (FrmArmyTree.cs) is a part of the IBBoard.WarFoundry.GUI.WinForms project and is copyright 2009 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
|
|
5 using System;
|
|
6 using System.Drawing;
|
|
7 using System.Collections.Generic;
|
|
8 using System.ComponentModel;
|
|
9 using System.Windows.Forms;
|
|
10 using IBBoard.Commands;
|
|
11 using IBBoard.Lang;
|
|
12 using IBBoard.Windows.Forms.I18N;
|
|
13 using IBBoard.Windows.Forms;
|
|
14 using IBBoard.WarFoundry.API;
|
|
15 using IBBoard.WarFoundry.API.Commands;
|
|
16 using IBBoard.WarFoundry.API.Objects;
|
|
17
|
|
18 namespace IBBoard.WarFoundry.GUI.WinForms
|
|
19 {
|
|
20 /// <summary>
|
|
21 /// Summary description for FrmArmyTree.
|
|
22 /// </summary>
|
|
23 public class FrmArmyTree : IBBoard.Windows.Forms.IBBForm
|
|
24 {
|
|
25 private System.Windows.Forms.TreeView treeView;
|
|
26 private IContainer components;
|
|
27 private Dictionary<string, TreeNode> nodes;
|
|
28 private System.Windows.Forms.ContextMenu contextMenu;
|
|
29 private IBBMenuItem miDeleteUnit;
|
|
30 private IBBMenuItem miEditUnit;
|
|
31
|
|
32 private ObjectAddDelegate UnitAddedMethod;
|
|
33 private ObjectRemoveDelegate UnitRemovedMethod;
|
|
34 private StringValChangedDelegate UnitNameChangedMethod, ArmyNameChangedMethod, TreeNameChangedMethod;
|
|
35 private ToolBar toolBar;
|
|
36 private IBBToolBarButton bttnEdit;
|
|
37 private IBBToolBarButton bttnDelete;
|
|
38 private ImageList imageList;
|
|
39 private CommandStack commandStack;
|
|
40 public GameSystem CurrentGameSystem
|
|
41 {
|
|
42 get { return WarFoundryCore.CurrentGameSystem; }
|
|
43 set { WarFoundryCore.CurrentGameSystem = value; }
|
|
44 }
|
|
45
|
|
46 public FrmArmyTree(CommandStack cmdStack)
|
|
47 {
|
|
48 commandStack = cmdStack;
|
|
49 InitializeComponent();
|
|
50 UnitAddedMethod = new ObjectAddDelegate(UnitAdded);
|
|
51 UnitRemovedMethod = new ObjectRemoveDelegate(UnitRemoved);
|
|
52 UnitNameChangedMethod = new StringValChangedDelegate(UpdateUnitName);
|
|
53 ArmyNameChangedMethod = new StringValChangedDelegate(UpdateArmyName);
|
|
54 TreeNameChangedMethod = new StringValChangedDelegate(FrmArmyTree_TreeNameChanged);
|
|
55 nodes = new Dictionary<string, TreeNode>();
|
|
56 this.Name = "ArmyTree";
|
|
57 WarFoundryCore.ArmyChanged+= new ArmyChangedDelegate(FrmArmyTree_ArmyChanged);
|
|
58 TranslateForm();
|
|
59 Translation.TranslationChanged += new MethodInvoker(TranslateForm);
|
|
60 }
|
|
61
|
|
62 private void TranslateForm()
|
|
63 {
|
|
64 ControlTranslator.TranslateControl(this);
|
|
65
|
|
66 foreach (Component comp in components.Components)
|
|
67 {
|
|
68 ControlTranslator.TranslateComponent(comp);
|
|
69 }
|
|
70
|
|
71 foreach (IBBMenuItem mi in contextMenu.MenuItems)
|
|
72 {
|
|
73 ControlTranslator.TranslateComponent(mi);
|
|
74 }
|
|
75 foreach (TreeNode node in nodes.Values)
|
|
76 {
|
|
77 node.Text = CreateNodeName((ICostedWarFoundryObject)node.Tag);
|
|
78 }
|
|
79 }
|
|
80
|
|
81 /// <summary>
|
|
82 /// Clean up any resources being used.
|
|
83 /// </summary>
|
|
84 protected override void Dispose( bool disposing )
|
|
85 {
|
|
86 if( disposing )
|
|
87 {
|
|
88 if(components != null)
|
|
89 {
|
|
90 components.Dispose();
|
|
91 }
|
|
92 }
|
|
93 base.Dispose( disposing );
|
|
94 Translation.TranslationChanged -= new MethodInvoker(TranslateForm);
|
|
95 WarFoundryCore.ArmyChanged -= new ArmyChangedDelegate(FrmArmyTree_ArmyChanged);
|
|
96 }
|
|
97
|
|
98 #region Windows Form Designer generated code
|
|
99 /// <summary>
|
|
100 /// Required method for Designer support - do not modify
|
|
101 /// the contents of this method with the code editor.
|
|
102 /// </summary>
|
|
103 private void InitializeComponent()
|
|
104 {
|
|
105 this.components = new System.ComponentModel.Container();
|
|
106 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmArmyTree));
|
|
107 this.contextMenu = new System.Windows.Forms.ContextMenu();
|
|
108 this.miEditUnit = new IBBoard.Windows.Forms.IBBMenuItem();
|
|
109 this.miDeleteUnit = new IBBoard.Windows.Forms.IBBMenuItem();
|
|
110 this.imageList = new System.Windows.Forms.ImageList(this.components);
|
|
111 this.toolBar = new System.Windows.Forms.ToolBar();
|
|
112 this.bttnEdit = new IBBoard.Windows.Forms.IBBToolBarButton();
|
|
113 this.bttnDelete = new IBBoard.Windows.Forms.IBBToolBarButton();
|
|
114 this.treeView = new System.Windows.Forms.TreeView();
|
|
115 this.SuspendLayout();
|
|
116 //
|
|
117 // contextMenu
|
|
118 //
|
|
119 this.contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
|
|
120 this.miEditUnit,
|
|
121 this.miDeleteUnit});
|
|
122 this.contextMenu.Popup += new System.EventHandler(this.contextMenu_Popup);
|
|
123 //
|
|
124 // miEditUnit
|
|
125 //
|
|
126 this.miEditUnit.Index = 0;
|
|
127 this.miEditUnit.Text = "&edit unit";
|
|
128 this.miEditUnit.Click += new System.EventHandler(this.miEdit_Click);
|
|
129 //
|
|
130 // miDeleteUnit
|
|
131 //
|
|
132 this.miDeleteUnit.Index = 1;
|
|
133 this.miDeleteUnit.Text = "&delete unit";
|
|
134 this.miDeleteUnit.Click += new System.EventHandler(this.miDelete_Click);
|
|
135 //
|
|
136 // imageList
|
|
137 //
|
|
138 this.imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList.ImageStream")));
|
|
139 this.imageList.TransparentColor = System.Drawing.Color.Transparent;
|
|
140 this.imageList.Images.SetKeyName(0, "gtk-edit.png");
|
|
141 this.imageList.Images.SetKeyName(1, "edit-delete.png");
|
|
142 //
|
|
143 // toolBar
|
|
144 //
|
|
145 this.toolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
|
|
146 this.bttnEdit,
|
|
147 this.bttnDelete});
|
|
148 this.toolBar.ButtonSize = new System.Drawing.Size(16, 16);
|
|
149 this.toolBar.DropDownArrows = true;
|
|
150 this.toolBar.ImageList = this.imageList;
|
|
151 this.toolBar.Location = new System.Drawing.Point(0, 0);
|
|
152 this.toolBar.Name = "toolBar";
|
|
153 this.toolBar.ShowToolTips = true;
|
|
154 this.toolBar.Size = new System.Drawing.Size(240, 28);
|
|
155 this.toolBar.TabIndex = 3;
|
|
156 this.toolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);
|
|
157 //
|
|
158 // bttnEdit
|
|
159 //
|
|
160 this.bttnEdit.Enabled = false;
|
|
161 this.bttnEdit.ImageIndex = 0;
|
|
162 this.bttnEdit.Name = "bttnEdit";
|
|
163 //
|
|
164 // bttnDelete
|
|
165 //
|
|
166 this.bttnDelete.Enabled = false;
|
|
167 this.bttnDelete.ImageIndex = 1;
|
|
168 this.bttnDelete.Name = "bttnDelete";
|
|
169 //
|
|
170 // treeView
|
|
171 //
|
|
172 this.treeView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
|
|
173 | System.Windows.Forms.AnchorStyles.Left)
|
|
174 | System.Windows.Forms.AnchorStyles.Right)));
|
|
175 this.treeView.ContextMenu = this.contextMenu;
|
|
176 this.treeView.FullRowSelect = true;
|
|
177 this.treeView.Location = new System.Drawing.Point(0, 28);
|
|
178 this.treeView.Name = "treeView";
|
|
179 this.treeView.Size = new System.Drawing.Size(240, 250);
|
|
180 this.treeView.TabIndex = 0;
|
193
|
181 this.treeView.DoubleClick += new System.EventHandler(this.treeView_DoubleClick);
|
190
|
182 this.treeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView_AfterSelect);
|
|
183 this.treeView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView_MouseDown);
|
|
184 //
|
|
185 // FrmArmyTree
|
|
186 //
|
|
187 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
|
|
188 this.ClientSize = new System.Drawing.Size(240, 277);
|
|
189 this.ControlBox = false;
|
|
190 this.Controls.Add(this.toolBar);
|
|
191 this.Controls.Add(this.treeView);
|
|
192 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
|
|
193 this.MaximizeBox = false;
|
|
194 this.MinimizeBox = false;
|
|
195 this.Name = "FrmArmyTree";
|
|
196 this.Text = "FrmArmyTree";
|
|
197 this.ResumeLayout(false);
|
|
198 this.PerformLayout();
|
|
199
|
|
200 }
|
|
201 #endregion
|
|
202
|
|
203 private void ClearArmy()
|
|
204 {
|
|
205 nodes.Clear();
|
|
206 treeView.Nodes.Clear();
|
|
207 }
|
|
208
|
|
209 private void SetArmy(Army army)
|
|
210 {
|
|
211 ClearArmy();
|
|
212
|
|
213 if (army != null)
|
|
214 {
|
|
215 ArmyCategory[] cats = army.Categories;
|
|
216 TreeNode[] catNodes = new TreeNode[cats.Length];
|
|
217 Unit[] units;
|
|
218
|
|
219 for (int i = 0; i < cats.Length; i++)
|
|
220 {
|
|
221 ArmyCategory cat = cats[i];
|
|
222 units = army.GetUnits(cat);
|
|
223 cat.NameChanged += TreeNameChangedMethod;
|
|
224 cat.PointsValueChanged += NodePointsValueChanged;
|
|
225 TreeNode[] unitNodes = new TreeNode[units.Length];
|
|
226 TreeNode temp;
|
|
227
|
|
228 for (int j = 0; j < units.Length; j++)
|
|
229 {
|
|
230 unitNodes[j] = CreateTreeNode(units[j]);
|
|
231 }
|
|
232
|
|
233 temp = new TreeNode(CreateNodeName(cat), unitNodes);
|
|
234 temp.Tag = cat;
|
|
235 catNodes[i] = temp;
|
|
236 nodes[cat.ID] = temp;
|
|
237 }
|
|
238
|
|
239 TreeNode root = new TreeNode(CreateNodeName(army), catNodes);
|
|
240 root.Tag = army;
|
|
241 army.PointsValueChanged += NodePointsValueChanged;
|
|
242 nodes[army.ID] = root;
|
|
243 treeView.Nodes.Add(root);
|
|
244 root.ExpandAll();
|
|
245 }
|
|
246 }
|
|
247
|
|
248 private string CreateNodeName(ICostedWarFoundryObject obj)
|
|
249 {
|
193
|
250 return Translation.GetTranslation("treeNodeText", "{0} ({1} pts)", obj.Name, obj.Points, CurrentGameSystem.GetPointsAbbrev(obj.Points));
|
190
|
251 }
|
|
252
|
|
253 private void NodePointsValueChanged(WarFoundryObject obj, double oldValue, double newValue)
|
|
254 {
|
|
255 if (obj is ICostedWarFoundryObject)
|
|
256 {
|
|
257 SetNodeName(obj);
|
|
258 }
|
|
259 }
|
|
260
|
|
261 private void SetNodeName(WarFoundryObject obj)
|
|
262 {
|
|
263 TreeNode node = nodes[obj.ID];
|
|
264
|
|
265 if (node != null)
|
|
266 {
|
|
267 node.Text = CreateNodeName((ICostedWarFoundryObject)obj);
|
|
268 }
|
|
269 }
|
|
270
|
|
271 private TreeNode CreateTreeNode(Unit unit)
|
|
272 {
|
|
273 TreeNode temp = new TreeNode(CreateNodeName(unit));
|
|
274 temp.Tag = unit;
|
|
275 unit.NameChanged+= UnitNameChangedMethod;
|
|
276 unit.PointsValueChanged += NodePointsValueChanged;
|
|
277 nodes[unit.ID] = temp;
|
|
278 return temp;
|
|
279 }
|
|
280
|
|
281 public void FrmArmyTree_ArmyChanged(Army oldArmy, Army newArmy)
|
|
282 {
|
|
283 if (oldArmy != null)
|
|
284 {
|
|
285 oldArmy.UnitAdded -= UnitAddedMethod;
|
|
286 oldArmy.UnitRemoved -= UnitRemovedMethod;
|
|
287 oldArmy.PointsValueChanged -= NodePointsValueChanged;
|
|
288 }
|
|
289
|
|
290 if (newArmy != null)
|
|
291 {
|
|
292 newArmy.UnitAdded += UnitAddedMethod;
|
|
293 newArmy.UnitRemoved += UnitRemovedMethod;
|
|
294 newArmy.PointsValueChanged += NodePointsValueChanged;
|
|
295 }
|
|
296
|
|
297 SetArmy(newArmy);
|
|
298 }
|
|
299
|
|
300 private void UnitAdded(WarFoundryObject obj)
|
|
301 {
|
|
302 if (obj is Unit)
|
|
303 {
|
|
304 Unit unit = (Unit)obj;
|
|
305 ArmyCategory cat = unit.Category;
|
|
306 TreeNode parent = nodes[cat.ID];
|
|
307 TreeNode unitNode = CreateTreeNode(unit);
|
|
308 parent.Nodes.Add(unitNode);
|
|
309 parent.Expand(); //make sure it's expanded
|
|
310 }
|
|
311 }
|
|
312
|
|
313 public new FrmMain MdiParent
|
|
314 {
|
|
315 get { return (FrmMain) base.MdiParent; }
|
|
316 set { base.MdiParent = value; }
|
|
317 }
|
|
318
|
|
319 private void UnitRemoved(WarFoundryObject obj)
|
|
320 {
|
|
321 if (obj is Unit)
|
|
322 {
|
|
323 Unit unit = (Unit)obj;
|
|
324 RemoveUnitFromTree(unit);
|
|
325 }
|
|
326 }
|
|
327
|
|
328 private void RemoveUnitFromTree(Unit unit)
|
|
329 {
|
|
330 TreeNode unitNode = nodes[unit.ID];
|
|
331 unit.NameChanged-= UnitNameChangedMethod;
|
|
332
|
|
333 if (unitNode!=null)
|
|
334 {
|
|
335 unitNode.Remove();
|
|
336 nodes.Remove(unit.ID);
|
|
337 }
|
|
338 }
|
|
339
|
|
340
|
|
341 private void contextMenu_Popup(object sender, System.EventArgs e)
|
|
342 {
|
|
343 TreeNode node = treeView.SelectedNode;
|
|
344
|
|
345 if (node!=null && node.Tag is Unit)
|
|
346 {
|
|
347 foreach(MenuItem item in contextMenu.MenuItems)
|
|
348 {
|
|
349 item.Visible = true;
|
|
350 }
|
|
351 }
|
|
352 else
|
|
353 {
|
|
354 foreach(MenuItem item in contextMenu.MenuItems)
|
|
355 {
|
|
356 item.Visible = false;
|
|
357 }
|
|
358 }
|
|
359 }
|
|
360
|
|
361 private void treeView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
|
|
362 {
|
|
363 if (e.Button == MouseButtons.Right)
|
|
364 {
|
|
365 TreeNode tn = treeView.GetNodeAt(e.X, e.Y);
|
|
366
|
|
367 if (tn!=null)
|
|
368 {
|
|
369 treeView.SelectedNode = tn;
|
|
370 }
|
|
371 else
|
|
372 {
|
|
373 treeView.SelectedNode = null;
|
|
374 }
|
|
375 }
|
|
376 }
|
|
377
|
|
378 private void miDelete_Click(object sender, System.EventArgs e)
|
|
379 {
|
|
380 DeleteUnit();
|
|
381 }
|
|
382
|
|
383 private void DeleteUnit()
|
|
384 {
|
|
385
|
|
386 TreeNode selected = treeView.SelectedNode;
|
|
387
|
|
388 if (selected.Tag != null && selected.Tag is Unit)
|
|
389 {
|
|
390 Unit unit = (Unit) selected.Tag;
|
|
391 commandStack.Execute(new RemoveUnitCommand(unit));
|
|
392 }
|
|
393 }
|
|
394
|
|
395 private void miEdit_Click(object sender, System.EventArgs e)
|
|
396 {
|
|
397 EditUnit();
|
|
398 }
|
|
399
|
|
400 private void treeView_DoubleClick(object sender, System.EventArgs e)
|
|
401 {
|
|
402 EditUnit();
|
|
403 }
|
|
404
|
|
405 private void EditUnit()
|
|
406 {
|
|
407 TreeNode selected = treeView.SelectedNode;
|
|
408 object tagData = selected.Tag;
|
|
409
|
|
410 if (tagData is Unit)
|
|
411 {
|
|
412 Unit unit = (Unit) tagData;
|
|
413 MdiParent.OpenUnitDialog(unit);
|
|
414 }
|
|
415 }
|
|
416
|
|
417 private void UpdateUnitName(WarFoundryObject obj, string oldValue, string newValue)
|
|
418 {
|
|
419 if (obj is ICostedWarFoundryObject)
|
|
420 {
|
|
421 SetNodeName(obj);
|
|
422 }
|
|
423 }
|
|
424
|
|
425 private void UpdateArmyName(WarFoundryObject obj, string oldValue, string newValue)
|
|
426 {
|
|
427 if (obj is Army)
|
|
428 {
|
|
429 Army army = (Army)obj;
|
|
430 TreeNode node = treeView.Nodes[0];
|
|
431
|
|
432 if (node!=null)
|
|
433 {
|
|
434 node.Text = army.Name;
|
|
435 }
|
|
436 }
|
|
437 }
|
|
438
|
|
439 private void FrmArmyTree_TreeNameChanged(WarFoundryObject obj, string oldValue, string newValue)
|
|
440 {
|
|
441 TreeNode node = nodes[obj.ID];
|
|
442
|
|
443 if (node!=null)
|
|
444 {
|
|
445 node.Text = obj.Name;
|
|
446 }
|
|
447 }
|
|
448
|
|
449 private void toolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
|
|
450 {
|
|
451 if (e.Button == bttnDelete)
|
|
452 {
|
|
453 DeleteUnit();
|
|
454 }
|
|
455 else if (e.Button == bttnEdit)
|
|
456 {
|
|
457 EditUnit();
|
|
458 }
|
|
459 }
|
|
460
|
|
461 private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
|
|
462 {
|
|
463 TreeNode node = treeView.SelectedNode;
|
|
464
|
|
465 bttnDelete.Enabled = (node != null && node.Tag is Unit);
|
|
466 bttnEdit.Enabled = bttnDelete.Enabled;
|
|
467 }
|
|
468 }
|
|
469 }
|