The Battle for Wesnoth  1.17.0-dev
label.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
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 
99  team_label_map labels_;
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;
115  terrain_label& operator=(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,
122  const color_t color = font::NORMAL_COLOR,
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  void draw();
212  bool hidden() const;
213  bool viewable(const display& disp) const;
214 
215  int handle_;
217 
220 
221  std::string category_;
222  std::string team_name_;
223 
227  int creator_;
228 
230 
233 
234  SDL_Rect get_rect() const;
235 };
const map_labels * parent_
Definition: label.hpp:231
void recalculate_shroud()
Definition: label.cpp:278
const map_location & location() const
Definition: label.hpp:179
const color_t & color() const
Definition: label.hpp:184
int creator_
Definition: label.hpp:227
map_labels & operator=(const map_labels &)
Definition: label.cpp:70
const std::vector< std::string > & all_categories() const
Definition: label.cpp:287
const std::string & category() const
Definition: label.hpp:159
t_string tooltip_
Definition: label.hpp:219
const t_string & tooltip() const
Definition: label.hpp:144
bool visible_in_fog_
Definition: label.hpp:224
const t_string & text() const
Definition: label.hpp:139
const std::string & team_name() const
Definition: label.hpp:154
bool visible_in_shroud() const
Definition: label.hpp:169
int creator() const
Definition: label.hpp:149
team_label_map labels_
Definition: label.hpp:99
map_location loc_
bool visible_in_shroud_
Definition: label.hpp:225
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:72
bool enabled() const
Definition: label.hpp:67
void set_text(const t_string &text)
Definition: label.hpp:189
std::map< map_location, terrain_label > label_map
Definition: label.hpp:35
std::vector< std::string > categories
Definition: label.hpp:102
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
void clear(const std::string &, bool force)
Definition: label.cpp:211
map_labels(const map_labels &)
Definition: label.cpp:55
const team * team_
Definition: label.hpp:97
void write(config &res) const
Definition: label.cpp:80
std::string category_
Definition: label.hpp:221
void enable(bool is_enabled)
Definition: label.cpp:254
color_t color_
Definition: label.hpp:229
const terrain_label * get_label(const map_location &loc, const std::string &team_name) const
Definition: label.hpp:48
bool immutable_
Definition: label.hpp:226
bool enabled_
Definition: label.hpp:100
int tooltip_handle_
Definition: label.hpp:216
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
const color_t NORMAL_COLOR
Encapsulates the map of the game.
Definition: location.hpp:38
void set_team(const team *)
Definition: label.cpp:139
~map_labels()
Definition: label.cpp:65
void recalculate_labels()
Definition: label.cpp:245
bool visible_in_fog() const
Definition: label.hpp:164
void clear_map(label_map &, bool)
Definition: label.cpp:226
const std::string & team_name() const
Definition: label.cpp:129
std::map< std::string, label_map > team_label_map
Definition: label.hpp:36
void read(const config &cfg)
Definition: label.cpp:92
std::string team_name_
Definition: label.hpp:222
To store label data Class implements logic for rendering.
Definition: label.hpp:110
terrain_label * add_label(T &&... args)
Definition: label.cpp:203
t_string text_
Definition: label.hpp:218
void clear_all()
Definition: label.cpp:240
bool immutable() const
Definition: label.hpp:174
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
map_location loc_
Definition: label.hpp:232
terrain_label * get_label_private(const map_location &loc, const std::string &team_name)
Definition: label.cpp:103
bool categories_dirty
Definition: label.hpp:103
std::string tooltip
Shown when hovering over an entry in the filter&#39;s drop-down list.
Definition: manager.cpp:219