-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
help_impl.hpp
401 lines (336 loc) · 13.7 KB
/
help_impl.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* Note:
* Prior to the creation of this file, all the code associated to the help
* browser existed in a single file src/help.cpp, including all of the
* widgets, topic generators, and implementation details. This totaled
* ~4000 lines of code.
*
* I have split it all up now, so that the gui aspects are separated from
* the content, the "front facing" part which the rest of the code base
* interacts with is in src/help/help.?pp, and the topic generators are
* separated. The remaining "guts" are here. It is implemented in a static
* singleton pattern, using "extern"'d variables, simply for ease of translation
* from the previous state. It would probably be a good idea to rewrite this
* guy as a proper C++ object. Feel free to do so, or to adopt some other
* design pattern.
*/
#pragma once
#include "exceptions.hpp" // for error
#include "font/sdl_ttf.hpp" // for line_width, relative_size
#include "gettext.hpp"
#include <cstring>
#include <list> // for list
#include <memory>
#include <ostream> // for operator<<, stringstream, etc
#include <string> // for string, allocator, etc
#include <utility> // for pair, make_pair
#include <vector> // for vector, etc
#include <SDL.h> // for SDL_Surface
#include <boost/logic/tribool.hpp>
#include <boost/optional.hpp>
class config;
class unit_type;
class terrain_type_data;
typedef std::shared_ptr<terrain_type_data> ter_data_cache;
namespace help { struct section; } // lines 51-51
namespace help {
/// Generate the help contents from the configurations given to the
/// manager.
void generate_contents();
typedef std::vector<section *> section_list;
/// Generate a topic text on the fly.
class topic_generator
{
public:
topic_generator() = default;
virtual std::string operator()() const = 0;
virtual ~topic_generator() {}
};
class text_topic_generator: public topic_generator {
std::string text_;
public:
text_topic_generator(const std::string& t): text_(t) {}
virtual std::string operator()() const { return text_; }
};
/// The text displayed in a topic. It is generated on the fly with the information
/// contained in generator_.
class topic_text
{
mutable std::vector< std::string > parsed_text_;
mutable std::shared_ptr<topic_generator> generator_;
public:
topic_text() = default;
~topic_text() = default;
topic_text(const std::string& t):
parsed_text_(),
generator_(std::make_shared<text_topic_generator>(t))
{
}
explicit topic_text(std::shared_ptr<topic_generator> g):
parsed_text_(),
generator_(g)
{
}
topic_text(const topic_text& t) = default;
topic_text(topic_text&& t) = default;
topic_text& operator=(topic_text&& t) = default;
topic_text& operator=(const topic_text& t) = default;
topic_text& operator=(std::shared_ptr<topic_generator> g);
const std::vector<std::string>& parsed_text() const;
};
/// A topic contains a title, an id and some text.
struct topic
{
topic() :
title(),
id(),
text()
{
}
topic(const std::string &_title, const std::string &_id) :
title(_title),
id(_id),
text()
{
}
topic(const std::string &_title, const std::string &_id, const std::string &_text)
: title(_title), id(_id), text(_text) {}
topic(const std::string &_title, const std::string &_id, std::shared_ptr<topic_generator> g)
: title(_title), id(_id), text(g) {}
/// Two topics are equal if their IDs are equal.
bool operator==(const topic &) const;
bool operator!=(const topic &t) const { return !operator==(t); }
/// Comparison on the ID.
bool operator<(const topic &) const;
std::string title, id;
mutable topic_text text;
};
typedef std::list<topic> topic_list;
/// A section contains topics and sections along with title and ID.
struct section {
section() :
title(""),
id(""),
topics(),
sections(),
level()
{
}
section(const section&);
section& operator=(const section&);
~section();
/// Two sections are equal if their IDs are equal.
bool operator==(const section &) const;
/// Comparison on the ID.
bool operator<(const section &) const;
/// Allocate memory for and add the section.
void add_section(const section &s);
void clear();
std::string title, id;
topic_list topics;
section_list sections;
int level;
};
/// To be used as a function object to locate sections and topics
/// with a specified ID.
class has_id
{
public:
has_id(const std::string &id) : id_(id) {}
bool operator()(const topic &t) { return t.id == id_; }
bool operator()(const section &s) { return s.id == id_; }
bool operator()(const section *s) { return s != nullptr && s->id == id_; }
private:
const std::string id_;
};
/// To be used as a function object when sorting topic lists on the title.
class title_less
{
public:
bool operator()(const topic &t1, const topic &t2) {
return translation::compare(t1.title, t2.title) < 0; }
};
/// To be used as a function object when sorting section lists on the title.
class section_less
{
public:
bool operator()(const section* s1, const section* s2) {
return translation::compare(s1->title, s2->title) < 0; }
};
class string_less
{
public:
bool operator() (const std::string &s1, const std::string &s2) const {
return translation::compare(s1, s2) < 0;
}
};
struct delete_section
{
void operator()(section *s) { delete s; }
};
struct create_section
{
section *operator()(const section *s) { return new section(*s); }
section *operator()(const section &s) { return new section(s); }
};
/// Thrown when the help system fails to parse something.
struct parse_error : public game::error
{
parse_error(const std::string& msg) : game::error(msg) {}
};
// Generator stuff below. Maybe move to a separate file? This one is
// getting crowded. Dunno if much more is needed though so I'll wait and
// see.
/// Dispatch generators to their appropriate functions.
void generate_sections(const config *help_cfg, const std::string &generator, section &sec, int level);
std::vector<topic> generate_topics(const bool sort_topics,const std::string &generator);
std::string generate_topic_text(const std::string &generator, const config *help_cfg,
const section &sec, const std::vector<topic>& generated_topics);
std::string generate_contents_links(const std::string& section_name, config const *help_cfg);
std::string generate_contents_links(const section &sec, const std::vector<topic>& topics);
/// return a hyperlink with the unit's name and pointing to the unit page
/// return empty string if this unit is hidden. If not yet discovered add the (?) suffix
std::string make_unit_link(const std::string& type_id);
/// return a list of hyperlinks to unit's pages (ordered or not)
std::vector<std::string> make_unit_links_list(
const std::vector<std::string>& type_id_list, bool ordered = false);
void generate_races_sections(const config *help_cfg, section &sec, int level);
void generate_terrain_sections(const config* help_cfg, section &sec, int level);
std::vector<topic> generate_unit_topics(const bool, const std::string& race);
void generate_unit_sections(const config *help_cfg, section &sec, int level, const bool, const std::string& race);
enum UNIT_DESCRIPTION_TYPE {FULL_DESCRIPTION, NO_DESCRIPTION, NON_REVEALING_DESCRIPTION};
/// Return the type of description that should be shown for a unit of
/// the given kind. This method is intended to filter out information
/// about units that should not be shown, for example due to not being
/// encountered.
UNIT_DESCRIPTION_TYPE description_type(const unit_type &type);
std::vector<topic> generate_ability_topics(const bool);
std::vector<topic> generate_time_of_day_topics(const bool);
std::vector<topic> generate_weapon_special_topics(const bool);
void generate_era_sections(const config *help_cfg, section &sec, int level);
std::vector<topic> generate_faction_topics(const config &, const bool);
std::vector<topic> generate_era_topics(const bool, const std::string & era_id);
std::vector<topic> generate_trait_topics(const bool);
/// Parse a help config, return the top level section. Return an empty
/// section if cfg is nullptr.
section parse_config(const config *cfg);
/// Recursive function used by parse_config.
void parse_config_internal(const config *help_cfg, const config *section_cfg,
section &sec, int level=0);
/// Return true if the section with id section_id is referenced from
/// another section in the config, or the toplevel.
bool section_is_referenced(const std::string §ion_id, const config &cfg);
/// Return true if the topic with id topic_id is referenced from
/// another section in the config, or the toplevel.
bool topic_is_referenced(const std::string &topic_id, const config &cfg);
/// Search for the topic with the specified identifier in the section
/// and its subsections. Return the found topic, or nullptr if none could
/// be found.
const topic *find_topic(const section &sec, const std::string &id);
/// Search for the section with the specified identifier in the section
/// and its subsections. Return the found section or nullptr if none could
/// be found.
const section *find_section(const section &sec, const std::string &id);
/// Parse a text string. Return a vector with the different parts of the
/// text. Each markup item is a separate part while the text between
/// markups are separate parts.
std::vector<std::string> parse_text(const std::string &text);
/// Convert the contents to wml attributes, surrounded within
/// [element_name]...[/element_name]. Return the resulting WML.
std::string convert_to_wml(const std::string &element_name, const std::string &contents);
/// Return the color the string represents. Return font::NORMAL_COLOR if
/// the string is empty or can't be matched against any other color.
color_t string_to_color(const std::string &s);
/// Make a best effort to word wrap s. All parts are less than width.
std::vector<std::string> split_in_width(const std::string &s, const int font_size, const unsigned width);
std::string remove_first_space(const std::string& text);
/// Prepend all chars with meaning inside attributes with a backslash.
std::string escape(const std::string &s);
/// Return the first word in s, not removing any spaces in the start of
/// it.
std::string get_first_word(const std::string &s);
/// Load the appropriate terrain types data to use
ter_data_cache load_terrain_types_data();
extern const config *game_cfg;
// The default toplevel.
extern help::section default_toplevel;
// All sections and topics not referenced from the default toplevel.
extern help::section hidden_sections;
extern int last_num_encountered_units;
extern int last_num_encountered_terrains;
extern boost::tribool last_debug_state;
extern std::vector<std::string> empty_string_vector;
extern const int max_section_level;
extern const int title_size;
extern const int title2_size;
extern const int box_width;
extern const int normal_font_size;
extern const unsigned max_history;
extern const std::string topic_img;
extern const std::string closed_section_img;
extern const std::string open_section_img;
// The topic to open by default when opening the help dialog.
extern const std::string default_show_topic;
extern const std::string unknown_unit_topic;
extern const std::string unit_prefix;
extern const std::string terrain_prefix;
extern const std::string race_prefix;
extern const std::string faction_prefix;
extern const std::string era_prefix;
extern const std::string variation_prefix;
extern const std::string ability_prefix;
// id starting with '.' are hidden
std::string hidden_symbol(bool hidden = true);
bool is_visible_id(const std::string &id);
/// Return true if the id is valid for user defined topics and
/// sections. Some IDs are special, such as toplevel and may not be
/// be defined in the config.
bool is_valid_id(const std::string &id);
// Helpers for making generation of topics easier.
inline std::string make_link(const std::string& text, const std::string& dst)
{
// some sorting done on list of links may rely on the fact that text is first
return "<ref>text='" + help::escape(text) + "' dst='" + help::escape(dst) + "'</ref>";
}
inline std::string jump_to(const unsigned pos)
{
std::stringstream ss;
ss << "<jump>to=" << pos << "</jump>";
return ss.str();
}
inline std::string jump(const unsigned amount)
{
std::stringstream ss;
ss << "<jump>amount=" << amount << "</jump>";
return ss.str();
}
inline std::string bold(const std::string &s)
{
std::stringstream ss;
ss << "<bold>text='" << help::escape(s) << "'</bold>";
return ss.str();
}
// A string to be displayed and its width.
typedef std::pair< std::string, unsigned > item;
typedef std::vector<std::vector<help::item> > table_spec;
// Create a table using the table specs. Return markup with jumps
// that create a table. The table spec contains a vector with
// vectors with pairs. The pairs are the markup string that should
// be in a cell, and the width of that cell.
std::string generate_table(const table_spec &tab, const unsigned int spacing=font::relative_size(20));
// Return the width for the image with filename.
unsigned image_width(const std::string &filename);
// Add to the vector v an help::item for the string s, preceded by the given image if any.
void push_tab_pair(std::vector<help::item> &v, const std::string &s, const boost::optional<std::string> &image = {}, unsigned padding = 0);
} // end namespace help