Skip to content

Commit

Permalink
Help Browser: Actually show the correct topics with the correct icons
Browse files Browse the repository at this point in the history
This also fixes the lag when opening help and makes the topic text appear.
(The topic text is not parsed at the moment, though.)
  • Loading branch information
CelticMinstrel committed Apr 10, 2017
1 parent 57efa5c commit 16c022b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 26 deletions.
1 change: 0 additions & 1 deletion data/gui/window/help_browser.cfg
Expand Up @@ -33,7 +33,6 @@

[image]
id = "topic_icon"
label = "help/topic.png"
linked_group = "images"
[/image]
[/column]
Expand Down
75 changes: 51 additions & 24 deletions src/gui/dialogs/help_browser.cpp
Expand Up @@ -53,54 +53,81 @@ help_browser::help_browser()
void help_browser::pre_show(window& window)
{
tree_view& topic_tree = find_widget<tree_view>(&window, "topic_tree", false);
multi_page& topic_pages = find_widget<multi_page>(&window, "topic_text_pages", false);

topic_tree.set_selection_change_callback(std::bind(&help_browser::on_topic_select, this, std::ref(window)));

window.keyboard_capture(&topic_tree);

unsigned id = 0;

for(const auto& topic : help_cfg_.child_range("topic")) {
std::map<std::string, string_map> data;
string_map item;
const config toc = help_cfg_.child("toplevel");

item["label"] = topic["title"];
data.emplace("topic_name", item);
for(const std::string& section : utils::split(toc["sections"])) {
add_topic(window, help_cfg_.find_child("section", "id", section), true);
}

topic_tree.add_node("topic", data).set_id(std::to_string(id));
for(const std::string& topic : utils::split(toc["topics"])) {
add_topic(window, help_cfg_.find_child("topic", "id", topic), false);
}

// FIXME: maybe using a multi page isn't a good idea here... :| it causes massive lag when opening.
item.clear();
data.clear();
on_topic_select(window);
}

item["label"] = topic["text"].empty() ? "" : topic["text"].str();
data.emplace("topic_text", item);
void help_browser::add_topic(window& window, const config& topic, 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;

topic_pages.add_page(data);
item["label"] = topic["title"];
data.emplace("topic_name", item);

++id;
}
item.clear();
item["label"] = expands ? "help/closed_section.png" : "help/topic.png";
data.emplace("topic_icon", item);

on_topic_select(window);
topic_tree.add_node("topic", data).set_id(std::string(expands ? "+" : "-") + topic["id"]);
}

void help_browser::on_topic_select(window& window)
{
tree_view& tree = find_widget<tree_view>(&window, "topic_tree", false);
multi_page& topic_pages = find_widget<multi_page>(&window, "topic_text_pages", false);
tree_view& topic_tree = find_widget<tree_view>(&window, "topic_tree", false);

if(tree.empty()) {
if(topic_tree.empty()) {
return;
}

assert(tree.selected_item());
assert(topic_tree.selected_item());
std::string topic_id = topic_tree.selected_item()->id();

if(tree.selected_item()->id() == "") {
if(topic_id == "") {
return;
}

const unsigned topic_i = lexical_cast<unsigned>(tree.selected_item()->id());
find_widget<multi_page>(&window, "topic_text_pages", false).select_page(topic_i);
if(topic_id[0] == '+') {
topic_id.replace(topic_id.begin(), topic_id.begin() + 1, 2, '.');
} else {
topic_id.erase(topic_id.begin());
}

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) {
return;
}

std::map<std::string, string_map> data;
string_map item;

item["label"] = topic["text"];
data.emplace("topic_text", item);

parsed_pages_.emplace(topic_id, topic_pages.get_page_count());
topic_pages.add_page(data);
window.invalidate_layout();
}

const unsigned topic_i = parsed_pages_.at(topic_id);
topic_pages.select_page(topic_i);

}

Expand Down
6 changes: 6 additions & 0 deletions src/gui/dialogs/help_browser.hpp
Expand Up @@ -17,6 +17,8 @@

#include "gui/dialogs/modal_dialog.hpp"

#include <map>

class config;
class CVideo;

Expand All @@ -41,13 +43,17 @@ class help_browser : public modal_dialog

const config& help_cfg_;

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

/** Inherited from modal_dialog, implemented by REGISTER_DIALOG. */
virtual const std::string& window_id() const;

/** Inherited from modal_dialog. */
void pre_show(window& window);

void on_topic_select(window& window);

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

} // namespace dialogs
Expand Down
3 changes: 2 additions & 1 deletion src/gui/dialogs/title_screen.cpp
Expand Up @@ -298,11 +298,12 @@ void title_screen::pre_show(window& win)
// Help
//
register_button(win, "help", hotkey::HOTKEY_HELP, [](window& w) {
help::help_manager help_manager(&game_config_manager::get()->game_config());

if(gui2::new_widgets) {
gui2::dialogs::help_browser::display(w.video());
}

help::help_manager help_manager(&game_config_manager::get()->game_config());
help::show_help(w.video());
});

Expand Down

0 comments on commit 16c022b

Please sign in to comment.