changeset 56:0c5fbb54bfb0

Re #60: Add UI to add/remove/edit weapons in GTK * Refactor enabling/disabling of radio buttons * Handle null selections * Move Dispose() to be an interface method and move call out to controller
author IBBoard <dev@ibboard.co.uk>
date Wed, 25 Aug 2010 20:04:27 +0000
parents eb7db8495bb5
children 293d204e40db
files FrmAddEquipment.cs UIControl/AddEquipmentUIControl.cs UIControl/Interfaces/IAddEquipmentUI.cs gtk-gui/gui.stetic
diffstat 4 files changed, 60 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/FrmAddEquipment.cs	Wed Aug 25 19:51:23 2010 +0000
+++ b/FrmAddEquipment.cs	Wed Aug 25 20:04:27 2010 +0000
@@ -17,6 +17,9 @@
 		
 		public event SingleArgMethodInvoker<UnitEquipmentItem> UnitEquipmentItemChoiceChanged;
 		
+		private bool limitsEnabled = false;
+		private bool ratioLimited = false;
+		
 		public FrmAddEquipment()
 		{
 			this.Build();
@@ -52,14 +55,26 @@
 		public void SetUnitEquipmentLimits(bool isRatioLimit, double minPercent, double maxPercent, int minNumber, int maxNumber)
 		{
 			log.DebugFormat("IsRatio? {0}. Limits: {1}->{2}, {3}%->{4}%", isRatioLimit, minNumber, maxNumber, minPercent, maxPercent);
-			rbEquipNumeric.Sensitive = isRatioLimit;
-			numericAmount.Sensitive = isRatioLimit;
+			ratioLimited = isRatioLimit;
+			SetEnabledState();
 			numericAmount.SetRange(minNumber, maxNumber);
 			percentageAmount.SetRange(minPercent, maxPercent);
 		}
 
 		public void SetUnitEquipmentLimitsEnabled(bool isEnabled)
 		{
+			limitsEnabled = isEnabled;
+			SetEnabledState();
+		}
+		
+		private void SetEnabledState()
+		{
+			rbEquipNumeric.Sensitive = (limitsEnabled && ratioLimited);
+			numericAmount.Sensitive = (limitsEnabled && ratioLimited);
+			rbEquipPercent.Sensitive = limitsEnabled;
+			percentageAmount.Sensitive = limitsEnabled;
+			rbEquipAll.Sensitive = limitsEnabled;
+			lblEquipAll.Sensitive = limitsEnabled;
 		}
 
 		public bool ShowControl()
@@ -67,7 +82,6 @@
 			int result = Run();
 			bool okayClicked = (result == (int)ResponseType.Ok);
 			this.Hide();
-			this.Dispose();
 			return okayClicked;
 		}
 		
@@ -75,6 +89,12 @@
 		{
 			log.Debug("Cancel clicked");
 			Respond(ResponseType.Cancel);
+		}	
+		
+		protected virtual void OkayButtonClicked(object sender, System.EventArgs e)
+		{
+			log.Debug("Okay clicked");
+			Respond(ResponseType.Ok);
 		}		
 	}
 }
--- a/UIControl/AddEquipmentUIControl.cs	Wed Aug 25 19:51:23 2010 +0000
+++ b/UIControl/AddEquipmentUIControl.cs	Wed Aug 25 20:04:27 2010 +0000
@@ -24,6 +24,7 @@
 		private void SetupUI()
 		{
 			CreateEquipmentUI();
+			ui.SetUnitEquipmentLimitsEnabled(false);
 			UnitEquipmentItem[] items = Arrays.Subtract(UnitEquipmentUtil.GetAllowedEquipmentItems(unit), unit.GetEquipment());
 			ui.SetUnitEquipmentItems(items);
 			ui.UnitEquipmentItemChoiceChanged += HandleUiUnitEquipmentItemChoiceChanged;
@@ -37,28 +38,38 @@
 
 		private void HandleUiUnitEquipmentItemChoiceChanged(UnitEquipmentItem equip)
 		{
-			bool equipIsRatioLimit = UnitEquipmentUtil.IsEquipmentRatioLimited(unit, equip);
-			double maxPercent = RoundPercentage(UnitEquipmentUtil.GetMaxEquipmentPercentage(unit, equip));
-			maxPercent = Math.Max(0, maxPercent);
-			double minPercent = RoundPercentage(UnitEquipmentUtil.GetMinEquipmentPercentage(unit, equip));
-			minPercent = Math.Max(0, minPercent);
-			int maxNumber = UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip);
-			maxNumber = Math.Max(0, maxNumber);
-			int minNumber = UnitEquipmentUtil.GetMinEquipmentCount(unit, equip);
-			minNumber = Math.Max(0, minNumber);
+			if (equip != null)
+			{
+				bool equipIsRatioLimit = UnitEquipmentUtil.IsEquipmentRatioLimited(unit, equip);
+				double maxPercent = RoundPercentage(UnitEquipmentUtil.GetMaxEquipmentPercentage(unit, equip));
+				maxPercent = Math.Max(0, maxPercent);
+				double minPercent = RoundPercentage(UnitEquipmentUtil.GetMinEquipmentPercentage(unit, equip));
+				minPercent = Math.Max(0, minPercent);
+				int maxNumber = UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip);
+				maxNumber = Math.Max(0, maxNumber);
+				int minNumber = UnitEquipmentUtil.GetMinEquipmentCount(unit, equip);
+				minNumber = Math.Max(0, minNumber);
 			
-			ui.SetUnitEquipmentLimits(equipIsRatioLimit, minPercent, maxPercent, minNumber, maxNumber);
-			
+				ui.SetUnitEquipmentLimits(equipIsRatioLimit, minPercent, maxPercent, minNumber, maxNumber);
+				ui.SetUnitEquipmentLimitsEnabled(true);
+			}
+			else
+			{
+				ui.SetUnitEquipmentLimits(true, 0, 0, 0, 0);
+				ui.SetUnitEquipmentLimitsEnabled(false);
+			}
 		}
 		
 		private double RoundPercentage(double percent)
 		{
 			return Math.Round(percent, 1);
 		}
-		
+
 		public void Show()
 		{
 			ui.ShowControl();
+			//Do command stuff here
+			ui.Dispose();
 		}
 	}
 }
--- a/UIControl/Interfaces/IAddEquipmentUI.cs	Wed Aug 25 19:51:23 2010 +0000
+++ b/UIControl/Interfaces/IAddEquipmentUI.cs	Wed Aug 25 20:04:27 2010 +0000
@@ -58,6 +58,19 @@
 		/// <code>true</code> if the control was closed with "Okay", else <code>false</code>
 		/// </returns>
 		bool ShowControl();
+	
+		/// <summary>
+		/// Releases all resource used by the <see cref="IBBoard.WarFoundry.GUI.GTK.UIControl.Interfaces.IAddEquipmentUI"/> object.
+		/// </summary>
+		/// <remarks>
+		/// Call <see cref="Dispose"/> when you are finished using the
+		/// <see cref="IBBoard.WarFoundry.GUI.GTK.UIControl.Interfaces.IAddEquipmentUI"/>. The <see cref="Dispose"/> method
+		/// leaves the <see cref="IBBoard.WarFoundry.GUI.GTK.UIControl.Interfaces.IAddEquipmentUI"/> in an unusable state.
+		/// After calling <see cref="Dispose"/>, you must release all references to the
+		/// <see cref="IBBoard.WarFoundry.GUI.GTK.UIControl.Interfaces.IAddEquipmentUI"/> so the garbage collector can reclaim
+		/// the memory that the <see cref="IBBoard.WarFoundry.GUI.GTK.UIControl.Interfaces.IAddEquipmentUI"/> was occupying.
+		/// </remarks>
+		void Dispose();
 	}
 }
 
--- a/gtk-gui/gui.stetic	Wed Aug 25 19:51:23 2010 +0000
+++ b/gtk-gui/gui.stetic	Wed Aug 25 20:04:27 2010 +0000
@@ -1289,6 +1289,7 @@
             <property name="Type">StockItem</property>
             <property name="StockId">gtk-ok</property>
             <property name="ResponseId">-5</property>
+            <signal name="Clicked" handler="OkayButtonClicked" />
             <property name="label">gtk-ok</property>
           </widget>
           <packing>