view UIControl/AbstractBaseEquipmentUIControl.cs @ 67:a7306b5a229e

Re #311: can't read ZIP file packed by Linux app Archive Manager/File Roller * Add SharpZipLib to GTK# GUI solution
author IBBoard <dev@ibboard.co.uk>
date Sat, 30 Oct 2010 14:30:29 +0000
parents 100626381159
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();
	}
}