Mercurial > repos > IBBoard.WarFoundry.GUI.WinForms
annotate FrmArmyTree.cs @ 55:346b4ac34997
Re #163: Unit cost on unit dialog
* Prevent overlap of labels
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 19 Sep 2009 10:48:46 +0000 |
parents | c972fb283675 |
children | f272e70a9f71 |
rev | line source |
---|---|
33 | 1 // This file (FrmArmyTree.cs) is a part of the IBBoard.WarFoundry.GUI.WinForms project and is copyright 2009 IBBoard. |
2 // | |
36 | 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. |
33 | 4 |
5 using System; | |
6 using System.Drawing; | |
45
1c74b51abac1
Re #137: Remove use of old collections
IBBoard <dev@ibboard.co.uk>
parents:
44
diff
changeset
|
7 using System.Collections.Generic; |
33 | 8 using System.ComponentModel; |
9 using System.Windows.Forms; | |
10 using IBBoard.Commands; | |
11 using IBBoard.WarFoundry.API; | |
12 using IBBoard.WarFoundry.API.Commands; | |
13 using IBBoard.Windows.Forms; | |
27
526fefefb16b
Fixes #91: Fix WinForms rendering of unit stats
IBBoard <dev@ibboard.co.uk>
parents:
24
diff
changeset
|
14 using IBBoard.WarFoundry.API.Objects; |
526fefefb16b
Fixes #91: Fix WinForms rendering of unit stats
IBBoard <dev@ibboard.co.uk>
parents:
24
diff
changeset
|
15 |
40
740350673006
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
16 namespace IBBoard.WarFoundry.GUI.WinForms |
33 | 17 { |
18 /// <summary> | |
19 /// Summary description for FrmArmyTree. | |
20 /// </summary> | |
21 public class FrmArmyTree : IBBoard.Windows.Forms.IBBForm | |
22 { | |
23 private System.Windows.Forms.TreeView treeView; | |
24 /// <summary> | |
25 /// Required designer variable. | |
26 /// </summary> | |
27 private System.ComponentModel.Container components = null; | |
49
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
28 private Dictionary<string, TreeNode> nodes; |
33 | 29 private System.Windows.Forms.ContextMenu contextMenu; |
30 private System.Windows.Forms.MenuItem miDelete; | |
31 private System.Windows.Forms.MenuItem miEdit; | |
49
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
32 private Dictionary<string, FrmUnit> unitWindows; |
33 | 33 |
34 private ObjectAddDelegate UnitAddedMethod; | |
35 private ObjectRemoveDelegate UnitRemovedMethod; | |
36 private StringValChangedDelegate UnitNameChangedMethod, ArmyNameChangedMethod, TreeNameChangedMethod; | |
37 private CommandStack commandStack; | |
38 | |
39 public FrmArmyTree(CommandStack cmdStack) | |
40 { | |
41 commandStack = cmdStack; | |
42 InitializeComponent(); | |
43 UnitAddedMethod = new ObjectAddDelegate(AddUnit); | |
44 UnitRemovedMethod = new ObjectRemoveDelegate(RemoveUnit); | |
45 UnitNameChangedMethod = new StringValChangedDelegate(UpdateUnitName); | |
46 ArmyNameChangedMethod = new StringValChangedDelegate(UpdateArmyName); | |
47 TreeNameChangedMethod = new StringValChangedDelegate(FrmArmyTree_TreeNameChanged); | |
49
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
48 nodes = new Dictionary<string, TreeNode>(); |
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
49 unitWindows = new Dictionary<string, FrmUnit>(); |
33 | 50 this.Name = "ArmyTree"; |
51 WarFoundryCore.ArmyChanged+= new ArmyChangedDelegate(FrmArmyTree_ArmyChanged); | |
52 } | |
53 | |
54 public FrmArmyTree(Army army, CommandStack cmdStack) : this(cmdStack) | |
55 { | |
56 SetArmy(army); | |
57 } | |
58 | |
59 /// <summary> | |
60 /// Clean up any resources being used. | |
61 /// </summary> | |
62 protected override void Dispose( bool disposing ) | |
63 { | |
64 if( disposing ) | |
65 { | |
66 if(components != null) | |
67 { | |
68 components.Dispose(); | |
69 } | |
70 } | |
71 base.Dispose( disposing ); | |
72 } | |
73 | |
74 #region Windows Form Designer generated code | |
75 /// <summary> | |
76 /// Required method for Designer support - do not modify | |
77 /// the contents of this method with the code editor. | |
78 /// </summary> | |
79 private void InitializeComponent() | |
80 { | |
81 this.treeView = new System.Windows.Forms.TreeView(); | |
82 this.contextMenu = new System.Windows.Forms.ContextMenu(); | |
83 this.miEdit = new System.Windows.Forms.MenuItem(); | |
84 this.miDelete = new System.Windows.Forms.MenuItem(); | |
85 this.SuspendLayout(); | |
86 // | |
87 // treeView | |
88 // | |
89 this.treeView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | |
90 | System.Windows.Forms.AnchorStyles.Left) | |
91 | System.Windows.Forms.AnchorStyles.Right))); | |
92 this.treeView.ContextMenu = this.contextMenu; | |
93 this.treeView.FullRowSelect = true; | |
94 this.treeView.ImageIndex = -1; | |
95 this.treeView.Location = new System.Drawing.Point(0, 0); | |
96 this.treeView.Name = "treeView"; | |
97 this.treeView.SelectedImageIndex = -1; | |
98 this.treeView.Size = new System.Drawing.Size(240, 278); | |
99 this.treeView.TabIndex = 0; | |
100 this.treeView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView_MouseDown); | |
101 this.treeView.DoubleClick += new System.EventHandler(this.treeView_DoubleClick); | |
102 // | |
103 // contextMenu | |
104 // | |
105 this.contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { | |
106 this.miEdit, | |
107 this.miDelete}); | |
108 this.contextMenu.Popup += new System.EventHandler(this.contextMenu_Popup); | |
109 // | |
110 // miEdit | |
111 // | |
112 this.miEdit.Index = 0; | |
113 this.miEdit.Text = "&Edit unit"; | |
114 this.miEdit.Click += new System.EventHandler(this.miEdit_Click); | |
115 // | |
116 // miDelete | |
117 // | |
118 this.miDelete.Index = 1; | |
119 this.miDelete.Text = "&Delete unit"; | |
120 this.miDelete.Click += new System.EventHandler(this.miDelete_Click); | |
121 // | |
122 // FrmArmyTree | |
123 // | |
124 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); | |
125 this.ClientSize = new System.Drawing.Size(240, 277); | |
126 this.ControlBox = false; | |
127 this.Controls.Add(this.treeView); | |
128 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow; | |
129 this.MaximizeBox = false; | |
130 this.MinimizeBox = false; | |
131 this.Name = "FrmArmyTree"; | |
132 this.Text = "FrmArmyTree"; | |
133 this.ResumeLayout(false); | |
134 | |
135 } | |
136 #endregion | |
137 | |
138 private void ClearArmy() | |
139 { | |
49
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
140 nodes.Clear(); |
33 | 141 treeView.Nodes.Clear(); |
49
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
142 FrmUnit[] unitForms = DictionaryUtils.ToArray(unitWindows); |
33 | 143 |
44
1486ccd744dc
Fixes #135: Closing army crashes WinForms
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
144 foreach (FrmUnit window in unitForms) |
33 | 145 { |
146 window.Close(); | |
147 } | |
148 | |
49
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
149 unitWindows.Clear(); |
33 | 150 } |
151 | |
152 private void SetArmy(Army army) | |
153 { | |
154 ClearArmy(); | |
155 | |
156 if (army != null) | |
157 { | |
158 ArmyCategory[] cats = army.Categories; | |
159 TreeNode[] catNodes = new TreeNode[cats.Length]; | |
160 Unit[] units; | |
161 | |
162 for (int i = 0; i < cats.Length; i++) | |
163 { | |
164 units = army.GetUnits(cats[i]); | |
165 cats[i].NameChanged += TreeNameChangedMethod; | |
166 TreeNode[] unitNodes = new TreeNode[units.Length]; | |
167 TreeNode temp; | |
168 | |
169 for (int j = 0; j < units.Length; j++) | |
170 { | |
171 unitNodes[j] = createTreeNode(units[j]); | |
172 } | |
173 | |
174 temp = new TreeNode(cats[i].Name, unitNodes); | |
175 temp.Tag = cats[i]; | |
176 catNodes[i] = temp; | |
49
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
177 nodes[cats[i].ID] = temp; |
33 | 178 } |
179 | |
180 TreeNode root = new TreeNode(army.Name, catNodes); | |
181 root.Tag = army; | |
182 treeView.Nodes.Add(root); | |
183 root.ExpandAll(); | |
184 } | |
185 } | |
186 | |
187 private TreeNode createTreeNode(Unit unit) | |
188 { | |
189 TreeNode temp = new TreeNode(unit.Name); | |
190 temp.Tag = unit; | |
191 unit.NameChanged+= UnitNameChangedMethod; | |
49
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
192 nodes[unit.ID] = temp; |
33 | 193 return temp; |
194 } | |
195 | |
196 /*private void FrmArmyTree_Move(object sender, System.EventArgs e) | |
197 { | |
198 if (ParentForm is FrmMain) | |
199 { | |
200 FrmMain main = (FrmMain)ParentForm; | |
201 main.Invoke(new MethodInvoker(main.MdiChildMoved)); | |
202 } | |
203 }*/ | |
204 | |
205 public void FrmArmyTree_ArmyChanged(Army oldArmy, Army newArmy) | |
206 { | |
207 if (oldArmy != null) | |
208 { | |
209 oldArmy.UnitAdded -= UnitAddedMethod; | |
210 oldArmy.UnitRemoved -= UnitRemovedMethod; | |
211 } | |
212 | |
213 if (newArmy != null) | |
214 { | |
215 newArmy.UnitAdded += UnitAddedMethod; | |
216 newArmy.UnitRemoved += UnitRemovedMethod; | |
217 } | |
218 | |
219 SetArmy(newArmy); | |
220 } | |
45
1c74b51abac1
Re #137: Remove use of old collections
IBBoard <dev@ibboard.co.uk>
parents:
44
diff
changeset
|
221 |
1c74b51abac1
Re #137: Remove use of old collections
IBBoard <dev@ibboard.co.uk>
parents:
44
diff
changeset
|
222 private void AddUnit(WarFoundryObject obj) |
33 | 223 { |
224 if (obj is Unit) | |
225 { | |
226 Unit unit = (Unit)obj; | |
227 ArmyCategory cat = unit.Category; | |
49
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
228 TreeNode parent = nodes[cat.ID]; |
33 | 229 TreeNode unitNode = createTreeNode(unit); |
230 parent.Nodes.Add(unitNode); | |
231 parent.Expand(); //make sure it's expanded | |
232 } | |
233 } | |
234 | |
45
1c74b51abac1
Re #137: Remove use of old collections
IBBoard <dev@ibboard.co.uk>
parents:
44
diff
changeset
|
235 private void RemoveUnit(WarFoundryObject obj) |
33 | 236 { |
237 if (obj is Unit) | |
238 { | |
239 Unit unit = (Unit)obj; | |
45
1c74b51abac1
Re #137: Remove use of old collections
IBBoard <dev@ibboard.co.uk>
parents:
44
diff
changeset
|
240 RemoveUnitFromTree(unit); |
33 | 241 } |
242 } | |
243 | |
45
1c74b51abac1
Re #137: Remove use of old collections
IBBoard <dev@ibboard.co.uk>
parents:
44
diff
changeset
|
244 private void RemoveUnitFromTree(Unit unit) |
33 | 245 { |
49
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
246 TreeNode unitNode = nodes[unit.ID]; |
33 | 247 unit.NameChanged-= UnitNameChangedMethod; |
248 | |
249 if (unitNode!=null) | |
250 { | |
251 unitNode.Remove(); | |
49
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
252 nodes.Remove(unit.ID); |
33 | 253 } |
254 } | |
255 | |
256 | |
257 private void contextMenu_Popup(object sender, System.EventArgs e) | |
258 { | |
259 TreeNode node = treeView.SelectedNode; | |
260 if (node!=null && node.Tag is Unit) | |
261 { | |
262 foreach(MenuItem item in contextMenu.MenuItems) | |
263 { | |
264 item.Visible = true; | |
265 } | |
266 } | |
267 else | |
268 { | |
269 foreach(MenuItem item in contextMenu.MenuItems) | |
270 { | |
271 item.Visible = false; | |
272 } | |
273 } | |
274 } | |
275 | |
276 private void treeView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) | |
277 { | |
278 if (e.Button == MouseButtons.Right) | |
279 { | |
280 TreeNode tn = treeView.GetNodeAt(e.X, e.Y); | |
281 | |
282 if (tn!=null) | |
283 { | |
284 treeView.SelectedNode = tn; | |
285 } | |
286 else | |
287 { | |
288 treeView.SelectedNode = null; | |
289 } | |
290 } | |
291 } | |
292 | |
293 private void miDelete_Click(object sender, System.EventArgs e) | |
294 { | |
295 TreeNode selected = treeView.SelectedNode; | |
296 | |
297 if (selected.Tag!=null && selected.Tag is Unit) | |
298 { | |
299 Unit unit = (Unit)selected.Tag; | |
300 commandStack.Execute(new RemoveUnitCommand(unit)); | |
301 } | |
302 } | |
303 | |
304 private void miEdit_Click(object sender, System.EventArgs e) | |
305 { | |
49
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
306 EditUnit(); |
33 | 307 } |
308 | |
309 private void treeView_DoubleClick(object sender, System.EventArgs e) | |
310 { | |
49
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
311 EditUnit(); |
33 | 312 } |
313 | |
49
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
314 private void EditUnit() |
33 | 315 { |
316 TreeNode selected = treeView.SelectedNode; | |
50
c972fb283675
Fixes #149: Crash with edit on null tree item
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
317 object tagData = selected.Tag; |
33 | 318 |
50
c972fb283675
Fixes #149: Crash with edit on null tree item
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
319 if (tagData is Unit) |
33 | 320 { |
50
c972fb283675
Fixes #149: Crash with edit on null tree item
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
321 Unit unit = (Unit) tagData; |
c972fb283675
Fixes #149: Crash with edit on null tree item
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
322 string unitID = unit.ID; |
33 | 323 |
50
c972fb283675
Fixes #149: Crash with edit on null tree item
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
324 if (unitWindows.ContainsKey(unitID)) |
33 | 325 { |
50
c972fb283675
Fixes #149: Crash with edit on null tree item
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
326 unitWindows[unitID].Focus(); |
33 | 327 } |
328 else | |
329 { | |
330 FrmUnit unitForm = new FrmUnit(unit, commandStack); | |
50
c972fb283675
Fixes #149: Crash with edit on null tree item
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
331 unitWindows.Add(unitID, unitForm); |
33 | 332 unitForm.MdiParent = this.MdiParent; |
333 unitForm.Closing+=new CancelEventHandler(unitForm_Closing); | |
334 unitForm.Show(); | |
335 } | |
336 } | |
337 } | |
338 | |
339 private void UpdateUnitName(WarFoundryObject obj, string oldValue, string newValue) | |
340 { | |
341 if (obj is Unit) | |
342 { | |
343 Unit unit = (Unit)obj; | |
49
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
344 TreeNode node = nodes[unit.ID]; |
33 | 345 |
346 if (node!=null) | |
347 { | |
348 node.Text = unit.Name; | |
349 } | |
350 } | |
351 } | |
352 | |
353 private void UpdateArmyName(WarFoundryObject obj, string oldValue, string newValue) | |
354 { | |
355 if (obj is Army) | |
356 { | |
357 Army army = (Army)obj; | |
358 TreeNode node = treeView.Nodes[0]; | |
359 | |
360 if (node!=null) | |
361 { | |
362 node.Text = army.Name; | |
363 } | |
364 } | |
365 } | |
366 | |
367 private void unitForm_Closing(object sender, CancelEventArgs e) | |
368 { | |
369 if (sender is FrmUnit) | |
370 { | |
371 FrmUnit unitForm = (FrmUnit)sender; | |
49
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
372 unitWindows.Remove(unitForm.Unit.ID); |
33 | 373 } |
374 } | |
375 | |
376 private void FrmArmyTree_TreeNameChanged(WarFoundryObject obj, string oldValue, string newValue) | |
377 { | |
49
93b3d23147ba
Fixes #160: Army tree is visible when no army is available
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
378 TreeNode node = nodes[obj.ID]; |
33 | 379 |
380 if (node!=null) | |
381 { | |
382 node.Text = obj.Name; | |
383 } | |
384 } | |
385 } | |
386 } |