The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
help.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 /**
16  * @file
17  * Routines for showing the help-dialog.
18  */
19 
20 #define GETTEXT_DOMAIN "wesnoth-help"
21 
22 #include "global.hpp"
23 #include "help.hpp"
24 
25 #include "config.hpp" // for config, etc
26 #include "events.hpp" // for raise_draw_event, pump, etc
27 #include "font.hpp" // for relative_size
28 #include "game_preferences.hpp"
29 #include "gettext.hpp" // for _
31 #include "help_browser.hpp" // for help_browser
32 #include "help_impl.hpp" // for hidden_symbol, toplevel, etc
33 #include "key.hpp" // for CKey
34 #include "log.hpp" // for LOG_STREAM, log_domain
35 #include "sdl/utils.hpp" // for surface
36 #include "show_dialog.hpp" // for dialog_frame, etc
37 #include "terrain/terrain.hpp" // for terrain_type
38 #include "units/unit.hpp" // for unit
39 #include "units/types.hpp" // for unit_type, unit_type_data, etc
40 #include "video.hpp" // for CVideo, resize_lock
41 #include "widgets/button.hpp" // for button
42 
43 #include <assert.h> // for assert
44 #include <algorithm> // for min
45 #include <ostream> // for basic_ostream, operator<<, etc
46 #include <vector> // for vector, vector<>::iterator
47 #include <SDL.h>
48 
49 
50 static lg::log_domain log_display("display");
51 #define WRN_DP LOG_STREAM(warn, log_display)
52 
53 static lg::log_domain log_help("help");
54 #define WRN_HP LOG_STREAM(warn, log_help)
55 #define DBG_HP LOG_STREAM(debug, log_help)
56 
57 namespace help {
58 
59 void show_unit_description(CVideo& video, const unit &u)
60 {
62 }
63 
65 {
66  help::show_terrain_help(video, t.id(), t.hide_in_editor() || t.is_combined());
67 }
68 
70 {
71  std::string var_id = t.get_cfg()["variation_id"].str();
72  if (var_id.empty())
73  var_id = t.get_cfg()["variation_name"].str();
74  bool hide_help = t.hide_help();
75  bool use_variation = false;
76  if (!var_id.empty()) {
77  const unit_type *parent = unit_types.find(t.id());
78  assert(parent);
79  if (hide_help) {
80  hide_help = parent->hide_help();
81  } else {
82  use_variation = true;
83  }
84  }
85 
86  if (use_variation)
87  help::show_variation_help(video, t.id(), var_id, hide_help);
88  else
89  help::show_unit_help(video, t.id(), t.show_variations_in_help(), hide_help);
90 }
91 
92 extern config dummy_cfg;
93 
94 help_manager::help_manager(const config *cfg) //, gamemap *_map)
95 {
96  game_cfg = cfg == nullptr ? &dummy_cfg : cfg;
97 // map = _map;
98 }
99 
101 {
102  game_cfg = nullptr;
103 // map = nullptr;
104  toplevel.clear();
106  // These last numbers must be reset so that the content is regenerated.
107  // Upon next start.
110 }
111 
112 /**
113  * Open the help browser, show topic with id show_topic.
114  *
115  * If show_topic is the empty string, the default topic will be shown.
116  */
117 void show_help(CVideo& video, const std::string& show_topic, int xloc, int yloc)
118 {
119  show_help(video, toplevel, show_topic, xloc, yloc);
120 }
121 
122 /**
123  * Open the help browser, show unit with id unit_id.
124  *
125  * If show_topic is the empty string, the default topic will be shown.
126  */
127 void show_unit_help(CVideo& video, const std::string& show_topic, bool has_variations, bool hidden, int xloc, int yloc)
128 {
129  show_help(video, toplevel,
130  hidden_symbol(hidden) + (has_variations ? ".." : "") + unit_prefix + show_topic, xloc, yloc);
131 }
132 
133 /**
134  * Open the help browser, show terrain with id terrain_id.
135  *
136  * If show_topic is the empty string, the default topic will be shown.
137  */
138 void show_terrain_help(CVideo& video, const std::string& show_topic, bool hidden, int xloc, int yloc)
139 {
140  show_help(video, toplevel, hidden_symbol(hidden) + terrain_prefix + show_topic, xloc, yloc);
141 }
142 
143 
144 
145 /**
146  * Open the help browser, show the variation of the unit matching.
147  */
148 void show_variation_help(CVideo& video, const std::string& unit, const std::string &variation, bool hidden, int xloc, int yloc)
149 {
150  show_help(video, toplevel, hidden_symbol(hidden) + variation_prefix + unit + "_" + variation, xloc, yloc);
151 }
152 
153 /**
154  * Open a help dialog using a toplevel other than the default.
155  *
156  * This allows for complete customization of the contents, although not in a
157  * very easy way.
158  */
159 void show_help(CVideo& video, const section &toplevel_sec,
160  const std::string& show_topic,
161  int xloc, int yloc)
162 {
163  const events::event_context dialog_events_context;
164  const gui::dialog_manager manager;
165 
166  CVideo& screen = video;
167  const surface& scr = screen.getSurface();
168 
169  const int width = std::min<int>(font::relative_size(1200), scr->w - font::relative_size(20));
170  const int height = std::min<int>(font::relative_size(850), scr->h - font::relative_size(150));
171  const int left_padding = font::relative_size(10);
172  const int right_padding = font::relative_size(10);
173  const int top_padding = font::relative_size(10);
174  const int bot_padding = font::relative_size(10);
175 
176  // If not both locations were supplied, put the dialog in the middle
177  // of the screen.
178  if (yloc <= -1 || xloc <= -1) {
179  xloc = scr->w / 2 - width / 2;
180  yloc = scr->h / 2 - height / 2;
181  }
182  std::vector<gui::button*> buttons_ptr;
183  gui::button close_button_(video, _("Close"));
184  buttons_ptr.push_back(&close_button_);
185 
186  gui::dialog_frame f(video, _("The Battle for Wesnoth Help"), gui::dialog_frame::default_style,
187  true, &buttons_ptr);
188  f.layout(xloc, yloc, width, height);
189  f.draw();
190 
191  // Find all unit_types that have not been constructed yet and fill in the information
192  // needed to create the help topics
194 
199  // More units or terrains encountered, update the contents.
204  }
205  try {
206  help_browser hb(video, toplevel_sec);
207  hb.set_location(xloc + left_padding, yloc + top_padding);
208  hb.set_width(width - left_padding - right_padding);
209  hb.set_height(height - top_padding - bot_padding);
210  if (show_topic != "") {
211  hb.show_topic(show_topic);
212  }
213  else {
215  }
216  hb.set_dirty(true);
218  CKey key;
219  for (;;) {
220  events::pump();
222  f.draw();
224  if (key[SDLK_ESCAPE]) {
225  // Escape quits from the dialog.
226  return;
227  }
228  for (std::vector<gui::button*>::iterator button_it = buttons_ptr.begin();
229  button_it != buttons_ptr.end(); ++button_it) {
230  if ((*button_it)->pressed()) {
231  // There is only one button, close.
232  return;
233  }
234  }
235  video.flip();
236  CVideo::delay(10);
237  }
238  }
239  catch (parse_error& e) {
240  std::stringstream msg;
241  msg << _("Parse error when parsing help text: ") << "'" << e.message << "'";
242  gui2::show_transient_message(video, "", msg.str());
243  }
244 }
245 
246 } // End namespace help.
void show_help(CVideo &video, const std::string &show_topic, int xloc, int yloc)
Open the help browser, show topic with id show_topic.
Definition: help.cpp:117
const std::string unit_prefix
Definition: help_impl.cpp:88
config dummy_cfg
Definition: help_impl.cpp:74
Definition: unit.hpp:95
static const style default_style
Definition: show_dialog.hpp:69
A section contains topics and sections along with title and ID.
Definition: help_impl.hpp:143
Definition: video.hpp:58
game_display * screen
Definition: resources.cpp:27
int relative_size(int size)
Definition: font.hpp:72
void flip()
Definition: video.cpp:496
help::section toplevel
Definition: help_impl.cpp:66
Thrown when the help system fails to parse something.
Definition: help_impl.hpp:221
bool hide_in_editor() const
Definition: terrain.hpp:40
void set_width(int w)
Definition: widget.cpp:119
void build_all(unit_type::BUILD_STATUS status)
Makes sure the all unit_types are built to the specified level.
Definition: types.cpp:1196
unit_type_data unit_types
Definition: types.cpp:1314
const std::string & id() const
Definition: terrain.hpp:37
void show_transient_message(CVideo &video, const std::string &title, const std::string &message, const std::string &image, const bool message_use_markup, const bool title_use_markup, const bool restore_background)
Shows a transient message to the user.
bool is_combined() const
Definition: terrain.hpp:77
void show_unit_description(CVideo &video, const unit &u)
Definition: help.cpp:59
const std::string terrain_prefix
Definition: help_impl.cpp:89
int last_num_encountered_terrains
Definition: help_impl.cpp:71
const config * game_cfg
Definition: help_impl.cpp:64
GLdouble GLdouble t
Definition: glew.h:1366
-file util.hpp
Definitions for the interface to Wesnoth Markup Language (WML).
surface & getSurface()
Definition: dummy_video.cpp:22
help::section hidden_sections
Definition: help_impl.cpp:68
const unit_type & type() const
The type of the unit (accounting for gender and variation).
Definition: unit.hpp:144
dimension_measurements layout(int x, int y, int w, int h)
help_manager(const config *game_config)
Definition: help.cpp:94
void set_dirty(bool dirty=true)
Definition: widget.cpp:217
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
std::string hidden_symbol(bool hidden)
Definition: help_impl.cpp:1347
std::set< t_translation::t_terrain > & encountered_terrains()
bool show_variations_in_help() const
Whether the unit type has at least one help-visible variation.
Definition: types.cpp:741
static lg::log_domain log_display("display")
void raise_draw_event()
Definition: events.cpp:565
void pump()
Definition: events.cpp:336
void show_terrain_description(CVideo &video, const terrain_type &t)
Definition: help.cpp:64
void raise_process_event()
Definition: events.cpp:539
const std::string variation_prefix
Definition: help_impl.cpp:93
void show_topic(const std::string &topic_id)
Display the topic with the specified identifier.
int last_num_encountered_units
Definition: help_impl.cpp:70
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
std::set< std::string > & encountered_units()
bool hide_help() const
Definition: types.cpp:561
const config & get_cfg() const
Definition: types.hpp:222
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
Definition: glew.h:1220
GLsizeiptr size
Definition: glew.h:1649
void show_variation_help(CVideo &video, const std::string &unit, const std::string &variation, bool hidden, int xloc, int yloc)
Open the help browser, show the variation of the unit matching.
Definition: help.cpp:148
const std::string default_show_topic
Definition: help_impl.cpp:86
void generate_contents()
Generate the help contents from the configurations given to the manager.
Definition: help_impl.cpp:1283
Standard logging facilities (interface).
std::string message
Definition: exceptions.hpp:29
static void delay(unsigned int milliseconds)
Definition: video.cpp:490
bool last_debug_state
Definition: help_impl.cpp:72
virtual void set_location(SDL_Rect const &rect)
Definition: widget.cpp:85
const unit_type * find(const std::string &key, unit_type::BUILD_STATUS status=unit_type::FULL) const
Finds a unit_type by its id() and makes sure it is built to the specified level.
Definition: types.cpp:1155
void show_unit_help(CVideo &video, const std::string &show_topic, bool has_variations, bool hidden, int xloc, int yloc)
Open the help browser, show unit with id unit_id.
Definition: help.cpp:127
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1220
#define e
Definition: help.cpp:57
void show_terrain_help(CVideo &video, const std::string &show_topic, bool hidden, int xloc, int yloc)
Open the help browser, show terrain with id terrain_id.
Definition: help.cpp:138
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
Class that keeps track of all the keys on the keyboard.
Definition: key.hpp:27
void set_height(int h)
Definition: widget.cpp:124
GLsizei const GLcharARB ** string
Definition: glew.h:4503
A help browser widget.
const std::string & id() const
The id for this unit_type.
Definition: types.hpp:115
GLclampf f
Definition: glew.h:3024
static lg::log_domain log_help("help")