The Battle for Wesnoth  1.15.2+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 /// To store label data
106 /// Class implements logic for rendering
108 {
109 public:
110  /** Delete copy ctor and assignment ops. */
111  terrain_label(const terrain_label&) = delete;
112  terrain_label& operator=(const terrain_label&) = delete;
113 
114  terrain_label(const map_labels& parent,
115  const t_string& text,
116  const int creator,
117  const std::string& team_name,
118  const map_location& loc,
119  const color_t color = font::NORMAL_COLOR,
120  const bool visible_in_fog = true,
121  const bool visible_in_shroud = false,
122  const bool immutable = false,
123  const std::string& category = "",
124  const t_string& tooltip = "");
125 
126  terrain_label(const map_labels&, const config&);
127 
128  /** Move ctor. */
130 
131  ~terrain_label();
132 
133  void write(config& res) const;
134  void read(const config& cfg);
135 
136  const t_string& text() const
137  {
138  return text_;
139  }
140 
141  const t_string& tooltip() const
142  {
143  return tooltip_;
144  }
145 
146  int creator() const
147  {
148  return creator_;
149  }
150 
151  const std::string& team_name() const
152  {
153  return team_name_;
154  }
155 
156  const std::string& category() const
157  {
158  return category_;
159  }
160 
161  bool visible_in_fog() const
162  {
163  return visible_in_fog_;
164  }
165 
166  bool visible_in_shroud() const
167  {
168  return visible_in_shroud_;
169  }
170 
171  bool immutable() const
172  {
173  return immutable_;
174  }
175 
176  const map_location& location() const
177  {
178  return loc_;
179  }
180 
181  const color_t& color() const
182  {
183  return color_;
184  }
185 
186  void set_text(const t_string& text)
187  {
188  text_ = text;
189  }
190 
191  void update_info(const t_string&, const int creator, const t_string&, const std::string&, const color_t);
192 
193  void update_info(const t_string& text,
194  const int creator,
195  const t_string& tooltip,
196  const std::string& team_name,
197  const color_t color,
198  const bool visible_in_fog,
199  const bool visible_in_shroud,
200  const bool immutable,
201  const std::string& category);
202 
203  void recalculate();
204  void calculate_shroud();
205 
206 private:
207  void clear();
208  void draw();
209  bool hidden() const;
210  bool viewable(const display& disp) const;
211 
212  int handle_;
214 
217 
218  std::string category_;
219  std::string team_name_;
220 
224  int creator_;
225 
227 
230 
231  SDL_Rect get_rect() const;
232 };
const map_labels * parent_
Definition: label.hpp:228
void recalculate_shroud()
Definition: label.cpp:277
const map_location & location() const
Definition: label.hpp:176
const color_t & color() const
Definition: label.hpp:181
int creator_
Definition: label.hpp:224
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:156
t_string tooltip_
Definition: label.hpp:216
const t_string & tooltip() const
Definition: label.hpp:141
bool visible_in_fog_
Definition: label.hpp:221
const t_string & text() const
Definition: label.hpp:136
const std::string & team_name() const
Definition: label.hpp:151
bool visible_in_shroud() const
Definition: label.hpp:166
int creator() const
Definition: label.hpp:146
team_label_map labels_
Definition: label.hpp:98
map_location loc_
bool visible_in_shroud_
Definition: label.hpp:222
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
bool enabled() const
Definition: label.hpp:66
void set_text(const t_string &text)
Definition: label.hpp:186
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:218
void enable(bool is_enabled)
Definition: label.cpp:253
color_t color_
Definition: label.hpp:226
const terrain_label * get_label(const map_location &loc, const std::string &team_name) const
Definition: label.hpp:47
bool immutable_
Definition: label.hpp:223
bool enabled_
Definition: label.hpp:99
int tooltip_handle_
Definition: label.hpp:213
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:42
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:161
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:219
To store label data Class implements logic for rendering.
Definition: label.hpp:107
terrain_label * add_label(T &&... args)
Definition: label.cpp:202
t_string text_
Definition: label.hpp:215
void clear_all()
Definition: label.cpp:239
bool immutable() const
Definition: label.hpp:171
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
map_location loc_
Definition: label.hpp:229
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