Campaign selection: Add search functionality
This commit is contained in:
parent
ca6cfefe85
commit
1467ccb97e
5 changed files with 72 additions and 8 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -229,7 +229,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,
|
||||
|
@ -526,4 +526,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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
@ -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();
|
||||
|
||||
for(const auto& level : levels) {
|
||||
add_campaign_to_tree(window, level->data());
|
||||
std::string was_selected;
|
||||
if (!tree.empty()) {
|
||||
was_selected = tree.selected_item()->id();
|
||||
tree.clear();
|
||||
}
|
||||
|
||||
if(!was_selected.empty()) {
|
||||
bool exists_in_filtered_result = false;
|
||||
for(const auto& level : levels) {
|
||||
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() && 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)
|
||||
|
@ -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);
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
@ -74,6 +75,8 @@ private:
|
|||
|
||||
void mod_toggled(window& window);
|
||||
|
||||
void filter_text_changed(text_box_base* textbox, const std::string &text);
|
||||
|
||||
ng::create_engine& engine_;
|
||||
|
||||
/** The chosen campaign. */
|
||||
|
@ -89,6 +92,8 @@ private:
|
|||
CAMPAIGN_ORDER current_sorting_;
|
||||
|
||||
bool currently_sorted_asc_;
|
||||
|
||||
std::string last_search_text_;
|
||||
};
|
||||
|
||||
} // namespace dialogs
|
||||
|
|
Loading…
Add table
Reference in a new issue