forked from ennorehling/csmapfx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregioninfos.cpp
More file actions
485 lines (395 loc) · 13.8 KB
/
Copy pathregioninfos.cpp
File metadata and controls
485 lines (395 loc) · 13.8 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include "main.h"
#include "fxhelper.h"
#include "regioninfos.h"
#include "symbols.h"
#include <algorithm>
// *********************************************************************************************************
// *** FXRegionInfos implementation
FXDEFMAP(FXRegionInfos) MessageMap[]=
{
//________Message_Type_____________________ID_______________Message_Handler_______
FXMAPFUNC(SEL_COMMAND, FXRegionInfos::ID_UPDATE, FXRegionInfos::onMapChange),
FXMAPFUNC(SEL_COMMAND, FXRegionInfos::ID_TOGGLEDESCRIPTION, FXRegionInfos::onToggleDescription),
FXMAPFUNC(SEL_UPDATE, FXRegionInfos::ID_TOGGLEDESCRIPTION, FXRegionInfos::onUpdateToggleDescription),
FXMAPFUNC(SEL_UPDATE, FXRegionInfos::ID_DESCRIPTION, FXRegionInfos::onUpdateDescription),
};
FXIMPLEMENT(FXRegionInfos,FXVerticalFrame,MessageMap, ARRAYNUMBER(MessageMap))
FXRegionInfos::FXRegionInfos(FXComposite* p, FXObject* tgt,FXSelector sel, FXuint opts, FXint x,FXint y,FXint w,FXint h)
: FXVerticalFrame(p, opts, x,y,w,h, 0,0,0,0, 0,0)
{
// set target etc.
setTarget(tgt);
setSelector(sel);
// create/load images for terrain types
for (int i = 0; i < datablock::TERRAIN_LAST; i++)
terrainIcons[i] = new FXGIFIcon(getApp(), data::terrainSymbols[i], 0, IMAGE_ALPHAGUESS);
// set style
setBorderColor(getApp()->getShadowColor());
setFrameStyle(FRAME_LINE);
// init variables
files = NULL;
show_description = false;
// create layout
tags.name = new FXLabel(this, "", terrainIcons[datablock::TERRAIN_UNKNOWN], ICON_BEFORE_TEXT|LAYOUT_FILL_X);
tags.matrixsep = new FXHorizontalSeparator(this, LAYOUT_FILL_X|SEPARATOR_LINE, 0,0,0,0, 0,0,0,0);
tags.matrixsep->setBorderColor(getBorderColor());
tags.matrixsep->hide();
tags.matrixframe = new FXHorizontalFrame(this, LAYOUT_SIDE_TOP|LAYOUT_FILL_X, 0,0,0,0, 0,0,0,0, 0,0);
tags.matrixframe->hide();
tags.leftmatrix = new FXMatrix(tags.matrixframe,3,MATRIX_BY_COLUMNS|LAYOUT_FILL_X, 0,0,0,0, 2,2,2,2, 0,0);
FXFrame *sep = new FXVerticalSeparator(tags.matrixframe,LAYOUT_FILL_Y|SEPARATOR_LINE, 0,0,0,0, 0,0,0,0);
sep->setBorderColor(getBorderColor());
tags.rightmatrix = new FXMatrix(tags.matrixframe,3,MATRIX_BY_COLUMNS|LAYOUT_FILL_X, 0,0,0,0, 2,2,2,2, 0,0);
tags.descsep = new FXHorizontalSeparator(this, LAYOUT_FILL_X|SEPARATOR_LINE, 0,0,0,0, 0,0,0,0);
tags.descsep->setBorderColor(getBorderColor());
tags.desc = new FXText(this, this,ID_DESCRIPTION, LAYOUT_FILL_X|TEXT_READONLY|TEXT_WORDWRAP|HSCROLLER_NEVER);
tags.desc->disable();
}
void FXRegionInfos::create()
{
FXVerticalFrame::create();
// create terrain icons for region list
for (int i = 0; i < datablock::TERRAIN_LAST; i++)
terrainIcons[i]->create();
}
FXRegionInfos::~FXRegionInfos()
{
// terrain icons for region list
for (int i = 0; i < datablock::TERRAIN_LAST; i++)
delete terrainIcons[i];
}
void FXRegionInfos::mapfiles(std::list<datafile> *f)
{
files = f;
}
inline FXString thousandsPoints(FXint value, bool plusSign = false)
{
FXString str = FXStringVal((value<0)?-value:value);
int n = 0;
for (int i = str.length(); i > 0; i--,n++)
if (n && !(n%3))
str.insert(i, '.');
if (value<0)
str = "-" + str;
else if (value && plusSign)
str = "+" + str;
return str;
}
void FXRegionInfos::clearLabels()
{
for (std::vector<FXLabel*>::iterator itor = tags.entries.begin(); itor != tags.entries.end(); itor++)
delete *itor;
tags.entries.clear();
}
void FXRegionInfos::createLabels(const FXString& name, const FXString& first_label, const FXString& second_label, int column)
{
FXMatrix *matrix = (column==0) ? tags.leftmatrix : tags.rightmatrix;
// create labels
FXLabel *lname = new FXLabel(matrix, name, NULL, JUSTIFY_LEFT|LAYOUT_FILL_COLUMN|LAYOUT_FILL_X);
FXLabel *lfirst = new FXLabel(matrix, first_label, NULL, JUSTIFY_RIGHT|LAYOUT_RIGHT);
FXLabel *lsecond = new FXLabel(matrix, second_label, NULL, JUSTIFY_RIGHT|LAYOUT_RIGHT);
lsecond->disable();
lname->create(); lfirst->create(); lsecond->create();
// put into list
tags.entries.push_back(lname);
tags.entries.push_back(lfirst);
tags.entries.push_back(lsecond);
}
void FXRegionInfos::setInfo(const std::list<Info>& info)
{
clearLabels();
int number = info.size();
int index = 0;
for (std::list<Info>::const_iterator itor = info.begin(); itor != info.end(); itor++, index++)
{
FXString value = thousandsPoints(itor->value);
FXString offset = "";
if (itor->offset)
offset = thousandsPoints(itor->offset, true);
if (itor->skill)
value += " (" + FXStringVal(itor->skill) + ")";
FXString infotip = "\t" + value;
if (offset.length())
infotip += " [" + offset + "]";
createLabels(itor->name+"\t"+itor->tip, value+infotip, offset+infotip, 2*index>=number);
}
if (number)
{
tags.matrixsep->show();
tags.matrixframe->show();
}
else
{
tags.matrixsep->hide();
tags.matrixframe->hide();
}
}
void FXRegionInfos::addEntry(std::list<Info>& info, FXString name, int value, int skill, int offset, FXString tip)
{
std::list<Info>::iterator itor;
for (itor = info.begin(); itor != info.end(); itor++)
if (itor->name == name)
{
itor->value += value;
itor->skill += skill;
itor->offset += offset;
break;
}
if (itor == info.end())
info.push_back(Info(name, tip, value, skill, offset));
}
void FXRegionInfos::collectData(std::list<Info>& info, datablock::itor region)
{
FXString Bauern = region->value("Bauern");
FXString Silber = region->value("Silber");
FXString Unterh = region->value("Unterh");
FXString Rekruten = region->value("Rekruten");
FXString Pferde = region->value("Pferde");
if (!Bauern.empty()) addEntry(info, "Bauern", atoi(Bauern.text()), 0, 0, "Anzahl der Bauern");
if (!Silber.empty()) addEntry(info, "Silber", atoi(Silber.text()), 0, 0, "Silbervorat der Bauern");
if (!Unterh.empty()) addEntry(info, "Unterh.max", atoi(Unterh.text()), 0, 0, "Maximale Anzahl Silber, dass per Unterhalung eingenommen werden kann");
if (!Rekruten.empty()) addEntry(info, "Rekruten", atoi(Rekruten.text()), 0, 0, FXString("Anzahl der m\u00f6glichen Regionen"));
if (!Pferde.empty()) addEntry(info, "Pferde", atoi(Pferde.text()), 0, 0, "Anzahl Pferde");
int factionId = -1;
bool myfaction = false;
if (selection.map & selection.ACTIVEFACTION)
factionId = selection.activefaction->info();
FXint Personen = 0, Parteipersonen = 0;
FXint Parteisilber = 0;
datablock::itor end = files->front().blocks().end();
datablock::itor block = region;
for (block++; block != end && block->depth() > region->depth(); block++)
{
if (block->type() == datablock::TYPE_RESOURCE)
{
/* RESOURCE 235898859
"Eisen";type
17;skill
5;number */
FXString type = block->value("type");
FXString skillStr = block->value(datakey::TYPE_SKILL);
FXString numberStr = block->value("number");
FXString infotip = "Resource ";
infotip += type;
// don't double-count resources in REGION block
if (FXString("Bauern|Silber|Pferde").find(type) != -1)
continue;
if (!type.empty())
{
FXint number = 0;
FXint skill = 0;
if (numberStr.length())
number = atoi(numberStr.text());
if (skillStr.length())
skill = atoi(skillStr.text());
addEntry(info, type, number, skill, 0, infotip);
}
}
else if (block->type() == datablock::TYPE_UNIT)
{
myfaction = false;
FXString id = block->value(datakey::TYPE_FACTION);
if (!id.empty())
if (factionId == atoi(id.text()))
myfaction = true;
FXString number = block->value(datakey::TYPE_NUMBER);
if (!number.empty())
{
Personen += atoi(number.text());
if (myfaction)
Parteipersonen += atoi(number.text());
}
}
else if (block->type() == datablock::TYPE_ITEMS)
{
// Silber der eigenen Partei zum Parteisilber zaehlen
if (myfaction)
{
FXString silber = block->value("Silber");
if (!silber.empty())
Parteisilber += atoi(silber.text());
}
}
}
if (Personen)
{
addEntry(info, "Personen", Personen, 0, 0, "Anzahl (sichtbarer) Personen in dieser Region");
}
if (Parteipersonen)
{
addEntry(info, L"Anh\u00e4nger", Parteipersonen, 0, 0, "Anzahl eigener Personen");
addEntry(info, "Parteisilber", Parteisilber, 0, 0, "Das Silber, welches Personen der Partei in dieser Region besitzen");
}
}
void FXRegionInfos::updateData()
{
//if (files->empty())
//return;
if (selection.selected & selection.MULTIPLE_REGIONS)
{
FXString label;
tags.name->setText(label.format("%lu Regionen", selection.regionsSelected.size()));
tags.name->setIcon(terrainIcons[0]); // Symbol fuer unbekanntes Terrain, da mehrere Regionen
tags.name->setHelpText(label + " markiert");
tags.name->setTipText(label + " markiert");
// Beschreibungsfeld kann weg
tags.desc->setText("");
std::list<Info> info;
// collect infos
datablock::itor notfound = files->begin()->blocks().end();
for (std::set<datablock*>::iterator itor = selection.regionsSelected.begin(); itor != selection.regionsSelected.end(); itor++)
{
datablock::itor region = files->begin()->region((*itor)->x(), (*itor)->y(), (*itor)->info());
if (region != notfound)
{
// collect
collectData(info, region);
}
}
// apply information entries (Bauern, Silber, Pferde...)
setInfo(info);
}
else if (selection.selected & selection.REGION)
{
// show information of newly selected region
datablock ®ion = *selection.region;
FXString name = region.value(datakey::TYPE_NAME);
if (name.empty())
name = region.terrainString();
if (name.empty())
name = "Unbekannt";
FXString label;
tags.name->setText(label.format("%s (%d,%d)", name.text(), region.x(), region.y()));
label.format("%s von %s (%d,%d)", region.terrainString().text(), name.text(), region.x(), region.y());
// Beschreibung an den Statusleisten-Text anfuegen.
FXString description = region.value(datakey::TYPE_DESCRIPTION);
if (!description.empty())
{
FXchar lastc = description.right(1)[0];
if (lastc != '.' && lastc != '!' && lastc != '?' && lastc != ')')
description += ".";
label += ". " + description;
}
tags.name->setHelpText(label);
// Beschreibung ins Beschreibungsfeld
if (show_description)
tags.desc->setText(description);
else
tags.desc->setText("");
// Beschreibung als Tooltip
for (label.clear(); description.length();)
{
if (label.length())
label += "\n";
label += description.before(' ', 7);
description = description.after(' ', 7);
}
tags.name->setTipText(label);
// select terrain image
FXint terrain = region.terrain();
tags.name->setIcon(terrainIcons[terrain]);
// collect information (Bauern, Silber, Pferde...)
std::list<Info> info;
// Bauern, Silber, Unterhaltung (Unterh), Rekruten, Parteisilber,
// Pferde, Laen, Eisen, Baeume, Schoesslinge
collectData(info, selection.region);
// apply information entries (Bauern, Silber, Pferde...)
setInfo(info);
}
else
{
if (selection.selected & selection.UNKNOWN_REGION)
{
FXString label;
tags.name->setText(label.format("Unbekannt (%d,%d)", selection.sel_x, selection.sel_y));
}
else
tags.name->setText("");
tags.name->setHelpText("");
tags.name->setIcon(terrainIcons[datablock::TERRAIN_UNKNOWN]);
tags.matrixsep->hide();
tags.matrixframe->hide();
clearLabels();
tags.desc->setText("");
}
}
long FXRegionInfos::onMapChange(FXObject*, FXSelector, void* ptr)
{
datafile::SelectionState *state = (datafile::SelectionState*)ptr;
// connected to a datafile list?
if (!files)
return 0;
bool needUpdate = false;
// any data changed, so need to update list?
if (selection.fileChange != state->fileChange)
{
selection.fileChange = state->fileChange;
selection.map = state->map;
selection.activefaction = state->activefaction;
needUpdate = true;
}
if (selection.selChange != state->selChange)
{
selection.selChange = state->selChange;
if ((selection.selected & selection.REGION) != (state->selected & selection.REGION)
|| (selection.selected & selection.REGION && selection.region != state->region))
needUpdate = true; // ignore changes that don't change selected region
if ((selection.selected & selection.UNKNOWN_REGION) != (state->selected & selection.UNKNOWN_REGION))
needUpdate = true; // ignore changes that don't change selected region
if (selection.selected & selection.UNKNOWN_REGION && (selection.sel_x != state->sel_x ||
selection.sel_y != state->sel_y || selection.sel_plane != state->sel_plane))
needUpdate = true; // ignore changes that don't change selected region
if ((selection.selected & selection.MULTIPLE_REGIONS) != (state->selected & selection.MULTIPLE_REGIONS)
|| (selection.selected & selection.MULTIPLE_REGIONS && selection.regionsSelected != state->regionsSelected))
needUpdate = true;
selection.selected = state->selected;
selection.region = state->region;
selection.faction = state->faction;
selection.building = state->building;
selection.ship = state->ship;
selection.unit = state->unit;
selection.sel_x = state->sel_x;
selection.sel_y = state->sel_y;
selection.sel_plane = state->sel_plane;
if (needUpdate) // expensive operation
selection.regionsSelected = state->regionsSelected;
}
if (needUpdate)
updateData();
return 1;
}
long FXRegionInfos::onToggleDescription(FXObject*,FXSelector,void*)
{
show_description = !show_description;
updateData();
return 1;
}
long FXRegionInfos::onUpdateToggleDescription(FXObject* sender,FXSelector,void*)
{
sender->handle(this, FXSEL(SEL_COMMAND, show_description?ID_CHECK:ID_UNCHECK), NULL);
return 1;
}
long FXRegionInfos::onUpdateDescription(FXObject* sender,FXSelector,void*)
{
if (tags.desc->getText().empty())
{
if (tags.desc->shown())
{
tags.desc->hide();
tags.descsep->hide();
tags.matrixframe->recalc();
}
}
else
{
tags.desc->setCursorPos(tags.desc->getText().length());
tags.desc->setVisibleRows(1+std::min(4,tags.desc->getCursorRow()));
if (!tags.desc->shown())
{
tags.desc->show();
tags.descsep->show();
tags.matrixframe->recalc();
}
}
return 1;
}