comparison FrmUnit.cs @ 0:7dd160dacb60

Initial commit of WarFoundry code
author IBBoard <dev@ibboard.co.uk>
date Fri, 19 Dec 2008 15:57:51 +0000
parents
children 42cf06b8f897
comparison
equal deleted inserted replaced
-1:000000000000 0:7dd160dacb60
1 using System;
2 using System.Drawing;
3 using System.Data;
4 using System.Collections;
5 using System.ComponentModel;
6 using System.Windows.Forms;
7 using IBBoard.Commands;
8 using IBBoard.Windows.Forms;
9 using IBBoard.WarFoundry.API;
10 using IBBoard.WarFoundry.API.Commands;
11
12 namespace IBBoard.WarFoundry
13 {
14 ///TODO: Separate weapons out into optional and required, where required only has button for replacing
15
16 /// <summary>
17 /// Summary description for FrmUnit.
18 /// </summary>
19 public class FrmUnit : IBBoard.Windows.Forms.IBBForm
20 {
21 private Unit unit;
22 private CommandStack commandStack;
23 private System.Windows.Forms.DataGrid statsGrid;
24 private System.Windows.Forms.TextBox tbUnitName;
25 private System.Windows.Forms.NumericUpDown unitSize;
26 private System.Windows.Forms.Label lblUnitSize;
27 private System.Windows.Forms.Button bttnAddWeapon;
28 private System.Windows.Forms.Button bttnRemoveWeapon;
29 private System.Windows.Forms.Button bttnEditWeapon;
30 private System.Windows.Forms.Label lblRequiredEquip;
31 private System.Windows.Forms.ListBox reqdList;
32 private System.Windows.Forms.ListBox optList;
33 private System.Windows.Forms.Label lblOptionalEquip;
34 private System.Windows.Forms.Button bttnReplaceWeapon;
35 private System.Windows.Forms.Button bttnEditReqdWeapon;
36 /// <summary>
37 /// Required designer variable.
38 /// </summary>
39 private System.ComponentModel.Container components = null;
40
41 public FrmUnit(Unit toDisplay, CommandStack cmdStack)
42 {
43 unit = toDisplay;
44 commandStack = cmdStack;
45 //
46 // Required for Windows Form Designer support
47 //
48 InitializeComponent();
49
50 tbUnitName.Text = unit.Name;
51 Text = unit.Name;
52 unit.NameChanged+=new StringValChangedDelegate(unit_NameChanged);
53 unit.UnitSizeChanged+= new IntValChangedDelegate(unit_UnitSizeChanged);
54 unit.UnitEquipmentAmountChanged+=new FloatValChangedDelegate(unit_UnitEquipmentAmountChanged);
55
56 if (unit.UnitType.MaxSize==unit.UnitType.MinSize)
57 {
58 unitSize.Value = unit.UnitType.MaxSize;
59 unitSize.Visible = false;
60 lblUnitSize.Visible = false;
61 }
62 else
63 {
64 unitSize.Value = unit.Size;
65 unitSize.Maximum = (unit.UnitType.MaxSize == -1 ? int.MaxValue : unit.UnitType.MaxSize);
66 unitSize.Minimum = unit.UnitType.MinSize;
67 }
68
69 setStats();
70 setWeapons();
71 }
72
73 private void setStats()
74 {
75 GameSystem system = unit.Army.GameSystem;
76 SystemStats stats = system.StandardSystemStats;
77
78 DataTable dt = new DataTable();
79 DataColumn[] dc = new DataColumn[stats.SlotCount+1];
80 dc[0] = new DataColumn("name");
81
82 DataGridTableStyle dgStyle = new DataGridTableStyle();
83 dgStyle.RowHeadersVisible = false;
84
85 DataGridTextBoxColumn colStyle = new DataGridTextBoxColumn();
86 colStyle.Width = statsGrid.ClientSize.Width - (stats.SlotCount * 40) - 4;
87 colStyle.MappingName = "name";
88 colStyle.HeaderText = "name";
89 colStyle.ReadOnly = true;
90 dgStyle.GridColumnStyles.Add(colStyle);
91
92 DataColumn tempCol;
93 int i = 1;
94
95 foreach (StatSlot stat in stats.StatSlots)
96 {
97 tempCol = new DataColumn(stat.Name);
98 dc[i] = tempCol;
99 colStyle = new DataGridTextBoxColumn();
100 colStyle.Alignment = HorizontalAlignment.Center;
101 colStyle.Width = 40;
102 colStyle.MappingName = stat.Name;
103 colStyle.HeaderText = stat.Name;
104 colStyle.ReadOnly = true;
105 dgStyle.GridColumnStyles.Add(colStyle);
106 i++;
107 }
108
109 dt.Columns.AddRange(dc);
110
111 DataRow dr = dt.NewRow();
112 dr.ItemArray = unit.UnitStats.StatsArray;
113 dt.Rows.Add(dr);
114 statsGrid.DataSource = dt;
115 statsGrid.TableStyles.Add(dgStyle);
116 }
117
118 private void setWeapons()
119 {
120 foreach(UnitEquipmentItem item in unit.GetEquipment())
121 {
122 if (item.IsRequired)
123 {
124 reqdList.Items.Add(UnitEquipmentItemObj.GetEquipObj(Unit, item));
125 }
126 else
127 {
128 optList.Items.Add(UnitEquipmentItemObj.GetEquipObj(Unit, item));
129 }
130 }
131 }
132
133 /// <summary>
134 /// Clean up any resources being used.
135 /// </summary>
136 protected override void Dispose( bool disposing )
137 {
138 //remove our leave events so that disposing doesn't trigger them
139 tbUnitName.Leave-= new System.EventHandler(this.tbUnitName_Leave);
140 unitSize.Leave-= new System.EventHandler(this.unitSize_Leave);
141
142 if( disposing )
143 {
144 if(components != null)
145 {
146 components.Dispose();
147 }
148 }
149 base.Dispose( disposing );
150 }
151
152 #region Windows Form Designer generated code
153 /// <summary>
154 /// Required method for Designer support - do not modify
155 /// the contents of this method with the code editor.
156 /// </summary>
157 private void InitializeComponent()
158 {
159 this.statsGrid = new System.Windows.Forms.DataGrid();
160 this.tbUnitName = new System.Windows.Forms.TextBox();
161 this.unitSize = new System.Windows.Forms.NumericUpDown();
162 this.lblUnitSize = new System.Windows.Forms.Label();
163 this.lblRequiredEquip = new System.Windows.Forms.Label();
164 this.bttnAddWeapon = new System.Windows.Forms.Button();
165 this.bttnRemoveWeapon = new System.Windows.Forms.Button();
166 this.reqdList = new System.Windows.Forms.ListBox();
167 this.bttnEditWeapon = new System.Windows.Forms.Button();
168 this.optList = new System.Windows.Forms.ListBox();
169 this.lblOptionalEquip = new System.Windows.Forms.Label();
170 this.bttnReplaceWeapon = new System.Windows.Forms.Button();
171 this.bttnEditReqdWeapon = new System.Windows.Forms.Button();
172 ((System.ComponentModel.ISupportInitialize)(this.statsGrid)).BeginInit();
173 ((System.ComponentModel.ISupportInitialize)(this.unitSize)).BeginInit();
174 this.SuspendLayout();
175 //
176 // statsGrid
177 //
178 this.statsGrid.AllowNavigation = false;
179 this.statsGrid.AllowSorting = false;
180 this.statsGrid.AlternatingBackColor = System.Drawing.SystemColors.Control;
181 this.statsGrid.BackgroundColor = System.Drawing.SystemColors.Control;
182 this.statsGrid.CaptionVisible = false;
183 this.statsGrid.DataMember = "";
184 this.statsGrid.GridLineColor = System.Drawing.SystemColors.ControlDarkDark;
185 this.statsGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
186 this.statsGrid.Location = new System.Drawing.Point(8, 32);
187 this.statsGrid.Name = "statsGrid";
188 this.statsGrid.PreferredColumnWidth = 40;
189 this.statsGrid.ReadOnly = true;
190 this.statsGrid.RowHeadersVisible = false;
191 this.statsGrid.SelectionBackColor = System.Drawing.SystemColors.Control;
192 this.statsGrid.SelectionForeColor = System.Drawing.SystemColors.WindowText;
193 this.statsGrid.Size = new System.Drawing.Size(600, 88);
194 this.statsGrid.TabIndex = 0;
195 this.statsGrid.TabStop = false;
196 //
197 // tbUnitName
198 //
199 this.tbUnitName.Location = new System.Drawing.Point(8, 8);
200 this.tbUnitName.Name = "tbUnitName";
201 this.tbUnitName.Size = new System.Drawing.Size(344, 20);
202 this.tbUnitName.TabIndex = 1;
203 this.tbUnitName.Text = "";
204 this.tbUnitName.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tbUnitName_KeyDown);
205 this.tbUnitName.Leave += new System.EventHandler(this.tbUnitName_Leave);
206 //
207 // unitSize
208 //
209 this.unitSize.Location = new System.Drawing.Point(528, 8);
210 this.unitSize.Name = "unitSize";
211 this.unitSize.Size = new System.Drawing.Size(80, 20);
212 this.unitSize.TabIndex = 1;
213 this.unitSize.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
214 this.unitSize.Value = new System.Decimal(new int[] {
215 1,
216 0,
217 0,
218 0});
219 this.unitSize.KeyDown += new System.Windows.Forms.KeyEventHandler(this.unitSize_KeyDown);
220 this.unitSize.Leave += new System.EventHandler(this.unitSize_Leave);
221 //
222 // lblUnitSize
223 //
224 this.lblUnitSize.Location = new System.Drawing.Point(424, 8);
225 this.lblUnitSize.Name = "lblUnitSize";
226 this.lblUnitSize.TabIndex = 0;
227 this.lblUnitSize.Text = "unit size";
228 this.lblUnitSize.TextAlign = System.Drawing.ContentAlignment.TopRight;
229 //
230 // lblRequiredEquip
231 //
232 this.lblRequiredEquip.Location = new System.Drawing.Point(8, 128);
233 this.lblRequiredEquip.Name = "lblRequiredEquip";
234 this.lblRequiredEquip.Size = new System.Drawing.Size(88, 32);
235 this.lblRequiredEquip.TabIndex = 3;
236 this.lblRequiredEquip.Text = "reqd equipment";
237 this.lblRequiredEquip.TextAlign = System.Drawing.ContentAlignment.TopRight;
238 //
239 // bttnAddWeapon
240 //
241 this.bttnAddWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System;
242 this.bttnAddWeapon.Location = new System.Drawing.Point(520, 200);
243 this.bttnAddWeapon.Name = "bttnAddWeapon";
244 this.bttnAddWeapon.Size = new System.Drawing.Size(88, 22);
245 this.bttnAddWeapon.TabIndex = 4;
246 this.bttnAddWeapon.Text = "add";
247 this.bttnAddWeapon.Click += new System.EventHandler(this.bttnAddWeapon_Click);
248 //
249 // bttnRemoveWeapon
250 //
251 this.bttnRemoveWeapon.Enabled = false;
252 this.bttnRemoveWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System;
253 this.bttnRemoveWeapon.Location = new System.Drawing.Point(520, 248);
254 this.bttnRemoveWeapon.Name = "bttnRemoveWeapon";
255 this.bttnRemoveWeapon.Size = new System.Drawing.Size(88, 22);
256 this.bttnRemoveWeapon.TabIndex = 5;
257 this.bttnRemoveWeapon.Text = "remove";
258 this.bttnRemoveWeapon.Click += new System.EventHandler(this.bttnRemoveWeapon_Click);
259 //
260 // reqdList
261 //
262 this.reqdList.Location = new System.Drawing.Point(104, 128);
263 this.reqdList.Name = "reqdList";
264 this.reqdList.Size = new System.Drawing.Size(408, 69);
265 this.reqdList.TabIndex = 6;
266 this.reqdList.DoubleClick += new System.EventHandler(this.reqdList_DoubleClick);
267 this.reqdList.SelectedIndexChanged += new System.EventHandler(this.reqdList_SelectedIndexChanged);
268 //
269 // bttnEditWeapon
270 //
271 this.bttnEditWeapon.Enabled = false;
272 this.bttnEditWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System;
273 this.bttnEditWeapon.Location = new System.Drawing.Point(520, 224);
274 this.bttnEditWeapon.Name = "bttnEditWeapon";
275 this.bttnEditWeapon.Size = new System.Drawing.Size(88, 22);
276 this.bttnEditWeapon.TabIndex = 7;
277 this.bttnEditWeapon.Text = "edit";
278 this.bttnEditWeapon.Click += new System.EventHandler(this.bttnEditWeapon_Click);
279 //
280 // optList
281 //
282 this.optList.Location = new System.Drawing.Point(104, 200);
283 this.optList.Name = "optList";
284 this.optList.Size = new System.Drawing.Size(408, 69);
285 this.optList.TabIndex = 9;
286 this.optList.DoubleClick += new System.EventHandler(this.optList_DoubleClick);
287 this.optList.SelectedIndexChanged += new System.EventHandler(this.optList_SelectedIndexChanged);
288 //
289 // lblOptionalEquip
290 //
291 this.lblOptionalEquip.Location = new System.Drawing.Point(8, 200);
292 this.lblOptionalEquip.Name = "lblOptionalEquip";
293 this.lblOptionalEquip.Size = new System.Drawing.Size(88, 32);
294 this.lblOptionalEquip.TabIndex = 8;
295 this.lblOptionalEquip.Text = "opt equipment";
296 this.lblOptionalEquip.TextAlign = System.Drawing.ContentAlignment.TopRight;
297 //
298 // bttnReplaceWeapon
299 //
300 this.bttnReplaceWeapon.Enabled = false;
301 this.bttnReplaceWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System;
302 this.bttnReplaceWeapon.Location = new System.Drawing.Point(520, 128);
303 this.bttnReplaceWeapon.Name = "bttnReplaceWeapon";
304 this.bttnReplaceWeapon.Size = new System.Drawing.Size(88, 22);
305 this.bttnReplaceWeapon.TabIndex = 10;
306 this.bttnReplaceWeapon.Text = "replace";
307 this.bttnReplaceWeapon.Click += new System.EventHandler(this.bttnReplaceWeapon_Click);
308 //
309 // bttnEditReqdWeapon
310 //
311 this.bttnEditReqdWeapon.Enabled = false;
312 this.bttnEditReqdWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System;
313 this.bttnEditReqdWeapon.Location = new System.Drawing.Point(520, 152);
314 this.bttnEditReqdWeapon.Name = "bttnEditReqdWeapon";
315 this.bttnEditReqdWeapon.Size = new System.Drawing.Size(88, 22);
316 this.bttnEditReqdWeapon.TabIndex = 11;
317 this.bttnEditReqdWeapon.Text = "edit";
318 this.bttnEditReqdWeapon.Click += new System.EventHandler(this.bttnEditReqdWeapon_Click);
319 //
320 // FrmUnit
321 //
322 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
323 this.ClientSize = new System.Drawing.Size(616, 314);
324 this.Controls.Add(this.bttnEditReqdWeapon);
325 this.Controls.Add(this.bttnReplaceWeapon);
326 this.Controls.Add(this.optList);
327 this.Controls.Add(this.lblOptionalEquip);
328 this.Controls.Add(this.bttnEditWeapon);
329 this.Controls.Add(this.reqdList);
330 this.Controls.Add(this.bttnRemoveWeapon);
331 this.Controls.Add(this.bttnAddWeapon);
332 this.Controls.Add(this.lblRequiredEquip);
333 this.Controls.Add(this.lblUnitSize);
334 this.Controls.Add(this.unitSize);
335 this.Controls.Add(this.tbUnitName);
336 this.Controls.Add(this.statsGrid);
337 this.Name = "FrmUnit";
338 this.ShowInTaskbar = false;
339 this.Text = "FrmUnit";
340 ((System.ComponentModel.ISupportInitialize)(this.statsGrid)).EndInit();
341 ((System.ComponentModel.ISupportInitialize)(this.unitSize)).EndInit();
342 this.ResumeLayout(false);
343
344 }
345 #endregion
346
347 public Unit Unit
348 {
349 get { return unit; }
350 }
351
352 private void tbUnitName_Leave(object sender, System.EventArgs e)
353 {
354 updateUnitName();
355 }
356
357 private void tbUnitName_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
358 {
359 if (e.KeyCode == Keys.Enter)
360 {
361 updateUnitName();
362 }
363 }
364
365 private void updateUnitName()
366 {
367 if (unit.Name!=tbUnitName.Text)
368 {
369 commandStack.Execute(new SetNameCommand(unit, tbUnitName.Text));
370 }
371 }
372
373 private void unitSize_Leave(object sender, System.EventArgs e)
374 {
375 updateUnitSize();
376 }
377
378 private void unitSize_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
379 {
380 if (e.KeyCode == Keys.Enter)
381 {
382 updateUnitSize();
383 }
384 }
385
386 private void updateUnitSize()
387 {
388 if (unit.Size!=unitSize.Value)
389 {
390 commandStack.Execute(new SetUnitSizeCommand(unit, (int)unitSize.Value));
391 }
392 }
393
394 private void unit_NameChanged(WarFoundryObject obj, string oldValue, string newValue)
395 {
396 if (obj is Unit && obj.Equals(unit))
397 {
398 Unit u = (Unit)obj;
399 tbUnitName.Text = obj.Name;
400 Text = obj.Name;
401 }
402 }
403
404 private void unit_UnitSizeChanged(WarFoundryObject obj, int oldValue, int newValue)
405 {
406 if (obj is Unit && obj.Equals(unit))
407 {
408 unitSize.Value = newValue;
409 }
410 }
411
412 private void reqdList_SelectedIndexChanged(object sender, System.EventArgs e)
413 {
414 bttnReplaceWeapon.Enabled = (reqdList.SelectedIndex>-1 && ((UnitEquipmentItemObj)reqdList.SelectedItem).Item.HasAlternatives());
415 bttnEditReqdWeapon.Enabled = (reqdList.SelectedIndex>-1);
416 }
417
418 private void optList_SelectedIndexChanged(object sender, System.EventArgs e)
419 {
420 bttnEditWeapon.Enabled = optList.SelectedIndex>-1;
421 bttnRemoveWeapon.Enabled = bttnEditWeapon.Enabled;
422 }
423
424 private void unit_UnitEquipmentAmountChanged(WarFoundryObject obj, float oldValue, float newValue)
425 {
426 if (obj is UnitEquipmentItem)
427 {
428 UnitEquipmentItem equip = (UnitEquipmentItem)obj;
429 ListBox weaponList = (equip.IsRequired ? reqdList : optList);
430
431 if (newValue==0)
432 {
433 weaponList.Items.Remove(UnitEquipmentItemObj.GetEquipObj(Unit, equip));
434 }
435 else
436 {
437 UnitEquipmentItemObj equipObj = UnitEquipmentItemObj.GetEquipObj(Unit, equip);
438 int idx = weaponList.Items.IndexOf(equipObj);
439
440 if (idx>-1)
441 {
442 weaponList.Items[idx] = equipObj;
443 }
444 else
445 {
446 weaponList.Items.Add(equipObj);
447 }
448 }
449 }
450 }
451
452 private void editWeapon(ListBox list)
453 {
454 FrmEditUnitEquipment editEquip = new FrmEditUnitEquipment(Unit, ((UnitEquipmentItemObj)list.SelectedItem).Item, commandStack);
455 editEquip.ShowDialog(this);
456 }
457
458 private void bttnEditWeapon_Click(object sender, System.EventArgs e)
459 {
460 editWeapon(optList);
461 }
462
463 private void optList_DoubleClick(object sender, System.EventArgs e)
464 {
465 editWeapon(optList);
466 }
467
468 private void reqdList_DoubleClick(object sender, System.EventArgs e)
469 {
470 editWeapon(reqdList);
471 }
472
473 private void addWeapon()
474 {
475 FrmNewUnitEquipment newEquip = new FrmNewUnitEquipment(Unit, commandStack);
476 newEquip.ShowDialog(this);
477 }
478
479 private void bttnAddWeapon_Click(object sender, System.EventArgs e)
480 {
481 addWeapon();
482 }
483
484 private void removeWeapon()
485 {
486 commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, unit.UnitType.GetEquipmentItem(((UnitEquipmentItemObj)optList.SelectedItem).Item.ID).EquipmentItem, 0));
487 }
488
489 private void bttnRemoveWeapon_Click(object sender, System.EventArgs e)
490 {
491 removeWeapon();
492 }
493
494 private void bttnEditReqdWeapon_Click(object sender, System.EventArgs e)
495 {
496 editWeapon(reqdList);
497 }
498
499 private void bttnReplaceWeapon_Click(object sender, System.EventArgs e)
500 {
501 FrmReplaceUnitEquipment replace = new FrmReplaceUnitEquipment(unit, ((UnitEquipmentItemObj)reqdList.SelectedItem).Item, commandStack);
502 replace.ShowDialog(this);
503 }
504 }
505 }