view FrmEditEquipment.cs @ 149:45c2485f37c0

* Initialise variable - xbuild is more picky than MonoDevelop
author IBBoard <dev@ibboard.co.uk>
date Sun, 27 Nov 2011 14:22:46 +0000
parents 3ec818926241
children
line wrap: on
line source

//  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;
using Gtk;
using IBBoard.WarFoundry.GUI.GTK.Util;
using IBBoard.GtkSharp;
using log4net.Repository.Hierarchy;
using log4net;
using IBBoard.GtkSharp.Translatable;
using IBBoard.Lang;

namespace IBBoard.WarFoundry.GUI.GTK
{
	public partial class FrmEditEquipment : TranslatableDialog, IEditEquipmentUI
	{
		private static ILog log = LogManager.GetLogger(typeof(FrmAddEquipment));

		public event MethodInvoker UnitEquipmentAmountTypeChanged;
		public event MethodInvoker UnitEquipmentAmountChanged;

		private bool isRatioLimited;

		public FrmEditEquipment()
		{
			this.Build();
			Translation.TranslationChanged += Retranslate;
			Translate();
		}

		private void Retranslate()
		{
			Translate();
		}

		public override void Dispose()
		{
			Translation.TranslationChanged -= Retranslate;
			base.Dispose();
		}

		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)
			{
				UnitEquipmentAmountChanged();
			}
		}

		private void OnUnitEquipmentAmountTypeChanged()
		{
			if (UnitEquipmentAmountTypeChanged != null)
			{
				UnitEquipmentAmountTypeChanged();
			}
		}

		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);
			numericAmount.SetRange(minNumber, maxNumber);
			percentageAmount.SetRange(minPercent, maxPercent);			
			SetEquipmentAmountType(isRatioLimit);
			isRatioLimited = isRatioLimit;
		}

		public void SetEquipmentAmountType(bool isRatioAmount)
		{
			if (isRatioAmount)
			{
				if (percentageAmount.Value == 100)
				{
					rbEquipAll.Active = true;
				}
				else
				{
					rbEquipPercent.Active = true;
				}
			}
			else
			{
				rbEquipNumeric.Active = true;
			}
		}

		public void SetUnitEquipmentLimitsEnabled(bool isEnabled)
		{
			SetNumericAmountEnabledState(isEnabled);
			SetPercentageAmountEnabledState(isEnabled);
		}

		public bool ShowControl()
		{
			int result = Run();
			bool okayClicked = (result == (int)ResponseType.Ok);
			this.Hide();
			return okayClicked;
		}

		protected virtual void CancelButtonClicked(object sender, System.EventArgs e)
		{
			log.Debug("Cancel clicked");
			Respond(ResponseType.Cancel);
		}	

		protected virtual void OkayButtonClicked(object sender, System.EventArgs e)
		{
			log.Debug("Okay clicked");
			Respond(ResponseType.Ok);
		}		

		public void SetOkayEnabledState(bool enabled)
		{
			bttnOkay.Sensitive = enabled;
		}

		protected virtual void SpinButtonValueChanged(object sender, System.EventArgs e)
		{
			OnUnitEquipmentAmountChanged();
		}		

		protected virtual void RadioButtonClicked(object sender, System.EventArgs e)
		{
			OnUnitEquipmentAmountTypeChanged();
		}

		public void SetNumericAmountEnabledState(bool enabled)
		{
			double minPercent = GetMinPercentage();
			rbEquipNumeric.Sensitive = enabled && !(isRatioLimited && minPercent == 100);
			numericAmount.Sensitive = rbEquipNumeric.Sensitive;
		}		

		public void SetPercentageAmountEnabledState(bool enabled)
		{
			if (enabled)
			{
				double minPercentage = GetMinPercentage();
				rbEquipPercent.Sensitive = isRatioLimited && minPercentage != 100;
				percentageAmount.Sensitive = rbEquipPercent.Sensitive;
				double maxPercentage = GetMaxPercentage();				
				rbEquipAll.Sensitive = isRatioLimited && maxPercentage == 100;
				lblEquipAll.Sensitive = rbEquipAll.Sensitive;
			}
			else
			{
				rbEquipPercent.Sensitive = false;
				percentageAmount.Sensitive = false;
				rbEquipAll.Sensitive = false;
				lblEquipAll.Sensitive = false;
			}
		}

		private double GetMaxPercentage()
		{
			double min, max;
			percentageAmount.GetRange(out min, out max);
			return max;
		}

		private double GetMinPercentage()
		{
			double min, max;
			percentageAmount.GetRange(out min, out max);
			return min;
		}		

		public bool IsRatioEquipmentAmount
		{
			get
			{
				return !rbEquipNumeric.Active;
			}
		}

		public int EquipmentNumericAmount
		{
			get
			{
				return (int)numericAmount.Value;
			}
			
			set
			{
				numericAmount.Value = value;
			}
		}

		public double EquipmentPercentageAmount
		{
			get
			{
				double percent;
				
				if (rbEquipAll.Active)
				{
					percent = 100;
				}
				else
				{
					percent = percentageAmount.Value;
				}
				
				return percent;
			}
			
			set
			{
				percentageAmount.Value = value;
			}
		}
	}
}