The Battle for Wesnoth  1.19.0-dev
label.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "color.hpp"
19 #include "font/standard_colors.hpp"
20 #include "map/location.hpp"
21 #include "tstring.hpp"
22 
23 #include <SDL2/SDL_rect.h>
24 #include <map>
25 #include <string>
26 
27 class config;
28 class display;
29 class team;
30 class terrain_label;
31 
33 {
34 public:
35  typedef std::map<map_location, terrain_label> label_map;
36  typedef std::map<std::string, label_map> team_label_map;
37 
38  map_labels(const map_labels&);
39  map_labels(const team*);
40 
41  ~map_labels();
42 
44 
45  void write(config& res) const;
46  void read(const config& cfg);
47 
48  const terrain_label* get_label(const map_location& loc, const std::string& team_name) const
49  {
50  return const_cast<map_labels*>(this)->get_label_private(loc, team_name);
51  }
52 
53  // search a team-only label, if fails then try public labels
54  const terrain_label* get_label(const map_location& loc) const;
55 
56  const terrain_label* set_label(const map_location& loc,
57  const t_string& text,
58  const int creator = -1,
59  const std::string& team = "",
60  const color_t color = font::NORMAL_COLOR,
61  const bool visible_in_fog = true,
62  const bool visible_in_shroud = false,
63  const bool immutable = false,
64  const std::string& category = "",
65  const t_string& tooltip = "");
66 
67  bool enabled() const
68  {
69  return enabled_;
70  }
71  void enable(bool is_enabled);
72 
73  void clear(const std::string&, bool force);
74 
75  void recalculate_labels();
76  void recalculate_shroud();
77 
78  bool visible_global_label(const map_location&) const;
79 
80  const std::string& team_name() const;
81  const std::vector<std::string>& all_categories() const;
82 
83  void set_team(const team*);
84 
85  void clear_all();
86 
87 private:
88  template<typename... T>
89  terrain_label* add_label(T&&... args);
90 
91  void clear_map(label_map&, bool);
92 
93  terrain_label* get_label_private(const map_location& loc, const std::string& team_name);
94  // Note: this is not an overload of get_label() so that we do not block
95  // outsiders from calling get_label for a non-const map_labels object.
96 
97  const team* team_;
98 
100  bool enabled_;
101 
102  mutable std::vector<std::string> categories;
103  mutable bool categories_dirty;
104 };
105 
106 /**
107  * To store label data
108  * Class implements logic for rendering
109  */
111 {
112 public:
113  /** Delete copy ctor and assignment ops. */
114  terrain_label(const terrain_label&) = delete;
116 
117  terrain_label(const map_labels& parent,
118  const t_string& text,
119  const int creator,
120  const std::string& team_name,
121  const map_location& loc,
123  const bool visible_in_fog = true,
124  const bool visible_in_shroud = false,
125  const bool immutable = false,
126  const std::string& category = "",
127  const t_string& tooltip = "");
128 
129  terrain_label(const map_labels&, const config&);
130 
131  /** Move ctor. */
133 
134  ~terrain_label();
135 
136  void write(config& res) const;
137  void read(const config& cfg);
138 
139  const t_string& text() const
140  {
141  return text_;
142  }
143 
144  const t_string& tooltip() const
145  {
146  return tooltip_;
147  }
148 
149  int creator() const
150  {
151  return creator_;
152  }
153 
154  const std::string& team_name() const
155  {
156  return team_name_;
157  }
158 
159  const std::string& category() const
160  {
161  return category_;
162  }
163 
164  bool visible_in_fog() const
165  {
166  return visible_in_fog_;
167  }
168 
169  bool visible_in_shroud() const
170  {
171  return visible_in_shroud_;
172  }
173 
174  bool immutable() const
175  {
176  return immutable_;
177  }
178 
179  const map_location& location() const
180  {
181  return loc_;
182  }
183 
184  const color_t& color() const
185  {
186  return color_;
187  }
188 
189  void set_text(const t_string& text)
190  {
191  text_ = text;
192  }
193 
194  void update_info(const t_string&, const int creator, const t_string&, const std::string&, const color_t);
195 
196  void update_info(const t_string& text,
197  const int creator,
198  const t_string& tooltip,
199  const std::string& team_name,
200  const color_t color,
201  const bool visible_in_fog,
202  const bool visible_in_shroud,
203  const bool immutable,
204  const std::string& category);
205 
206  void recalculate();
207  void calculate_shroud();
208 
209 private:
210  void clear();
211  bool hidden() const;
212  bool viewable(const display& disp) const;
213 
214  int handle_;
216 
219 
220  std::string category_;
221  std::string team_name_;
222 
226  int creator_;
227 
229 
232 
233  SDL_Rect get_rect() const;
234 };
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition: display.hpp:81
void write(config &res) const
Definition: label.cpp:80
void clear_map(label_map &, bool)
Definition: label.cpp:226
const std::vector< std::string > & all_categories() const
Definition: label.cpp:287
const team * team_
Definition: label.hpp:97
bool enabled() const
Definition: label.hpp:67
const std::string & team_name() const
Definition: label.cpp:129
bool visible_global_label(const map_location &) const
Returns whether or not a global (non-team) label can be shown at a specified location.
Definition: label.cpp:267
terrain_label * add_label(T &&... args)
Definition: label.cpp:203
const terrain_label * set_label(const map_location &loc, const t_string &text, const int creator=-1, const std::string &team="", const color_t color=font::NORMAL_COLOR, const bool visible_in_fog=true, const bool visible_in_shroud=false, const bool immutable=false, const std::string &category="", const t_string &tooltip="")
Definition: label.cpp:147
const terrain_label * get_label(const map_location &loc, const std::string &team_name) const
Definition: label.hpp:48
void recalculate_shroud()
Definition: label.cpp:278
terrain_label * get_label_private(const map_location &loc, const std::string &team_name)
Definition: label.cpp:103
void clear_all()
Definition: label.cpp:240
map_labels & operator=(const map_labels &)
Definition: label.cpp:70
void enable(bool is_enabled)
Definition: label.cpp:254
void set_team(const team *)
Definition: label.cpp:139
void clear(const std::string &, bool force)
Definition: label.cpp:211
bool enabled_
Definition: label.hpp:100
team_label_map labels_
Definition: label.hpp:99
std::vector< std::string > categories
Definition: label.hpp:102
bool categories_dirty
Definition: label.hpp:103
map_labels(const map_labels &)
Definition: label.cpp:55
void recalculate_labels()
Definition: label.cpp:245
~map_labels()
Definition: label.cpp:65
std::map< std::string, label_map > team_label_map
Definition: label.hpp:36
std::map< map_location, terrain_label > label_map
Definition: label.hpp:35
void read(const config &cfg)
Definition: label.cpp:92
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:74
To store label data Class implements logic for rendering.
Definition: label.hpp:111
~terrain_label()
Definition: label.cpp:380
terrain_label(const terrain_label &)=delete
Delete copy ctor and assignment ops.
bool visible_in_shroud() const
Definition: label.hpp:169
bool visible_in_fog_
Definition: label.hpp:223
t_string tooltip_
Definition: label.hpp:218
bool immutable() const
Definition: label.hpp:174
int creator() const
Definition: label.hpp:149
terrain_label & operator=(const terrain_label &)=delete
std::string category_
Definition: label.hpp:220
color_t color_
Definition: label.hpp:228
void update_info(const t_string &, const int creator, const t_string &, const std::string &, const color_t)
Definition: label.cpp:447
bool visible_in_fog() const
Definition: label.hpp:164
const t_string & tooltip() const
Definition: label.hpp:144
std::string team_name_
Definition: label.hpp:221
int tooltip_handle_
Definition: label.hpp:215
void set_text(const t_string &text)
Definition: label.hpp:189
void write(config &res) const
Definition: label.cpp:432
map_location loc_
Definition: label.hpp:231
int creator_
Definition: label.hpp:226
const map_labels * parent_
Definition: label.hpp:230
SDL_Rect get_rect() const
Definition: label.cpp:501
bool immutable_
Definition: label.hpp:225
bool hidden() const
This is a lightweight test used to see if labels are revealed as a result of unit actions (i....
Definition: label.cpp:573
void clear()
Definition: label.cpp:642
const color_t & color() const
Definition: label.hpp:184
void recalculate()
Definition: label.cpp:525
bool viewable(const display &disp) const
This is a test used to see if we should bother with the overhead of actually creating a label.
Definition: label.cpp:619
const std::string & category() const
Definition: label.hpp:159
bool visible_in_shroud_
Definition: label.hpp:224
const t_string & text() const
Definition: label.hpp:139
const map_location & location() const
Definition: label.hpp:179
void calculate_shroud()
Definition: label.cpp:480
void read(const config &cfg)
Definition: label.cpp:385
const std::string & team_name() const
Definition: label.hpp:154
t_string text_
Definition: label.hpp:217
std::string tooltip
Shown when hovering over an entry in the filter's drop-down list.
Definition: manager.cpp:211
const color_t NORMAL_COLOR
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
Encapsulates the map of the game.
Definition: location.hpp:38