comparison API/Commands/AbstractSetUnitEquipmentAmountCommand.cs @ 337:3c4a6403a88c

* Fix capitalisation so that new files are in the namespace no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 03 Apr 2011 18:50:32 +0000
parents
children c8002429ab45
comparison
equal deleted inserted replaced
336:3631c1493c7f 337:3c4a6403a88c
1 // This file (AbstractSetUnitEquipmentAmountCommand.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
2 //
3 // 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.
4
5 using System;
6 using IBBoard.Commands;
7 using IBBoard.Lang;
8 using IBBoard.WarFoundry.API.Objects;
9 using IBBoard.WarFoundry.API.Util;
10
11 namespace IBBoard.WarFoundry.API.Commands
12 {
13 /// <summary>
14 /// Abstract parent class for commands that set the amount of an equipment item a unit has to a fixed numeric or ratio value
15 /// </summary>
16 public abstract class AbstractSetUnitEquipmentAmountCommand : Command
17 {
18 private Unit unit;
19 private UnitEquipmentItem equip;
20 private double oldAmount;
21 private bool oldAmountWasRatio;
22
23 public AbstractSetUnitEquipmentAmountCommand(Unit unit, UnitEquipmentItem item)
24 {
25 this.unit = unit;
26 equip = item;
27 oldAmount = UnitEquipmentUtil.GetEquipmentAmount(unit, equip);
28 oldAmountWasRatio = UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, equip);
29 }
30
31 public override bool CanExecute()
32 {
33 return (unit != null && equip != null);
34 }
35
36 public override string Description
37 {
38 get
39 {
40 return Translation.GetTranslation("setEquipmentAmountCommandDescription", "set {0} amount for {1} to {2}", equip.Name, unit.Name, GetNewAmountString());
41 }
42 }
43
44 /// <summary>
45 /// Gets the string representation for the new amount of the equipment item to take
46 /// </summary>
47 /// <returns>
48 /// the string representation for the new amount of the equipment item to take
49 /// </returns>
50 protected abstract string GetNewAmountString();
51
52 public override string UndoDescription
53 {
54 get
55 {
56 return Translation.GetTranslation("setEquipmentAmountCommandUndoDescription", "set {0} amount for {1} to {2}", equip.Name, unit.Name, GetOldAmountString());
57 }
58 }
59
60 /// <summary>
61 /// Gets the string representation for the old amount of the equipment item to take
62 /// </summary>
63 /// <returns>
64 /// the string representation for the old amount of the equipment item to take
65 /// </returns>
66 protected string GetOldAmountString()
67 {
68 return oldAmountWasRatio ? GetRatioAmountString(oldAmount, UnitEquipmentRatioSelection.CalculateNumberTaken(Unit, EquipItem, oldAmount)) : GetNumberAmountString((int)oldAmount);
69 }
70
71 protected string GetNumberAmountString(int number)
72 {
73 return Translation.GetTranslation("equipmentAmountNumber", "{0}", number);
74 }
75
76 protected string GetRatioAmountString(double amount, int number)
77 {
78 string amountString;
79
80 if (amount == 100)
81 {
82 amountString = Translation.GetTranslation("equipmentAmountAll", "all ({1})", amount, number);
83 }
84 else
85 {
86 amountString = Translation.GetTranslation("equipmentAmountPercentage", "{0}% ({1})", amount, number);
87 }
88
89 return amountString;
90 }
91
92 public override bool Execute()
93 {
94 this.Redo();
95 return true;
96 }
97
98 public override void Undo()
99 {
100 if (oldAmountWasRatio)
101 {
102 unit.SetEquipmentRatio(equip, oldAmount);
103 }
104 else
105 {
106 unit.SetEquipmentAmount(equip, (int)oldAmount);
107 }
108 }
109
110 public UnitEquipmentItem EquipItem
111 {
112 get { return equip; }
113 }
114
115 public Unit Unit
116 {
117 get { return unit; }
118 }
119 }
120 }