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