diff --git a/src/guiengine/widgets/list_widget.cpp b/src/guiengine/widgets/list_widget.cpp index 819ad7d5e..c942fcff6 100644 --- a/src/guiengine/widgets/list_widget.cpp +++ b/src/guiengine/widgets/list_widget.cpp @@ -479,7 +479,7 @@ EventPropagation ListWidget::transmitEvent(Widget* w, m_header_elements[col].getIrrlichtElement()->setPressed(true); */ - if (m_listener) m_listener->onColumnClicked(m_sort_col); + if (m_listener) m_listener->onColumnClicked(m_sort_col, m_sort_desc, m_sort_default); return EVENT_BLOCK; } diff --git a/src/guiengine/widgets/list_widget.hpp b/src/guiengine/widgets/list_widget.hpp index 44df80f89..0a0a3b12d 100644 --- a/src/guiengine/widgets/list_widget.hpp +++ b/src/guiengine/widgets/list_widget.hpp @@ -39,7 +39,7 @@ namespace GUIEngine public: virtual ~IListWidgetHeaderListener(){} - virtual void onColumnClicked(int columnId) = 0; + virtual void onColumnClicked(int column_id, bool sort_desc, bool sort_default) = 0; }; /** \brief A vertical list widget with text entries diff --git a/src/states_screens/addons_screen.cpp b/src/states_screens/addons_screen.cpp index f50f9ec4d..cc4f063b1 100644 --- a/src/states_screens/addons_screen.cpp +++ b/src/states_screens/addons_screen.cpp @@ -162,10 +162,6 @@ void AddonsScreen::init() m_reloading = false; - m_sort_desc = false; - m_sort_default = true; - m_sort_col = 0; - getWidget("category")->setActive(false); if(UserConfigParams::logAddons()) @@ -232,7 +228,7 @@ void AddonsScreen::tearDown() * updated. * \param type Must be 'kart' or 'track'. */ -void AddonsScreen::loadList() +void AddonsScreen::loadList(bool sort_desc) { #ifndef SERVER_ONLY // Get the filter by words. @@ -287,7 +283,7 @@ void AddonsScreen::loadList() sorted_list.push_back(&addon); } - sorted_list.insertionSort(/*start=*/0, m_sort_desc); + sorted_list.insertionSort(/*start=*/0, sort_desc); GUIEngine::ListWidget* w_list = getWidget("list_addons"); @@ -420,33 +416,20 @@ void AddonsScreen::loadList() } // loadList // ---------------------------------------------------------------------------- -void AddonsScreen::onColumnClicked(int column_id) +void AddonsScreen::onColumnClicked(int column_id, bool sort_desc, bool sort_default) { - if (m_sort_col != column_id) - { - m_sort_desc = false; - m_sort_default = false; - } - else - { - if (!m_sort_default) m_sort_desc = !m_sort_desc; - m_sort_default = !m_sort_desc && !m_sort_default; - } - - m_sort_col = column_id; - switch(column_id) { case 0: - Addon::setSortOrder(m_sort_default ? Addon::SO_DEFAULT : Addon::SO_NAME); + Addon::setSortOrder(sort_default ? Addon::SO_DEFAULT : Addon::SO_NAME); break; case 1: - Addon::setSortOrder(m_sort_default ? Addon::SO_DEFAULT : Addon::SO_DATE); + Addon::setSortOrder(sort_default ? Addon::SO_DEFAULT : Addon::SO_DATE); break; default: assert(0); break; } // switch /** \brief Toggle the sort order after column click **/ - loadList(); + loadList(sort_desc && !sort_default); } // onColumnClicked // ---------------------------------------------------------------------------- diff --git a/src/states_screens/addons_screen.hpp b/src/states_screens/addons_screen.hpp index 339ec49c6..fb44d7e34 100644 --- a/src/states_screens/addons_screen.hpp +++ b/src/states_screens/addons_screen.hpp @@ -73,13 +73,6 @@ private: bool m_reloading; - /** \brief To check (and set) if sort order is descending **/ - bool m_sort_desc; - - bool m_sort_default; - - int m_sort_col; - /** List of date filters **/ std::vector m_date_filters; @@ -88,7 +81,7 @@ private: public: /** Load the addons into the main list.*/ - void loadList(); + void loadList(bool sort_desc = false); /** \brief implement callback from parent class GUIEngine::Screen */ virtual void loadedFromFile() OVERRIDE; @@ -102,7 +95,7 @@ public: /** \brief implement callback from parent class GUIEngine::Screen */ virtual void beforeAddingWidget() OVERRIDE; - virtual void onColumnClicked(int columnId) OVERRIDE; + virtual void onColumnClicked(int column_id, bool sort_desc, bool sort_default) OVERRIDE; virtual void init() OVERRIDE; virtual void tearDown() OVERRIDE; diff --git a/src/states_screens/ghost_replay_selection.cpp b/src/states_screens/ghost_replay_selection.cpp index 3f1ce2dad..b9313f879 100644 --- a/src/states_screens/ghost_replay_selection.cpp +++ b/src/states_screens/ghost_replay_selection.cpp @@ -32,7 +32,6 @@ using namespace GUIEngine; */ GhostReplaySelection::GhostReplaySelection() : Screen("ghost_replay_selection.stkgui") { - m_sort_desc = true; m_is_comparing = false; m_replay_to_compare_uid = 0; } // GhostReplaySelection @@ -424,11 +423,16 @@ void GhostReplaySelection::onConfirm() /** Change the sort order if a column was clicked. * \param column_id ID of the column that was clicked. */ -void GhostReplaySelection::onColumnClicked(int column_id) +void GhostReplaySelection::onColumnClicked(int column_id, bool sort_desc, bool sort_default) { // Begin by resorting the list to default defaultSort(); + if (sort_default) + { + loadList(); + return; + } int diff_difficulty = m_same_difficulty ? 1 : 0; int diff_linear = m_active_mode_is_linear ? 0 : 1; @@ -461,10 +465,7 @@ void GhostReplaySelection::onColumnClicked(int column_id) else assert(0); - /** \brief Toggle the sort order after column click **/ - m_sort_desc = !m_sort_desc; - - ReplayPlay::get()->sortReplay(m_sort_desc); + ReplayPlay::get()->sortReplay(sort_desc); loadList(); } // onColumnClicked diff --git a/src/states_screens/ghost_replay_selection.hpp b/src/states_screens/ghost_replay_selection.hpp index 4ae4d9c58..d34f9acf6 100644 --- a/src/states_screens/ghost_replay_selection.hpp +++ b/src/states_screens/ghost_replay_selection.hpp @@ -54,7 +54,6 @@ private: bool m_same_difficulty; bool m_same_version; bool m_best_times; - bool m_sort_desc; bool m_is_comparing; bool m_active_mode_is_linear; RaceManager::MinorRaceModeType m_active_mode; @@ -90,7 +89,7 @@ public: /** \brief implement callback from parent class GUIEngine::Screen */ virtual void beforeAddingWidget() OVERRIDE; - virtual void onColumnClicked(int columnId) OVERRIDE; + virtual void onColumnClicked(int column_id, bool sort_desc, bool sort_default) OVERRIDE; virtual void init() OVERRIDE; diff --git a/src/states_screens/online_profile_friends.cpp b/src/states_screens/online_profile_friends.cpp index 2db9b595b..55f19837e 100644 --- a/src/states_screens/online_profile_friends.cpp +++ b/src/states_screens/online_profile_friends.cpp @@ -34,7 +34,8 @@ using namespace irr::gui; using namespace Online; int OnlineProfileFriends::m_sort_column = 0; -bool OnlineProfileFriends::m_sort_increasing = true; +bool OnlineProfileFriends::m_sort_desc = false; +bool OnlineProfileFriends::m_sort_default = true; // ----------------------------------------------------------------------------- /** Constructor for a display of all friends. @@ -81,8 +82,6 @@ void OnlineProfileFriends::beforeAddingWidget() void OnlineProfileFriends::init() { OnlineProfileBase::init(); - m_sort_column = 0; - m_sort_increasing = true; m_profile_tabs->select( m_friends_tab->m_properties[PROP_ID], PLAYER_ID_GAME_MASTER ); m_profile_tabs->setFocusForPlayer(PLAYER_ID_GAME_MASTER); @@ -120,15 +119,11 @@ void OnlineProfileFriends::eventCallback(Widget* widget, } // eventCallback // ---------------------------------------------------------------------------- -void OnlineProfileFriends::onColumnClicked(int column_id) +void OnlineProfileFriends::onColumnClicked(int column_id, bool sort_desc, bool sort_default) { - if (column_id == m_sort_column) - m_sort_increasing = !m_sort_increasing; - else - { - m_sort_increasing = true; - } m_sort_column = column_id; + m_sort_desc = sort_desc; + m_sort_default = sort_default; displayResults(); } // onColumnClicked @@ -140,20 +135,14 @@ bool OnlineProfileFriends::compareFriends(int f1, int f2) switch (m_sort_column) { case 0: // sort by name - if (m_sort_increasing) - return p1->getUserName().lower_ignore_case(p2->getUserName()); - else - return p2->getUserName().lower_ignore_case(p1->getUserName()); + return p1->getUserName().lower_ignore_case(p2->getUserName()); case 1: // sort by date { OnlineProfile::RelationInfo *r1 = p1->getRelationInfo(); OnlineProfile::RelationInfo *r2 = p2->getRelationInfo(); // While we are comparing dates that are strings, they are normalised // i.e. contain leading 0 etc, so a string compare works as expected. - if (m_sort_increasing) - return r1->getDate().lower_ignore_case(r2->getDate()); - else - return r2->getDate().lower_ignore_case(r1->getDate()); + return r1->getDate().lower_ignore_case(r2->getDate()); } case 2: // sort by online status { @@ -162,16 +151,12 @@ bool OnlineProfileFriends::compareFriends(int f1, int f2) // In case of same online status, sort by name if (r1->isOnline() == r2->isOnline()) { - if (m_sort_increasing) - return p1->getUserName().lower_ignore_case(p2->getUserName()); - else - return p2->getUserName().lower_ignore_case(p1->getUserName()); + return p1->getUserName().lower_ignore_case(p2->getUserName()); } else - if (m_sort_increasing) - return r1->isOnline() < r2->isOnline(); - else - return r2->isOnline() < r1->isOnline(); + { + return r1->isOnline() < r2->isOnline(); + } } default: break; @@ -187,7 +172,8 @@ void OnlineProfileFriends::displayResults() { m_friends_list_widget->clear(); OnlineProfile::IDList friends = m_visiting_profile->getFriends(); - std::sort(friends.begin(), friends.end(), compareFriends); + (m_sort_desc && !m_sort_default) ? std::sort(friends.rbegin(), friends.rend(), compareFriends) + : std::sort(friends.begin(), friends.end(), compareFriends); for (unsigned int i = 0; i < friends.size(); i++) { std::vector row; diff --git a/src/states_screens/online_profile_friends.hpp b/src/states_screens/online_profile_friends.hpp index 6aa7d7b97..fd45c1980 100644 --- a/src/states_screens/online_profile_friends.hpp +++ b/src/states_screens/online_profile_friends.hpp @@ -48,11 +48,13 @@ private: GUIEngine::TextBoxWidget *m_search_box_widget; bool m_waiting_for_friends; + /** Which column to use for sorting. */ static int m_sort_column; - /** True is sorting should be increasing. */ - static bool m_sort_increasing; + static bool m_sort_desc; + + static bool m_sort_default; void displayResults(); static bool compareFriends(int f1, int f2); @@ -72,7 +74,7 @@ public: virtual void onUpdate(float delta) OVERRIDE; virtual void beforeAddingWidget() OVERRIDE; - virtual void onColumnClicked(int columnId) OVERRIDE; + virtual void onColumnClicked(int column_id, bool sort_desc, bool sort_default) OVERRIDE; // ------------------------------------------------------------------------ /** Triggers a reload of the friend list next time this menu is shown. */ diff --git a/src/states_screens/server_selection.cpp b/src/states_screens/server_selection.cpp index 33f22f7bb..0e8ab3355 100644 --- a/src/states_screens/server_selection.cpp +++ b/src/states_screens/server_selection.cpp @@ -200,10 +200,18 @@ void ServerSelection::loadList(unsigned sort_case) /** Change the sort order if a column was clicked. * \param column_id ID of the column that was clicked. */ -void ServerSelection::onColumnClicked(int column_id) +void ServerSelection::onColumnClicked(int column_id, bool sort_desc, bool sort_default) { - m_sort_desc = !m_sort_desc; - loadList(column_id); + if (sort_default) + { + m_sort_desc = false; + loadList(/* distance */ 5); + } + else + { + m_sort_desc = sort_desc; + loadList(column_id); + } } // onColumnClicked // ---------------------------------------------------------------------------- diff --git a/src/states_screens/server_selection.hpp b/src/states_screens/server_selection.hpp index dd7ac6dff..a8d54ad57 100644 --- a/src/states_screens/server_selection.hpp +++ b/src/states_screens/server_selection.hpp @@ -80,7 +80,7 @@ public: /** \brief implement callback from parent class GUIEngine::Screen */ virtual void beforeAddingWidget() OVERRIDE; - virtual void onColumnClicked(int columnId) OVERRIDE; + virtual void onColumnClicked(int column_id, bool sort_desc, bool sort_default) OVERRIDE; virtual void init() OVERRIDE;