The Battle for Wesnoth  1.19.7+dev
help.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file
18  * Routines for showing the help-dialog.
19  */
20 
21 #define GETTEXT_DOMAIN "wesnoth-help"
22 
23 #include "help/help.hpp"
24 
25 #include "config.hpp" // for config, etc
26 #include "events.hpp" // for draw, pump, etc
27 #include "font/constants.hpp" // for relative_size
29 #include "game_config_manager.hpp"
30 #include "gettext.hpp" // for _
32 #include "gui/widgets/settings.hpp"
33 #include "help/help_impl.hpp" // for hidden_symbol, toplevel, etc
34 #include "key.hpp" // for CKey
35 #include "log.hpp" // for LOG_STREAM, log_domain
36 #include "terrain/terrain.hpp" // for terrain_type
37 #include "units/unit.hpp" // for unit
38 #include "units/types.hpp" // for unit_type, unit_type_data, etc
39 #include "video.hpp" // for game_canvas_size
40 #include "widgets/button.hpp" // for button
41 
42 #include <cassert> // for assert
43 #include <algorithm> // for min
44 #include <vector> // for vector, vector<>::iterator
45 
46 
47 static lg::log_domain log_display("display");
48 #define WRN_DP LOG_STREAM(warn, log_display)
49 
50 static lg::log_domain log_help("help");
51 #define ERR_HELP LOG_STREAM(err, log_help)
52 
53 namespace help {
54 /**
55  * Open a help dialog using a specified toplevel.
56  *
57  * This would allow for complete customization of the contents, although not in a
58  * very easy way. It's used as the internal implementation of the other help*
59  * functions.
60  *
61  *@pre The help_manager must already exist; this is different to the functions
62  * declared in help.hpp, which is why this one's declaration is in the .cpp
63  * file. Because this takes a section as an argument, it wouldn't make sense
64  * for it to call ensure_cache_lifecycle() internally - if the help_manager
65  * doesn't already exist, that would likely destroy the referenced object at
66  * the point that this function exited.
67  */
68 void show_with_toplevel(const section &toplevel, const std::string& show_topic="");
69 
70 
72 {
73  auto cache_lifecycle = ensure_cache_lifecycle();
75 }
76 
78 {
79  auto cache_lifecycle = ensure_cache_lifecycle();
80  help::show_terrain_help(t.id(), t.hide_help());
81 }
82 
84 {
85  auto cache_lifecycle = ensure_cache_lifecycle();
86  std::string var_id = t.get_cfg()["variation_id"].str();
87  if (var_id.empty())
88  var_id = t.get_cfg()["variation_name"].str();
89  bool hide_help = t.hide_help();
90  bool use_variation = false;
91  if (!var_id.empty()) {
92  const unit_type *parent = unit_types.find(t.id());
93  assert(parent);
94  if (hide_help) {
95  hide_help = parent->hide_help();
96  } else {
97  use_variation = true;
98  }
99  }
100 
101  if (use_variation)
102  help::show_variation_help(t.id(), var_id, hide_help);
103  else
104  help::show_unit_help(t.id(), t.show_variations_in_help(), hide_help);
105 }
106 
108 {
109  assert(!game_cfg);
110  assert(cfg);
111  // This is a global rawpointer in the help:: namespace.
112  game_cfg = cfg;
113 }
114 
115 std::unique_ptr<help_manager> ensure_cache_lifecycle()
116 {
117  // The internals of help_manager are that this global raw pointer is
118  // non-null if and only if an instance of help_manager already exists.
119  if(game_cfg)
120  return nullptr;
121  return std::make_unique<help_manager>(&game_config_manager::get()->game_config());
122 }
123 
125 {
126  game_cfg = nullptr;
129  // These last numbers must be reset so that the content is regenerated.
130  // Upon next start.
133 }
134 
135 /**
136  * Open the help browser, show topic with id show_topic.
137  *
138  * If show_topic is the empty string, the default topic will be shown.
139  */
140 void show_help(const std::string& show_topic)
141 {
142  auto cache_lifecycle = ensure_cache_lifecycle();
144 }
145 
146 /**
147  * Open the help browser, show unit with id unit_id.
148  *
149  * If show_topic is the empty string, the default topic will be shown.
150  */
151 void show_unit_help(const std::string& show_topic, bool has_variations, bool hidden)
152 {
153  auto cache_lifecycle = ensure_cache_lifecycle();
155  hidden_symbol(hidden) + (has_variations ? ".." : "") + unit_prefix + show_topic);
156 }
157 
158 /**
159  * Open the help browser, show terrain with id terrain_id.
160  *
161  * If show_topic is the empty string, the default topic will be shown.
162  */
163 void show_terrain_help(const std::string& show_topic, bool hidden)
164 {
165  auto cache_lifecycle = ensure_cache_lifecycle();
167 }
168 
169 /**
170  * Open the help browser, show the variation of the unit matching.
171  */
172 void show_variation_help(const std::string& unit, const std::string &variation, bool hidden)
173 {
174  auto cache_lifecycle = ensure_cache_lifecycle();
176 }
177 
178 void init_help() {
179  // Find all unit_types that have not been constructed yet and fill in the information
180  // needed to create the help topics
182 
183  auto& enc_units = prefs::get().encountered_units();
184  auto& enc_terrains = prefs::get().encountered_terrains();
185  if(enc_units.size() != size_t(last_num_encountered_units) ||
186  enc_terrains.size() != size_t(last_num_encountered_terrains) ||
189  // More units or terrains encountered, update the contents.
190  last_num_encountered_units = enc_units.size();
191  last_num_encountered_terrains = enc_terrains.size();
194  }
195 }
196 
197 /**
198  * Open a help dialog using a toplevel other than the default.
199  *
200  * This allows for complete customization of the contents, although not in a
201  * very easy way.
202  */
203 void show_with_toplevel(const section &toplevel_sec, const std::string& show_topic)
204 {
205  gui2::dialogs::help_browser::display(toplevel_sec, show_topic);
206 }
207 
208 } // End namespace help.
double t
Definition: astarsearch.cpp:63
static game_config_manager * get()
A class grating read only view to a vector of config objects, viewed as one config with all children ...
std::set< t_translation::terrain_code > & encountered_terrains()
std::set< std::string > & encountered_units()
static prefs & get()
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:1265
void build_all(unit_type::BUILD_STATUS status)
Makes sure the all unit_types are built to the specified level.
Definition: types.cpp:1307
A single unit type that the player may recruit.
Definition: types.hpp:43
@ HELP_INDEXED
Definition: types.hpp:74
bool hide_help() const
Definition: types.cpp:626
This class represents a single unit of a specific type.
Definition: unit.hpp:133
Definitions for the interface to Wesnoth Markup Language (WML).
const unit_type & type() const
This unit's type, accounting for gender and variation.
Definition: unit.hpp:355
static lg::log_domain log_display("display")
static lg::log_domain log_help("help")
Standard logging facilities (interface).
Game configuration data as global variables.
Definition: build_info.cpp:61
const bool & debug
Definition: game_config.cpp:94
std::string hidden_symbol(bool hidden)
Definition: help_impl.cpp:1427
std::unique_ptr< help_manager > ensure_cache_lifecycle()
Helper function for any of the show_help functions to control the cache's lifecycle; can also be used...
Definition: help.cpp:115
void show_help(const std::string &show_topic)
Open the help browser, show topic with id show_topic.
Definition: help.cpp:140
int last_num_encountered_units
Definition: help_impl.cpp:67
const std::string unit_prefix
Definition: help_impl.cpp:84
const std::string variation_prefix
Definition: help_impl.cpp:89
void show_terrain_description(const terrain_type &t)
Definition: help.cpp:77
void show_terrain_help(const std::string &show_topic, bool hidden)
Open the help browser, show terrain with id terrain_id.
Definition: help.cpp:163
void show_with_toplevel(const section &toplevel, const std::string &show_topic="")
Open a help dialog using a specified toplevel.
Definition: help.cpp:203
void generate_contents()
Generate the help contents from the configurations given to the manager.
Definition: help_impl.cpp:1366
const std::string terrain_prefix
Definition: help_impl.cpp:85
void init_help()
Definition: help.cpp:178
boost::tribool last_debug_state
Definition: help_impl.cpp:69
help::section default_toplevel
Definition: help_impl.cpp:63
void show_unit_description(const unit &u)
Definition: help.cpp:71
const game_config_view * game_cfg
Definition: help_impl.cpp:61
int last_num_encountered_terrains
Definition: help_impl.cpp:68
void show_unit_help(const std::string &show_topic, bool has_variations, bool hidden)
Open the help browser, show unit with id unit_id.
Definition: help.cpp:151
help::section hidden_sections
Definition: help_impl.cpp:65
void show_variation_help(const std::string &unit, const std::string &variation, bool hidden)
Open the help browser, show the variation of the unit matching.
Definition: help.cpp:172
This file contains object "key", which is used to store information about keys while annotation parsi...
This file contains the settings handling of the widget library.
help_manager(const game_config_view *game_config)
Definition: help.cpp:107
A section contains topics and sections along with title and ID.
Definition: help_impl.hpp:148
unit_type_data unit_types
Definition: types.cpp:1500