diff FrmEditEquipment.cs @ 70:4b82515586ac

Re #60: Add UI to add/remove/edit weapons in GTK * Fix widget enabling/disabling (sometimes enabling ratio input for numeric limit) * Correct behaviour of Edit dialog (setting wrong value on edit) * Add setting of limits based on specific equipment item to base dialog controller * Make sure that we catch all radio button click events from Edit dialog * Add ability to ignore and listen to widgets to make sure that changes don't cascade TODO: See if code can be tidied up and common classes created
author IBBoard <dev@ibboard.co.uk>
date Sat, 06 Nov 2010 17:03:13 +0000
parents e3fe48c4d794
children 7055b24cfc79
line wrap: on
line diff
--- a/FrmEditEquipment.cs	Sat Nov 06 11:44:26 2010 +0000
+++ b/FrmEditEquipment.cs	Sat Nov 06 17:03:13 2010 +0000
@@ -1,6 +1,7 @@
 //  This file (FrmEditEquipment.cs) is a part of the IBBoard.WarFoundry.GUI.GTK project and is copyright 2010 IBBoard
 // 
 //  The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
+
 using System;
 using IBBoard.WarFoundry.GUI.GTK.UIControl.Interfaces;
 using IBBoard.WarFoundry.API.Objects;
@@ -9,6 +10,7 @@
 using IBBoard.GtkSharp;
 using log4net.Repository.Hierarchy;
 using log4net;
+
 namespace IBBoard.WarFoundry.GUI.GTK
 {
 	public partial class FrmEditEquipment : Dialog, IEditEquipmentUI
@@ -16,12 +18,12 @@
 		private static ILog log = LogManager.GetLogger(typeof(FrmAddEquipment));
 		
 		public event SingleArgMethodInvoker<UnitEquipmentItem> UnitEquipmentItemChoiceChanged;
+
 		public event MethodInvoker UnitEquipmentAmountTypeChanged;
+
 		public event MethodInvoker UnitEquipmentAmountChanged;
-		
-		private bool limitsEnabled = false;
-		private bool ratioLimited = false;
-		
+		private bool isRatioLimited;
+				
 		public FrmEditEquipment()
 		{
 			this.Build();
@@ -32,6 +34,24 @@
 			equipColumn.SetCellDataFunc(equipCell, GtkWarFoundryUtil.RenderWarFoundryObjectName);	
 		}
 		
+		public void ListenToWidgets()
+		{
+			rbEquipAll.Clicked += RadioButtonClicked;
+			rbEquipNumeric.Clicked += RadioButtonClicked;
+			rbEquipPercent.Clicked += RadioButtonClicked;
+			numericAmount.ValueChanged += SpinButtonValueChanged;
+			percentageAmount.ValueChanged += SpinButtonValueChanged;
+		}
+		
+		public void IgnoreWidgets()
+		{
+			rbEquipAll.Clicked -= RadioButtonClicked;
+			rbEquipNumeric.Clicked -= RadioButtonClicked;
+			rbEquipPercent.Clicked -= RadioButtonClicked;
+			numericAmount.ValueChanged -= SpinButtonValueChanged;
+			percentageAmount.ValueChanged -= SpinButtonValueChanged;
+		}
+		
 		private void OnUnitEquipmentAmountChanged()
 		{
 			if (UnitEquipmentAmountChanged != null)
@@ -51,13 +71,17 @@
 		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);
-			ratioLimited = isRatioLimit;
 			numericAmount.SetRange(minNumber, maxNumber);
-			percentageAmount.SetRange(minPercent, maxPercent);
-			
-			if (isRatioLimit)
+			percentageAmount.SetRange(minPercent, maxPercent);			
+			SetEquipmentAmountType(isRatioLimit);
+			isRatioLimited = isRatioLimit;
+		}
+
+		public void SetEquipmentAmountType(bool isRatioAmount)
+		{
+			if (isRatioAmount)
 			{
-				if (minPercent == 100)
+				if (percentageAmount.Value == 100)
 				{
 					rbEquipAll.Active = true;
 				}
@@ -98,12 +122,12 @@
 			Respond(ResponseType.Ok);
 		}		
 		
-		public void SetOkayEnabledState (bool enabled)
+		public void SetOkayEnabledState(bool enabled)
 		{
 			buttonOk.Sensitive = enabled;
 		}
 				
-		protected virtual void SpinButtonValueChanged (object sender, System.EventArgs e)
+		protected virtual void SpinButtonValueChanged(object sender, System.EventArgs e)
 		{
 			OnUnitEquipmentAmountChanged();
 		}		
@@ -113,10 +137,11 @@
 			OnUnitEquipmentAmountTypeChanged();
 		}
 		
-		public void SetNumericAmountEnabledState (bool enabled)
+		public void SetNumericAmountEnabledState(bool enabled)
 		{
-			rbEquipNumeric.Sensitive = enabled;
-			numericAmount.Sensitive = enabled;
+			double minPercent = GetMinPercentage();
+			rbEquipNumeric.Sensitive = enabled && !(isRatioLimited && minPercent == 100);
+			numericAmount.Sensitive = rbEquipNumeric.Sensitive;
 		}		
 		
 		public void SetPercentageAmountEnabledState(bool enabled)
@@ -124,11 +149,11 @@
 			if (enabled)
 			{
 				double minPercentage = GetMinPercentage();
-				rbEquipPercent.Sensitive = minPercentage != 100;
-				percentageAmount.Sensitive = minPercentage != 100;
-				double maxPercentage = GetMaxPercentage();
-				rbEquipAll.Sensitive = ratioLimited && maxPercentage == 100;
-				lblEquipAll.Sensitive = ratioLimited && maxPercentage == 100;
+				rbEquipPercent.Sensitive = isRatioLimited && minPercentage != 100;
+				percentageAmount.Sensitive = rbEquipPercent.Sensitive;
+				double maxPercentage = GetMaxPercentage();				
+				rbEquipAll.Sensitive = isRatioLimited && maxPercentage == 100;
+				lblEquipAll.Sensitive = rbEquipAll.Sensitive;
 			}
 			else
 			{
@@ -161,7 +186,6 @@
 			}
 		}
 		
-		
 		public int EquipmentNumericAmount
 		{
 			get
@@ -175,7 +199,6 @@
 			}
 		}
 		
-		
 		public double EquipmentPercentageAmount
 		{
 			get