The Battle for Wesnoth  1.19.11+dev
help.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2025
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
27 #include "game_config_manager.hpp"
28 #include "gettext.hpp" // for _
30 #include "gui/widgets/settings.hpp"
31 #include "help/help_impl.hpp" // for hidden_symbol, toplevel, etc
32 #include "log.hpp" // for LOG_STREAM, log_domain
33 #include "terrain/terrain.hpp" // for terrain_type
34 #include "units/unit.hpp" // for unit
35 #include "units/types.hpp" // for unit_type, unit_type_data, etc
36 
37 #include <cassert> // for assert
38 #include <algorithm> // for min
39 #include <vector> // for vector, vector<>::iterator
40 
41 
42 static lg::log_domain log_display("display");
43 #define WRN_DP LOG_STREAM(warn, log_display)
44 
45 static lg::log_domain log_help("help");
46 #define ERR_HELP LOG_STREAM(err, log_help)
47 
48 namespace help {
49 /**
50  * Open a help dialog using a specified toplevel.
51  *
52  * This would allow for complete customization of the contents, although not in a
53  * very easy way. It's used as the internal implementation of the other help*
54  * functions.
55  *
56  *@pre The help_manager must already exist; this is different to the functions
57  * declared in help.hpp, which is why this one's declaration is in the .cpp
58  * file. Because this takes a section as an argument, it wouldn't make sense
59  * for it to call ensure_cache_lifecycle() internally - if the help_manager
60  * doesn't already exist, that would likely destroy the referenced object at
61  * the point that this function exited.
62  */
63 void show_with_toplevel(const section& toplevel, const std::string& show_topic = "");
64 
66 {
67  show_help(hidden_symbol(t.hide_help()) + terrain_prefix + t.id());
68 }
69 
70 std::string get_unit_type_help_id(const unit_type& t)
71 {
72  std::string var_id = t.variation_id();
73  if(var_id.empty()) {
74  var_id = t.variation_name();
75  }
76  bool hide_help = t.hide_help();
77  bool use_variation = false;
78 
79  if(!var_id.empty()) {
80  const unit_type* parent = unit_types.find(t.id());
81  assert(parent);
82  if (hide_help) {
83  hide_help = parent->hide_help();
84  } else {
85  use_variation = true;
86  }
87  }
88 
89  if(use_variation) {
90  return hidden_symbol(hide_help) + variation_prefix + t.id() + "_" + var_id;
91  } else {
92  return hidden_symbol(hide_help) + (t.show_variations_in_help() ? ".." : "") + unit_prefix + t.id();
93  }
94 }
95 
97 {
99 }
100 
102 {
104 }
105 
107 {
108  assert(!game_cfg);
109  assert(cfg);
110  // This is a global rawpointer in the help:: namespace.
111  game_cfg = cfg;
112 }
113 
114 std::unique_ptr<help_manager> ensure_cache_lifecycle()
115 {
116  // The internals of help_manager are that this global raw pointer is
117  // non-null if and only if an instance of help_manager already exists.
118  if(game_cfg)
119  return nullptr;
120  return std::make_unique<help_manager>(&game_config_manager::get()->game_config());
121 }
122 
124 {
125  game_cfg = nullptr;
128  // These last numbers must be reset so that the content is regenerated.
129  // Upon next start.
132 }
133 
134 /**
135  * Open the help browser, show topic with id show_topic.
136  *
137  * If show_topic is the empty string, the default topic will be shown.
138  */
139 void show_help(const std::string& show_topic)
140 {
141  auto cache_lifecycle = ensure_cache_lifecycle();
143 }
144 
145 void init_help() {
146  // Find all unit_types that have not been constructed yet and fill in the information
147  // needed to create the help topics
149 
150  auto& enc_units = prefs::get().encountered_units();
151  auto& enc_terrains = prefs::get().encountered_terrains();
152  if(enc_units.size() != size_t(last_num_encountered_units) ||
153  enc_terrains.size() != size_t(last_num_encountered_terrains) ||
156  // More units or terrains encountered, update the contents.
157  last_num_encountered_units = enc_units.size();
158  last_num_encountered_terrains = enc_terrains.size();
161  }
162 }
163 
164 /**
165  * Open a help dialog using a toplevel other than the default.
166  *
167  * This allows for complete customization of the contents, although not in a
168  * very easy way.
169  */
170 void show_with_toplevel(const section &toplevel_sec, const std::string& show_topic)
171 {
172  gui2::dialogs::help_browser::display(toplevel_sec, show_topic);
173 }
174 
175 } // 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:95
std::string hidden_symbol(bool hidden)
Definition: help_impl.cpp:1425
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:114
void show_help(const std::string &show_topic)
Open the help browser, show topic with id show_topic.
Definition: help.cpp:139
int last_num_encountered_units
Definition: help_impl.cpp:67
const std::string unit_prefix
Definition: help_impl.cpp:84
std::string get_unit_type_help_id(const unit_type &t)
Given a unit type, find the corresponding help topic's id.
Definition: help.cpp:70
const std::string variation_prefix
Definition: help_impl.cpp:89
void show_terrain_description(const terrain_type &t)
Definition: help.cpp:65
void show_with_toplevel(const section &toplevel, const std::string &show_topic="")
Open a help dialog using a specified toplevel.
Definition: help.cpp:170
void generate_contents()
Generate the help contents from the configurations given to the manager.
Definition: help_impl.cpp:1364
const std::string terrain_prefix
Definition: help_impl.cpp:85
void init_help()
Definition: help.cpp:145
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:96
const game_config_view * game_cfg
Definition: help_impl.cpp:61
int last_num_encountered_terrains
Definition: help_impl.cpp:68
help::section hidden_sections
Definition: help_impl.cpp:65
This file contains the settings handling of the widget library.
help_manager(const game_config_view *game_config)
Definition: help.cpp:106
A section contains topics and sections along with title and ID.
Definition: help_impl.hpp:148
unit_type_data unit_types
Definition: types.cpp:1504