/
label.cpp
656 lines (565 loc) · 16.2 KB
/
label.cpp
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
/*
Copyright (C) 2003 - 2016 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "global.hpp"
#include "display.hpp"
#include "floating_label.hpp"
#include "game_board.hpp"
#include "game_data.hpp"
#include "map/label.hpp"
#include "resources.hpp"
#include "tooltips.hpp"
#include "formula/string_utils.hpp"
//Our definition of map labels being obscured is if the tile is obscured,
//or the tile below is obscured. This is because in the case where the tile
//itself is visible, but the tile below is obscured, the bottom half of the
//tile will still be shrouded, and the label being drawn looks weird.
inline bool is_shrouded(const display& disp, const map_location& loc)
{
return disp.shrouded(loc) || disp.shrouded(map_location(loc.x,loc.y+1));
}
/// Rather simple test for a hex being fogged.
/// This only exists because is_shrouded() does. (The code looks nicer if
/// the test for being fogged looks similar to the test for being shrouded.)
inline bool is_fogged(const display& disp, const map_location& loc)
{
return disp.fogged(loc);
}
map_labels::map_labels(const display &disp, const team *team) :
disp_(disp), team_(team), labels_(), enabled_(true), categories_dirty(true)
{
}
map_labels::map_labels(const map_labels& other) :
disp_(other.disp_), team_(other.team_), labels_(), enabled_(true)
{
config cfg;
other.write(cfg);
read(cfg);
}
map_labels::~map_labels()
{
clear_all();
}
map_labels& map_labels::operator=(const map_labels& other)
{
if(this != &other) {
this->~map_labels();
new (this) map_labels(other);
}
return *this;
}
void map_labels::write(config& res) const
{
for (team_label_map::const_iterator labs = labels_.begin(); labs != labels_.end(); ++labs)
{
for(label_map::const_iterator i = labs->second.begin(); i != labs->second.end(); ++i) {
config item;
i->second->write(item);
res.add_child("label",item);
}
}
}
void map_labels::read(const config &cfg)
{
clear_all();
for (const config &i : cfg.child_range("label"))
{
const map_location loc(i, resources::gamedata);
terrain_label *label = new terrain_label(*this, i);
add_label(loc, label);
}
recalculate_labels();
}
terrain_label* map_labels::get_label_private(const map_location& loc, const std::string& team_name)
{
team_label_map::const_iterator label_map = labels_.find(team_name);
if (label_map != labels_.end()) {
map_labels::label_map::const_iterator itor = label_map->second.find(loc);;
if (itor != label_map->second.end())
return itor->second;
}
return nullptr;
}
const terrain_label* map_labels::get_label(const map_location& loc) const
{
const terrain_label* res = get_label(loc, team_name());
// no such team label, we try global label, except if it's what we just did
// NOTE: This also avoid infinite recursion
if (res == nullptr && team_name() != "") {
return get_label(loc, "");
}
return res;
}
const display& map_labels::disp() const
{
return disp_;
}
const std::string& map_labels::team_name() const
{
if (team_)
{
return team_->team_name();
}
static const std::string empty;
return empty;
}
void map_labels::set_team(const team* team)
{
if ( team_ != team )
{
team_ = team;
categories_dirty = true;
}
}
const terrain_label* map_labels::set_label(const map_location& loc,
const t_string& text,
const int creator,
const std::string& team_name,
const SDL_Color color,
const bool visible_in_fog,
const bool visible_in_shroud,
const bool immutable,
const std::string& category,
const t_string& tooltip )
{
terrain_label* res = nullptr;
// See if there is already a label in this location for this team.
// (We do not use get_label_private() here because we might need
// the label_map as well as the terrain_label.)
team_label_map::iterator current_label_map = labels_.find(team_name);
label_map::iterator current_label;
if ( current_label_map != labels_.end()
&& (current_label = current_label_map->second.find(loc)) != current_label_map->second.end() )
{
// Found old checking if need to erase it
if(text.str().empty())
{
// Erase the old label.
delete current_label->second;
current_label_map->second.erase(current_label);
// Restore the global label in the same spot, if any.
if ( terrain_label* global_label = get_label_private(loc, "") )
global_label->recalculate();
}
else
{
current_label->second->update_info(text, creator, tooltip, team_name, color, visible_in_fog, visible_in_shroud, immutable, category);
res = current_label->second;
}
}
else if(!text.str().empty())
{
// See if we will be replacing a global label.
terrain_label* global_label = get_label_private(loc, "");
// Add the new label.
res = new terrain_label(text,
creator,
team_name,
loc,
*this,
color,
visible_in_fog,
visible_in_shroud,
immutable,
category,
tooltip);
add_label(loc, res);
// Hide the old label.
if ( global_label != nullptr )
global_label->recalculate();
}
categories_dirty = true;
return res;
}
void map_labels::add_label(const map_location &loc, terrain_label *new_label)
{
labels_[new_label->team_name()][loc] = new_label;
categories_dirty = true;
}
void map_labels::clear(const std::string& team_name, bool force)
{
team_label_map::iterator i = labels_.find(team_name);
if (i != labels_.end())
{
clear_map(i->second, force);
}
i = labels_.find("");
if (i != labels_.end())
{
clear_map(i->second, force);
}
categories_dirty = true;
}
void map_labels::clear_map(label_map &m, bool force)
{
label_map::iterator i = m.begin();
while (i != m.end())
{
if (!i->second->immutable() || force) {
delete i->second;
m.erase(i++);
} else ++i;
}
categories_dirty = true;
}
void map_labels::clear_all()
{
for (team_label_map::value_type &m : labels_)
{
clear_map(m.second, true);
}
labels_.clear();
}
void map_labels::recalculate_labels()
{
for (team_label_map::value_type &m : labels_)
{
for (label_map::value_type &l : m.second)
{
l.second->recalculate();
}
}
}
void map_labels::enable(bool is_enabled) {
if (is_enabled != enabled_) {
enabled_ = is_enabled;
recalculate_labels();
}
}
/**
* Returns whether or not a global (non-team) label can be shown at a
* specified location.
* (Global labels are suppressed in favor of team labels.)
*/
bool map_labels::visible_global_label(const map_location& loc) const
{
const team_label_map::const_iterator glabels = labels_.find(team_name());
return glabels == labels_.end()
|| glabels->second.find(loc) == glabels->second.end();
}
void map_labels::recalculate_shroud()
{
for (team_label_map::value_type &m : labels_)
{
for (label_map::value_type &l : m.second)
{
l.second->calculate_shroud();
}
}
}
const std::vector<std::string>& map_labels::all_categories() const {
if(categories_dirty) {
categories_dirty = false;
categories.clear();
categories.push_back("team");
for(size_t i = 1; i <= resources::gameboard->teams().size(); i++) {
categories.push_back("side:" + std::to_string(i));
}
std::set<std::string> unique_cats;
for (const team_label_map::value_type& m : labels_) {
for (const label_map::value_type& l : m.second) {
if(l.second->category().empty()) continue;
unique_cats.insert("cat:" + l.second->category());
}
}
std::copy(unique_cats.begin(), unique_cats.end(), std::back_inserter(categories));
}
return categories;
}
/// creating new label
terrain_label::terrain_label(const t_string& text,
const int creator,
const std::string& team_name,
const map_location& loc,
const map_labels& parent,
const SDL_Color color,
const bool visible_in_fog,
const bool visible_in_shroud,
const bool immutable,
const std::string& category,
const t_string& tooltip ) :
handle_(0),
text_(text),
tooltip_(tooltip),
category_(category),
team_name_(team_name),
visible_in_fog_(visible_in_fog),
visible_in_shroud_(visible_in_shroud),
immutable_(immutable),
creator_(creator),
color_(color),
parent_(&parent),
loc_(loc)
{
draw();
}
/// Load label from config
terrain_label::terrain_label(const map_labels &parent, const config &cfg) :
handle_(0),
text_(),
tooltip_(),
team_name_(),
visible_in_fog_(true),
visible_in_shroud_(false),
immutable_(true),
creator_(-1),
color_(),
parent_(&parent),
loc_()
{
read(cfg);
}
terrain_label::~terrain_label()
{
clear();
}
void terrain_label::read(const config &cfg)
{
const variable_set &vs = *resources::gamedata;
loc_ = map_location(cfg, &vs);
SDL_Color color = font::LABEL_COLOR;
std::string tmp_color = cfg["color"];
text_ = cfg["text"];
tooltip_ = cfg["tooltip"];
team_name_ = cfg["team_name"].str();
visible_in_fog_ = cfg["visible_in_fog"].to_bool(true);
visible_in_shroud_ = cfg["visible_in_shroud"].to_bool();
immutable_ = cfg["immutable"].to_bool(true);
category_ = cfg["category"].str();
int side = cfg["side"].to_int(-1);
if(side >= 0) {
creator_ = side - 1;
} else if(cfg["side"].str() == "current") {
config::attribute_value current_side = vs.get_variable_const("side_number");
if(!current_side.empty()) {
creator_ = current_side.to_int();
}
}
text_ = utils::interpolate_variables_into_tstring(text_, vs); // Not moved to rendering, as that would depend on variables at render-time
team_name_ = utils::interpolate_variables_into_string(team_name_, vs);
tmp_color = utils::interpolate_variables_into_string(tmp_color, vs);
if(!tmp_color.empty()) {
color = string_to_color(tmp_color);
}
color_ = color;
}
void terrain_label::write(config& cfg) const
{
loc_.write(cfg);
cfg["text"] = text();
cfg["tooltip"] = tooltip();
cfg["team_name"] = (this->team_name());
cfg["color"] = cfg_color();
cfg["visible_in_fog"] = visible_in_fog_;
cfg["visible_in_shroud"] = visible_in_shroud_;
cfg["immutable"] = immutable_;
cfg["category"] = category_;
cfg["side"] = creator_ + 1;
}
const t_string& terrain_label::text() const
{
return text_;
}
const t_string& terrain_label::tooltip() const
{
return tooltip_;
}
int terrain_label::creator() const
{
return creator_;
}
const std::string& terrain_label::category() const
{
return category_;
}
const std::string& terrain_label::team_name() const
{
return team_name_;
}
bool terrain_label::visible_in_fog() const
{
return visible_in_fog_;
}
bool terrain_label::visible_in_shroud() const
{
return visible_in_shroud_;
}
bool terrain_label::immutable() const
{
return immutable_;
}
const map_location& terrain_label::location() const
{
return loc_;
}
const SDL_Color& terrain_label::color() const
{
return color_;
}
std::string terrain_label::cfg_color() const
{
std::stringstream buf;
const unsigned int red = static_cast<unsigned int>(color_.r);
const unsigned int green = static_cast<unsigned int>(color_.g);
const unsigned int blue = static_cast<unsigned int>(color_.b);
const unsigned int alpha = static_cast<unsigned int>(color_.a);
buf << red << "," << green << "," << blue << "," << alpha;
return buf.str();
}
void terrain_label::set_text(const t_string& text)
{
text_ = text;
}
void terrain_label::update_info(const t_string& text,
const int creator,
const t_string& tooltip,
const std::string& team_name,
const SDL_Color color)
{
color_ = color;
text_ = text;
tooltip_ = tooltip;
team_name_ = team_name;
creator_ = creator;
draw();
}
void terrain_label::update_info(const t_string& text,
const int creator,
const t_string& tooltip,
const std::string& team_name,
const SDL_Color color,
const bool visible_in_fog,
const bool visible_in_shroud,
const bool immutable,
const std::string& category)
{
visible_in_fog_ = visible_in_fog;
visible_in_shroud_ = visible_in_shroud;
immutable_ = immutable;
category_ = category;
update_info(text, creator, tooltip, team_name, color);
}
void terrain_label::recalculate()
{
draw();
}
void terrain_label::calculate_shroud()
{
if (handle_) {
font::show_floating_label(handle_, !hidden());
}
if (tooltip_.empty() || hidden()) {
tooltips::remove_tooltip(tooltip_handle_);
tooltip_handle_ = 0;
return;
}
//tooltips::update_tooltip(tooltip_handle, get_rect(), tooltip_.str(), "", true);
if (tooltip_handle_)
tooltips::update_tooltip(tooltip_handle_,get_rect(), tooltip_.str(), "", true);
else
tooltip_handle_ = tooltips::add_tooltip(get_rect(), tooltip_.str());
}
SDL_Rect terrain_label::get_rect() const
{
SDL_Rect rect;
int hex_size = parent_->disp().hex_size();
rect.x = parent_->disp().get_location_x(loc_) + hex_size / 4;
rect.y = parent_->disp().get_location_y(loc_);
rect.h = parent_->disp().hex_size();
rect.w = parent_->disp().hex_size() - hex_size/2;
return rect;
}
void terrain_label::draw()
{
if (text_.empty() && tooltip_.empty())
return;
clear();
if ( !viewable(parent_->disp().get_disp_context()) )
return;
const map_location loc_nextx(loc_.x+1,loc_.y);
const map_location loc_nexty(loc_.x,loc_.y+1);
const int xloc = (parent_->disp().get_location_x(loc_) +
parent_->disp().get_location_x(loc_nextx)*2)/3;
const int yloc = parent_->disp().get_location_y(loc_nexty) - font::SIZE_NORMAL;
// If a color is specified don't allow to override it with markup. (prevents faking map labels for example)
// FIXME: @todo Better detect if it's team label and not provided by
// the scenario.
bool use_markup = color_ == font::LABEL_COLOR;
font::floating_label flabel(text_.str());
flabel.set_color(color_);
flabel.set_position(xloc, yloc);
flabel.set_clip_rect(parent_->disp().map_outside_area());
flabel.set_width(font::SIZE_NORMAL * 13);
flabel.set_height(font::SIZE_NORMAL * 4);
flabel.set_scroll_mode(font::ANCHOR_LABEL_MAP);
flabel.use_markup(use_markup);
handle_ = font::add_floating_label(flabel);
calculate_shroud();
}
/**
* This is a lightweight test used to see if labels are revealed as a result
* of unit actions (i.e. fog/shroud clearing). It should not contain any tests
* that are invariant during unit movement (disregarding potential WML events);
* those belong in visible().
*/
bool terrain_label::hidden() const
{
// Respect user's label preferences
std::string category = "cat:" + category_;
std::string creator = "side:" + std::to_string(creator_ + 1);
const std::vector<std::string>& hidden_categories = parent_->disp().get_disp_context().hidden_label_categories();
if(std::find(hidden_categories.begin(), hidden_categories.end(), category) != hidden_categories.end())
return true;
if(creator_ >= 0 && std::find(hidden_categories.begin(), hidden_categories.end(), creator) != hidden_categories.end())
return true;
if(!team_name().empty() && std::find(hidden_categories.begin(), hidden_categories.end(), "team") != hidden_categories.end())
return true;
// Fog can hide some labels.
if ( !visible_in_fog_ && is_fogged(parent_->disp(), loc_) )
return true;
// Shroud can hide some labels.
if ( !visible_in_shroud_ && is_shrouded(parent_->disp(), loc_) )
return true;
return false;
}
/**
* This is a test used to see if we should bother with the overhead of actually
* creating a label. Conditions that can change during unit movement (disregarding
* potential WML events) should not be listed here; they belong in hidden().
*/
bool terrain_label::viewable(const display_context & dc) const
{
if ( !parent_->enabled() )
return false;
// In the editor, all labels are viewable.
if ( dc.teams().empty() )
return true;
// Observers are not privvy to team labels.
const bool can_see_team_labels = !dc.is_observer();
// Global labels are shown unless covered by a team label.
if ( team_name_.empty() )
return !can_see_team_labels || parent_->visible_global_label(loc_);
// Team labels are only shown to members of the team.
return can_see_team_labels && parent_->team_name() == team_name_;
}
void terrain_label::clear()
{
if (handle_)
{
font::remove_floating_label(handle_);
handle_ = 0;
}
if (tooltip_handle_)
{
tooltips::remove_tooltip(tooltip_handle_);
tooltip_handle_ = 0;
}
}