comparison FrmUnit.cs @ 8:755e60be28be

Re #61 - Complete structure of WarFoundry API objects * Update to using UnitEquipmentItem where properties have moved from EquipmentItem * Add UnitEquipmentChoice as replacement for UnitEquipmentItemObj
author IBBoard <dev@ibboard.co.uk>
date Sun, 05 Apr 2009 13:50:41 +0000
parents 8935971e307c
children 19bdbb80999c
comparison
equal deleted inserted replaced
7:9828ba4f3f36 8:755e60be28be
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. 3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
4 4
5 using System; 5 using System;
6 using System.Drawing; 6 using System.Drawing;
7 using System.Data; 7 using System.Data;
8 using System.Collections; 8 using System.Collections.Generic;
9 using System.ComponentModel; 9 using System.ComponentModel;
10 using System.Windows.Forms; 10 using System.Windows.Forms;
11 using IBBoard.Commands; 11 using IBBoard.Commands;
12 using IBBoard.Windows.Forms; 12 using IBBoard.Windows.Forms;
13 using IBBoard.WarFoundry.API; 13 using IBBoard.WarFoundry.API;
14 using IBBoard.WarFoundry.API.Commands; 14 using IBBoard.WarFoundry.API.Commands;
15 using IBBoard.WarFoundry.API.Objects; 15 using IBBoard.WarFoundry.API.Objects;
16 using IBBoard.WarFoundry.GUI.WinForms.Util;
16 17
17 namespace IBBoard.WarFoundry 18 namespace IBBoard.WarFoundry
18 { 19 {
19 ///TODO: Separate weapons out into optional and required, where required only has button for replacing 20 ///TODO: Separate weapons out into optional and required, where required only has button for replacing
20 21
22 /// Summary description for FrmUnit. 23 /// Summary description for FrmUnit.
23 /// </summary> 24 /// </summary>
24 public class FrmUnit : IBBoard.Windows.Forms.IBBForm 25 public class FrmUnit : IBBoard.Windows.Forms.IBBForm
25 { 26 {
26 private Unit unit; 27 private Unit unit;
28 private Dictionary<UnitEquipmentItem, UnitEquipmentChoice> equipmentChoices = new Dictionary<UnitEquipmentItem, UnitEquipmentChoice>();
27 private CommandStack commandStack; 29 private CommandStack commandStack;
28 private System.Windows.Forms.DataGrid statsGrid; 30 private System.Windows.Forms.DataGrid statsGrid;
29 private System.Windows.Forms.TextBox tbUnitName; 31 private System.Windows.Forms.TextBox tbUnitName;
30 private System.Windows.Forms.NumericUpDown unitSize; 32 private System.Windows.Forms.NumericUpDown unitSize;
31 private System.Windows.Forms.Label lblUnitSize; 33 private System.Windows.Forms.Label lblUnitSize;
54 56
55 tbUnitName.Text = unit.Name; 57 tbUnitName.Text = unit.Name;
56 Text = unit.Name; 58 Text = unit.Name;
57 unit.NameChanged+=new StringValChangedDelegate(unit_NameChanged); 59 unit.NameChanged+=new StringValChangedDelegate(unit_NameChanged);
58 unit.UnitSizeChanged+= new IntValChangedDelegate(unit_UnitSizeChanged); 60 unit.UnitSizeChanged+= new IntValChangedDelegate(unit_UnitSizeChanged);
59 unit.UnitEquipmentAmountChanged+=new FloatValChangedDelegate(unit_UnitEquipmentAmountChanged); 61 unit.UnitEquipmentAmountChanged+=new DoubleValChangedDelegate(unit_UnitEquipmentAmountChanged);
60 62
61 if (unit.UnitType.MaxSize==unit.UnitType.MinSize) 63 if (unit.UnitType.MaxSize==unit.UnitType.MinSize)
62 { 64 {
63 unitSize.Value = unit.UnitType.MaxSize; 65 unitSize.Value = unit.UnitType.MaxSize;
64 unitSize.Visible = false; 66 unitSize.Visible = false;
124 { 126 {
125 foreach(UnitEquipmentItem item in unit.GetEquipment()) 127 foreach(UnitEquipmentItem item in unit.GetEquipment())
126 { 128 {
127 if (item.IsRequired) 129 if (item.IsRequired)
128 { 130 {
129 reqdList.Items.Add(UnitEquipmentItemObj.GetEquipObj(Unit, item)); 131 reqdList.Items.Add(GetEquipmentChoice(item));
130 } 132 }
131 else 133 else
132 { 134 {
133 optList.Items.Add(UnitEquipmentItemObj.GetEquipObj(Unit, item)); 135 optList.Items.Add(GetEquipmentChoice(item));
134 } 136 }
135 } 137 }
136 } 138 }
139
140 private UnitEquipmentChoice GetEquipmentChoice(UnitEquipmentItem item)
141 {
142 UnitEquipmentChoice choice = null;
143 equipmentChoices.TryGetValue(item, out choice);
144
145 if (choice == null)
146 {
147 choice = new UnitEquipmentChoice(Unit, item);
148 equipmentChoices[item] = choice;
149 }
150
151 return choice;
152 }
137 153
138 /// <summary> 154 /// <summary>
139 /// Clean up any resources being used. 155 /// Clean up any resources being used.
140 /// </summary> 156 /// </summary>
141 protected override void Dispose( bool disposing ) 157 protected override void Dispose( bool disposing )
414 } 430 }
415 } 431 }
416 432
417 private void reqdList_SelectedIndexChanged(object sender, System.EventArgs e) 433 private void reqdList_SelectedIndexChanged(object sender, System.EventArgs e)
418 { 434 {
419 bttnReplaceWeapon.Enabled = (reqdList.SelectedIndex>-1 && ((UnitEquipmentItemObj)reqdList.SelectedItem).Item.HasAlternatives()); 435 bttnReplaceWeapon.Enabled = (reqdList.SelectedIndex>-1 && ((UnitEquipmentChoice)reqdList.SelectedItem).Item.HasAlternatives());
420 bttnEditReqdWeapon.Enabled = (reqdList.SelectedIndex>-1); 436 bttnEditReqdWeapon.Enabled = (reqdList.SelectedIndex>-1);
421 } 437 }
422 438
423 private void optList_SelectedIndexChanged(object sender, System.EventArgs e) 439 private void optList_SelectedIndexChanged(object sender, System.EventArgs e)
424 { 440 {
425 bttnEditWeapon.Enabled = optList.SelectedIndex>-1; 441 bttnEditWeapon.Enabled = optList.SelectedIndex>-1;
426 bttnRemoveWeapon.Enabled = bttnEditWeapon.Enabled; 442 bttnRemoveWeapon.Enabled = bttnEditWeapon.Enabled;
427 } 443 }
428 444
429 private void unit_UnitEquipmentAmountChanged(WarFoundryObject obj, float oldValue, float newValue) 445 private void unit_UnitEquipmentAmountChanged(WarFoundryObject obj, double oldValue, double newValue)
430 { 446 {
431 if (obj is UnitEquipmentItem) 447 if (obj is UnitEquipmentItem)
432 { 448 {
433 UnitEquipmentItem equip = (UnitEquipmentItem)obj; 449 UnitEquipmentItem equip = (UnitEquipmentItem)obj;
434 ListBox weaponList = (equip.IsRequired ? reqdList : optList); 450 ListBox weaponList = (equip.IsRequired ? reqdList : optList);
435 451
436 if (newValue==0) 452 if (newValue==0)
437 { 453 {
438 weaponList.Items.Remove(UnitEquipmentItemObj.GetEquipObj(Unit, equip)); 454 weaponList.Items.Remove(GetEquipmentChoice(equip));
439 } 455 }
440 else 456 else
441 { 457 {
442 UnitEquipmentItemObj equipObj = UnitEquipmentItemObj.GetEquipObj(Unit, equip); 458 UnitEquipmentChoice equipObj = GetEquipmentChoice(equip);
443 int idx = weaponList.Items.IndexOf(equipObj); 459 int idx = weaponList.Items.IndexOf(equipObj);
444 460
445 if (idx>-1) 461 if (idx>-1)
446 { 462 {
447 weaponList.Items[idx] = equipObj; 463 weaponList.Items[idx] = equipObj;
454 } 470 }
455 } 471 }
456 472
457 private void editWeapon(ListBox list) 473 private void editWeapon(ListBox list)
458 { 474 {
459 FrmEditUnitEquipment editEquip = new FrmEditUnitEquipment(Unit, ((UnitEquipmentItemObj)list.SelectedItem).Item, commandStack); 475 FrmEditUnitEquipment editEquip = new FrmEditUnitEquipment(Unit, ((UnitEquipmentChoice)list.SelectedItem).Item, commandStack);
460 editEquip.ShowDialog(this); 476 editEquip.ShowDialog(this);
461 } 477 }
462 478
463 private void bttnEditWeapon_Click(object sender, System.EventArgs e) 479 private void bttnEditWeapon_Click(object sender, System.EventArgs e)
464 { 480 {
486 addWeapon(); 502 addWeapon();
487 } 503 }
488 504
489 private void removeWeapon() 505 private void removeWeapon()
490 { 506 {
491 commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, unit.UnitType.GetEquipmentItem(((UnitEquipmentItemObj)optList.SelectedItem).Item.ID).EquipmentItem, 0)); 507 commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, ((UnitEquipmentChoice)optList.SelectedItem).Item, 0));
492 } 508 }
493 509
494 private void bttnRemoveWeapon_Click(object sender, System.EventArgs e) 510 private void bttnRemoveWeapon_Click(object sender, System.EventArgs e)
495 { 511 {
496 removeWeapon(); 512 removeWeapon();
501 editWeapon(reqdList); 517 editWeapon(reqdList);
502 } 518 }
503 519
504 private void bttnReplaceWeapon_Click(object sender, System.EventArgs e) 520 private void bttnReplaceWeapon_Click(object sender, System.EventArgs e)
505 { 521 {
506 FrmReplaceUnitEquipment replace = new FrmReplaceUnitEquipment(unit, ((UnitEquipmentItemObj)reqdList.SelectedItem).Item, commandStack); 522 FrmReplaceUnitEquipment replace = new FrmReplaceUnitEquipment(unit, ((UnitEquipmentChoice)reqdList.SelectedItem).Item, commandStack);
507 replace.ShowDialog(this); 523 replace.ShowDialog(this);
508 } 524 }
509 } 525 }
510 } 526 }