view UIControl/AbstractBaseEquipmentUIControl.cs @ 129:ac4231d6c3f0

* Add extra icon sizes for scaling
author IBBoard <dev@ibboard.co.uk>
date Sat, 01 Oct 2011 20:30:45 +0100
parents fac0636ae13b
children
line wrap: on
line source

//  This file (AbstractBaseEquipmentUIControl.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.Commands;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.GUI.GTK.UIControl.Interfaces;
using IBBoard.Lang;
using IBBoard.WarFoundry.API.Util;

namespace IBBoard.WarFoundry.GUI.GTK.UIControl
{
	public abstract class AbstractBaseEquipmentUIControl<UI_TYPE> where UI_TYPE : IBaseEquipmentUI
	{
		protected CommandStack commandStack;
		protected Unit unit;
		protected UI_TYPE ui;
		protected UnitEquipmentItem equipItem;
		protected double minPercentage, maxPercentage;
		protected int minNumber, maxNumber;
		protected bool isRatioAmount;
		protected double equipmentAmount;

		public AbstractBaseEquipmentUIControl(Unit unit, CommandStack commandStack)
		{
			this.unit = unit;
			this.commandStack = commandStack;
		}

		private void SetupUI()
		{
			ui = CreateEquipmentUI();
			ui.SetUnitEquipmentLimitsEnabled(false);
			ui.SetOkayEnabledState(false);
			CompleteUISetup();
			ui.UnitEquipmentAmountChanged += HandleUnitEquipmentAmountChanged;
			ui.UnitEquipmentAmountTypeChanged += HandleUnitEquipmentAmountChanged;
			ui.ListenToWidgets();
		}
		
		/// <summary>
		/// Creates the UI component that will be displayed to the user and returns it
		/// </summary>
		/// <returns>
		/// the UI component to display to the user
		/// </returns>

		protected abstract UI_TYPE CreateEquipmentUI();
		
		/// <summary>
		/// Completes any additional user interface setup.
		/// </summary>

		protected virtual void CompleteUISetup()
		{
			//Do nothing
		}

		protected void HandleUnitEquipmentAmountChanged()
		{
			SetUnitEquipmentValuesFromUI();
		}

		protected void SetUnitEquipmentLimits(UnitEquipmentItem equip)
		{			
			ui.IgnoreWidgets();
			
			if (equip != null)
			{
				bool equipIsRatioLimit = UnitEquipmentUtil.IsEquipmentRatioLimited(unit, equip);
				maxPercentage = GetMaxPercentageLimit(equip);
				minPercentage = GetMinPercentageLimit(equip);
				maxNumber = GetMaxNumericLimit(equip);
				minNumber = GetMinNumericLimit(equip);
			
				ui.SetUnitEquipmentLimits(equipIsRatioLimit, minPercentage, maxPercentage, minNumber, maxNumber);
				ui.SetUnitEquipmentLimitsEnabled(true);
				ui.SetOkayEnabledState(HasNonZeroEquipmentAmount());
				SetEquipmentAmountControlEnabledStates();
			}
			else
			{
				maxPercentage = minPercentage = 0;
				maxNumber = minNumber = 0;
				ui.SetUnitEquipmentLimits(false, minPercentage, maxPercentage, minNumber, maxNumber);
				ui.SetUnitEquipmentLimitsEnabled(false);
				ui.SetOkayEnabledState(false);
			}
			
			ui.ListenToWidgets();
		}

		protected void SetUnitEquipmentValuesFromEquipment(UnitEquipmentItem equip)
		{			
			isRatioAmount = UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, equip);
			equipmentAmount = UnitEquipmentUtil.GetEquipmentAmount(unit, equip);			
			SetUnitEquipmentValues();
			SetEquipmentAmountControlEnabledStates();
		}

		/// <summary>
		/// Sets the unit equipment values on the UI
		/// </summary>

		protected void SetUnitEquipmentValuesFromUI()
		{			
			isRatioAmount = ui.IsRatioEquipmentAmount;
			
			if (isRatioAmount)
			{
				equipmentAmount = ui.EquipmentPercentageAmount;
			}
			else
			{
				int equipmentIntAmount = ui.EquipmentNumericAmount;
				equipmentAmount = equipmentIntAmount;
			}
			
			SetUnitEquipmentValues();
		}

		private void SetUnitEquipmentValues()
		{			
			ui.IgnoreWidgets();
			
			if (isRatioAmount)
			{
				SetEquipmentAmountsFromPercentage(equipmentAmount);
			}
			else
			{
				int equipmentIntAmount = (int)equipmentAmount;
				SetEquipmentAmountsFromNumber(equipmentIntAmount);
			}
			
			ui.SetOkayEnabledState(equipmentAmount != 0);
			ui.ListenToWidgets();
		}

		private void SetEquipmentAmountsFromPercentage(double equipAmount)
		{
			if (equipAmount > maxPercentage)
			{
				string percentageTooLarge = Translation.GetTranslation("equipPercentageTooLarge", "the current percentage ({0}%) was larger than the maximum for the equipment item ({1}%) - the maximum value will be used instead", equipAmount, maxPercentage);
				string percentageTooLargeTitle = Translation.GetTranslation("equipPercentageTooLargeTitle", "equipment percentage too large");
				ShowMessage(percentageTooLarge, percentageTooLargeTitle);
				equipAmount = maxPercentage;
			}
			else
			{
				if (equipAmount < minPercentage)
				{
					string percentageTooSmall = Translation.GetTranslation("equipPercentageTooSmall", "the current percentage ({0}%) was smaller than the minimum for the equipment item ({1}%) - the minimum value will be used instead", equipAmount, minPercentage);
					string percentageTooSmallTitle = Translation.GetTranslation("equipPercentageTooSmallTitle", "equipment percentage too small");
					ShowMessage(percentageTooSmall, percentageTooSmallTitle);
					equipAmount = minPercentage;
				}

			}			
			ui.EquipmentNumericAmount = CalculateNumericValueFromPercentage(equipAmount);
			ui.EquipmentPercentageAmount = equipAmount;
		}

		private int CalculateNumericValueFromPercentage(double percent)
		{
			int calcedAmount = (int)CustomMath.IBBMath.Round((unit.Size * (percent / 100.0)), equipItem.RoundNumberUp);
			return Math.Min(Math.Max(calcedAmount, minNumber), maxNumber);
		}

		private void ShowMessage(string message, string title)
		{
			Gtk.MessageDialog dialog = new Gtk.MessageDialog(null, Gtk.DialogFlags.Modal, Gtk.MessageType.Warning, Gtk.ButtonsType.Ok, false, message);
			dialog.Title = title;
			dialog.Run();
			dialog.Hide();
			dialog.Dispose();
		}

		private void SetEquipmentAmountsFromNumber(int equipAmount)
		{
			if (equipAmount > maxNumber)
			{
				string amountTooLarge = Translation.GetTranslation("equipNumberTooLarge", "the current amount ({0}) was larger than the maximum for the equipment item ({1}) - the maximum value will be used instead", equipAmount, maxNumber);
				string amountTooLargeTitle = Translation.GetTranslation("equipNumberTooLargeTitle", "equipment amount too large");
				ShowMessage(amountTooLarge, amountTooLargeTitle);
				equipAmount = maxNumber;
			}
			else
			{
				if (equipAmount < minNumber)
				{
					string amountTooSmall = Translation.GetTranslation("equipNumberTooSmall", "the current amount ({0}) was smaller than the minimum for the equipment item ({1}) - the minimum value will be used instead", equipAmount, minNumber);
					string amountTooSmallTitle = Translation.GetTranslation("equipNumberTooSmallTitle", "equipment amount too small");
					ShowMessage(amountTooSmall, amountTooSmallTitle);
					equipAmount = minNumber;
				}

			}			
			ui.EquipmentPercentageAmount = CalcualtePercentageValueFromNumber(equipAmount);
			ui.EquipmentNumericAmount = equipAmount;
		}

		private double CalcualtePercentageValueFromNumber(int number)
		{
			double calcedAmount = RoundPercentage(CustomMath.IBBMath.Percentage(number, unit.Size));
			return Math.Min(Math.Max(calcedAmount, minPercentage), maxPercentage);
		}

		protected void SetEquipmentAmountControlEnabledStates()
		{
			ui.SetNumericAmountEnabledState(!isRatioAmount);
			ui.SetPercentageAmountEnabledState(true);
		}

		protected double GetMaxPercentageLimit(UnitEquipmentItem equip)
		{
			double maxPercent = RoundPercentage(UnitEquipmentUtil.GetMaxEquipmentPercentage(unit, equip));
			return Math.Max(0, maxPercent);
		}

		protected double GetMinPercentageLimit(UnitEquipmentItem equip)
		{
			double minPercent = RoundPercentage(UnitEquipmentUtil.GetMinEquipmentPercentage(unit, equip));
			return Math.Max(0, minPercent);
		}

		protected int GetMaxNumericLimit(UnitEquipmentItem equip)
		{
			int maxNumber = UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip);
			return Math.Max(0, maxNumber);
		}

		protected int GetMinNumericLimit(UnitEquipmentItem equip)
		{
			int minNumber = UnitEquipmentUtil.GetMinEquipmentCount(unit, equip);
			return Math.Max(0, minNumber);
		}

		protected bool HasNonZeroEquipmentAmount()
		{
			bool nonZero;
			
			if (isRatioAmount)
			{
				nonZero = (ui.EquipmentPercentageAmount > 0);
			}
			else
			{
				nonZero = (ui.EquipmentNumericAmount > 0);
			}
			
			return nonZero;
		}

		private double RoundPercentage(double percent)
		{
			return Math.Round(percent, 1);
		}

		public void Show()
		{			
			SetupUI();
			bool okayed = ui.ShowControl();
			
			if (okayed)
			{
				DoProcessing();
			}
			
			ui.Dispose();
		}	
		
		/// <summary>
		/// Does the processing required for the control when the "OK" button was clicked
		/// </summary>

		protected abstract void DoProcessing();
	}
}