/
help.cpp
3869 lines (3374 loc) · 119 KB
/
help.cpp
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2003 - 2014 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.
*/
/**
* @file
* Routines for showing the help-dialog.
*/
#define GETTEXT_DOMAIN "wesnoth-help"
#include "global.hpp"
#include "asserts.hpp"
#include "help.hpp"
#include "about.hpp"
#include "display.hpp"
#include "display_context.hpp"
#include "exceptions.hpp"
#include "game_preferences.hpp"
#include "gettext.hpp"
#include "gui/dialogs/transient_message.hpp"
#include "language.hpp"
#include "log.hpp"
#include "map.hpp"
#include "marked-up_text.hpp"
#include "resources.hpp"
#include "sound.hpp"
#include "unit.hpp"
#include "unit_helper.hpp"
#include "wml_separators.hpp"
#include "serialization/parser.hpp"
#include "time_of_day.hpp"
#include "tod_manager.hpp"
#include <boost/foreach.hpp>
#include <queue>
static lg::log_domain log_display("display");
#define WRN_DP LOG_STREAM(warn, log_display)
static lg::log_domain log_help("help");
#define WRN_HP LOG_STREAM(warn, log_help)
#define DBG_HP LOG_STREAM(debug, log_help)
namespace help {
help_button::help_button(display& disp, const std::string &help_topic)
: dialog_button(disp.video(), _("Help")), disp_(disp), topic_(help_topic), help_hand_(NULL)
{}
help_button::~help_button() {
delete help_hand_;
}
int help_button::action(gui::dialog_process_info &info) {
if(!topic_.empty()) {
show_help();
info.clear_buttons();
}
return gui::CONTINUE_DIALOG;
}
void help_button::show_help()
{
help::show_help(disp_, topic_);
}
bool help_button::can_execute_command(const hotkey::hotkey_command& cmd, int/*index*/) const
{
hotkey::HOTKEY_COMMAND command = cmd.id;
return (topic_.empty() == false && command == hotkey::HOTKEY_HELP) || command == hotkey::HOTKEY_SCREENSHOT;
}
void help_button::join() {
dialog_button::join();
//wait until we join the event context to start a hotkey handler
delete help_hand_;
help_hand_ = new hotkey::basic_handler(&disp_, this);
}
void help_button::leave() {
dialog_button::leave();
//now kill the hotkey handler
delete help_hand_;
help_hand_ = NULL;
}
/// Generate the help contents from the configurations given to the
/// manager.
static void generate_contents();
struct section;
typedef std::vector<section *> section_list;
/// Generate a topic text on the fly.
class topic_generator
{
unsigned count;
friend class topic_text;
public:
topic_generator(): count(1) {}
virtual std::string operator()() const = 0;
virtual ~topic_generator() {}
};
class text_topic_generator: public topic_generator {
std::string text_;
public:
text_topic_generator(std::string const &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 topic_generator *generator_;
public:
~topic_text();
topic_text():
parsed_text_(),
generator_(NULL)
{
}
topic_text(std::string const &t):
parsed_text_(),
generator_(new text_topic_generator(t))
{
}
explicit topic_text(topic_generator *g):
parsed_text_(),
generator_(g)
{
}
topic_text &operator=(topic_generator *g);
topic_text(topic_text const &t);
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, 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 != NULL && 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 strcoll(t1.title.c_str(), t2.title.c_str()) < 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 strcoll(s1->title.c_str(), s2->title.c_str()) < 0; }
};
class string_less
{
public:
bool operator() (const std::string &s1, const std::string &s2) const {
return strcoll(s1.c_str(), s2.c_str()) < 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); }
};
/// The menu to the left in the help browser, where topics can be
/// navigated through and chosen.
class help_menu : public gui::menu
{
public:
help_menu(CVideo &video, const section &toplevel, int max_height=-1);
int process();
/// Make the topic the currently selected one, and expand all
/// sections that need to be expanded to show it.
void select_topic(const topic &t);
/// If a topic has been chosen, return that topic, otherwise
/// NULL. If one topic is returned, it will not be returned again,
/// if it is not re-chosen.
const topic *chosen_topic();
private:
/// Information about an item that is visible in the menu.
struct visible_item {
visible_item(const section *_sec, const std::string &visible_string);
visible_item(const topic *_t, const std::string &visible_string);
// Invariant, one if these should be NULL. The constructors
// enforce it.
const topic *t;
const section *sec;
std::string visible_string;
bool operator==(const visible_item &vis_item) const;
bool operator==(const section &sec) const;
bool operator==(const topic &t) const;
};
/// Regenerate what items are visible by checking what sections are
/// expanded.
void update_visible_items(const section &top_level, unsigned starting_level=0);
/// Return true if the section is expanded.
bool expanded(const section &sec);
/// Mark a section as expanded. Do not update the visible items or
/// anything.
void expand(const section &sec);
/// Contract (close) a section. That is, mark it as not expanded,
/// visible items are not updated.
void contract(const section &sec);
/// Return the string to use as the prefix for the icon part of the
/// menu-string at the specified level.
std::string indented_icon(const std::string &icon, const unsigned level);
/// Return the string to use as the menu-string for sections at the
/// specified level.
std::string get_string_to_show(const section &sec, const unsigned level);
/// Return the string to use as the menu-string for topics at the
/// specified level.
std::string get_string_to_show(const topic &topic, const unsigned level);
/// Draw the currently visible items.
void display_visible_items();
/// Internal recursive thingie. did_expand will be true if any
/// section was expanded, otherwise untouched.
bool select_topic_internal(const topic &t, const section &sec);
std::vector<visible_item> visible_items_;
const section &toplevel_;
std::set<const section*> expanded_;
surface_restorer restorer_;
topic const *chosen_topic_;
visible_item selected_item_;
};
/// Thrown when the help system fails to parse something.
struct parse_error : public game::error
{
parse_error(const std::string& msg) : game::error(msg) {}
};
/// The area where the content is shown in the help browser.
class help_text_area : public gui::scrollarea
{
public:
help_text_area(CVideo &video, const section &toplevel);
/// Display the topic.
void show_topic(const topic &t);
/// Return the ID that is cross-referenced at the (screen)
/// coordinates x, y. If no cross-reference is there, return the
/// empty string.
std::string ref_at(const int x, const int y);
protected:
virtual void scroll(unsigned int pos);
virtual void set_inner_location(const SDL_Rect& rect);
private:
enum ALIGNMENT {LEFT, MIDDLE, RIGHT, HERE};
/// Convert a string to an alignment. Throw parse_error if
/// unsuccessful.
ALIGNMENT str_to_align(const std::string &s);
/// An item that is displayed in the text area. Contains the surface
/// that should be blitted along with some other information.
struct item {
item(surface surface, int x, int y, const std::string& text="",
const std::string& reference_to="", bool floating=false,
bool box=false, ALIGNMENT alignment=HERE);
item(surface surface, int x, int y,
bool floating, bool box=false, ALIGNMENT=HERE);
/// Relative coordinates of this item.
SDL_Rect rect;
surface surf;
// If this item contains text, this will contain that text.
std::string text;
// If this item contains a cross-reference, this is the id
// of the referenced topic.
std::string ref_to;
// If this item is floating, that is, if things should be filled
// around it.
bool floating;
bool box;
ALIGNMENT align;
};
/// Function object to find an item at the specified coordinates.
class item_at {
public:
item_at(const int x, const int y) : x_(x), y_(y) {}
bool operator()(const item&) const;
private:
const int x_, y_;
};
/// Update the vector with the items of the shown topic, creating
/// surfaces for everything and putting things where they belong.
void set_items();
// Create appropriate items from configs. Items will be added to the
// internal vector. These methods check that the necessary
// attributes are specified.
void handle_ref_cfg(const config &cfg);
void handle_img_cfg(const config &cfg);
void handle_bold_cfg(const config &cfg);
void handle_italic_cfg(const config &cfg);
void handle_header_cfg(const config &cfg);
void handle_jump_cfg(const config &cfg);
void handle_format_cfg(const config &cfg);
void draw_contents();
/// Add an item with text. If ref_dst is something else than the
/// empty string, the text item will be underlined to show that it
/// is a cross-reference. The item will also remember what the
/// reference points to. If font_size is below zero, the default
/// will be used.
void add_text_item(const std::string& text, const std::string& ref_dst="",
bool broken_link = false,
int font_size=-1, bool bold=false, bool italic=false,
SDL_Color color=font::NORMAL_COLOR);
/// Add an image item with the specified attributes.
void add_img_item(const std::string& path, const std::string& alignment, const bool floating,
const bool box);
/// Move the current input point to the next line.
void down_one_line();
/// Adjust the heights of the items in the last row to make it look
/// good .
void adjust_last_row();
/// Return the width that remain on the line the current input point is at.
int get_remaining_width();
/// Return the least x coordinate at which something of the
/// specified height can be drawn at the specified y coordinate
/// without interfering with floating images.
int get_min_x(const int y, const int height=0);
/// Analogous with get_min_x but return the maximum X.
int get_max_x(const int y, const int height=0);
/// Find the lowest y coordinate where a floating img of the
/// specified width and at the specified x coordinate can be
/// placed. Start looking at desired_y and continue downwards. Only
/// check against other floating things, since text and inline
/// images only can be above this place if called correctly.
int get_y_for_floating_img(const int width, const int x, const int desired_y);
/// Add an item to the internal list, update the locations and row
/// height.
void add_item(const item& itm);
std::list<item> items_;
std::list<item *> last_row_;
const section &toplevel_;
topic const *shown_topic_;
const int title_spacing_;
// The current input location when creating items.
std::pair<int, int> curr_loc_;
const unsigned min_row_height_;
unsigned curr_row_height_;
/// The height of all items in total.
int contents_height_;
};
/// A help browser widget.
class help_browser : public gui::widget
{
public:
help_browser(display &disp, const section &toplevel);
void adjust_layout();
/// Display the topic with the specified identifier. Open the menu
/// on the right location and display the topic in the text area.
void show_topic(const std::string &topic_id);
protected:
virtual void update_location(SDL_Rect const &rect);
virtual void process_event();
virtual void handle_event(const SDL_Event &event);
private:
/// Update the current cursor, set it to the reference cursor if
/// mousex, mousey is over a cross-reference, otherwise, set it to
/// the normal cursor.
void update_cursor();
void show_topic(const topic &t, bool save_in_history=true);
/// Move in the topic history. Pop an element from from and insert
/// it in to. Pop at the fronts if the maximum number of elements is
/// exceeded.
void move_in_history(std::deque<const topic *> &from, std::deque<const topic *> &to);
display &disp_;
help_menu menu_;
help_text_area text_area_;
const section &toplevel_;
bool ref_cursor_; // If the cursor currently is the hyperlink cursor.
std::deque<const topic *> back_topics_, forward_topics_;
gui::button back_button_, forward_button_;
topic const *shown_topic_;
};
void show_unit_description(const unit &u)
{
help::show_unit_description(u.type());
}
void show_terrain_description(const terrain_type &t)
{
help::show_terrain_help(*display::get_singleton(), t.id(), t.hide_in_editor() || t.is_combined());
}
void show_unit_description(const unit_type &t)
{
std::string var_id = t.get_cfg()["variation_id"].str();
if (var_id.empty())
var_id = t.get_cfg()["variation_name"].str();
bool hide_help = t.hide_help();
bool use_variation = false;
if (!var_id.empty()) {
const unit_type *parent = unit_types.find(t.id());
assert(parent);
if (hide_help) {
hide_help = parent->hide_help();
} else {
use_variation = true;
}
}
if (use_variation)
help::show_variation_help(*display::get_singleton(), t.id(), var_id, hide_help);
else
help::show_unit_help(*display::get_singleton(), t.id(), !t.variations().empty(), hide_help);
}
// 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.
static void generate_sections(const config *help_cfg, const std::string &generator, section &sec, int level);
static std::vector<topic> generate_topics(const bool sort_topics,const std::string &generator);
static std::string generate_topic_text(const std::string &generator, const config *help_cfg,
const section &sec, const std::vector<topic>& generated_topics);
static std::string generate_about_text();
static std::string generate_contents_links(const std::string& section_name, config const *help_cfg);
static 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
static std::string make_unit_link(const std::string& type_id);
/// return a list of hyperlinks to unit's pages (ordered or not)
static std::vector<std::string> make_unit_links_list(
const std::vector<std::string>& type_id_list, bool ordered = false);
static void generate_races_sections(const config *help_cfg, section &sec, int level);
static void generate_terrain_sections(const config* help_cfg, section &sec, int level);
static std::vector<topic> generate_unit_topics(const bool, const std::string& race);
static 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.
static UNIT_DESCRIPTION_TYPE description_type(const unit_type &type);
static std::vector<topic> generate_ability_topics(const bool);
static std::vector<topic> generate_time_of_day_topics(const bool);
static std::vector<topic> generate_weapon_special_topics(const bool);
static void generate_era_sections(const config *help_cfg, section &sec, int level);
static std::vector<topic> generate_faction_topics(const config &, const bool);
static std::vector<topic> generate_era_topics(const bool, const std::string & era_id);
/// Parse a help config, return the top level section. Return an empty
/// section if cfg is NULL.
static section parse_config(const config *cfg);
/// Recursive function used by parse_config.
static 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.
static 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.
static 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 NULL if none could
/// be found.
static 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 NULL if none could
/// be found.
static 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.
static 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.
static 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.
static SDL_Color string_to_color(const std::string &s);
/// Make a best effort to word wrap s. All parts are less than width.
static std::vector<std::string> split_in_width(const std::string &s, const int font_size, const unsigned width);
static std::string remove_first_space(const std::string& text);
/// Prepend all chars with meaning inside attributes with a backslash.
static std::string escape(const std::string &s)
{
return utils::escape(s, "'\\");
}
/// Return the first word in s, not removing any spaces in the start of
/// it.
static std::string get_first_word(const std::string &s);
} // namespace help
namespace {
const config *game_cfg = NULL;
// The default toplevel.
help::section toplevel;
// All sections and topics not referenced from the default toplevel.
help::section hidden_sections;
int last_num_encountered_units = -1;
int last_num_encountered_terrains = -1;
bool last_debug_state = game_config::debug;
config dummy_cfg;
std::vector<std::string> empty_string_vector;
const int max_section_level = 15;
const int title_size = font::SIZE_LARGE;
const int title2_size = font::SIZE_15;
const int box_width = 2;
const int normal_font_size = font::SIZE_SMALL;
const unsigned max_history = 100;
const std::string topic_img = "help/topic.png";
const std::string closed_section_img = "help/closed_section.png";
const std::string open_section_img = "help/open_section.png";
const std::string indentation_img = "help/indentation.png";
// The topic to open by default when opening the help dialog.
const std::string default_show_topic = "introduction_topic";
const std::string unknown_unit_topic = ".unknown_unit";
const std::string unit_prefix = "unit_";
const std::string terrain_prefix = "terrain_";
const std::string race_prefix = "race_";
const std::string faction_prefix = "faction_";
const std::string era_prefix = "era_";
const std::string variation_prefix = "variation_";
// id starting with '.' are hidden
static std::string hidden_symbol(bool hidden = true) {
return (hidden ? "." : "");
}
static bool is_visible_id(const std::string &id) {
return (id.empty() || id[0] != '.');
}
/// 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.
static bool is_valid_id(const std::string &id) {
if (id == "toplevel") {
return false;
}
if (id.find(unit_prefix) == 0 || id.find(hidden_symbol() + unit_prefix) == 0) {
return false;
}
if (id.find("ability_") == 0) {
return false;
}
if (id.find("weaponspecial_") == 0) {
return false;
}
if (id == "hidden") {
return false;
}
return true;
}
/// Class to be used as a function object when generating the about
/// text. Translate the about dialog formatting to format suitable
/// for the help dialog.
class about_text_formatter {
public:
std::string operator()(const std::string &s) {
if (s.empty()) return s;
// Format + as headers, and the rest as normal text.
if (s[0] == '+')
return " \n<header>text='" + help::escape(s.substr(1)) + "'</header>";
if (s[0] == '-')
return s.substr(1);
return s;
}
};
}
// Helpers for making generation of topics easier.
static 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>";
}
static std::string jump_to(const unsigned pos)
{
std::stringstream ss;
ss << "<jump>to=" << pos << "</jump>";
return ss.str();
}
static std::string jump(const unsigned amount)
{
std::stringstream ss;
ss << "<jump>amount=" << amount << "</jump>";
return ss.str();
}
static std::string bold(const std::string &s)
{
std::stringstream ss;
ss << "<bold>text='" << help::escape(s) << "'</bold>";
return ss.str();
}
typedef std::vector<std::vector<std::pair<std::string, unsigned int > > > 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.
static std::string generate_table(const table_spec &tab, const unsigned int spacing=font::relative_size(20))
{
table_spec::const_iterator row_it;
std::vector<std::pair<std::string, unsigned> >::const_iterator col_it;
unsigned int num_cols = 0;
for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
if (row_it->size() > num_cols) {
num_cols = row_it->size();
}
}
std::vector<unsigned int> col_widths(num_cols, 0);
// Calculate the width of all columns, including spacing.
for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
unsigned int col = 0;
for (col_it = row_it->begin(); col_it != row_it->end(); ++col_it) {
if (col_widths[col] < col_it->second + spacing) {
col_widths[col] = col_it->second + spacing;
}
++col;
}
}
std::vector<unsigned int> col_starts(num_cols);
// Calculate the starting positions of all columns
for (unsigned int i = 0; i < num_cols; ++i) {
unsigned int this_col_start = 0;
for (unsigned int j = 0; j < i; ++j) {
this_col_start += col_widths[j];
}
col_starts[i] = this_col_start;
}
std::stringstream ss;
for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
unsigned int col = 0;
for (col_it = row_it->begin(); col_it != row_it->end(); ++col_it) {
ss << jump_to(col_starts[col]) << col_it->first;
++col;
}
ss << "\n";
}
return ss.str();
}
// Return the width for the image with filename.
static unsigned image_width(const std::string &filename)
{
image::locator loc(filename);
surface surf(image::get_image(loc));
if (surf != NULL) {
return surf->w;
}
return 0;
}
static void push_tab_pair(std::vector<std::pair<std::string, unsigned int> > &v, const std::string &s)
{
v.push_back(std::make_pair(s, font::line_width(s, normal_font_size)));
}
namespace help {
help_manager::help_manager(const config *cfg) //, gamemap *_map)
{
game_cfg = cfg == NULL ? &dummy_cfg : cfg;
// map = _map;
}
void generate_contents()
{
toplevel.clear();
hidden_sections.clear();
if (game_cfg != NULL) {
const config *help_config = &game_cfg->child("help");
if (!*help_config) {
help_config = &dummy_cfg;
}
try {
toplevel = parse_config(help_config);
// Create a config object that contains everything that is
// not referenced from the toplevel element. Read this
// config and save these sections and topics so that they
// can be referenced later on when showing help about
// specified things, but that should not be shown when
// opening the help browser in the default manner.
config hidden_toplevel;
std::stringstream ss;
BOOST_FOREACH(const config §ion, help_config->child_range("section"))
{
const std::string id = section["id"];
if (find_section(toplevel, id) == NULL) {
// This section does not exist referenced from the
// toplevel. Hence, add it to the hidden ones if it
// is not referenced from another section.
if (!section_is_referenced(id, *help_config)) {
if (ss.str() != "") {
ss << ",";
}
ss << id;
}
}
}
hidden_toplevel["sections"] = ss.str();
ss.str("");
BOOST_FOREACH(const config &topic, help_config->child_range("topic"))
{
const std::string id = topic["id"];
if (find_topic(toplevel, id) == NULL) {
if (!topic_is_referenced(id, *help_config)) {
if (ss.str() != "") {
ss << ",";
}
ss << id;
}
}
}
hidden_toplevel["topics"] = ss.str();
config hidden_cfg = *help_config;
// Change the toplevel to our new, custom built one.
hidden_cfg.clear_children("toplevel");
hidden_cfg.add_child("toplevel", hidden_toplevel);
hidden_sections = parse_config(&hidden_cfg);
}
catch (parse_error& e) {
std::stringstream msg;
msg << "Parse error when parsing help text: '" << e.message << "'";
std::cerr << msg.str() << std::endl;
}
}
}
help_manager::~help_manager()
{
game_cfg = NULL;
// map = NULL;
toplevel.clear();
hidden_sections.clear();
// These last numbers must be reset so that the content is regenerated.
// Upon next start.
last_num_encountered_units = -1;
last_num_encountered_terrains = -1;
}
bool section_is_referenced(const std::string §ion_id, const config &cfg)
{
if (const config &toplevel = cfg.child("toplevel"))
{
const std::vector<std::string> toplevel_refs
= utils::quoted_split(toplevel["sections"]);
if (std::find(toplevel_refs.begin(), toplevel_refs.end(), section_id)
!= toplevel_refs.end()) {
return true;
}
}
BOOST_FOREACH(const config §ion, cfg.child_range("section"))
{
const std::vector<std::string> sections_refd
= utils::quoted_split(section["sections"]);
if (std::find(sections_refd.begin(), sections_refd.end(), section_id)
!= sections_refd.end()) {
return true;
}
}
return false;
}
bool topic_is_referenced(const std::string &topic_id, const config &cfg)
{
if (const config &toplevel = cfg.child("toplevel"))
{
const std::vector<std::string> toplevel_refs
= utils::quoted_split(toplevel["topics"]);
if (std::find(toplevel_refs.begin(), toplevel_refs.end(), topic_id)
!= toplevel_refs.end()) {
return true;
}
}
BOOST_FOREACH(const config §ion, cfg.child_range("section"))
{
const std::vector<std::string> topics_refd
= utils::quoted_split(section["topics"]);
if (std::find(topics_refd.begin(), topics_refd.end(), topic_id)
!= topics_refd.end()) {
return true;
}
}
return false;
}
void parse_config_internal(const config *help_cfg, const config *section_cfg,
section &sec, int level)
{
if (level > max_section_level) {
std::cerr << "Maximum section depth has been reached. Maybe circular dependency?"
<< std::endl;
}
else if (section_cfg != NULL) {
const std::vector<std::string> sections = utils::quoted_split((*section_cfg)["sections"]);
sec.level = level;
std::string id = level == 0 ? "toplevel" : (*section_cfg)["id"].str();
if (level != 0) {
if (!is_valid_id(id)) {
std::stringstream ss;
ss << "Invalid ID, used for internal purpose: '" << id << "'";
throw parse_error(ss.str());
}
}
std::string title = level == 0 ? "" : (*section_cfg)["title"].str();
sec.id = id;
sec.title = title;
std::vector<std::string>::const_iterator it;
// Find all child sections.
for (it = sections.begin(); it != sections.end(); ++it) {
if (const config &child_cfg = help_cfg->find_child("section", "id", *it))
{
section child_section;
parse_config_internal(help_cfg, &child_cfg, child_section, level + 1);
sec.add_section(child_section);
}
else {
std::stringstream ss;
ss << "Help-section '" << *it << "' referenced from '"
<< id << "' but could not be found.";
throw parse_error(ss.str());
}
}
generate_sections(help_cfg, (*section_cfg)["sections_generator"], sec, level);
//TODO: harmonize topics/sections sorting
if ((*section_cfg)["sort_sections"] == "yes") {
std::sort(sec.sections.begin(),sec.sections.end(), section_less());
}
bool sort_topics = false;
bool sort_generated = true;
if ((*section_cfg)["sort_topics"] == "yes") {
sort_topics = true;
sort_generated = false;
} else if ((*section_cfg)["sort_topics"] == "no") {
sort_topics = false;