Skip to content

Commit

Permalink
Help: massive refactor and cleanup of the help backend
Browse files Browse the repository at this point in the history
This basically splits all the stuff in help/help_impl.*pp into multiple files by their
function, and refactors the entire workflow into a proper object-oriented interface in
modern C++.

There are still a features missing (such as hidden section parsing in the manager) that
I'll get back to.

Few incidental changes and fixes:
* Terrain topics now sorted alphabetically.
* Help text now small
* Fixed wrong toggle button id in browser. This is what was making it impossible to expand
  any sections.

The GUI2 help browser is now back in working order, inasmuch as you can view all sections'
and topics' text (save units').
  • Loading branch information
Vultraz authored and CelticMinstrel committed Oct 24, 2018
1 parent 0dc8869 commit 81388ad
Show file tree
Hide file tree
Showing 26 changed files with 3,137 additions and 988 deletions.
4 changes: 2 additions & 2 deletions data/gui/window/help_browser.cfg
Expand Up @@ -67,7 +67,7 @@

{_GUI_NODE "section" (
[toggle_button]
id = "tree_view_node_icon"
id = "tree_view_node_toggle"
definition = "help_section_toggle"
linked_group = "images"
[/toggle_button]
Expand Down Expand Up @@ -188,7 +188,7 @@
vertical_grow = true

[scroll_label]
definition = "default"
definition = "default_small"
id = "topic_text"
use_markup = true
[/scroll_label]
Expand Down
10 changes: 8 additions & 2 deletions source_lists/wesnoth
Expand Up @@ -264,9 +264,15 @@ gui/widgets/viewport.cpp
gui/widgets/widget.cpp
gui/widgets/widget_helpers.cpp
halo.cpp
help/constants.cpp
help/help.cpp
help/help_impl.cpp
help/help_topic_generators.cpp
help/manager.cpp
help/section.cpp
help/section_generators.cpp
help/topic.cpp
help/topic_generators.cpp
help/topic_text_generators.cpp
help/utils.cpp
hotkey/hotkey_handler.cpp
hotkey/hotkey_handler_mp.cpp
hotkey/hotkey_handler_sp.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/editor/controller/editor_controller.cpp
Expand Up @@ -159,6 +159,8 @@ void editor_controller::init_music(const config& game_config)

editor_controller::~editor_controller()
{
help::reset();

resources::tod_manager = nullptr;
resources::filter_con = nullptr;

Expand Down
100 changes: 35 additions & 65 deletions src/gui/dialogs/help_browser.cpp
Expand Up @@ -16,47 +16,29 @@

#include "gui/dialogs/help_browser.hpp"

#include "game_config_manager.hpp"
#include "font/pango/escape.hpp"
#include "font/pango/hyperlink.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/image.hpp"
#include "gui/widgets/multi_page.hpp"
#include "gui/widgets/scroll_label.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/text_box.hpp"
#include "gui/widgets/tree_view.hpp"
#include "gui/widgets/tree_view_node.hpp"
#include "gui/widgets/window.hpp"

#ifdef GUI2_EXPERIMENTAL_LISTBOX
#include "gui/widgets/list.hpp"
#else
#include "gui/widgets/listbox.hpp"
#endif

#include "help/help.hpp"
#include "help/help_impl.hpp"
#include "font/pango/escape.hpp"
#include "font/pango/hyperlink.hpp"
#include "help/section.hpp"
#include "help/topic.hpp"
#include "help/utils.hpp"

namespace gui2
{
namespace dialogs
{

REGISTER_DIALOG(help_browser)

help_browser::help_browser(const help::section& toplevel, const std::string& initial)
: initial_topic_(initial.empty() ? help::default_show_topic : initial)
: initial_topic_(initial)
, toplevel_(toplevel)
{
if(initial_topic_.compare(0, 2, "..") == 0) {
initial_topic_.replace(0, 2, "+");
} else {
initial_topic_.insert(0, "-");
}
help::init_help();
}

void help_browser::pre_show(window& window)
Expand All @@ -69,8 +51,11 @@ void help_browser::pre_show(window& window)
next_button.set_visible(widget::visibility::hidden);
back_button.set_visible(widget::visibility::hidden);

connect_signal_mouse_left_click(back_button, std::bind(&help_browser::on_history_navigate, this, std::ref(window), true));
connect_signal_mouse_left_click(next_button, std::bind(&help_browser::on_history_navigate, this, std::ref(window), false));
connect_signal_mouse_left_click(back_button,
std::bind(&help_browser::on_history_navigate, this, std::ref(window), true));

connect_signal_mouse_left_click(next_button,
std::bind(&help_browser::on_history_navigate, this, std::ref(window), false));

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

Expand All @@ -86,21 +71,21 @@ void help_browser::pre_show(window& window)

void help_browser::add_topics_for_section(const help::section& parent_section, tree_view_node& parent_node)
{
for(const help::section* section : parent_section.sections) {
for(const auto& section : parent_section.sections()) {
tree_view_node& section_node = add_topic(section->id, section->title, true, parent_node);

add_topics_for_section(*section, section_node);
}

for(const help::topic& topic : parent_section.topics) {
if(topic.id.compare(0,2,"..") != 0) {
for(const help::topic& topic : parent_section.topics()) {
if(topic.id.compare(0, 2, "..") != 0) {
add_topic(topic.id, topic.title, false, parent_node);
}
}
}

tree_view_node& help_browser::add_topic(const std::string& topic_id, const std::string& topic_title,
bool expands, tree_view_node& parent)
tree_view_node& help_browser::add_topic(
const std::string& topic_id, const std::string& topic_title, bool expands, tree_view_node& parent)
{
std::map<std::string, string_map> data;
string_map item;
Expand All @@ -119,12 +104,14 @@ static std::string format_help_text(const config& cfg)
std::stringstream ss;
for(auto& item : cfg.all_children_range()) {
if(item.key == "text") {
ss << font::escape_text(item.cfg["text"]);
//ss << font::escape_text(item.cfg["text"]);
ss << item.cfg["text"];
} else if(item.key == "ref") {
if(item.cfg["dst"].empty()) {
std::stringstream msg;
msg << "Ref markup must have dst attribute. Please submit a bug"
" report if you have not modified the game files yourself. Erroneous config: " << cfg;
" report if you have not modified the game files yourself. Erroneous config: "
<< cfg;
throw help::parse_error(msg.str());
};
// TODO: Get the proper link shade from somewhere
Expand All @@ -141,31 +128,9 @@ static std::string format_help_text(const config& cfg)
throw help::parse_error("Jump markup must have either a to or an amount attribute.");
}
ss << '\t';
} else if(item.key == "format") {
if(item.cfg["text"].empty()) {
throw help::parse_error("Header markup must have text attribute.");
}

ss << "<span";
if(item.cfg.has_attribute("font_size")) {
ss << " size='" << item.cfg["font_size"].to_int(font::SIZE_NORMAL) << "'";
}

if(item.cfg.has_attribute("color")) {
ss << " color='" << help::string_to_color(item.cfg["color"]).to_hex_string() << "'";
}

if(item.cfg["bold"]) {
ss << " font_weight='bold'";
}

if(item.cfg["italic"]) {
ss << " font_style='italic'";
}

ss << '>' << font::escape_text(item.cfg["text"]) << "</span>";
}
}

return ss.str();
}

Expand Down Expand Up @@ -197,15 +162,15 @@ void help_browser::on_topic_select(window& window)

auto iter = parsed_pages_.find(topic_id);
if(iter == parsed_pages_.end()) {
const help::topic* topic = help::find_topic(sec, topic_id);
const help::topic* topic = sec.find_topic(topic_id);
if(topic == nullptr) {
return;
}

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

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

item.clear();
Expand All @@ -215,6 +180,7 @@ void help_browser::on_topic_select(window& window)
parsed_pages_.emplace(topic_id, topic_pages.get_page_count());
topic_pages.add_page(data);

// TODO: refactor out
window.invalidate_layout();
}

Expand All @@ -226,8 +192,9 @@ void help_browser::on_topic_select(window& window)
history_pos_ = std::prev(history_.end());

if(history_pos_ != history_.begin()) {
find_widget<button>(&window, "back", false).set_visible(widget::visibility::visible);
find_widget<button>(&window, "back", false).set_visible(widget::visibility::visible);
}

find_widget<button>(&window, "next", false).set_visible(widget::visibility::hidden);

const unsigned topic_i = parsed_pages_.at(topic_id);
Expand All @@ -237,15 +204,18 @@ void help_browser::on_topic_select(window& window)
void help_browser::on_history_navigate(window& window, bool backwards)
{
if(backwards) {
history_pos_--;
--history_pos_;
} else {
history_pos_++;
++history_pos_;
}

find_widget<button>(&window, "back", false).set_visible(
history_pos_ == history_.begin() ? widget::visibility::hidden : widget::visibility::visible);
find_widget<button>(&window, "next", false).set_visible(
history_pos_ == std::prev(history_.end()) ? widget::visibility::hidden : widget::visibility::visible);
find_widget<button>(&window, "back", false).set_visible(history_pos_ == history_.begin()
? widget::visibility::hidden
: widget::visibility::visible);

find_widget<button>(&window, "next", false).set_visible(history_pos_ == std::prev(history_.end())
? widget::visibility::hidden
: widget::visibility::visible);

const unsigned topic_i = parsed_pages_.at(*history_pos_);
find_widget<multi_page>(&window, "topic_text_pages", false).select_page(topic_i);
Expand Down
2 changes: 1 addition & 1 deletion src/gui/dialogs/help_browser.hpp
Expand Up @@ -22,7 +22,7 @@
class config;

namespace help {
struct section;
class section;
}

namespace gui2
Expand Down
2 changes: 1 addition & 1 deletion src/gui/widgets/unit_preview_pane.cpp
Expand Up @@ -33,7 +33,7 @@
#include "preferences/game.hpp"
#include "gettext.hpp"
#include "help/help.hpp"
#include "help/help_impl.hpp"
#include "help/utils.hpp"
#include "play_controller.hpp"
#include "resources.hpp"
#include "team.hpp"
Expand Down
36 changes: 36 additions & 0 deletions src/help/constants.cpp
@@ -0,0 +1,36 @@
/*
Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#include "help/constants.hpp"

namespace help
{
const unsigned max_section_recursion_level = 15;
const unsigned max_history = 100;

const std::string default_topic = "..introduction";
const std::string unknown_unit_topic = ".unknown_unit";

const std::string ability_prefix = "ability_";
const std::string era_prefix = "era_";
const std::string faction_prefix = "faction_";
const std::string race_prefix = "race_";
const std::string terrain_prefix = "terrain_";
const std::string tod_prefix = "time_of_day_";
const std::string trait_prefix = "traits_";
const std::string unit_prefix = "unit_";
const std::string variation_prefix = "variation_";
const std::string weapon_special_prefix = "weaponspecial_";

} // namespace help
49 changes: 49 additions & 0 deletions src/help/constants.hpp
@@ -0,0 +1,49 @@
/*
Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#pragma once

#include <string>

namespace help
{
extern const unsigned max_section_recursion_level;
extern const unsigned max_history;

//
// Constant topics IDs
//

/** Topic to open by default when opening the help browser. */
extern const std::string default_topic;

/** Topic to show when a unit hasn't been encountered in-game yet */
extern const std::string unknown_unit_topic;

//
// Standard topic ID prefixes
//

extern const std::string ability_prefix;
extern const std::string era_prefix;
extern const std::string faction_prefix;
extern const std::string race_prefix;
extern const std::string terrain_prefix;
extern const std::string tod_prefix;
extern const std::string trait_prefix;
extern const std::string unit_prefix;
extern const std::string variation_prefix;
extern const std::string weapon_special_prefix;

} // namespace help

0 comments on commit 81388ad

Please sign in to comment.