Skip to content

Commit

Permalink
Campaign selection: Add search functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
dvladf authored and Vultraz committed Jan 27, 2019
1 parent a52e207 commit 397b8e1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
17 changes: 17 additions & 0 deletions data/gui/window/campaign_dialog.cfg
Expand Up @@ -195,6 +195,23 @@

[grid]

[row]
grow_factor = 0

[column]
grow_factor = 1
border = "all"
border_size = 5
horizontal_alignment = "left"

[text_box]
id = "filter_box"
definition = "default"
{FILTER_TEXT_BOX_HINT}
[/text_box]
[/column]
[/row]

[row]
grow_factor = 1

Expand Down
15 changes: 14 additions & 1 deletion src/gettext.cpp
Expand Up @@ -227,7 +227,7 @@ namespace
}

generator_.use_ansi_encoding(false);
generator_.categories(bl::message_facet | bl::information_facet | bl::collation_facet | bl::formatting_facet);
generator_.categories(bl::message_facet | bl::information_facet | bl::collation_facet | bl::formatting_facet | bl::convert_facet);
generator_.characters(bl::char_facet);
// We cannot have current_locale_ be a non boost-generated locale since it might not supply
// the bl::info facet. As soon as we add message paths, update_locale_internal might fail,
Expand Down Expand Up @@ -515,4 +515,17 @@ std::string strftime(const std::string& format, const std::tm* time)
return dummy.str();
}

bool ci_search(const std::string& s1, const std::string& s2)
{
std::lock_guard<std::mutex> lock(get_mutex());

const std::locale& locale = get_manager().get_locale();

std::string ls1 = bl::to_lower(s1, locale);
std::string ls2 = bl::to_lower(s2, locale);

return std::search(ls1.begin(), ls1.end(),
ls2.begin(), ls2.end()) != ls1.end();
}

}
2 changes: 2 additions & 0 deletions src/gettext.hpp
Expand Up @@ -83,6 +83,8 @@ namespace translation
int icompare(const std::string& s1,const std::string& s2);

std::string strftime(const std::string& format, const std::tm* time);

bool ci_search(const std::string& s1, const std::string& s2);
}

//#define _(String) translation::dsgettext(GETTEXT_DOMAIN,String)
Expand Down
37 changes: 32 additions & 5 deletions src/gui/dialogs/campaign_selection.cpp
Expand Up @@ -24,6 +24,7 @@
#include "gui/widgets/multimenu_button.hpp"
#include "gui/widgets/scroll_label.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/text_box.hpp"
#include "gui/widgets/toggle_button.hpp"
#include "gui/widgets/tree_view.hpp"
#include "gui/widgets/tree_view_node.hpp"
Expand Down Expand Up @@ -149,17 +150,28 @@ void campaign_selection::sort_campaigns(window& window, campaign_selection::CAMP
tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);

// Remember which campaign was selected...
std::string was_selected = tree.selected_item()->id();

tree.clear();
std::string was_selected;
if (!tree.empty()) {
was_selected = tree.selected_item()->id();
tree.clear();
}

bool exists_in_filtered_result = false;
for(const auto& level : levels) {
add_campaign_to_tree(window, level->data());
if (translation::ci_search(level->name(), last_search_text_)) {
add_campaign_to_tree(window, level->data());

if (!exists_in_filtered_result) {
exists_in_filtered_result = level->id() == was_selected;
}
}
}

if(!was_selected.empty()) {
if(!was_selected.empty() && exists_in_filtered_result) {
find_widget<tree_view_node>(&window, was_selected, false).select_node();
}

campaign_selected(window);
}

void campaign_selection::toggle_sorting_selection(window& window, CAMPAIGN_ORDER order)
Expand Down Expand Up @@ -197,8 +209,23 @@ void campaign_selection::toggle_sorting_selection(window& window, CAMPAIGN_ORDER
sort_campaigns(window, current_sorting_, currently_sorted_asc_);
}

void campaign_selection::filter_text_changed(text_box_base* textbox, const std::string& text)
{
if (text == last_search_text_) {
return;
}

last_search_text_ = text;
window& window = *textbox->get_window();
sort_campaigns(window, current_sorting_, currently_sorted_asc_);
}

void campaign_selection::pre_show(window& window)
{
text_box* filter = find_widget<text_box>(&window, "filter_box", false, true);
filter->set_text_changed_callback(
std::bind(&campaign_selection::filter_text_changed, this, _1, _2));

/***** Setup campaign tree. *****/
tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);

Expand Down
5 changes: 5 additions & 0 deletions src/gui/dialogs/campaign_selection.hpp
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include "gui/dialogs/modal_dialog.hpp"
#include "gui/widgets/text_box_base.hpp"

#include "game_initialization/create_engine.hpp"

Expand Down Expand Up @@ -74,6 +75,8 @@ class campaign_selection : public modal_dialog

void mod_toggled(window& window);

void filter_text_changed(text_box_base* textbox, const std::string &text);

ng::create_engine& engine_;

/** The chosen campaign. */
Expand All @@ -89,6 +92,8 @@ class campaign_selection : public modal_dialog
CAMPAIGN_ORDER current_sorting_;

bool currently_sorted_asc_;

std::string last_search_text_;
};

} // namespace dialogs
Expand Down

0 comments on commit 397b8e1

Please sign in to comment.