Update order dropdown when the player changes sorting option
Follow-up to commit 3b88de6cbc
.
This commit is contained in:
parent
0b77514031
commit
529ab50275
5 changed files with 53 additions and 16 deletions
|
@ -212,25 +212,25 @@ const std::vector<std::pair<ADDON_TYPE, std::string>> addon_manager::type_filter
|
|||
};
|
||||
|
||||
const std::vector<addon_manager::addon_order> addon_manager::all_orders_{
|
||||
{N_("addons_order^Name ($order)"),
|
||||
{N_("addons_order^Name ($order)"), 0,
|
||||
[](const addon_info& a, const addon_info& b) { return a.title < b.title; },
|
||||
[](const addon_info& a, const addon_info& b) { return a.title > b.title; }},
|
||||
{N_("addons_order^Author ($order)"),
|
||||
{N_("addons_order^Author ($order)"), 1,
|
||||
[](const addon_info& a, const addon_info& b) { return a.author < b.author; },
|
||||
[](const addon_info& a, const addon_info& b) { return a.author > b.author; }},
|
||||
{N_("addons_order^Size ($order)"),
|
||||
{N_("addons_order^Size ($order)"), 2,
|
||||
[](const addon_info& a, const addon_info& b) { return a.size < b.size; },
|
||||
[](const addon_info& a, const addon_info& b) { return a.size > b.size; }},
|
||||
{N_("addons_order^Downloads ($order)"),
|
||||
{N_("addons_order^Downloads ($order)"), 3,
|
||||
[](const addon_info& a, const addon_info& b) { return a.downloads < b.downloads; },
|
||||
[](const addon_info& a, const addon_info& b) { return a.downloads > b.downloads; }},
|
||||
{N_("addons_order^Type ($order)"),
|
||||
{N_("addons_order^Type ($order)"), 4,
|
||||
[](const addon_info& a, const addon_info& b) { return a.display_type() < b.display_type(); },
|
||||
[](const addon_info& a, const addon_info& b) { return a.display_type() > b.display_type(); }},
|
||||
{N_("addons_order^Last updated ($order)"),
|
||||
{N_("addons_order^Last updated ($order)"), -1,
|
||||
[](const addon_info& a, const addon_info& b) { return a.updated < b.updated; },
|
||||
[](const addon_info& a, const addon_info& b) { return a.updated > b.updated; }},
|
||||
{N_("addons_order^First uploaded ($order)"),
|
||||
{N_("addons_order^First uploaded ($order)"), -1,
|
||||
[](const addon_info& a, const addon_info& b) { return a.created < b.created; },
|
||||
[](const addon_info& a, const addon_info& b) { return a.created > b.created; }}
|
||||
};
|
||||
|
@ -437,6 +437,9 @@ void addon_manager::pre_show(window& window)
|
|||
window.keyboard_capture(&filter);
|
||||
list.add_list_to_keyboard_chain();
|
||||
|
||||
list.set_callback_order_change(std::bind(&addon_manager::on_order_changed, this, std::ref(window),
|
||||
std::placeholders::_1, std::placeholders::_2));
|
||||
|
||||
// Use handle the special addon_list retval to allow installing addons on double click
|
||||
window.set_exit_hook(std::bind(&addon_manager::exit_hook, this, std::ref(window)));
|
||||
}
|
||||
|
@ -603,9 +606,9 @@ void addon_manager::order_addons(window& window)
|
|||
{
|
||||
const menu_button& order_menu = find_widget<const menu_button>(&window, "order_dropdown", false);
|
||||
const addon_order& order_struct = all_orders_.at(order_menu.get_value() / 2);
|
||||
sort_order order = order_menu.get_value() % 2 == 0 ? sort_order::ascending : sort_order::descending;
|
||||
listbox::SORT_ORDER order = order_menu.get_value() % 2 == 0 ? listbox::SORT_ASCENDING : listbox::SORT_DESCENDING;
|
||||
addon_list::addon_sort_func func;
|
||||
if(order == sort_order::ascending) {
|
||||
if(order == listbox::SORT_ASCENDING) {
|
||||
func = order_struct.sort_func_asc;
|
||||
} else {
|
||||
func = order_struct.sort_func_desc;
|
||||
|
@ -614,6 +617,18 @@ void addon_manager::order_addons(window& window)
|
|||
find_widget<addon_list>(&window, "addons", false).set_addon_order(func);
|
||||
}
|
||||
|
||||
void addon_manager::on_order_changed(window& window, unsigned int sort_column, listbox::SORT_ORDER order)
|
||||
{
|
||||
menu_button& order_menu = find_widget<menu_button>(&window, "order_dropdown", false);
|
||||
auto order_it = std::find_if(all_orders_.begin(), all_orders_.end(),
|
||||
[sort_column](const addon_order& order) {return order.column_index == static_cast<int>(sort_column);});
|
||||
int index = 2 * (order_it - all_orders_.begin());
|
||||
if(order == listbox::SORT_DESCENDING) {
|
||||
++index;
|
||||
}
|
||||
order_menu.set_value(index);
|
||||
}
|
||||
|
||||
template<void(addon_manager::*fptr)(const addon_info& addon, window& window)>
|
||||
void addon_manager::execute_action_on_selected_addon(window& window)
|
||||
{
|
||||
|
|
|
@ -47,16 +47,15 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
enum class sort_order {ascending, descending};
|
||||
|
||||
struct addon_order
|
||||
{
|
||||
std::string label;
|
||||
int column_index; // -1 if there is no such column
|
||||
addon_list::addon_sort_func sort_func_asc;
|
||||
addon_list::addon_sort_func sort_func_desc;
|
||||
|
||||
addon_order(std::string label_, addon_list::addon_sort_func sort_func_asc_, addon_list::addon_sort_func sort_func_desc_)
|
||||
: label(label_), sort_func_asc(sort_func_asc_), sort_func_desc(sort_func_desc_)
|
||||
addon_order(std::string label_, int column, addon_list::addon_sort_func sort_func_asc_, addon_list::addon_sort_func sort_func_desc_)
|
||||
: label(label_), column_index(column), sort_func_asc(sort_func_asc_), sort_func_desc(sort_func_desc_)
|
||||
{}
|
||||
};
|
||||
|
||||
|
@ -139,6 +138,7 @@ private:
|
|||
|
||||
void apply_filters(window& window);
|
||||
void order_addons(window& window);
|
||||
void on_order_changed(window& window, unsigned int sort_column, listbox::SORT_ORDER order);
|
||||
void show_help();
|
||||
|
||||
boost::dynamic_bitset<> get_name_filter_visibility(const window& window) const;
|
||||
|
|
|
@ -131,6 +131,11 @@ public:
|
|||
/** Adds the internal listbox to the keyboard event chain. */
|
||||
void add_list_to_keyboard_chain();
|
||||
|
||||
/** Sets up a callback that will be called when the player changes the sorting order. */
|
||||
void set_callback_order_change(std::function<void(unsigned, listbox::SORT_ORDER)> callback) {
|
||||
get_listbox().set_callback_order_change(callback);
|
||||
}
|
||||
|
||||
/** See @ref styled_widget::set_active. */
|
||||
virtual void set_active(const bool) override
|
||||
{
|
||||
|
|
|
@ -61,6 +61,7 @@ listbox::listbox(const implementation::builder_styled_widget& builder,
|
|||
, list_builder_(list_builder)
|
||||
, need_layout_(false)
|
||||
, orders_()
|
||||
, callback_order_change_()
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -596,14 +597,20 @@ void listbox::order_by_column(unsigned column, widget& widget)
|
|||
}
|
||||
}
|
||||
|
||||
if(selectable.get_value() > orders_[column].second.size()) {
|
||||
SORT_ORDER order = static_cast<SORT_ORDER>(selectable.get_value());
|
||||
|
||||
if(static_cast<unsigned int>(order) > orders_[column].second.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(selectable.get_value() == SORT_NONE) {
|
||||
if(order == SORT_NONE) {
|
||||
order_by(std::less<unsigned>());
|
||||
} else {
|
||||
order_by(orders_[column].second[selectable.get_value() - 1]);
|
||||
order_by(orders_[column].second[order - 1]);
|
||||
}
|
||||
|
||||
if(callback_order_change_ != nullptr) {
|
||||
callback_order_change_(column, order);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include "gui/core/window_builder.hpp"
|
||||
|
||||
#include <boost/dynamic_bitset.hpp>
|
||||
#include <functional>
|
||||
|
||||
namespace gui2
|
||||
{
|
||||
|
@ -291,6 +292,12 @@ public:
|
|||
/** Deactivates all sorting toggle buttons at the top, making the list look like it's not sorted. */
|
||||
void mark_as_unsorted();
|
||||
|
||||
/** Registers a callback to be called when the active sorting option changes. */
|
||||
void set_callback_order_change(std::function<void(unsigned, SORT_ORDER)> callback)
|
||||
{
|
||||
callback_order_change_ = callback;
|
||||
}
|
||||
|
||||
protected:
|
||||
/***** ***** ***** ***** keyboard functions ***** ***** ***** *****/
|
||||
|
||||
|
@ -357,6 +364,9 @@ private:
|
|||
|
||||
typedef std::vector<std::pair<selectable_item*, generator_sort_array>> torder_list;
|
||||
torder_list orders_;
|
||||
|
||||
std::function<void(unsigned, SORT_ORDER)> callback_order_change_;
|
||||
|
||||
/**
|
||||
* Resizes the content.
|
||||
*
|
||||
|
|
Loading…
Add table
Reference in a new issue