Mercurial > repos > IBDev-IBBoard.WarFoundry.API
comparison api/Commands/AbstractSetUnitEquipmentAmountCommand.cs @ 101:f7b9423c2a5a
Big mess of updates, breaking our rules on "commit little and often" because the code was so ugly.
This revision will be broken for the WinForms UI, but as MonoDevelop/eSVN don't have a way of committing multiple projects in one go it can't be helped (Eclipse's Team Sync view could handle it)
Fixes #122: Make usage of percentage or ratio common
* All usage of ratio amounts for equipment items should now assume percentage
* Properly calculate number taken for ratio selection (divide by 0 now we're using percentages)
Fixes #118: Allow equipment amounts of "ratio" equipment to be define as absolute or ratio amounts
* Added extra commands that differentiate between ratio and absolute amounts
Fixes #120: Numeric limit equipment items show large percentages
* Now made formatting treat ratios as percentages (don't multiply by 100)
* Move string formatting to UnitEquipmentItem...Selection classes
* Add method to Unit to say whether an equipment item is a numeric or ratio amount
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Thu, 13 Aug 2009 21:09:20 +0000 |
parents | |
children | 2f3cafb69799 |
comparison
equal
deleted
inserted
replaced
100:38e788859199 | 101:f7b9423c2a5a |
---|---|
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 under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. | |
4 // | |
5 | |
6 using System; | |
7 using IBBoard.Commands; | |
8 using IBBoard.Lang; | |
9 using IBBoard.WarFoundry.API.Objects; | |
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 = unit.GetEquipmentAmount(equip); | |
28 oldAmountWasRatio = unit.GetEquipmentAmountIsRatio(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 "Set " + StringManipulation.CutToLength(equip.Name, 20) + " ratio for " + StringManipulation.CutToLength(unit.Name, 20) + " to " + 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 return "Set " + StringManipulation.CutToLength(equip.Name, 20) + " ratio for " + StringManipulation.CutToLength(unit.Name, 20) + " to " + GetOldAmountString(); | |
56 } | |
57 } | |
58 | |
59 /// <summary> | |
60 /// Gets the string representation for the old amount of the equipment item to take | |
61 /// </summary> | |
62 /// <returns> | |
63 /// the string representation for the old amount of the equipment item to take | |
64 /// </returns> | |
65 protected string GetOldAmountString() | |
66 { | |
67 string oldAmountString; | |
68 if (oldAmountWasRatio) | |
69 { | |
70 oldAmountString = UnitEquipmentRatioSelection.GetEquipmentAmountString(oldAmount); | |
71 } | |
72 else | |
73 { | |
74 oldAmountString = UnitEquipmentNumericSelection.GetEquipmentAmountString(oldAmount); | |
75 } | |
76 | |
77 return oldAmountString; | |
78 } | |
79 | |
80 | |
81 public override bool Execute() | |
82 { | |
83 this.Redo(); | |
84 return true; | |
85 } | |
86 | |
87 public override void Undo () | |
88 { | |
89 if (oldAmountWasRatio) | |
90 { | |
91 unit.SetEquipmentRatio(equip, oldAmount); | |
92 } | |
93 else | |
94 { | |
95 unit.SetEquipmentAmount(equip, (int)oldAmount); | |
96 } | |
97 } | |
98 | |
99 | |
100 public UnitEquipmentItem EquipItem | |
101 { | |
102 get { return equip; } | |
103 } | |
104 | |
105 public Unit Unit | |
106 { | |
107 get { return unit; } | |
108 } | |
109 } | |
110 } |