Repository navigation
Expand file tree
/
Copy pathstatistics.cpp
More file actions
764 lines (648 loc) · 23.5 KB
/
Copy pathstatistics.cpp
File metadata and controls
764 lines (648 loc) · 23.5 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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#include "fxhelper.h"
#include "statistics.h"
#include "FXMenuSeparatorEx.h"
#include <algorithm>
// *********************************************************************************************************
// *** FXStatistics implementation
FXDEFMAP(FXStatistics) MessageMap[]=
{
//________Message_Type_____________________ID_______________Message_Handler_______
FXMAPFUNC(SEL_COMMAND, FXStatistics::ID_FACTIONBOX, FXStatistics::onChangeFaction),
FXMAPFUNC(SEL_COMMAND, FXStatistics::ID_FILTERBOX, FXStatistics::onChangeFilter),
FXMAPFUNC(SEL_UPDATE, FXStatistics::ID_FILTERBOX, FXStatistics::updChangeFilter),
FXMAPFUNC(SEL_RIGHTBUTTONRELEASE, FXStatistics::ID_LIST, FXStatistics::onPopup),
FXMAPFUNC(SEL_COMMAND, FXStatistics::ID_POPUP_CLICKED, FXStatistics::onPopupClicked),
FXMAPFUNC(SEL_COMMAND, FXStatistics::ID_UPDATE, FXStatistics::onMapChange),
FXMAPFUNC(SEL_QUERY_HELP, 0, FXStatistics::onQueryHelp),
};
FXIMPLEMENT(FXStatistics,FXVerticalFrame,MessageMap, ARRAYNUMBER(MessageMap))
FXStatistics::FXStatistics(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
setTarget(tgt);
setSelector(sel);
// set style
setBorderColor(getApp()->getShadowColor());
setFrameStyle(FRAME_LINE);
select.faction = 0; // no faction selected
select.filter = select.FILTER_PERSONS|select.FILTER_ITEMS|select.FILTER_TALENTS;
// create faction combobox
factionBox = new FXComboBox(this, 0, this,ID_FACTIONBOX, COMBOBOX_STATIC|LAYOUT_FILL_X|FRAME_RAISED);
// create filter checkboxes
FXHorizontalFrame *filterboxes = new FXHorizontalFrame(this, 0, 0,0,0,0, 0,0,0,0, 0,0);
opTab = new FXToolBarTab(filterboxes, NULL,0, TOOLBARTAB_HORIZONTAL, 0,0,0,0);
opTab->setTipText("Filteroptionen ein- und ausblenden");
FXMatrix *optionmatrix = new FXMatrix(filterboxes, 3, MATRIX_BY_COLUMNS|PACK_UNIFORM_HEIGHT, 0,0,0,0, 2,2,2,2, 2,2);
FXToggleButton *btn;
btn = new FXToggleButton(optionmatrix, "Personen", "Personen", NULL,NULL, this,ID_FILTERBOX, TOGGLEBUTTON_TOOLBAR|TOGGLEBUTTON_KEEPSTATE|FRAME_RAISED|LAYOUT_FILL_COLUMN|LAYOUT_FILL_X);
btn->setUserData((void*)select.FILTER_PERSONS);
btn = new FXToggleButton(optionmatrix, FXString(L"Gegenst\u00e4nde"), FXString(L"Gegenst\u00e4nde"), NULL,NULL, this,ID_FILTERBOX, TOGGLEBUTTON_TOOLBAR|TOGGLEBUTTON_KEEPSTATE|FRAME_RAISED|LAYOUT_FILL_COLUMN|LAYOUT_FILL_X);
btn->setUserData((void*)select.FILTER_ITEMS);
btn = new FXToggleButton(optionmatrix, "Talente", "Talente", NULL,NULL, this,ID_FILTERBOX, TOGGLEBUTTON_TOOLBAR|TOGGLEBUTTON_KEEPSTATE|FRAME_RAISED|LAYOUT_FILL_COLUMN|LAYOUT_FILL_X);
btn->setUserData((void*)select.FILTER_TALENTS);
btn = new FXToggleButton(optionmatrix, FXString(L"Geb\u00e4ude"), FXString(L"Geb\u00e4ude"), NULL,NULL, this,ID_FILTERBOX, TOGGLEBUTTON_TOOLBAR|TOGGLEBUTTON_KEEPSTATE|FRAME_RAISED|LAYOUT_FILL_COLUMN|LAYOUT_FILL_X);
btn->setUserData((void*)select.FILTER_BUILDINGS);
btn = new FXToggleButton(optionmatrix, "Schiffe", "Schiffe", NULL,NULL, this,ID_FILTERBOX, TOGGLEBUTTON_TOOLBAR|TOGGLEBUTTON_KEEPSTATE|FRAME_RAISED|LAYOUT_FILL_COLUMN|LAYOUT_FILL_X);
btn->setUserData((void*)select.FILTER_SHIPS);
// create list
list = new FXList(this, this,ID_LIST, LIST_BROWSESELECT|LAYOUT_FILL_X|LAYOUT_FILL_Y);
// hide the selection:
list->setSelTextColor(list->getTextColor());
list->setSelBackColor(list->getBackColor());
}
void FXStatistics::create()
{
FXVerticalFrame::create();
}
FXStatistics::~FXStatistics()
{
}
void FXStatistics::setMapFile(std::shared_ptr<datafile>& f)
{
mapFile = f;
}
void FXStatistics::loadState(FXRegistry& reg)
{
// load options
select.filter = reg.readUnsignedEntry("STATISTICS", "FILTER", select.filter);
FXint hidefilters = reg.readUnsignedEntry("STATISTICS", "HIDE_FILTERS", 0);
opTab->collapse(hidefilters != 0);
}
void FXStatistics::saveState(FXRegistry& reg)
{
// save options
reg.writeUnsignedEntry("STATISTICS", "FILTER", select.filter);
reg.writeUnsignedEntry("STATISTICS", "HIDE_FILTERS", opTab->isCollapsed());
}
void FXStatistics::collectData(std::map<FXString, entry> &persons, std::map<FXString, entry> &items,
std::map<std::pair<FXString,int>, entry> &talents, std::map<FXString, entry> &ships,
std::map<FXString, entry> &buildings, const datablock::itor& region)
{
bool unitInFaction = false;
int unitId = 0, personsInUnit = 0;
if (!mapFile) return;
FXint item = factionBox->getCurrentItem();
FXival selected_faction = (FXival)factionBox->getItemData(item);
datablock::itor end = mapFile->blocks().end();
for (datablock::itor block = std::next(region); block != end && block->depth() > region->depth(); block++)
{
if (block->type() == block_type::TYPE_SHIP)
{
int faction = block->valueInt(TYPE_FACTION, 0);
if (selected_faction == 0 || faction == selected_faction)
{
int size = atoi(block->value("Groesse").text());
// <Shiptype>: <Size>
FXString type = block->value(TYPE_TYPE);
if (!type.empty())
ships[type].list.push_back(std::make_pair(block->info(), size));
}
}
else if (block->type() == block_type::TYPE_BUILDING)
{
int faction = block->valueInt(TYPE_FACTION, 0);
if (selected_faction == 0 || faction == selected_faction)
{
int size = atoi(block->value("Groesse").text());
// <Buildingtype>: <Size>
FXString type = block->value(TYPE_TYPE);
if (!type.empty())
buildings[type].list.push_back(std::make_pair(block->info(), size));
}
}
else if (block->type() == block_type::TYPE_UNIT)
{
unitId = block->info();
int faction = datafile::getFactionIdForUnit(*block);
unitInFaction = (faction == selected_faction || selected_faction == 0);
}
if (!unitInFaction)
continue;
// collect statistics from this block
if (block->type() == block_type::TYPE_UNIT)
{
FXString val, type = block->value(TYPE_TYPE);
// <Race>: #
val = block->value(TYPE_NUMBER);
if (!type.empty() && !val.empty())
{
personsInUnit = atoi(val.text());
persons[type].list.push_back(std::make_pair(unitId, personsInUnit));
}
else
personsInUnit = 0;
val = block->value(TYPE_HERO);
if (!val.empty()) {
persons["Helden"].list.push_back(std::make_pair(unitId, personsInUnit));
}
// Aura: #
val = block->value(TYPE_AURA);
if (!val.empty())
items["Aura"].list.push_back(std::make_pair(unitId, atoi(val.text())));
}
// collect statistics from this block
if (block->type() == block_type::TYPE_ITEMS)
{
// <Item>: #
for (datakey::list_type::const_iterator itor = block->data().begin(); itor != block->data().end(); ++itor) {
if (!itor->value().empty())
items[itor->key()].list.push_back(std::make_pair(unitId, atoi(itor->value().text())));
}
}
// collect statistics from this block
if (block->type() == block_type::TYPE_TALENTS)
{
//stats_talents, numberInUnit
// <Talent> <Level>: #
for (datakey::list_type::const_iterator itor = block->data().begin(); itor != block->data().end(); ++itor)
if (!itor->value().empty())
{
FXString talent = itor->value().after(' ');
int level = atoi(talent.text());
talents[std::make_pair(itor->key(), level)].list.push_back(std::make_pair(unitId, personsInUnit));
}
}
}
}
void FXStatistics::updateList()
{
// clear old list
list->clearItems();
entries.clear();
// generate new list
std::map<FXString, entry> persons;
std::map<FXString, entry> items;
std::map<FXString, entry> ships;
std::map<FXString, entry> buildings;
std::map<std::pair<FXString,int>, entry> talents;
// collect data
if (!selection.regionsSelected.empty())
{
for (datablock* block : selection.regionsSelected)
{
datablock::itor region;
if (mapFile->getRegion(region, *block)) {
// collect if selected
collectData(persons, items, talents, ships, buildings, region);
}
}
}
else if (selection.selected & selection.REGION)
{
collectData(persons, items, talents, ships, buildings, selection.region);
}
// add statistics to the list
bool list_persons = (select.filter & select.FILTER_PERSONS) && !persons.empty();
bool list_items = (select.filter & select.FILTER_ITEMS) && !items.empty();
bool list_talents = (select.filter & select.FILTER_TALENTS) && !talents.empty();
bool list_ships = (select.filter & select.FILTER_SHIPS) && !ships.empty();
bool list_buildings = (select.filter & select.FILTER_BUILDINGS) && !buildings.empty();
// add persons to list if not filtered out
if (list_persons)
{
for (std::map<FXString, entry>::iterator itor = persons.begin(); itor != persons.end(); itor++)
{
const entry& en = itor->second;
// calculate sum of units persons
int sum = 0;
for (std::vector<std::pair<int, int> >::iterator unit = itor->second.list.begin(); unit != itor->second.list.end(); unit++)
sum += unit->second;
FXString label;
label.format("%s: %d", itor->first.text(), sum);
int idx = list->appendItem(label);
list->setItemData(idx, (void*)0); // 0: unit, 1: building, 2: ship
// save additional data
entries[idx] = en;
}
if (list_items || list_talents || list_buildings || list_ships)
list->appendItem("--------------------------");
}
// add items (and aura) to list
if (list_items)
{
for (std::map<FXString, entry>::iterator itor = items.begin(); itor != items.end(); itor++)
{
const entry& en = itor->second;
// calculate sum of units items
int sum = 0;
for (std::vector<std::pair<int, int> >::iterator unit = itor->second.list.begin(); unit != itor->second.list.end(); unit++)
sum += unit->second;
FXString label;
label.format("%s: %d", itor->first.text(), sum);
int idx = list->appendItem(label);
list->setItemData(idx, (void*)0); // 0: unit, 1: building, 2: ship
// save additional data
entries[idx] = en;
}
if (list_talents || list_buildings || list_ships)
list->appendItem("--------------------------");
}
// add talents to list
if (list_talents)
{
for (std::map<std::pair<FXString,int>, entry>::iterator itor = talents.begin(); itor != talents.end(); itor++)
{
const entry& en = itor->second;
// calculate sum of units items
int sum = 0;
for (std::vector<std::pair<int, int> >::iterator unit = itor->second.list.begin(); unit != itor->second.list.end(); unit++)
sum += unit->second;
FXString label;
label.format("%s %d: %d", itor->first.first.text(), itor->first.second, sum);
int idx = list->appendItem(label);
list->setItemData(idx, (void*)0); // 0: unit, 1: building, 2: ship
// save additional data
entries[idx] = en;
}
if (list_buildings || list_ships)
list->appendItem("--------------------------");
}
// add castles and buildings
if (list_buildings)
{
for (std::map<FXString, entry>::iterator itor = buildings.begin(); itor != buildings.end(); itor++)
{
const entry& en = itor->second;
// calculate sum of units items
int sum = 0, min_ = -1, max_ = -1;
for (std::vector<std::pair<int, int> >::iterator building = itor->second.list.begin(); building != itor->second.list.end(); building++)
{
sum++;
if (min_ == -1 || building->second < min_)
min_ = building->second;
if (building->second > max_)
max_ = building->second;
}
FXString label;
if (min_==max_)
label.format("%s: %d (%d)", itor->first.text(), sum, min_);
else
label.format("%s: %d (%d-%d)", itor->first.text(), sum, min_, max_);
int idx = list->appendItem(label);
list->setItemData(idx, (void*)1); // 0: unit, 1: building, 2: ship
// save additional data
entries[idx] = en;
}
if (list_ships)
list->appendItem("--------------------------");
}
// add castles and buildings
if (list_ships)
{
for (std::map<FXString, entry>::iterator itor = ships.begin(); itor != ships.end(); itor++)
{
const entry& en = itor->second;
FXString label;
label.format("%s: %zd", itor->first.text(), itor->second.list.size());
int idx = list->appendItem(label);
list->setItemData(idx, (void*)2); // 0: unit, 1: building, 2: ship
// save additional data
entries[idx] = en;
}
}
}
long FXStatistics::onChangeFaction(FXObject*, FXSelector, void*)
{
// the faction to list statistics for
int item = factionBox->getCurrentItem();
if (item >= 0) {
FXival factionId = (FXival)factionBox->getItemData(item);
if (factionId >= 0) {
select.faction = factionId;
}
}
// change statistics in list
updateList();
return 1;
}
long FXStatistics::onChangeFilter(FXObject* sender, FXSelector, void* ptr)
{
if (!sender || !sender->isMemberOf(&FXId::metaClass))
return 0;
FXId *item = (FXId*)sender;
FXival udata = (FXival)item->getUserData();
if (ptr) // new button state
select.filter |= udata; // set filter
else
select.filter &= ~udata; // unset filter
// change statistics in list
updateList();
return 1;
}
long FXStatistics::updChangeFilter(FXObject* sender, FXSelector, void*)
{
if (!sender || !sender->isMemberOf(&FXId::metaClass))
return 0;
FXId *item = (FXId*)sender;
FXival mask = (FXival)item->getUserData();
sender->handle(this, FXSEL(SEL_COMMAND, (select.filter & mask)?ID_CHECK:ID_UNCHECK), NULL);
return 1;
}
long FXStatistics::onPopup(FXObject* sender,FXSelector sel, void* ptr)
{
FXVerticalFrame::onRightBtnRelease(sender, sel, ptr);
// no data, no popup:
if (!mapFile)
return 0;
FXEvent* event = (FXEvent*)ptr;
int itemIdx = list->getCursorItem();
if (itemIdx < 0) {
itemIdx = list->getItemAt(event->click_x, event->click_y);
if (itemIdx < 0) {
return 0;
}
}
FXListItem* item = list->getItem(itemIdx);
if (!item)
return 0;
FXuval entryType = (FXuval)item->getData(); // 0: unit, 1: building, 2: ship
FXMenuPane menu(this);
new FXMenuSeparatorEx(&menu, item->getText());
std::map<int, entry>::iterator en = entries.find(itemIdx);
if (en != entries.end())
{
entry& entry = en->second;
for (std::vector<std::pair<int, int> >::iterator itor = entry.list.begin(); itor != entry.list.end(); itor++)
{
datablock::itor block;
FXString label, name;
if (entryType == 0) {
if (!mapFile->getUnit(block, itor->first)) continue; // get unit
}
else if (entryType == 1) {
if (!mapFile->getBuilding(block, itor->first)) continue; // get building/castle
}
else if (entryType == 2) {
if (!mapFile->getShip(block, itor->first)) continue; // get ship
}
else {
// something done effed up
continue;
}
name = block->getName();
label.format("%s (%s): ", name.text(), block->id().text());
if (entryType > 0) {
label += FXString(L"Gr\u00f6\u00dfe ");
}
label += FXStringVal(itor->second);
FXMenuCommand *menuitem = new FXMenuCommand(&menu, label, NULL, this, ID_POPUP_CLICKED);
menuitem->setUserData((void*)&*block);
}
}
// show popup
menu.create();
menu.popup(NULL, event->root_x,event->root_y);
getApp()->runModalWhileShown(&menu);
return 1;
}
long FXStatistics::onPopupClicked(FXObject* sender,FXSelector, void*)
{
// no data, no popup
if (!mapFile)
return 0;
FXMenuCommand *cmd = static_cast<FXMenuCommand*>(sender);
datablock* dblock = (datablock*)cmd->getUserData();
datablock::itor main = mapFile->blocks().end();
datablock::itor iparent = mapFile->blocks().end();
for (datablock::itor block = mapFile->blocks().begin(); block != mapFile->blocks().end(); block++)
{
// nothing to search for
if (!dblock)
break;
// handle only regions, factions (+alliances), buildings, ships and units
if (block->type() != block_type::TYPE_REGION &&
block->type() != block_type::TYPE_ALLIANCE &&
block->type() != block_type::TYPE_FACTION &&
block->type() != block_type::TYPE_BUILDING &&
block->type() != block_type::TYPE_SHIP &&
block->type() != block_type::TYPE_UNIT)
continue;
if (dblock && dblock == &*block)
{
main = block;
dblock = NULL; // to indicate that block was found.
}
else if (block->type() == block_type::TYPE_REGION)
{
iparent = block;
}
}
if (main == mapFile->blocks().end())
{
main = iparent;
iparent = mapFile->blocks().end();
}
if (main != mapFile->blocks().end())
{
// send new selection to main window
selection.selected = 0;
FXString clipboard;
if (main->type() == block_type::TYPE_REGION)
{
selection.selected = selection.REGION;
selection.region = main;
}
if (main->type() == block_type::TYPE_FACTION)
{
selection.selected = selection.FACTION;
selection.faction = main;
if (iparent != mapFile->blocks().end())
{
selection.selected |= selection.REGION;
selection.region = iparent;
}
}
if (main->type() == block_type::TYPE_BUILDING)
{
selection.selected = selection.BUILDING;
selection.building = main;
clipboard = main->id();
if (iparent != mapFile->blocks().end())
{
selection.selected |= selection.REGION;
selection.region = iparent;
}
}
if (main->type() == block_type::TYPE_SHIP)
{
selection.selected = selection.SHIP;
selection.ship = main;
clipboard = main->id();
if (iparent != mapFile->blocks().end())
{
selection.selected |= selection.REGION;
selection.region = iparent;
}
}
if (main->type() == block_type::TYPE_UNIT)
{
selection.selected = selection.UNIT;
selection.unit = main;
if (iparent != mapFile->blocks().end())
{
selection.selected |= selection.FACTION;
selection.faction = iparent;
}
}
getShell()->handle(this, FXSEL(SEL_COMMAND, ID_UPDATE), &selection);
if (!clipboard.empty()) {
getTarget()->handle(this, FXSEL(SEL_CLIPBOARD_REQUEST, ID_SETSTRINGVALUE), (void *)clipboard.text());
}
return 1;
}
return 0;
}
static FXint SortFactionBox(const FXListItem *lhs, const FXListItem *rhs)
{
const FXString& l = lhs->getText();
const FXString& r = rhs->getText();
if (l > r) return 1;
if (l < r) return -1;
return 0;
}
bool FXStatistics::collectFactionList(std::set<int> &factions, datablock::itor region)
{
if (!mapFile) return false;
// list factions of selected region
bool bFound = false;
FXival OwnFaction = mapFile->getActiveFactionId();
datablock::itor end = mapFile->blocks().end();
for (datablock::itor block = std::next(region); block != end && block->depth() > region->depth(); block++)
{
if (block->type() == block_type::TYPE_UNIT)
{
FXival factionId = datafile::getFactionIdForUnit(*block);
if (factions.find(factionId) == factions.end())
{
factions.insert(factionId);
if (factionId < 0 || OwnFaction == factionId) {
continue;
}
FXint index = appendFaction(factionId);
// select previously selected faction again
if (index >= 0 && select.faction == factionId) {
factionBox->setCurrentItem(index);
bFound = true;
}
}
}
}
return bFound;
}
FXint FXStatistics::prependFaction(FXint factionId)
{
return factionBox->prependItem(mapFile->getFactionName(factionId),
(void *)(uintptr_t)factionId);
}
FXint FXStatistics::appendFaction(FXint factionId)
{
return factionBox->appendItem(mapFile->getFactionName(factionId),
(void *)(uintptr_t)factionId);
}
long FXStatistics::onMapChange(FXObject* /*sender*/, FXSelector, void* ptr)
{
datafile::SelectionState *pstate = (datafile::SelectionState*)ptr;
bool needUpdate = false;
// any data changed, so need to update list?
if (selection.fileChange != pstate->fileChange)
{
selection = *pstate;
needUpdate = true;
}
else if (selection.selChange != pstate->selChange)
{
if ((selection.selected & selection.REGION) != (pstate->selected & selection.REGION)
|| (selection.selected & selection.REGION && selection.region != pstate->region))
{
needUpdate = true; // ignore changes that don't change selected region
}
else if (selection.regionsSelected != pstate->regionsSelected) {
needUpdate = true;
}
selection = *pstate;
if (needUpdate)
selection.regionsSelected = pstate->regionsSelected;
}
if (needUpdate)
{
if ((selection.selected & selection.REGION) || !selection.regionsSelected.empty())
{
// clear Box and create new one
FXString prevSelection;
FXint prev = factionBox->getCurrentItem();
if (prev>=0)
prevSelection = factionBox->getItem(prev);
factionBox->clearItems();
factionBox->setCurrentItem(-1);
std::set<int> factions;
bool bSelection = false;
if (!selection.regionsSelected.empty())
{
for (datablock* block : selection.regionsSelected)
{
datablock::itor region;
if (mapFile->getRegion(region, *block)) {
bSelection |= collectFactionList(factions, region);
}
}
}
else if (selection.selected & selection.REGION)
{
bSelection |= collectFactionList(factions, selection.region);
}
factionBox->setSortFunc(SortFactionBox);
factionBox->sortItems();
FXival AnonFaction = (int)datafile::special_faction::ANONYMOUS;
if (factions.find(AnonFaction) != factions.end()) {
FXint index = prependFaction(AnonFaction);
if (index >= 0 && select.faction == AnonFaction) {
factionBox->setCurrentItem(index);
bSelection = true;
}
}
FXival TraitorFaction = (int)datafile::special_faction::TRAITOR;
if (factions.find(TraitorFaction) != factions.end()) {
FXint index = prependFaction(TraitorFaction);
if (index >= 0 && select.faction == TraitorFaction) {
factionBox->setCurrentItem(index);
bSelection = true;
}
}
FXival OwnFaction = mapFile->getActiveFactionId();
if (factions.find(OwnFaction) != factions.end()) {
FXint index = prependFaction(OwnFaction);
if (index >= 0 && select.faction == OwnFaction) {
factionBox->setCurrentItem(index);
bSelection = true;
}
}
if (prev >= 0) {
prev = factionBox->findItem(prevSelection);
if (prev >= 0) {
factionBox->setCurrentItem(prev);
bSelection = true;
}
}
FXint index = factionBox->appendItem("Alles");
if (!bSelection) {
factionBox->setCurrentItem(index);
}
// set size of combo box list
factionBox->setNumVisible(std::min(factionBox->getNumItems(), 10));
// change statistics in list
updateList();
}
else
{
factionBox->clearItems();
list->clearItems();
entries.clear();
}
}
return 1;
}
long FXStatistics::onQueryHelp(FXObject* /*sender*/, FXSelector, void* /*ptr*/)
{
return 0;
}