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 
31 class map_labels
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 
42 	map_labels& operator=(const map_labels&);
43 
44 	void write(config& res) const;
45 	void read(const config& cfg);
46 
get_label(const map_location & loc,const std::string & team_name) const47 	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 
enabled() const66 	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
107 class terrain_label
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. */
129 	terrain_label(terrain_label&&);
130 
131 	~terrain_label();
132 
133 	void write(config& res) const;
134 	void read(const config& cfg);
135 
text() const136 	const t_string& text() const
137 	{
138 		return text_;
139 	}
140 
tooltip() const141 	const t_string& tooltip() const
142 	{
143 		return tooltip_;
144 	}
145 
creator() const146 	int creator() const
147 	{
148 		return creator_;
149 	}
150 
team_name() const151 	const std::string& team_name() const
152 	{
153 		return team_name_;
154 	}
155 
category() const156 	const std::string& category() const
157 	{
158 		return category_;
159 	}
160 
visible_in_fog() const161 	bool visible_in_fog() const
162 	{
163 		return visible_in_fog_;
164 	}
165 
visible_in_shroud() const166 	bool visible_in_shroud() const
167 	{
168 		return visible_in_shroud_;
169 	}
170 
immutable() const171 	bool immutable() const
172 	{
173 		return immutable_;
174 	}
175 
location() const176 	const map_location& location() const
177 	{
178 		return loc_;
179 	}
180 
color() const181 	const color_t& color() const
182 	{
183 		return color_;
184 	}
185 
set_text(const t_string & text)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_;
213 	int tooltip_handle_;
214 
215 	t_string text_;
216 	t_string tooltip_;
217 
218 	std::string category_;
219 	std::string team_name_;
220 
221 	bool visible_in_fog_;
222 	bool visible_in_shroud_;
223 	bool immutable_;
224 	int creator_;
225 
226 	color_t color_;
227 
228 	const map_labels* parent_;
229 	map_location loc_;
230 
231 	SDL_Rect get_rect() const;
232 };
233