The Battle for Wesnoth  1.19.17+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 "units/types.hpp"
30 #include "units/unit.hpp"
31 
32 #include <boost/logic/tribool.hpp>
33 #include <cassert>
34 
35 namespace help {
36 
37 std::string get_unit_type_help_id(const unit_type& t)
38 {
39  std::string var_id = t.variation_id();
40  if(var_id.empty()) {
41  var_id = t.variation_name();
42  }
43  bool hide_help = t.hide_help();
44  bool use_variation = false;
45 
46  if(!var_id.empty()) {
47  const unit_type* parent = unit_types.find(t.id());
48  assert(parent);
49  if (hide_help) {
50  hide_help = parent->hide_help();
51  } else {
52  use_variation = true;
53  }
54  }
55 
56  if(use_variation) {
57  return hidden_symbol(hide_help) + variation_prefix + t.id() + "_" + var_id;
58  } else {
59  return hidden_symbol(hide_help) + (t.show_variations_in_help() ? ".." : "") + unit_prefix + t.id();
60  }
61 }
62 
64 {
66 }
67 
69 {
71 }
72 
74 {
75  show_help(hidden_symbol(t.hide_help()) + terrain_prefix + t.id());
76 }
77 
78 void show_with_toplevel(const section& toplevel_sec, const std::string& show_topic)
79 {
80  gui2::dialogs::help_browser::display(toplevel_sec, show_topic);
81 }
82 
83 void show_help(const std::string& show_topic)
84 {
85  const auto manager = help_manager::get_instance();
86  show_with_toplevel(manager->regenerate(), show_topic);
87 }
88 
89 //
90 // Help Manager Implementation
91 //
92 
94 {
95 public:
96  friend class help_manager;
97 
98  /**
99  * Regenerates the cached help topics if necessary.
100  *
101  * @returns the current toplevel section.
102  */
103  const section& regenerate();
104 
105 private:
108 
109  boost::tribool last_debug_state_{boost::indeterminate};
110 
111  /** The default toplevel. */
113 
114  /** All sections and topics not referenced from the default toplevel. */
116 };
117 
119 {
120  // Find all unit_types that have not been constructed yet and fill in the information
121  // needed to create the help topics
123 
124  const auto& enc_units = prefs::get().encountered_units();
125  const auto& enc_terrains = prefs::get().encountered_terrains();
126 
127  // More units or terrains encountered, or debug mode toggled
128  if(boost::indeterminate(last_debug_state_)
129  || enc_units.size() != last_num_encountered_units_
130  || enc_terrains.size() != last_num_encountered_terrains_
132  ) {
133  last_num_encountered_units_ = enc_units.size();
134  last_num_encountered_terrains_ = enc_terrains.size();
136 
137  // Update the contents
139  }
140 
141  return toplevel_section_;
142 }
143 
146 {
147 }
148 
149 /** Defined out-of-line so the implementation class is visible. */
150 help_manager::~help_manager() = default;
151 
152 std::shared_ptr<help_manager> help_manager::get_instance()
153 {
154  // Null if no manager instance exists
155  std::shared_ptr instance = singleton_.lock();
156 
157  if(!instance) {
158  instance.reset(new help_manager);
159  singleton_ = instance;
160  }
161 
162  assert(instance);
163  return instance;
164 }
165 
167 {
168  return impl_->regenerate();
169 }
170 
171 } // End namespace help.
double t
Definition: astarsearch.cpp:63
std::size_t last_num_encountered_terrains_
Definition: help.cpp:107
section toplevel_section_
The default toplevel.
Definition: help.cpp:112
std::size_t last_num_encountered_units_
Definition: help.cpp:106
boost::tribool last_debug_state_
Definition: help.cpp:109
section hidden_sections_
All sections and topics not referenced from the default toplevel.
Definition: help.cpp:115
const section & regenerate()
Regenerates the cached help topics if necessary.
Definition: help.cpp:118
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:152
const section & regenerate()
Regenerates the cached help topics if necessary.
Definition: help.cpp:166
help_manager()
Private default constructor.
Definition: help.cpp:144
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:1272
void build_all(unit_type::BUILD_STATUS status)
Makes sure the all unit_types are built to the specified level.
Definition: types.cpp:1316
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:581
This class represents a single unit of a specific type.
Definition: unit.hpp:136
const unit_type & type() const
This unit's type, accounting for gender and variation.
Definition: unit.hpp:358
const bool & debug
Definition: game_config.cpp:95
std::string hidden_symbol(bool hidden)
Definition: help_impl.cpp:1276
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:37
const std::string variation_prefix
Definition: help_impl.cpp:68
void show_terrain_description(const terrain_type &t)
Definition: help.cpp:73
void show_with_toplevel(const section &toplevel_sec, const std::string &show_topic)
Definition: help.cpp:78
const std::string terrain_prefix
Definition: help_impl.cpp:64
void show_unit_description(const unit &u)
Definition: help.cpp:63
std::pair< section, section > generate_contents()
Generate the help contents from the configurations given to the manager.
Definition: help_impl.cpp:1221
void show_help(const std::string &show_topic)
Open the help browser.
Definition: help.cpp:83
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:1513