GUI2/Listbox: allow setting all sorters at once (#9629)

This commit is contained in:
Charles Dang 2024-12-05 22:45:52 -05:00 committed by GitHub
parent c4f7523e00
commit 5175beb1e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 84 additions and 82 deletions

View file

@ -106,8 +106,10 @@ void game_load::pre_show()
keyboard_capture(filter);
add_to_keyboard_chain(&list);
list.register_sorting_option(0, [this](const int i) { return games_[i].name(); });
list.register_sorting_option(1, [this](const int i) { return games_[i].modified(); });
list.set_sorting_options(
[this](const std::size_t i) { return games_[i].name(); },
[this](const std::size_t i) { return games_[i].modified(); }
);
populate_game_list();

View file

@ -197,31 +197,33 @@ void game_stats::pre_show()
}
// Sorting options for the status list
stats_list.register_sorting_option(0, [this](const int i) {
unit_const_ptr leader = get_leader(i + 1);
return leader ? leader->name() : t_string();
});
stats_list.register_sorting_option(1, [this](const int i) { return board_.teams()[i].user_team_name(); });
stats_list.register_sorting_option(2, [this](const int i) { return board_.teams()[i].gold(); });
stats_list.register_sorting_option(3, [this](const int i) { return board_.teams()[i].villages(); });
stats_list.register_sorting_option(4, [this](const int i) { return team_data_[i].units; });
stats_list.register_sorting_option(5, [this](const int i) { return team_data_[i].upkeep; });
stats_list.register_sorting_option(6, [this](const int i) { return team_data_[i].net_income; });
stats_list.set_sorting_options(
[this](const std::size_t i) {
unit_const_ptr leader = get_leader(i + 1);
return leader ? leader->name() : t_string();
},
[this](const std::size_t i) { return board_.teams()[i].user_team_name(); },
[this](const std::size_t i) { return board_.teams()[i].gold(); },
[this](const std::size_t i) { return board_.teams()[i].villages(); },
[this](const std::size_t i) { return team_data_[i].units; },
[this](const std::size_t i) { return team_data_[i].upkeep; },
[this](const std::size_t i) { return team_data_[i].net_income; }
);
// Sorting options for the settings list
settings_list.register_sorting_option(0, [this](const int i) {
unit_const_ptr leader = get_leader(i + 1);
return leader ? leader->name() : t_string();
});
settings_list.register_sorting_option(1, [this](const int i) { return board_.teams()[i].side(); });
settings_list.register_sorting_option(2, [this](const int i) { return board_.teams()[i].start_gold(); });
settings_list.register_sorting_option(3, [this](const int i) { return board_.teams()[i].base_income(); });
settings_list.register_sorting_option(4, [this](const int i) { return board_.teams()[i].village_gold(); });
settings_list.register_sorting_option(5, [this](const int i) { return board_.teams()[i].village_support(); });
settings_list.register_sorting_option(6, [this](const int i) { return board_.teams()[i].uses_fog(); });
settings_list.register_sorting_option(7, [this](const int i) { return board_.teams()[i].uses_shroud(); });
settings_list.set_sorting_options(
[this](const std::size_t i) {
unit_const_ptr leader = get_leader(i + 1);
return leader ? leader->name() : t_string();
},
[this](const std::size_t i) { return board_.teams()[i].side(); },
[this](const std::size_t i) { return board_.teams()[i].start_gold(); },
[this](const std::size_t i) { return board_.teams()[i].base_income(); },
[this](const std::size_t i) { return board_.teams()[i].village_gold(); },
[this](const std::size_t i) { return board_.teams()[i].village_support(); },
[this](const std::size_t i) { return board_.teams()[i].uses_fog(); },
[this](const std::size_t i) { return board_.teams()[i].uses_shroud(); }
);
//
// Set up tab control

View file

@ -841,16 +841,18 @@ void preferences_dialog::initialize_callbacks()
text_box& filter = find_widget<text_box>("filter");
filter.set_text_changed_callback(std::bind(&preferences_dialog::hotkey_filter_callback, this));
// Action column
hotkey_list.register_sorting_option(0, [this](const int i) { return visible_hotkeys_[i]->description; });
hotkey_list.set_sorting_options(
// Action column
[this](const std::size_t i) { return visible_hotkeys_[i]->description; },
// Hotkey column
hotkey_list.register_sorting_option(1, [this](const int i) { return hotkey::get_names(visible_hotkeys_[i]->id); });
// Hotkey column
[this](const std::size_t i) { return hotkey::get_names(visible_hotkeys_[i]->id); },
// Scope columns
hotkey_list.register_sorting_option(2, [this](const int i) { return !visible_hotkeys_[i]->scope[hotkey::SCOPE_GAME]; });
hotkey_list.register_sorting_option(3, [this](const int i) { return !visible_hotkeys_[i]->scope[hotkey::SCOPE_EDITOR]; });
hotkey_list.register_sorting_option(4, [this](const int i) { return !visible_hotkeys_[i]->scope[hotkey::SCOPE_MAIN_MENU]; });
// Scope columns
[this](const std::size_t i) { return !visible_hotkeys_[i]->scope[hotkey::SCOPE_GAME]; },
[this](const std::size_t i) { return !visible_hotkeys_[i]->scope[hotkey::SCOPE_EDITOR]; },
[this](const std::size_t i) { return !visible_hotkeys_[i]->scope[hotkey::SCOPE_MAIN_MENU]; }
);
hotkey_list.set_active_sorting_option({0, sort_order::type::ascending}, true);

View file

@ -115,8 +115,10 @@ void unit_create::pre_show()
<< std::endl;
}
list.register_sorting_option(0, [this](const int i) { return (*units_[i]).race()->plural_name(); });
list.register_sorting_option(1, [this](const int i) { return (*units_[i]).type_name(); });
list.set_sorting_options(
[this](const std::size_t i) { return units_[i]->race()->plural_name(); },
[this](const std::size_t i) { return units_[i]->type_name(); }
);
// Select the first entry on sort if no previous selection was provided.
list.set_active_sorting_option({0, sort_order::type::ascending}, choice_.empty());

View file

@ -144,17 +144,19 @@ void unit_list::pre_show()
}
}
list.register_sorting_option(0, [this](const int i) { return unit_list_[i]->type_name(); });
list.register_sorting_option(1, [this](const int i) { return unit_list_[i]->name(); });
list.register_sorting_option(2, [this](const int i) { return unit_list_[i]->movement_left(); });
list.register_sorting_option(3, [this](const int i) { return unit_list_[i]->hitpoints(); });
list.register_sorting_option(4, [this](const int i) {
const unit& u = *unit_list_[i];
return std::tuple(u.level(), -static_cast<int>(u.experience_to_advance()));
});
list.register_sorting_option(5, [this](const int i) { return unit_list_[i]->experience(); });
list.register_sorting_option(6, [this](const int i) {
return !unit_list_[i]->trait_names().empty() ? unit_list_[i]->trait_names().front() : t_string(); });
list.set_sorting_options(
[this](const std::size_t i) { return unit_list_[i]->type_name(); },
[this](const std::size_t i) { return unit_list_[i]->name(); },
[this](const std::size_t i) { return unit_list_[i]->movement_left(); },
[this](const std::size_t i) { return unit_list_[i]->hitpoints(); },
[this](const std::size_t i) {
const unit& u = *unit_list_[i];
return std::tuple(u.level(), -static_cast<int>(u.experience_to_advance()));
},
[this](const std::size_t i) { return unit_list_[i]->experience(); },
[this](const std::size_t i) {
return !unit_list_[i]->trait_names().empty() ? unit_list_[i]->trait_names().front() : t_string(); }
);
list_item_clicked();
}

View file

@ -286,16 +286,18 @@ void unit_recall::pre_show()
}
}
list.register_sorting_option(0, [this](const int i) { return recall_list_[i]->type_name(); });
list.register_sorting_option(1, [this](const int i) { return recall_list_[i]->name(); });
list.register_sorting_option(2, [this](const int i) {
const unit& u = *recall_list_[i];
return std::tuple(u.level(), -static_cast<int>(u.experience_to_advance()));
});
list.register_sorting_option(3, [this](const int i) { return recall_list_[i]->experience(); });
list.register_sorting_option(4, [this](const int i) {
return !recall_list_[i]->trait_names().empty() ? recall_list_[i]->trait_names().front() : t_string();
});
list.set_sorting_options(
[this](const std::size_t i) { return recall_list_[i]->type_name(); },
[this](const std::size_t i) { return recall_list_[i]->name(); },
[this](const std::size_t i) {
const unit& u = *recall_list_[i];
return std::tuple(u.level(), -static_cast<int>(u.experience_to_advance()));
},
[this](const std::size_t i) { return recall_list_[i]->experience(); },
[this](const std::size_t i) {
return !recall_list_[i]->trait_names().empty() ? recall_list_[i]->trait_names().front() : t_string();
}
);
list.set_active_sorting_option(sort_last.first >= 0 ? sort_last : sort_default, true);

View file

@ -375,11 +375,13 @@ void addon_list::finalize_setup()
{
listbox& list = get_listbox();
list.register_sorting_option(0, [this](const int i) { return t_string(addon_vector_[i]->display_title_full()); });
list.register_sorting_option(1, [this](const int i) { return addon_vector_[i]->author; });
list.register_sorting_option(2, [this](const int i) { return addon_vector_[i]->size; });
list.register_sorting_option(3, [this](const int i) { return addon_vector_[i]->downloads; });
list.register_sorting_option(4, [this](const int i) { return t_string(addon_vector_[i]->display_type()); });
list.set_sorting_options(
[this](const std::size_t i) { return t_string(addon_vector_[i]->display_title_full()); },
[this](const std::size_t i) { return addon_vector_[i]->author; },
[this](const std::size_t i) { return addon_vector_[i]->size; },
[this](const std::size_t i) { return addon_vector_[i]->downloads; },
[this](const std::size_t i) { return t_string(addon_vector_[i]->display_type()); }
);
auto order = std::pair(0, sort_order::type::ascending);
list.set_active_sorting_option(order);

View file

@ -600,15 +600,6 @@ void listbox::order_by(const generator_base::order_func& func)
update_layout();
}
void listbox::set_column_order(unsigned col, generator_sort_array&& func)
{
if(col >= orders_.size()) {
orders_.resize(col + 1);
}
orders_[col].second = std::move(func);
}
bool listbox::sort_helper::less(const t_string& lhs, const t_string& rhs)
{
return translation::icompare(lhs, rhs) < 0;

View file

@ -255,8 +255,6 @@ public:
void order_by(const generator_base::order_func& func);
private:
void set_column_order(unsigned col, generator_sort_array&& func);
struct sort_helper
{
template<typename T>
@ -273,22 +271,22 @@ private:
};
public:
template<typename Func>
void register_sorting_option(const int col, const Func& f)
template<typename... Args>
void set_sorting_options(Args&&... functors)
{
set_column_order(col, {
[f](int lhs, int rhs) { return sort_helper::less(f(lhs), f(rhs)); },
[f](int lhs, int rhs) { return sort_helper::more(f(lhs), f(rhs)); }
});
orders_ = {{ nullptr, {
[f = functors](int lhs, int rhs) { return sort_helper::less(f(lhs), f(rhs)); },
[f = functors](int lhs, int rhs) { return sort_helper::more(f(lhs), f(rhs)); }
}}...};
}
using order_pair = std::pair<int, sort_order::type>;
/**
* Sorts the listbox by a pre-set sorting option. The corresponding header widget will also be toggled.
* The sorting option should already have been registered by @ref listbox::register_sorting_option().
* The sorting option should already have been registered by @ref listbox::set_sorting_options().
*
* @param sort_by Pair of column index and sort direction. The column (first arguemnt)
* @param sort_by Pair of column index and sort direction. The column (first argument)
* argument will be sorted in the specified direction (second argument)
*
* @param select_first If true, the first row post-sort will be selected. If false (default),
@ -372,8 +370,7 @@ private:
/** Contains the builder for the new items. */
builder_grid_const_ptr list_builder_;
typedef std::vector<std::pair<selectable_item*, generator_sort_array>> torder_list;
torder_list orders_;
std::vector<std::pair<selectable_item*, generator_sort_array>> orders_;
std::function<void(unsigned, sort_order::type)> callback_order_change_;