view api/Commands/AbstractSetUnitEquipmentAmountCommand.cs @ 289:650bbe79b884 WarFoundry_v0.1

Fixes #336: Command descriptions don't refresh on language change * Remove cached string values and just return the text each time
author IBBoard <dev@ibboard.co.uk>
date Sun, 02 Jan 2011 21:01:20 +0000
parents 65553d2c8612
children
line wrap: on
line source

//  This file (AbstractSetUnitEquipmentAmountCommand.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 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.Lang;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.API.Util;

namespace IBBoard.WarFoundry.API.Commands
{
	/// <summary>
	/// Abstract parent class for commands that set the amount of an equipment item a unit has to a fixed numeric or ratio value
	/// </summary>
	public abstract class AbstractSetUnitEquipmentAmountCommand : Command
	{
		private Unit unit;
		private UnitEquipmentItem equip;
		private double oldAmount;
		private bool oldAmountWasRatio;

		public AbstractSetUnitEquipmentAmountCommand(Unit unit, UnitEquipmentItem item)
		{
			this.unit = unit;
			equip = item;
			oldAmount = UnitEquipmentUtil.GetEquipmentAmount(unit, equip);
			oldAmountWasRatio = UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, equip);
		}

		public override bool CanExecute()
		{
			return (unit != null && equip != null);
		}

		public override string Description
		{
			get
			{
				return Translation.GetTranslation("setEquipmentAmountCommandDescription", "set {0} amount for {1} to {2}", equip.Name, unit.Name, GetNewAmountString());
			}
		}

		/// <summary>
		/// Gets the string representation for the new amount of the equipment item to take
		/// </summary>
		/// <returns>
		/// the string representation for the new amount of the equipment item to take
		/// </returns>
		protected abstract string GetNewAmountString();

		public override string UndoDescription
		{
			get
			{
				return Translation.GetTranslation("setEquipmentAmountCommandUndoDescription", "set {0} amount for {1} to {2}", equip.Name, unit.Name, GetOldAmountString());
			}
		}

		/// <summary>
		/// Gets the string representation for the old amount of the equipment item to take
		/// </summary>
		/// <returns>
		/// the string representation for the old amount of the equipment item to take
		/// </returns>
		protected string GetOldAmountString()
		{
			return oldAmountWasRatio ? GetRatioAmountString(oldAmount, UnitEquipmentRatioSelection.CalculateNumberTaken(Unit, EquipItem, oldAmount)) : GetNumberAmountString((int)oldAmount);
		}

		protected string GetNumberAmountString(int number)
		{
			return Translation.GetTranslation("equipmentAmountNumber", "{0}", number);
		}

		protected string GetRatioAmountString(double amount, int number)
		{
			string amountString;
			
			if (amount == 100)
			{
				amountString = Translation.GetTranslation("equipmentAmountAll", "all ({1})", amount, number);
			}
			else
			{
				amountString = Translation.GetTranslation("equipmentAmountPercentage", "{0}% ({1})", amount, number);
			}
			
			return amountString;
		}

		public override bool Execute()
		{
			this.Redo();
			return true;
		}

		public override void Undo()
		{
			if (oldAmountWasRatio)
			{
				unit.SetEquipmentRatio(equip, oldAmount);
			}
			else
			{
				unit.SetEquipmentAmount(equip, (int)oldAmount);
			}
		}

		public UnitEquipmentItem EquipItem
		{
			get { return equip; }
		}

		public Unit Unit
		{
			get { return unit; }
		}
	}
}