view FrmArmyTree.cs @ 234:c06d0865ff27

Fixes #388: Add "Add unit" options to army tree * Add button, context menu and double-click hooks for adding units
author IBBoard <dev@ibboard.co.uk>
date Sat, 21 Jan 2012 11:36:23 +0000
parents 870877498db9
children 2c3474d652e1
line source
1 ´╗┐using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using IBBoard.Commands;
9 using IBBoard.WarFoundry.API.Objects;
10 using IBBoard.WarFoundry.API;
11 using IBBoard.Lang;
12 using IBBoard.Windows.Forms.I18N;
13 using IBBoard.WarFoundry.API.Commands;
14 using IBBoard.Windows.Forms;
16 namespace IBBoard.WarFoundry.GUI.WinForms
17 {
18 public partial class FrmArmyTree : IBBoard.Windows.Forms.IBBForm
19 {
20 private Dictionary<string, TreeNode> nodes;
21 private ObjectAddDelegate UnitAddedMethod;
22 private ObjectRemoveDelegate UnitRemovedMethod;
23 private StringValChangedDelegate UnitNameChangedMethod, ArmyNameChangedMethod, TreeNameChangedMethod;
24 private CommandStack commandStack;
25 public GameSystem CurrentGameSystem
26 {
27 get { return WarFoundryCore.CurrentGameSystem; }
28 set { WarFoundryCore.CurrentGameSystem = value; }
29 }
31 public FrmArmyTree(CommandStack cmdStack)
32 {
33 commandStack = cmdStack;
34 InitializeComponent();
35 UnitAddedMethod = new ObjectAddDelegate(UnitAdded);
36 UnitRemovedMethod = new ObjectRemoveDelegate(UnitRemoved);
37 UnitNameChangedMethod = new StringValChangedDelegate(UpdateUnitName);
38 ArmyNameChangedMethod = new StringValChangedDelegate(UpdateArmyName);
39 TreeNameChangedMethod = new StringValChangedDelegate(FrmArmyTree_TreeNameChanged);
40 nodes = new Dictionary<string, TreeNode>();
41 this.Name = "ArmyTree";
42 miDeleteUnit.Name = "miDeleteUnit";
43 miEditUnit.Name = "miEditUnit";
44 miEditArmy.Name = "miEditArmy";
45 WarFoundryCore.ArmyChanged += new ArmyChangedDelegate(FrmArmyTree_ArmyChanged);
46 TranslateForm();
47 Translation.TranslationChanged += new MethodInvoker(TranslateForm);
48 }
50 private void TranslateForm()
51 {
52 ControlTranslator.TranslateControl(this);
54 foreach (Component comp in components.Components)
55 {
56 ControlTranslator.TranslateComponent(comp);
57 }
59 foreach (IBBToolStripMenuItem mi in contextMenuStrip.Items)
60 {
61 ControlTranslator.TranslateComponent(mi);
62 }
64 foreach (TreeNode node in nodes.Values)
65 {
66 node.Text = CreateNodeName((ICostedWarFoundryObject)node.Tag);
67 }
68 }
69 private void ClearArmy()
70 {
71 nodes.Clear();
72 treeView.Nodes.Clear();
73 }
75 private void SetArmy(Army army)
76 {
77 ClearArmy();
79 if (army != null)
80 {
81 ArmyCategory[] cats = army.Categories;
82 TreeNode[] catNodes = new TreeNode[cats.Length];
83 Unit[] units;
85 for (int i = 0; i < cats.Length; i++)
86 {
87 ArmyCategory cat = cats[i];
88 units = army.GetUnits(cat);
89 cat.NameChanged += TreeNameChangedMethod;
90 cat.PointsValueChanged += NodePointsValueChanged;
91 TreeNode[] unitNodes = new TreeNode[units.Length];
92 TreeNode temp;
94 for (int j = 0; j < units.Length; j++)
95 {
96 unitNodes[j] = CreateTreeNode(units[j]);
97 }
99 temp = new TreeNode(CreateNodeName(cat), unitNodes);
100 temp.Tag = cat;
101 catNodes[i] = temp;
102 nodes[cat.ID] = temp;
103 }
105 TreeNode root = new TreeNode(CreateNodeName(army), catNodes);
106 root.Tag = army;
107 army.PointsValueChanged += NodePointsValueChanged;
108 nodes[army.ID] = root;
109 treeView.Nodes.Add(root);
110 root.ExpandAll();
111 }
112 }
114 private string CreateNodeName(ICostedWarFoundryObject obj)
115 {
116 return Translation.GetTranslation("treeNodeText", "{0} ({1} pts)", obj.Name, obj.Points, CurrentGameSystem.GetPointsAbbrev(obj.Points));
117 }
119 private void NodePointsValueChanged(WarFoundryObject obj, double oldValue, double newValue)
120 {
121 if (obj is ICostedWarFoundryObject)
122 {
123 SetNodeName(obj);
124 }
125 }
127 private void SetNodeName(WarFoundryObject obj)
128 {
129 TreeNode node = nodes[obj.ID];
131 if (node != null)
132 {
133 node.Text = CreateNodeName((ICostedWarFoundryObject)obj);
134 }
135 }
137 private TreeNode CreateTreeNode(Unit unit)
138 {
139 TreeNode temp = new TreeNode(CreateNodeName(unit));
140 temp.Tag = unit;
141 unit.NameChanged += UnitNameChangedMethod;
142 unit.PointsValueChanged += NodePointsValueChanged;
143 nodes[unit.ID] = temp;
144 return temp;
145 }
147 public void FrmArmyTree_ArmyChanged(Army oldArmy, Army newArmy)
148 {
149 if (oldArmy != null)
150 {
151 oldArmy.NameChanged -= ArmyNameChangedMethod;
152 oldArmy.UnitAdded -= UnitAddedMethod;
153 oldArmy.UnitRemoved -= UnitRemovedMethod;
154 oldArmy.PointsValueChanged -= NodePointsValueChanged;
155 }
157 if (newArmy != null)
158 {
159 newArmy.NameChanged += ArmyNameChangedMethod;
160 newArmy.UnitAdded += UnitAddedMethod;
161 newArmy.UnitRemoved += UnitRemovedMethod;
162 newArmy.PointsValueChanged += NodePointsValueChanged;
163 }
165 SetArmy(newArmy);
166 }
168 private void UnitAdded(WarFoundryObject obj)
169 {
170 if (obj is Unit)
171 {
172 Unit unit = (Unit)obj;
173 ArmyCategory cat = unit.Category;
174 TreeNode parent = nodes[cat.ID];
175 TreeNode unitNode = CreateTreeNode(unit);
176 parent.Nodes.Add(unitNode);
177 parent.Expand(); //make sure it's expanded
178 }
179 }
181 public new FrmMain MdiParent
182 {
183 get { return (FrmMain)base.MdiParent; }
184 set { base.MdiParent = value; }
185 }
187 private void UnitRemoved(WarFoundryObject obj)
188 {
189 if (obj is Unit)
190 {
191 Unit unit = (Unit)obj;
192 RemoveUnitFromTree(unit);
193 }
194 }
196 private void RemoveUnitFromTree(Unit unit)
197 {
198 TreeNode unitNode = nodes[unit.ID];
199 unit.NameChanged -= UnitNameChangedMethod;
201 if (unitNode != null)
202 {
203 unitNode.Remove();
204 nodes.Remove(unit.ID);
205 }
206 }
208 private void treeView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
209 {
210 if (e.Button == MouseButtons.Right)
211 {
212 TreeNode tn = treeView.GetNodeAt(e.X, e.Y);
214 if (tn != null)
215 {
216 treeView.SelectedNode = tn;
217 }
218 else
219 {
220 treeView.SelectedNode = null;
221 }
222 }
223 }
225 private void miDelete_Click(object sender, System.EventArgs e)
226 {
227 DeleteUnit();
228 }
230 private void DeleteUnit()
231 {
233 TreeNode selected = treeView.SelectedNode;
235 if (selected.Tag != null && selected.Tag is Unit)
236 {
237 Unit unit = (Unit)selected.Tag;
238 commandStack.Execute(new RemoveUnitCommand(unit));
239 }
240 }
242 private void miEdit_Click(object sender, System.EventArgs e)
243 {
244 EditTreeItem();
245 }
247 private void treeView_DoubleClick(object sender, System.EventArgs e)
248 {
249 EditTreeItem();
250 }
252 private void EditTreeItem()
253 {
254 TreeNode selected = treeView.SelectedNode;
255 object tagData = selected.Tag;
257 if (tagData is Unit)
258 {
259 Unit unit = (Unit)tagData;
260 MdiParent.OpenUnitDialog(unit);
261 }
262 else if (tagData is Army)
263 {
264 EditArmy((Army)tagData);
265 }
266 else if (tagData is ArmyCategory)
267 {
268 AddUnitFromCategory((ArmyCategory)tagData);
269 }
270 }
272 private void UpdateUnitName(WarFoundryObject obj, string oldValue, string newValue)
273 {
274 if (obj is ICostedWarFoundryObject)
275 {
276 SetNodeName(obj);
277 }
278 }
280 private void UpdateArmyName(WarFoundryObject obj, string oldValue, string newValue)
281 {
282 if (obj is Army)
283 {
284 SetNodeName((Army)obj);
285 }
286 }
288 private void FrmArmyTree_TreeNameChanged(WarFoundryObject obj, string oldValue, string newValue)
289 {
290 TreeNode node = nodes[obj.ID];
292 if (node != null)
293 {
294 node.Text = obj.Name;
295 }
296 }
298 private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
299 {
300 TreeNode node = treeView.SelectedNode;
301 bttnAdd.Enabled = (node != null && node.Tag is ArmyCategory);
302 bttnDelete.Enabled = (node != null && node.Tag is Unit);
303 bttnEdit.Enabled = (node != null && (node.Tag is Unit || node.Tag is Army));
304 }
306 private void miEditArmy_Click(object sender, EventArgs e)
307 {
308 Army army = treeView.SelectedNode.Tag as Army;
309 EditArmy(army);
310 }
312 private void EditArmy(Army army)
313 {
314 FrmEditArmy editForm = new FrmEditArmy(commandStack, army);
315 editForm.ShowDialog(this.ParentForm);
316 }
318 private void bttnEdit_Click(object sender, EventArgs e)
319 {
320 EditTreeItem();
321 }
323 private void bttnDelete_Click(object sender, EventArgs e)
324 {
325 DeleteUnit();
326 }
328 private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
329 {
330 TreeNode node = treeView.SelectedNode;
331 Object tag = (node != null) ? node.Tag : null;
332 bool tagIsUnit = (tag is Unit);
333 bool tagIsArmy = (tag is Army);
334 bool tagIsCategory = (tag is ArmyCategory);
336 if (!tagIsUnit && !tagIsArmy && !tagIsCategory)
337 {
338 e.Cancel = true;
339 }
340 else
341 {
342 miDeleteUnit.Visible = tagIsUnit;
343 miEditUnit.Visible = tagIsUnit;
344 miEditArmy.Visible = tagIsArmy;
345 miAddUnit.Visible = tagIsCategory;
346 }
347 }
349 private void miAddUnit_Click(object sender, EventArgs e)
350 {
351 AddUnit();
352 }
354 private void AddUnit()
355 {
356 ArmyCategory tagData = treeView.SelectedNode.Tag as ArmyCategory;
357 AddUnitFromCategory(tagData);
358 }
360 private void AddUnitFromCategory(ArmyCategory tagData)
361 {
362 MdiParent.AddUnitFromCategory(tagData.Category);
363 }
365 private void bttnAdd_Click(object sender, EventArgs e)
366 {
367 AddUnit();
368 }
369 }
370 }