Mercurial > repos > IBDev-IBBoard.WarFoundry.GUI.WinForms
annotate FrmArmyTree.cs @ 48:cf644b2c65e6 WarFoundry_v0.1beta_Winforms
* Change assembly number to 0.0.1.* because we've not reached 0.1 yet, never mind 1.0!
* Add "COPYING" to the copied files so that we have a license
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 13 Sep 2009 19:03:37 +0000 |
parents | 1c74b51abac1 |
children | 93b3d23147ba |
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; | |
45
1c74b51abac1
Re #137: Remove use of old collections
IBBoard <dev@ibboard.co.uk>
parents:
44
diff
changeset
|
28 private Dictionary<string, TreeNode> htNodes; |
33 | 29 private System.Windows.Forms.ContextMenu contextMenu; |
30 private System.Windows.Forms.MenuItem miDelete; | |
31 private System.Windows.Forms.MenuItem miEdit; | |
45
1c74b51abac1
Re #137: Remove use of old collections
IBBoard <dev@ibboard.co.uk>
parents:
44
diff
changeset
|
32 private Dictionary<string, FrmUnit> htUnitWindows; |
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); | |
45
1c74b51abac1
Re #137: Remove use of old collections
IBBoard <dev@ibboard.co.uk>
parents:
44
diff
changeset
|
48 htNodes = new Dictionary<string, TreeNode>(); |
1c74b51abac1
Re #137: Remove use of old collections
IBBoard <dev@ibboard.co.uk>
parents:
44
diff
changeset
|
49 htUnitWindows = 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 { | |
140 htNodes.Clear(); | |
141 treeView.Nodes.Clear(); | |
45
1c74b51abac1
Re #137: Remove use of old collections
IBBoard <dev@ibboard.co.uk>
parents:
44
diff
changeset
|
142 FrmUnit[] unitForms = DictionaryUtils.ToArray(htUnitWindows); |
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 | |
149 htUnitWindows.Clear(); | |
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; | |
177 htNodes[cats[i].ID] = temp; | |
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; | |
192 htNodes[unit.ID] = temp; | |
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; | |
45
1c74b51abac1
Re #137: Remove use of old collections
IBBoard <dev@ibboard.co.uk>
parents:
44
diff
changeset
|
228 TreeNode parent = htNodes[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 { |
45
1c74b51abac1
Re #137: Remove use of old collections
IBBoard <dev@ibboard.co.uk>
parents:
44
diff
changeset
|
246 TreeNode unitNode = htNodes[unit.ID]; |
33 | 247 unit.NameChanged-= UnitNameChangedMethod; |
248 | |
249 if (unitNode!=null) | |
250 { | |
251 unitNode.Remove(); | |
252 htNodes.Remove(unit.ID); | |
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 { | |
306 editUnit(); | |
307 } | |
308 | |
309 private void treeView_DoubleClick(object sender, System.EventArgs e) | |
310 { | |
311 editUnit(); | |
312 } | |
313 | |
314 private void editUnit() | |
315 { | |
316 TreeNode selected = treeView.SelectedNode; | |
317 | |
318 if (selected.Tag!=null && selected.Tag is Unit) | |
319 { | |
320 Unit unit = (Unit)selected.Tag; | |
321 | |
322 if (htUnitWindows.ContainsKey(unit.ID)) | |
323 { | |
324 ((FrmUnit)htUnitWindows[unit.ID]).Focus(); | |
325 } | |
326 else | |
327 { | |
328 FrmUnit unitForm = new FrmUnit(unit, commandStack); | |
329 htUnitWindows.Add(unit.ID, unitForm); | |
330 unitForm.MdiParent = this.MdiParent; | |
331 unitForm.Closing+=new CancelEventHandler(unitForm_Closing); | |
332 unitForm.Show(); | |
333 } | |
334 } | |
335 } | |
336 | |
337 private void UpdateUnitName(WarFoundryObject obj, string oldValue, string newValue) | |
338 { | |
339 if (obj is Unit) | |
340 { | |
341 Unit unit = (Unit)obj; | |
45
1c74b51abac1
Re #137: Remove use of old collections
IBBoard <dev@ibboard.co.uk>
parents:
44
diff
changeset
|
342 TreeNode node = htNodes[unit.ID]; |
33 | 343 |
344 if (node!=null) | |
345 { | |
346 node.Text = unit.Name; | |
347 } | |
348 } | |
349 } | |
350 | |
351 private void UpdateArmyName(WarFoundryObject obj, string oldValue, string newValue) | |
352 { | |
353 if (obj is Army) | |
354 { | |
355 Army army = (Army)obj; | |
356 TreeNode node = treeView.Nodes[0]; | |
357 | |
358 if (node!=null) | |
359 { | |
360 node.Text = army.Name; | |
361 } | |
362 } | |
363 } | |
364 | |
365 private void unitForm_Closing(object sender, CancelEventArgs e) | |
366 { | |
367 if (sender is FrmUnit) | |
368 { | |
369 FrmUnit unitForm = (FrmUnit)sender; | |
370 htUnitWindows.Remove(unitForm.Unit.ID); | |
371 } | |
372 } | |
373 | |
374 private void FrmArmyTree_TreeNameChanged(WarFoundryObject obj, string oldValue, string newValue) | |
375 { | |
45
1c74b51abac1
Re #137: Remove use of old collections
IBBoard <dev@ibboard.co.uk>
parents:
44
diff
changeset
|
376 TreeNode node = htNodes[obj.ID]; |
33 | 377 |
378 if (node!=null) | |
379 { | |
380 node.Text = obj.Name; | |
381 } | |
382 } | |
383 } | |
384 } |