view api/Commands/AbstractSetUnitEquipmentAmountCommand.cs @ 214:1b718b67f7f6

Re #179: Make sure that translations are used throughout UI * Fix locations in the API that are used in the front-end
author IBBoard <dev@ibboard.co.uk>
date Sat, 21 Nov 2009 21:27:07 +0000
parents 2f3cafb69799
children 391446c9b250
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;
		private string equipName;
		private string unitName;
		
		public AbstractSetUnitEquipmentAmountCommand(Unit unit, UnitEquipmentItem item)
		{
			this.unit = unit;
			equip = item;
			oldAmount = UnitEquipmentUtil.GetEquipmentAmount(unit, equip);
			oldAmountWasRatio = UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, equip);
			equipName = StringManipulation.CutToLength(equip.Name, 20);
			unitName = StringManipulation.CutToLength(unit.Name, 20);
		}

		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}", equipName, unitName, 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
			{
				string undoDescription;
				
				if (oldAmount == 0)
				{
					undoDescription = Translation.GetTranslation("setEquipmentAmountCommandRemoveDescription", "remove {0} from {1}", equipName, unitName);
				}
				else
				{
					undoDescription = Translation.GetTranslation("setEquipmentAmountCommandUndoDescription", "set {0} amount for {1} to {2}", equipName, unitName, GetOldAmountString());
				}
				
				return undoDescription;
			}
		}

		/// <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; }
		}
	}
}