The Battle for Wesnoth  1.19.27+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 #include "help/help_impl.hpp"
25 
28 #include "terrain/terrain.hpp"
29 #include "terrain/type_data.hpp"
30 #include "units/types.hpp"
31 #include "units/unit.hpp"
32 
33 #include <boost/logic/tribool.hpp>
34 #include <cassert>
35 
36 namespace help {
37 
38 std::string get_unit_type_help_id(const unit_type& t)
39 {
40  std::string var_id = t.variation_id();
41  if(var_id.empty()) {
42  var_id = t.variation_name();
43  }
44  bool hide_help = t.hide_help();
45  bool use_variation = false;
46 
47  if(!var_id.empty()) {
48  const unit_type* parent = unit_types.find(t.id());
49  assert(parent);
50  if (hide_help) {
51  hide_help = parent->hide_help();
52  } else {
53  use_variation = true;
54  }
55  }
56 
57  if(use_variation) {
58  return hidden_symbol(hide_help) + variation_prefix + t.id() + "_" + var_id;
59  } else {
60  return hidden_symbol(hide_help) + (t.show_variations_in_help() ? ".." : "") + unit_prefix + t.id();
61  }
62 }
63 
65 {
67 }
68 
70 {
72 }
73 
75 {
76  show_help(hidden_symbol(t.hide_help()) + terrain_prefix + t.id());
77 }
78 
79 static void show_with_toplevel(const section& toplevel_sec, const std::string& show_topic)
80 {
81  gui2::dialogs::help_browser::display(toplevel_sec, show_topic);
82 }
83 
84 void show_help(const std::string& show_topic)
85 {
86  const auto manager = help_manager::get_instance();
87  show_with_toplevel(manager->regenerate(), show_topic);
88 }
89 
90 //
91 // Help Manager Implementation
92 //
93 
95 {
96 public:
97  friend class help_manager;
98 
99  /**
100  * Regenerates the cached help topics if necessary.
101  *
102  * @returns the current toplevel section.
103  */
104  const section& regenerate();
105 
106 private:
109  std::size_t last_num_terrain_types_{0};
110 
111  boost::tribool last_debug_state_{boost::indeterminate};
112 
113  /** The default toplevel. */
115 
116  /** All sections and topics not referenced from the default toplevel. */
118 };
119 
121 {
122  // Find all unit_types that have not been constructed yet and fill in the information
123  // needed to create the help topics
125 
126  const auto& enc_units = prefs::get().encountered_units();
127  const auto& enc_terrains = prefs::get().encountered_terrains();
128 
129  // Terrain combinations (base+overlay) can be synthesised ad-hoc as in the editor
130  // describing whatever is under the cursor, which grows this list without touching
131  // encountered_terrains.
132  const std::size_t num_terrain_types = terrain_type_data::get()->list().size();
133 
134  // More units or terrains encountered, more terrain types known, or debug mode toggled
135  if(boost::indeterminate(last_debug_state_)
136  || enc_units.size() != last_num_encountered_units_
137  || enc_terrains.size() != last_num_encountered_terrains_
138  || num_terrain_types != last_num_terrain_types_
140  ) {
141  last_num_encountered_units_ = enc_units.size();
142  last_num_encountered_terrains_ = enc_terrains.size();
143  last_num_terrain_types_ = num_terrain_types;
145 
146  // Update the contents
148  }
149 
150  return toplevel_section_;
151 }
152 
155 {
156 }
157 
158 /** Defined out-of-line so the implementation class is visible. */
159 help_manager::~help_manager() = default;
160 
161 std::shared_ptr<help_manager> help_manager::get_instance()
162 {
163  // Null if no manager instance exists
164  std::shared_ptr instance = singleton_.lock();
165 
166  if(!instance) {
167  instance.reset(new help_manager);
168  singleton_ = instance;
169  }
170 
171  assert(instance);
172  return instance;
173 }
174 
176 {
177  return impl_->regenerate();
178 }
179 
180 } // End namespace help.
double t
Definition: astarsearch.cpp:63
std::size_t last_num_encountered_terrains_
Definition: help.cpp:108
section toplevel_section_
The default toplevel.
Definition: help.cpp:114
std::size_t last_num_encountered_units_
Definition: help.cpp:107
boost::tribool last_debug_state_
Definition: help.cpp:111
section hidden_sections_
All sections and topics not referenced from the default toplevel.
Definition: help.cpp:117
const section & regenerate()
Regenerates the cached help topics if necessary.
Definition: help.cpp:120
The help implementation caches data parsed from the game_config.
Definition: help.hpp:41
~help_manager()
Defined out-of-line so the implementation class is visible.
std::unique_ptr< implementation > impl_
Pointer-to-implementation to reduce include dependencies.
Definition: help.hpp:66
static std::weak_ptr< help_manager > singleton_
Definition: help.hpp:71
static std::shared_ptr< help_manager > get_instance()
Returns the existing help_manager instance, or a newly allocated object otherwise.
Definition: help.cpp:161
const section & regenerate()
Regenerates the cached help topics if necessary.
Definition: help.cpp:175
help_manager()
Private default constructor.
Definition: help.cpp:153
std::set< t_translation::terrain_code > & encountered_terrains()
std::set< std::string > & encountered_units()
static prefs & get()
static terrain_type_data * get()
Definition: type_data.cpp:28
const t_translation::ter_list & list() const
Definition: type_data.cpp:110
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:1253
void build_all(unit_type::BUILD_STATUS status)
Makes sure the all unit_types are built to the specified level.
Definition: types.cpp:1297
A single unit type that the player may recruit.
Definition: types.hpp:43
@ HELP_INDEXED
Definition: types.hpp:77
bool hide_help() const
Definition: types.cpp:582
This class represents a single unit of a specific type.
Definition: unit.hpp:39
const unit_type & type() const
This unit's type, accounting for gender and variation.
Definition: unit.hpp:261
const bool & debug
Definition: game_config.cpp:96
std::string hidden_symbol(bool hidden)
Definition: help_impl.cpp:1403
const std::string unit_prefix
Definition: help_impl.cpp:63
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:38
const std::string variation_prefix
Definition: help_impl.cpp:68
void show_terrain_description(const terrain_type &t)
Definition: help.cpp:74
const std::string terrain_prefix
Definition: help_impl.cpp:64
static void show_with_toplevel(const section &toplevel_sec, const std::string &show_topic)
Definition: help.cpp:79
void show_unit_description(const unit &u)
Definition: help.cpp:64
std::pair< section, section > generate_contents()
Generate the help contents from the configurations given to the manager.
Definition: help_impl.cpp:1348
void show_help(const std::string &show_topic)
Open the help browser.
Definition: help.cpp:84
Contains the implementation details for lexical_cast and shouldn't be used directly.
A section contains topics and sections along with title and ID.
Definition: help_impl.hpp:110
unit_type_data unit_types
Definition: types.cpp:1499