view api/Objects/UnitEquipmentNumericSelection.cs @ 115:d0c60b3204c1

Re #53: Add XML army saving * Make file identifiers public * Add army identifier to comment no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sat, 22 Aug 2009 19:41:15 +0000
parents 2f3cafb69799
children 36adabb1c3ea
line wrap: on
line source

// This file (UnitEquipmentNumericSelection.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;

namespace IBBoard.WarFoundry.API.Objects
{	
	/// <summary>
	/// An object to hold the selection of a unit's equipment where the selection was made as an absolute number
	/// </summary>
	public class UnitEquipmentNumericSelection : AbstractUnitEquipmentItemSelection
	{	
		public UnitEquipmentNumericSelection(Unit unit, UnitEquipmentItem item, double amount) : base(unit, item, amount)
		{
		}
		
		public UnitEquipmentNumericSelection(Unit unit, UnitEquipmentItem item) : base(unit, item, item.MinNumber)
		{
		}
					
		public override int NumberTaken
		{
			get
			{
				return (AmountTaken == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : (int) AmountTaken);
			}
		}
		
		protected bool IsWholeNumber(double newValue)
		{
			return newValue == Math.Round(newValue);
		}
		
		protected override bool IsInRange(double newValue)
		{
			bool isInRange = IsWholeNumber(newValue);
			
			if (newValue == WarFoundryCore.INFINITY)
			{
				isInRange = (EquipmentItem.MaxNumber == WarFoundryCore.INFINITY);
			}
			else if (isInRange)
			{
				int minLimit = (EquipmentItem.MinNumber == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : EquipmentItem.MinNumber);
				int maxLimit = (EquipmentItem.MaxNumber == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : EquipmentItem.MaxNumber);
				isInRange = (minLimit <= newValue) && (newValue <= maxLimit);
			}
			
			return isInRange;
		}

		public override string GetEquipmentAmountString ()
		{
			return GetEquipmentAmountString(AmountTaken);
		}

		/// <summary>
		/// Gets a string representing the amount of equipment taken when the amount is a numeric selection
		/// </summary>
		/// <param name="amount">
		/// A <see cref="System.Double"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/>
		/// </returns>
		public static string GetEquipmentAmountString(double amount)
		{
			string amountString;
			
			if (amount == WarFoundryCore.INFINITY)
			{
				amountString = "all"; //TODO: Translate
			}
			else
			{
				amountString = amount.ToString();
			}

			return amountString;
		}
	}
}