diff 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
line wrap: on
line diff
--- a/FrmUnit.cs	Sat Mar 07 21:08:31 2009 +0000
+++ b/FrmUnit.cs	Sun Apr 05 13:50:41 2009 +0000
@@ -5,7 +5,7 @@
 using System;
 using System.Drawing;
 using System.Data;
-using System.Collections;
+using System.Collections.Generic;
 using System.ComponentModel;
 using System.Windows.Forms;
 using IBBoard.Commands;
@@ -13,6 +13,7 @@
 using IBBoard.WarFoundry.API;
 using IBBoard.WarFoundry.API.Commands;
 using IBBoard.WarFoundry.API.Objects;
+using IBBoard.WarFoundry.GUI.WinForms.Util;
 
 namespace IBBoard.WarFoundry
 {
@@ -24,6 +25,7 @@
 	public class FrmUnit : IBBoard.Windows.Forms.IBBForm
 	{
 		private Unit unit;
+        private Dictionary<UnitEquipmentItem, UnitEquipmentChoice> equipmentChoices = new Dictionary<UnitEquipmentItem, UnitEquipmentChoice>();
         private CommandStack commandStack;
 		private System.Windows.Forms.DataGrid statsGrid;
 		private System.Windows.Forms.TextBox tbUnitName;
@@ -56,7 +58,7 @@
 			Text = unit.Name;
 			unit.NameChanged+=new StringValChangedDelegate(unit_NameChanged);
 			unit.UnitSizeChanged+= new IntValChangedDelegate(unit_UnitSizeChanged);
-			unit.UnitEquipmentAmountChanged+=new FloatValChangedDelegate(unit_UnitEquipmentAmountChanged);
+			unit.UnitEquipmentAmountChanged+=new DoubleValChangedDelegate(unit_UnitEquipmentAmountChanged);
 
 			if (unit.UnitType.MaxSize==unit.UnitType.MinSize)
 			{
@@ -126,15 +128,29 @@
 			{
 				if (item.IsRequired)
 				{
-					reqdList.Items.Add(UnitEquipmentItemObj.GetEquipObj(Unit, item));
+                    reqdList.Items.Add(GetEquipmentChoice(item));
 				}
 				else
 				{
-					optList.Items.Add(UnitEquipmentItemObj.GetEquipObj(Unit, item));
+                    optList.Items.Add(GetEquipmentChoice(item));
 				}
 			}
 		}
 
+        private UnitEquipmentChoice GetEquipmentChoice(UnitEquipmentItem item)
+        {
+            UnitEquipmentChoice choice = null;
+            equipmentChoices.TryGetValue(item, out choice);
+
+            if (choice == null)
+            {
+                choice = new UnitEquipmentChoice(Unit, item);
+                equipmentChoices[item] = choice;
+            }
+
+            return choice;
+        }
+
 		/// <summary>
 		/// Clean up any resources being used.
 		/// </summary>
@@ -416,7 +432,7 @@
 
 		private void reqdList_SelectedIndexChanged(object sender, System.EventArgs e)
 		{
-			bttnReplaceWeapon.Enabled = (reqdList.SelectedIndex>-1 && ((UnitEquipmentItemObj)reqdList.SelectedItem).Item.HasAlternatives());
+			bttnReplaceWeapon.Enabled = (reqdList.SelectedIndex>-1 && ((UnitEquipmentChoice)reqdList.SelectedItem).Item.HasAlternatives());
 			bttnEditReqdWeapon.Enabled = (reqdList.SelectedIndex>-1);
 		}
 
@@ -426,7 +442,7 @@
 			bttnRemoveWeapon.Enabled = bttnEditWeapon.Enabled;
 		}
 
-		private void unit_UnitEquipmentAmountChanged(WarFoundryObject obj, float oldValue, float newValue)
+		private void unit_UnitEquipmentAmountChanged(WarFoundryObject obj, double oldValue, double newValue)
 		{
 			if (obj is UnitEquipmentItem)
 			{
@@ -435,11 +451,11 @@
 
 				if (newValue==0)
 				{
-					weaponList.Items.Remove(UnitEquipmentItemObj.GetEquipObj(Unit, equip));
+					weaponList.Items.Remove(GetEquipmentChoice(equip));
 				}
 				else
 				{
-					UnitEquipmentItemObj equipObj = UnitEquipmentItemObj.GetEquipObj(Unit, equip);
+					UnitEquipmentChoice equipObj = GetEquipmentChoice(equip);
 					int idx = weaponList.Items.IndexOf(equipObj);
 
 					if (idx>-1)
@@ -456,7 +472,7 @@
 
 		private void editWeapon(ListBox list)
 		{
-			FrmEditUnitEquipment editEquip = new FrmEditUnitEquipment(Unit, ((UnitEquipmentItemObj)list.SelectedItem).Item, commandStack);
+			FrmEditUnitEquipment editEquip = new FrmEditUnitEquipment(Unit, ((UnitEquipmentChoice)list.SelectedItem).Item, commandStack);
 			editEquip.ShowDialog(this);
 		}
 
@@ -488,7 +504,7 @@
 
 		private void removeWeapon()
 		{
-			commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, unit.UnitType.GetEquipmentItem(((UnitEquipmentItemObj)optList.SelectedItem).Item.ID).EquipmentItem, 0));
+			commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, ((UnitEquipmentChoice)optList.SelectedItem).Item, 0));
 		}
 
 		private void bttnRemoveWeapon_Click(object sender, System.EventArgs e)
@@ -503,7 +519,7 @@
 
 		private void bttnReplaceWeapon_Click(object sender, System.EventArgs e)
 		{
-			FrmReplaceUnitEquipment replace = new FrmReplaceUnitEquipment(unit, ((UnitEquipmentItemObj)reqdList.SelectedItem).Item, commandStack);
+			FrmReplaceUnitEquipment replace = new FrmReplaceUnitEquipment(unit, ((UnitEquipmentChoice)reqdList.SelectedItem).Item, commandStack);
 			replace.ShowDialog(this);
 		}
 	}