view Util/UnitEquipmentChoice.cs @ 246:b57a7cf844f8

Fixes #426: Changing unit size doesn't trigger all validation * Hook validation in to army composition changed event * Make sure we're ignoring old armies and listening to the new one
author IBBoard <dev@ibboard.co.uk>
date Sat, 01 Sep 2012 16:12:48 +0100
parents eaa7b639d390
children
line wrap: on
line source

// This file (UnitEquipmentChoice.cs) is a part of the IBBoard.WarFoundry.GUI.WinForms 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.Lang;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.API.Util;
using IBBoard.WarFoundry.API;

namespace IBBoard.WarFoundry.GUI.WinForms.Util
{
    /// <summary>
    /// A helper object that holds an equipment choice for a unit.
    /// </summary>

    public class UnitEquipmentChoice
    {
        private Unit unit;
		private UnitEquipmentItem item;
        public GameSystem CurrentGameSystem
        {
            get { return WarFoundryCore.CurrentGameSystem; }
            set { WarFoundryCore.CurrentGameSystem = value; }
        }

        public UnitEquipmentChoice(Unit unit, UnitEquipmentItem unitItem)
        {
            this.unit = unit;
            item = unitItem;
        }

        public override string ToString()
        {
			String translation = "";

			if (item.Cost == 0)
			{
				translation = Translation.GetTranslation("equipmentAmountWithZeroCost", "{0} ({1} - free)", item.Name, GetAmountString());
			}
			else
			{
                translation = Translation.GetTranslation("equipmentAmountWithCost", "{0} ({1} at {2}pts each)", item.Name, GetAmountString(), item.Cost, CurrentGameSystem.GetPointsAbbrev(item.Cost));
			}

			return translation;
        }

		private string GetAmountString()
		{
			double amount = UnitEquipmentUtil.GetEquipmentAmount(unit, item);
			string amountString = "";

			if (UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, item))
			{
				int number = UnitEquipmentUtil.GetEquipmentAmountTaken(unit, item);

				if (amount == 100)
				{
					amountString = Translation.GetTranslation("equipmentChoiceAmountAll", "all ({1})", amount, number);
				}
				else
				{
					amountString = Translation.GetTranslation("equipmentChoiceAmountPercentage", "{0}% ({1})", amount, number);
				}
			}
			else
			{
				amountString = Translation.GetTranslation("equipmentChoiceAmountNumber", "{0}", amount);
			}

			return amountString;
		}

        public UnitEquipmentItem Item
        {
            get { return item; }
        }
    }
}