diff Widgets/UnitDisplayWidget.cs @ 68:7028e24b67ec

Re #60: Add UI to add/remove/edit weapons in GTK * Add "Replace" dialog Note: Dialog null refs because of a bad assumption in the base class - base constructor calls SetupUI before Replace constructor has set all of its values
author IBBoard <dev@ibboard.co.uk>
date Wed, 03 Nov 2010 21:02:54 +0000
parents 77448375d2f9
children 91354245218a
line wrap: on
line diff
--- a/Widgets/UnitDisplayWidget.cs	Sat Oct 30 14:30:29 2010 +0000
+++ b/Widgets/UnitDisplayWidget.cs	Wed Nov 03 21:02:54 2010 +0000
@@ -22,7 +22,6 @@
 	public partial class UnitDisplayWidget : Gtk.Bin
 	{
 		private static ILog log = LogManager.GetLogger(typeof(UnitDisplayWidget));
-		
 		private WFObjects.Unit unit;
 		private CommandStack stack;
 		
@@ -46,10 +45,11 @@
 			SetWeapons();
 		}
 
-		private void HandleEquipmentListSelectionChanged (object sender, EventArgs e)
+		private void HandleEquipmentListSelectionChanged(object sender, EventArgs e)
 		{
 			SetButtonsEnabledState();	
 		}
+
 		private void SetButtonsEnabledState()
 		{			
 			UnitEquipmentItem equipItem = GetSelectedEquipmentItem();
@@ -57,6 +57,7 @@
 			bttnEditEquipment.Sensitive = (UnitEquipmentUtil.CanEditEquipmentAmount(unit, equipItem));
 			bttnRemoveEquipment.Sensitive = (equipItem != null && !equipItem.IsRequired);
 		}
+
 		private UnitEquipmentItem GetSelectedEquipmentItem()
 		{
 			return (UnitEquipmentItem)TreeUtils.GetSelectedItem(equipmentList);
@@ -64,17 +65,17 @@
 
 		private void SetStats()
 		{
-            CellRendererText renderer = new CellRendererText();
-            unitStats.AppendColumn(Translation.GetTranslation("UnitNameColumn", "Unit Type", null), renderer, new TreeCellDataFunc(RenderUnitName));
-            
-            TreeCellDataFunc statFunc = new TreeCellDataFunc(RenderUnitStat);
-            Stat[] stats = unit.UnitStatsArray;
+			CellRendererText renderer = new CellRendererText();
+			unitStats.AppendColumn(Translation.GetTranslation("UnitNameColumn", "Unit Type", null), renderer, new TreeCellDataFunc(RenderUnitName));
+			
+			TreeCellDataFunc statFunc = new TreeCellDataFunc(RenderUnitStat);
+			Stat[] stats = unit.UnitStatsArray;
 			
 			int length = stats.Length;
 
 			for (int i = 0; i < length; i++)
 			{
-	            unitStats.AppendColumn(stats[i].ParentSlotName, renderer, statFunc);
+				unitStats.AppendColumn(stats[i].ParentSlotName, renderer, statFunc);
 			}
 
 			TreeStore model = new TreeStore(typeof(WFObjects.Unit));
@@ -160,7 +161,6 @@
 				{
 					amountString = Translation.GetTranslation("equipmentChoiceAmountAll", "all ({1})", amount, number);
 				}
-
 				else
 				{
 					amountString = Translation.GetTranslation("equipmentChoiceAmountPercentage", "{0}% ({1})", amount, number);
@@ -195,22 +195,26 @@
 			{
 				((ListStore)equipmentList.Model).AppendValues(obj);
 			}
-			else if (newValue == 0)
+			else
 			{
-				TreeIter treeIter = TreeUtils.GetItemIter(equipmentList, obj);
-				((ListStore)equipmentList.Model).Remove(ref treeIter);
-			}
-			
+				if (newValue == 0)
+				{
+					TreeIter treeIter = TreeUtils.GetItemIter(equipmentList, obj);
+					((ListStore)equipmentList.Model).Remove(ref treeIter);
+				}
+
+			}			
 			equipmentList.QueueDraw();
 		}
 
-		protected virtual void OnUnitSizeFocusOut (object o, Gtk.FocusOutEventArgs args)
+		protected virtual void OnUnitSizeFocusOut(object o, Gtk.FocusOutEventArgs args)
 		{
 			SetNewUnitSize();
 		}
 
 		[GLib.ConnectBefore ()]
-		protected virtual void OnUnitSizeKeyPress (object o, Gtk.KeyPressEventArgs args)
+
+		protected virtual void OnUnitSizeKeyPress(object o, Gtk.KeyPressEventArgs args)
 		{
 			if (args.Event.Key == Gdk.Key.Return || args.Event.Key == Gdk.Key.KP_Enter)
 			{
@@ -220,20 +224,21 @@
 		
 		private void SetNewUnitSize()
 		{
-			if (unitSize.Value!=unit.Size)
+			if (unitSize.Value != unit.Size)
 			{
 				SetUnitSizeCommand cmd = new SetUnitSizeCommand(unit, (int)Math.Round(unitSize.Value));
 				stack.Execute(cmd);
 			}
 		}
 
-		protected virtual void OnUnitNameFocusOut (object o, Gtk.FocusOutEventArgs args)
+		protected virtual void OnUnitNameFocusOut(object o, Gtk.FocusOutEventArgs args)
 		{
 			SetNewUnitName();
 		}
 
 		[GLib.ConnectBefore ()]
-		protected virtual void OnUnitNameKeyPress (object o, Gtk.KeyPressEventArgs args)
+
+		protected virtual void OnUnitNameKeyPress(object o, Gtk.KeyPressEventArgs args)
 		{
 			if (args.Event.Key == Gdk.Key.Return || args.Event.Key == Gdk.Key.KP_Enter)
 			{
@@ -264,7 +269,7 @@
 		protected virtual void HandleRemoveButtonActivated(object sender, System.EventArgs e)
 		{
 			UnitEquipmentItem item = GetSelectedEquipmentItem();
-			log.Debug("Remove "+item);
+			log.Debug("Remove " + item);
 				
 			if (item != null)
 			{
@@ -285,6 +290,19 @@
 			}
 		}
 		
+		protected virtual void HandleReplaceButtonClicked(object sender, System.EventArgs e)
+		{
+			UnitEquipmentItem item = GetSelectedEquipmentItem();
+			log.Debug("Replace " + item);
+				
+			if (item != null)
+			{
+				ReplaceEquipmentUIControl addEquipment = new ReplaceEquipmentUIControl(unit, item, stack);
+				addEquipment.Show();
+			}
+		}
+		
+		
 		
 	}
 }