view UIControl/AbstractBaseEquipmentUIControl.cs @ 66:100626381159

Re #60: Add UI to add/remove/edit weapons in GTK * Rip apart UI controls and put all the common code in a base class * Add additional interfaces for common UI behaviour between equipment widgets Not tested, but should provide a base for Replace widget
author IBBoard <dev@ibboard.co.uk>
date Sat, 04 Sep 2010 20:11:05 +0000
parents
children 3b4a646b4054
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;
			SetupUI();
		}

		private void SetupUI()
		{
			ui = CreateEquipmentUI();
			ui.SetUnitEquipmentLimitsEnabled(false);
			ui.SetOkayEnabledState(false);
			ui.UnitEquipmentAmountChanged += SetUnitEquipmentValues;
			ui.UnitEquipmentAmountTypeChanged += SetUnitEquipmentValues;
			CompleteUISetup();
		}
		
		/// <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()
		{
			SetUnitEquipmentValues();
		}
		
		/// <summary>
		/// Sets the unit equipment values on the UI
		/// </summary>
		protected void SetUnitEquipmentValues()
		{
			ui.SetOkayEnabledState(HasNonZeroEquipmentAmount());
			isRatioAmount = ui.IsRatioEquipmentAmount;
			
			if (isRatioAmount)
			{
				equipmentAmount = ui.EquipmentPercentageAmount;
				SetEquipmentAmountsFromPercentage(equipmentAmount);
			}

			else
			{
				int equipmentIntAmount = ui.EquipmentNumericAmount;
				equipmentAmount = equipmentIntAmount;
				SetEquipmentAmountsFromNumber(equipmentIntAmount);
			}
		}

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