Mercurial > repos > IBBoard.WarFoundry.API
view API/Commands/AbstractSetUnitEquipmentAmountCommand.cs @ 493:6b0ca39025ed
Re #423: Support nested units in GTK UI
* Swap order of removing unit from parent so that we validation at the appropriate time
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 06 Aug 2012 20:44:26 +0100 |
parents | c8002429ab45 |
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, UnitEquipmentUtil.CalculateEquipmentAmountTakenFromRatio(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; } } } }