-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfopanel.cpp
More file actions
371 lines (339 loc) · 11.7 KB
/
infopanel.cpp
File metadata and controls
371 lines (339 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "infopanel.hpp"
namespace TerraNova{
InfoPanel::InfoPanel(SDL_Renderer* ren, const GFXObject& source,
const int x, const int y):
GFXObject(ren, File::SpritePath() / "ui" / "info_panel", x, y) {
UpdateFromSource(source);
}
void InfoPanel::Render() const {
GFXObject::Render();
for (const auto& div : dividers) div->Render();
for (const auto& ico : icons ) ico->Render();
}
void InfoPanel::MoveTo(int x, int y) {
for (const auto& div : dividers) {
div->MoveTo(div->X() - X() + x, div->Y() - Y() + y);
}
for (const auto& ico : icons) {
ico->MoveTo(ico->X() - X() + x, ico->Y() - Y() + y);
}
GFXObject::MoveTo(x, y);
}
void InfoPanel::MoveTo(SDL_Rect newLayout) {
MoveTo(newLayout.x, newLayout.y);
}
void InfoPanel::UpdateFromSource(const GFXObject& source) {
if (source.IsUnit()) {
UpdateFromUnit(dynamic_cast<const Unit&>(source));
return;
}
if (source.IsBuilding()) {
UpdateFromBuilding(dynamic_cast<const Building&>(source));
return;
}
if (source.IsBuildingPrototype()) {
UpdateFromBuildingPrototype(
dynamic_cast<const BuildingPrototype&>(source) );
return;
}
}
void InfoPanel::UpdateFromUnit(const Unit& source) {
if (panelMode != INFO_PANEL_UNIT) {
dividers = UnitModeDividers();
panelMode = INFO_PANEL_UNIT;
}
icons = IconsFromUnit(source);
}
std::vector<std::unique_ptr<UIElement>> InfoPanel::MakeDividers(
const int numberOfHorizontal, const int* horizontalX,
const int* horizontalY , const int* horizontalL,
const int numberOfVertical , const int* verticalX ,
const int* verticalY , const int* verticalL) {
std::vector<std::unique_ptr<UIElement>> output;
for (int i = 0; i < numberOfHorizontal; ++i) {
std::unique_ptr<UIElement> newDiv = std::make_unique<UIElement>(
ren, "info_panel_horizontal_divider", X() + horizontalX[i],
Y() + horizontalY[i] );
newDiv->Resize(horizontalL[i], newDiv->H());
output.push_back(std::move(newDiv));
}
for (int i = 0; i < numberOfVertical; ++i) {
std::unique_ptr<UIElement> newDiv = std::make_unique<UIElement>(
ren, "info_panel_vertical_divider", X() + verticalX[i],
Y() + verticalY[i] );
newDiv->Resize(newDiv->W(), verticalL[i]);
output.push_back(std::move(newDiv));
}
return output;
}
namespace UnitPanel {
constexpr int numberOfHorizontal = 2;
constexpr int horizontalX[numberOfHorizontal] = { 154, 154 };
constexpr int horizontalY[numberOfHorizontal] = { 54 , 104 };
constexpr int horizontalL[numberOfHorizontal] = { 250, 250 };
constexpr int numberOfVertical = 1;
constexpr int verticalX[numberOfVertical] = { 204 };
constexpr int verticalY[numberOfVertical] = { 4 };
constexpr int verticalL[numberOfVertical] = { 150 };
enum IconType { UICON_PORTRAIT, UICON_FACTION, UICON_SPECIES, UICON_WEAPON};
enum TextType { UTEXT_NONE, UTEXT_NAME, UTEXT_HEALTH, UTEXT_WEAPON };
constexpr int numberOfIcons = 4;
constexpr IconType iconType[numberOfIcons] = { UICON_PORTRAIT,
UICON_FACTION, UICON_SPECIES, UICON_WEAPON };
constexpr int iconX[numberOfIcons] = { 5 , 156, 156, 156 };
constexpr int iconY[numberOfIcons] = { 5 , 6 , 56 , 106 };
constexpr TextType iconText[numberOfIcons] = { UTEXT_NONE, UTEXT_NAME,
UTEXT_HEALTH, UTEXT_WEAPON };
constexpr int textX[numberOfIcons] = { 0 , 140, 140, 145 };
constexpr int textY[numberOfIcons] = { 0 , 25 , 25 , 25 };
constexpr int textW[numberOfIcons] = { 0 , 199, 199, 199 };
constexpr int textH[numberOfIcons] = { 0 , 48 , 48 , 48 };
} // namespace UnitPanel
// locations of these dividers given in the above UnitPanel namespace
std::vector<std::unique_ptr<UIElement>> InfoPanel::UnitModeDividers() {
return MakeDividers(UnitPanel::numberOfHorizontal, UnitPanel::horizontalX,
UnitPanel::horizontalY, UnitPanel::horizontalL,
UnitPanel::numberOfVertical, UnitPanel::verticalX,
UnitPanel::verticalY, UnitPanel::verticalL);
}
std::vector<std::unique_ptr<UIElement>> InfoPanel::IconsFromUnit(
const Unit& source) {
std::vector<std::unique_ptr<UIElement>> output;
for (int i = 0; i < UnitPanel::numberOfIcons; ++i) {
File::Path iconPath = "";
switch (UnitPanel::iconType[i]) {
case UnitPanel::UICON_PORTRAIT: {
iconPath = File::Path("portraits") / "portrait_";
iconPath += source.Spec()->PathName();
break;
}
case UnitPanel::UICON_FACTION: {
iconPath = "factioncolor_p";
iconPath += std::to_string(source.Faction());
break;
}
case UnitPanel::UICON_SPECIES: {
iconPath = "healthicon_";
iconPath += source.Species();
break;
}
case UnitPanel::UICON_WEAPON: {
if (source.Spec()->Attack(0)) {
iconPath = File::Path("weapons") /
source.Spec()->Attack(0)->PathName();
iconPath += "_icon";
} else {
iconPath = File::Path("weapons") / "null_attack_icon";
}
break;
}
}
std::unique_ptr<UIElement> newIcon = std::make_unique<UIElement>(ren,
iconPath.string(), X() + UnitPanel::iconX[i],
Y() + UnitPanel::iconY[i] );
if (UnitPanel::iconText[i] != UnitPanel::UTEXT_NONE) {
std::string iconText("");
SDL_Rect textBox = MakeSDLRect(
UnitPanel::textX[i], UnitPanel::textY[i],
UnitPanel::textW[i], UnitPanel::textH[i] );
switch (UnitPanel::iconText[i]) {
case UnitPanel::UTEXT_NONE: {
break;
}
case UnitPanel::UTEXT_NAME: {
iconText = source.Name();
break;
}
case UnitPanel::UTEXT_HEALTH: {
iconText = std::to_string(source.Health()) + "/"
+ std::to_string(source.MaxHealth());
break;
}
case UnitPanel::UTEXT_WEAPON: {
if (source.Spec()->Attack(0) == nullptr) {
iconText = "Unarmed";
} else {
iconText = std::to_string(static_cast<int>(
std::floor(100*source.Accuracy()) ))
+ "% | " + std::to_string(source.AttackRate())
+ "x " + std::to_string(source.Damage()) + " "
+ source.DamageType();
}
break;
}
}
if (!iconText.empty()) newIcon->AddText(iconText, textBox);
}
output.push_back(std::move(newIcon));
}
return output;
}
void InfoPanel::UpdateFromBuilding(const Building& source) {
if (panelMode != INFO_PANEL_BUILDING) {
dividers = BuildingModeDividers();
panelMode = INFO_PANEL_BUILDING;
}
icons = IconsFromBuilding(source);
}
void InfoPanel::UpdateFromBuildingPrototype(const BuildingPrototype& source) {
if (panelMode != INFO_PANEL_BUILDING) {
dividers = BuildingModeDividers();
panelMode = INFO_PANEL_BUILDING;
}
icons = IconsFromBuildingPrototype(source);
}
namespace BldgPanel {
constexpr int numberOfHorizontal = 3;
constexpr int horizontalX[numberOfHorizontal] = { 154, 154 };
constexpr int horizontalY[numberOfHorizontal] = { 54 , 104 };
constexpr int horizontalL[numberOfHorizontal] = { 250, 250 };
constexpr int numberOfVertical = 5;
constexpr int verticalX[numberOfVertical] = { 204, 204, 254, 304, 354 };
constexpr int verticalY[numberOfVertical] = { 54 , 104, 104, 104, 104 };
constexpr int verticalL[numberOfVertical] = { 50 , 50 , 50 , 50 , 50 };
enum IconType { BICON_PORTRAIT, BICON_FACTION, BICON_POWER, BICON_FOOD,
BICON_CARBON, BICON_SILICON, BICON_IRON };
enum TextType { BTEXT_NONE, BTEXT_NAME, BTEXT_POWER, BTEXT_FOOD,
BTEXT_CARBON, BTEXT_SILICON, BTEXT_IRON };
constexpr int numberOfIcons = 7;
constexpr IconType iconType[numberOfIcons] = { BICON_PORTRAIT,
BICON_FACTION, BICON_POWER, BICON_FOOD, BICON_CARBON, BICON_SILICON,
BICON_IRON };
constexpr int iconX[numberOfIcons] = { 5 , 156, 206, 156, 206, 256, 306 };
constexpr int iconY[numberOfIcons] = { 5 , 56 , 56 , 106, 106, 106, 106 };
constexpr TextType iconText[numberOfIcons] = { BTEXT_NAME, BTEXT_NONE,
BTEXT_POWER, BTEXT_FOOD, BTEXT_CARBON, BTEXT_SILICON, BTEXT_IRON };
constexpr int textX[numberOfIcons] = { 225, 0 , 74 , 25 , 25 , 25 , 25 };
constexpr int textY[numberOfIcons] = { 25 , 0 , 25 , 30 , 30 , 30 , 30 };
constexpr int textW[numberOfIcons] = { 399, 0 , 199, 48 , 48 , 48 , 48 };
constexpr int textH[numberOfIcons] = { 48 , 0 , 48 , 48 , 48 , 48 , 48 };
} // namespace BldgPanel
std::vector<std::unique_ptr<UIElement>> InfoPanel::BuildingModeDividers() {
return MakeDividers(BldgPanel::numberOfHorizontal, BldgPanel::horizontalX,
BldgPanel::horizontalY, BldgPanel::horizontalL,
BldgPanel::numberOfVertical, BldgPanel::verticalX,
BldgPanel::verticalY, BldgPanel::verticalL);
}
std::vector<std::unique_ptr<UIElement>> InfoPanel::IconsFromBuilding(
const Building& source) {
std::vector<std::unique_ptr<UIElement>> output;
output = IconsFromBuildingType(*source.Type(), source.PoweredOn());
output.push_back(BuildingFactionIcon(source.Faction()));
return output;
}
std::vector<std::unique_ptr<UIElement>> InfoPanel::IconsFromBuildingPrototype(
const BuildingPrototype& source) {
std::vector<std::unique_ptr<UIElement>> output;
output = IconsFromBuildingType(*source.Type(), false);
output.push_back(BuildingFactionIcon(0));
return output;
}
std::vector<std::unique_ptr<UIElement>> InfoPanel::IconsFromBuildingType(
const BuildingType& source, const bool poweredOn) {
std::vector<std::unique_ptr<UIElement>> output;
for (int i = 0; i < BldgPanel::numberOfIcons; ++i) {
File::Path iconPath = "";
switch (BldgPanel::iconType[i]) {
case BldgPanel::BICON_PORTRAIT: {
iconPath = File::Path("portraits") / "portrait_";
iconPath += source.PathName();
break;
}
case BldgPanel::BICON_FACTION: {
break;
}
case BldgPanel::BICON_POWER: {
if (poweredOn) {
iconPath = "power_on";
} else {
iconPath = "power_off";
}
break;
}
case BldgPanel::BICON_FOOD: {
iconPath = "food_cost";
break;
}
case BldgPanel::BICON_CARBON: {
iconPath = "carbon_cost";
break;
}
case BldgPanel::BICON_SILICON: {
iconPath = "silicon_cost";
break;
}
case BldgPanel::BICON_IRON: {
iconPath = "iron_cost";
break;
}
}
if (iconPath.empty()) continue;
std::unique_ptr<UIElement> newIcon = std::make_unique<UIElement>(ren,
iconPath.string(), X() + BldgPanel::iconX[i],
Y() + BldgPanel::iconY[i] );
if (BldgPanel::iconText[i] != BldgPanel::BTEXT_NONE) {
std::string iconText("");
SDL_Rect textBox = MakeSDLRect(
BldgPanel::textX[i], BldgPanel::textY[i],
BldgPanel::textW[i], BldgPanel::textH[i] );
textcolor_t textColor = BLACK;
switch (BldgPanel::iconText[i]) {
case BldgPanel::BTEXT_NONE: {
break;
}
case BldgPanel::BTEXT_NAME: {
iconText = source.Name();
break;
}
case BldgPanel::BTEXT_POWER: {
if (poweredOn) {
if (source.PowerProduction() > 0) {
iconText = "+"
+ std::to_string(source.PowerProduction());
textColor = BLUE;
} else {
iconText = std::to_string(source.PowerConsumption());
textColor = BLACK;
}
} else {
iconText = "0";
textColor = BLACK;
}
break;
}
case BldgPanel::BTEXT_FOOD: {
iconText = std::to_string(source.Cost()[0]);
break;
}
case BldgPanel::BTEXT_CARBON: {
iconText = std::to_string(source.Cost()[1]);
break;
}
case BldgPanel::BTEXT_SILICON: {
iconText = std::to_string(source.Cost()[2]);
break;
}
case BldgPanel::BTEXT_IRON: {
iconText = std::to_string(source.Cost()[3]);
break;
}
}
if (!iconText.empty()) {
newIcon->AddText(iconText, textBox, nullptr, textColor);
}
}
output.push_back(std::move(newIcon));
}
return output;
}
std::unique_ptr<UIElement> InfoPanel::BuildingFactionIcon(const char faction) {
for (int i = 0; i < BldgPanel::numberOfIcons; ++i) {
if (BldgPanel::iconType[i] == BldgPanel::BICON_FACTION) {
std::string iconName = "factioncolor_p" + std::to_string(faction);
return std::make_unique<UIElement>(ren, iconName,
X() + BldgPanel::iconX[i], Y() + BldgPanel::iconY[i]);
}
}
return nullptr;
}
} // namespace TerraNova