view UIControl/AbstractBaseEquipmentUIControl.cs @ 73:19e7123aafd5

Re #307: Add support for multiple stat lines * Fix GTK assert by casting array of stats to an object * Fix stat rendering by checking for Stat[] type instead of old Unit check
author IBBoard <dev@ibboard.co.uk>
date Tue, 09 Nov 2010 20:54:38 +0000
parents 4b82515586ac
children fac0636ae13b
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");
				//				MessageBox.Show(ParentForm, 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");
				//				MessageBox.Show(ParentForm, 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 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");
				//MessageBox.Show(ParentForm, 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");
				//MessageBox.Show(ParentForm, 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();
	}
}