Mercurial > repos > IBBoard.WarFoundry.API
annotate api/Objects/ArmyCategory.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 | cb3759c3ea19 |
children | 2f3cafb69799 |
rev | line source |
---|---|
15 | 1 // This file (ArmyCategory.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 | |
82 | 5 using System; |
0 | 6 using System.Collections.Generic; |
82 | 7 using IBBoard.WarFoundry.API.Requirements; |
8 | |
9 namespace IBBoard.WarFoundry.API.Objects | |
10 { | |
11 /// <summary> | |
12 /// Summary description for ArmyCategory. | |
13 /// </summary> | |
14 public class ArmyCategory : WarFoundryObject | |
15 { | |
16 private Category category; | |
17 private Army parentArmy; | |
18 private double pointsTotal; | |
19 private List<Unit> units; | |
20 private Dictionary<string, int> unitTypes; | |
21 private DoubleValChangedDelegate PointsValueChangedMethod; | |
22 public event DoubleValChangedDelegate PointsValueChanged; | |
23 public event ObjectAddDelegate UnitAdded; | |
89 | 24 public event ObjectRemoveDelegate UnitRemoved; |
83
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
25 public event FailedUnitRequirementDelegate FailedRequirement; |
82 | 26 |
27 public ArmyCategory(Army army, Category cat) : base() | |
28 { | |
29 parentArmy = army; | |
30 category = cat; | |
31 cat.NameChanged+=new StringValChangedDelegate(cat_NameChanged); | |
32 PointsValueChangedMethod = new DoubleValChangedDelegate(PointsValueChangedHandler); | |
33 units = new List<Unit>(); | |
34 unitTypes = new Dictionary<string,int>(); | |
35 } | |
36 | |
37 public Category Category | |
38 { | |
39 get { return category; } | |
40 } | |
41 | |
42 public Army ParentArmy | |
43 { | |
44 get { return parentArmy; } | |
45 } | |
46 | |
47 public override string ID | |
48 { | |
49 get | |
50 { | |
51 return Category.ID; | |
52 } | |
53 set | |
54 { | |
55 Category.ID = value; | |
56 } | |
57 } | |
58 | |
59 public override string Name | |
60 { | |
61 get { return category.Name; } | |
62 set | |
63 { | |
64 category.Name = value; | |
65 } | |
66 } | |
67 | |
68 internal void AddUnit(Unit unit) | |
69 { | |
70 List<FailedUnitRequirement> failedReqs = ParentArmy.CanAddUnit(unit); | |
71 units.Add(unit); | |
72 unit.Category = this; | |
0 | 73 unit.PointsValueChanged+= PointsValueChangedMethod; |
82 | 74 int unitTypeCount; |
75 unitTypes.TryGetValue(unit.UnitType.ID, out unitTypeCount); | |
76 unitTypes[unit.UnitType.ID] = (int)unitTypeCount + 1; | |
58
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
77 TotalPoints+= unit.PointsValue; |
83
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
78 OnUnitAdded(unit, failedReqs); |
82 | 79 } |
80 | |
81 internal void RemoveUnit(Unit unit) | |
82 { | |
83
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
83 List<FailedUnitRequirement> failedReqs = ParentArmy.CanRemoveUnit(unit); |
82 | 84 units.Remove(unit); |
85 unitTypes[unit.UnitType.ID] = ((int)unitTypes[unit.UnitType.ID])-1; | |
86 TotalPoints-= unit.PointsValue; | |
58
e53ed2d613a1
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
87 unit.PointsValueChanged-= PointsValueChangedMethod; |
83
89cc29b4c012
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
88 OnUnitRemoved(unit, failedReqs); |
82 | 89 } |
90 | |
91 public int GetUnitTypeCount(UnitType unitType) | |
92 { | |
93 return unitTypes.ContainsKey(unitType.ID) ? (int)unitTypes[unitType.ID] : 0; | |
94 } | |
95 | |
96 public Unit[] GetUnits() | |
97 { | |
98 return units.ToArray(); | |
99 } | |
100 | |
101 private double TotalPoints | |
102 { | |
103 get { return pointsTotal; } | |
104 set | |
105 { | |
106 double oldVal = pointsTotal; | |
107 pointsTotal = value; | |
108 | |
109 if (oldVal!=pointsTotal) | |
110 { | |
111 OnPointsValueChanged(oldVal, pointsTotal); | |
112 } | |
113 } | |
114 } | |
115 | |
116 public double PointsTotal | |
117 { | |
118 get { return TotalPoints; } | |
119 } | |
120 | |
121 private void PointsValueChangedHandler(WarFoundryObject obj, double oldVal, double newVal) | |
122 { | |
123 if (obj is Unit) | |
124 { | |
125 double diff = newVal - oldVal; | |
126 TotalPoints+= diff; | |
89 | 127 } |
128 } | |
129 | |
130 protected void OnUnitAdded(Unit unit) | |
131 { | |
132 OnUnitAdded(unit, null); | |
133 } | |
134 | |
135 protected void OnUnitAdded(Unit unit, List<FailedUnitRequirement> failedReqs) | |
136 { | |
137 if (UnitAdded != null) | |
138 { | |
139 UnitAdded(unit); | |
140 } | |
141 | |
142 if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0) | |
143 { | |
144 FailedRequirement(failedReqs); | |
145 } | |
146 } | |
147 | |
148 protected void OnUnitRemoved(Unit unit) | |
149 { | |
150 OnUnitRemoved(unit, null); | |
151 } | |
152 | |
153 protected void OnUnitRemoved(Unit unit, List<FailedUnitRequirement> failedReqs) | |
154 { | |
155 if (UnitRemoved != null) | |
156 { | |
157 UnitRemoved(unit); | |
158 } | |
159 | |
160 if (FailedRequirement != null && failedReqs != null && failedReqs.Count > 0) | |
161 { | |
162 FailedRequirement(failedReqs); | |
163 } | |
82 | 164 } |
165 | |
166 protected virtual void OnPointsValueChanged(double oldValue, double newValue) | |
167 { | |
168 if (PointsValueChanged!=null) | |
169 { | |
170 PointsValueChanged(this, oldValue, newValue); | |
171 } | |
172 } | |
173 | |
174 protected void cat_NameChanged(WarFoundryObject obj, string oldValue, string newValue) | |
175 { | |
176 OnNameChanged(oldValue, newValue); | |
177 } | |
178 | |
179 public int GetPointsPercentage() | |
180 { | |
181 return (int)Math.Round((PointsTotal / ParentArmy.MaxPoints) * 100, 0); | |
182 } | |
183 } | |
184 } |