Skip to content

Commit

Permalink
Help Viewer: Show topics with generated text
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Apr 21, 2017
1 parent da22fff commit 300af0b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
28 changes: 14 additions & 14 deletions src/gui/dialogs/help_browser.cpp
Expand Up @@ -36,6 +36,7 @@
#endif

#include "help/help.hpp"
#include "help/help_impl.hpp"

namespace gui2
{
Expand All @@ -46,8 +47,8 @@ REGISTER_DIALOG(help_browser)

help_browser::help_browser()
: initial_topic_("introduction")
, help_cfg_(game_config_manager::get()->game_config().child("help"))
{
help::init_help();
}

void help_browser::pre_show(window& window)
Expand All @@ -58,32 +59,30 @@ void help_browser::pre_show(window& window)

window.keyboard_capture(&topic_tree);

const config toc = help_cfg_.child("toplevel");

for(const std::string& section : utils::split(toc["sections"])) {
add_topic(window, help_cfg_.find_child("section", "id", section), true);
for(const help::section* section : help::default_toplevel.sections) {
add_topic(window, section->id, section->title, true);
}

for(const std::string& topic : utils::split(toc["topics"])) {
add_topic(window, help_cfg_.find_child("topic", "id", topic), false);
for(const help::topic& topic : help::default_toplevel.topics) {
add_topic(window, topic.id, topic.title, false);
}

on_topic_select(window);
}

void help_browser::add_topic(window& window, const config& topic, bool expands, tree_view_node*) {
void help_browser::add_topic(window& window, const std::string& topic_id, const std::string& topic_title, bool expands, tree_view_node*) {
tree_view& topic_tree = find_widget<tree_view>(&window, "topic_tree", false);
std::map<std::string, string_map> data;
string_map 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(window& window)
Expand All @@ -108,17 +107,18 @@ void help_browser::on_topic_select(window& window)
topic_id.erase(topic_id.begin());
}

help::section& sec = help::default_toplevel;
auto iter = parsed_pages_.find(topic_id);
if(iter == parsed_pages_.end()) {
const config& topic = help_cfg_.find_child("topic", "id", topic_id);
if(!topic) {
const help::topic* topic = help::find_topic(sec, topic_id);
if(topic == nullptr) {
return;
}

std::map<std::string, string_map> data;
string_map 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());
Expand Down
4 changes: 1 addition & 3 deletions src/gui/dialogs/help_browser.hpp
Expand Up @@ -41,8 +41,6 @@ class help_browser : public modal_dialog
private:
std::string initial_topic_;

const config& help_cfg_;

std::map<std::string, int> parsed_pages_;

/** Inherited from modal_dialog, implemented by REGISTER_DIALOG. */
Expand All @@ -53,7 +51,7 @@ class help_browser : public modal_dialog

void on_topic_select(window& window);

void add_topic(window& window, const config& topic, bool expands, class tree_view_node* parent = nullptr);
void add_topic(window& window, const std::string& topic_id, const std::string& topic_title, bool expands, class tree_view_node* parent = nullptr);
};

} // namespace dialogs
Expand Down
32 changes: 18 additions & 14 deletions src/help/help.cpp
Expand Up @@ -149,6 +149,23 @@ void show_variation_help(CVideo& video, const std::string& unit, const std::stri
show_help(video, 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);

if(preferences::encountered_units().size() != size_t(last_num_encountered_units) ||
preferences::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 = preferences::encountered_units().size();
last_num_encountered_terrains = preferences::encountered_terrains().size();
last_debug_state = game_config::debug;
generate_contents();
}
}

/**
* Open a help dialog using a toplevel other than the default.
*
Expand Down Expand Up @@ -187,20 +204,7 @@ void show_help(CVideo& video, const section &toplevel_sec,
f.layout(xloc, yloc, width, height);
f.draw();

// 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 (preferences::encountered_units().size() != size_t(last_num_encountered_units) ||
preferences::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 = preferences::encountered_units().size();
last_num_encountered_terrains = preferences::encountered_terrains().size();
last_debug_state = game_config::debug;
generate_contents();
}
init_help();
try {
help_browser hb(video, toplevel_sec);
hb.set_location(xloc + left_padding, yloc + top_padding);
Expand Down
2 changes: 2 additions & 0 deletions src/help/help.hpp
Expand Up @@ -30,6 +30,8 @@ struct help_manager {
};

struct section;
void init_help();

/// Open a help dialog using a toplevel other than the default. This
/// allows for complete customization of the contents, although not in a
/// very easy way.
Expand Down

0 comments on commit 300af0b

Please sign in to comment.