Help Viewer: Show topics with generated text

This commit is contained in:
Celtic Minstrel 2017-04-10 02:49:45 -04:00 committed by Celtic Minstrel
parent 38bfb1838a
commit 63ad2f4875
4 changed files with 38 additions and 32 deletions

View file

@ -29,6 +29,8 @@
#else
#endif
#include "help/help.hpp"
#include "help/help_impl.hpp"
namespace gui2::dialogs
{
@ -38,8 +40,8 @@ REGISTER_DIALOG(help_browser)
help_browser::help_browser()
: modal_dialog(window_id())
, initial_topic_("introduction")
, help_cfg_(game_config_manager::get()->game_config().mandatory_child("help"))
{
help::init_help();
}
void help_browser::pre_show(window& window)
@ -51,32 +53,30 @@ void help_browser::pre_show(window& window)
window.keyboard_capture(&topic_tree);
const config toc = help_cfg_.mandatory_child("toplevel");
for(const std::string& section : utils::split(toc["sections"])) {
add_topic(help_cfg_.find_child("section", "id", section).value(), true);
for(const help::section& section : help::default_toplevel.sections) {
add_topic(section.id, section.title, true);
}
for(const std::string& topic : utils::split(toc["topics"])) {
add_topic(help_cfg_.find_child("topic", "id", topic).value(), false);
for(const help::topic& topic : help::default_toplevel.topics) {
add_topic(topic.id, topic.title, false);
}
on_topic_select();
}
void help_browser::add_topic(const config& topic, bool expands, tree_view_node*) {
void help_browser::add_topic(const std::string& topic_id, const std::string& topic_title, bool expands, tree_view_node*) {
tree_view& topic_tree = find_widget<tree_view>(this, "topic_tree", false);
widget_data data;
widget_item item;
item["label"] = topic["title"];
item["label"] = topic_title;
data.emplace("topic_name", item);
item.clear();
item["label"] = expands ? "help/closed_section.png" : "help/topic.png";
item["label"] = expands ? help::closed_section_img : help::topic_img;
data.emplace("topic_icon", item);
topic_tree.add_node("topic", data).set_id(std::string(expands ? "+" : "-") + topic["id"]);
topic_tree.add_node("topic", data).set_id(std::string(expands ? "+" : "-") + topic_id);
}
void help_browser::on_topic_select()
@ -101,17 +101,18 @@ void help_browser::on_topic_select()
topic_id.erase(topic_id.begin());
}
help::section& sec = help::default_toplevel;
auto iter = parsed_pages_.find(topic_id);
if(iter == parsed_pages_.end()) {
auto topic = help_cfg_.find_child("topic", "id", topic_id);
if(!topic.has_value()) {
const help::topic* topic = help::find_topic(sec, topic_id);
if(topic == nullptr) {
return;
}
widget_data data;
widget_item item;
item["label"] = topic["text"];
item["label"] = utils::join(topic->text.parsed_text(), "");
data.emplace("topic_text", item);
parsed_pages_.emplace(topic_id, topic_pages.get_page_count());

View file

@ -34,8 +34,6 @@ public:
private:
std::string initial_topic_;
const config& help_cfg_;
std::map<std::string, int> parsed_pages_;
virtual const std::string& window_id() const override;
@ -44,7 +42,7 @@ private:
void on_topic_select();
void add_topic(const config& topic, bool expands, class tree_view_node* parent = nullptr);
void add_topic(const std::string& topic_id, const std::string& topic_title, bool expands, class tree_view_node* parent = nullptr);
};
} // namespace dialogs

View file

@ -175,6 +175,25 @@ void show_variation_help(const std::string& unit, const std::string &variation,
show_with_toplevel(default_toplevel, hidden_symbol(hidden) + variation_prefix + unit + "_" + variation, xloc, yloc);
}
void init_help() {
// Find all unit_types that have not been constructed yet and fill in the information
// needed to create the help topics
unit_types.build_all(unit_type::HELP_INDEXED);
auto& enc_units = prefs::get().encountered_units();
auto& enc_terrains = prefs::get().encountered_terrains();
if(enc_units.size() != size_t(last_num_encountered_units) ||
enc_terrains.size() != size_t(last_num_encountered_terrains) ||
last_debug_state != game_config::debug ||
last_num_encountered_units < 0) {
// More units or terrains encountered, update the contents.
last_num_encountered_units = enc_units.size();
last_num_encountered_terrains = enc_terrains.size();
last_debug_state = game_config::debug;
generate_contents();
}
}
/**
* Open a help dialog using a toplevel other than the default.
*
@ -212,21 +231,7 @@ void show_with_toplevel(const section &toplevel_sec,
);
f.layout(xloc, yloc, width, height);
// Find all unit_types that have not been constructed yet and fill in the information
// needed to create the help topics
unit_types.build_all(unit_type::HELP_INDEXED);
if (prefs::get().encountered_units().size() != size_t(last_num_encountered_units) ||
prefs::get().encountered_terrains().size() != size_t(last_num_encountered_terrains) ||
last_debug_state != game_config::debug ||
last_num_encountered_units < 0)
{
// More units or terrains encountered, update the contents.
last_num_encountered_units = prefs::get().encountered_units().size();
last_num_encountered_terrains = prefs::get().encountered_terrains().size();
last_debug_state = game_config::debug;
generate_contents();
}
init_help();
try {
help_browser hb(toplevel_sec);
hb.set_location(xloc + left_padding, yloc + top_padding);

View file

@ -56,6 +56,8 @@ struct help_manager {
*/
std::unique_ptr<help_manager> ensure_cache_lifecycle();
void init_help();
/**
* Open the help browser. The help browser will have the topic with id
* show_topic open if it is not the empty string. The default topic