Ticket #2405: lobby_game_list_sorting_without_arrows.patch

File lobby_game_list_sorting_without_arrows.patch, 4.3 KB (added by Vladislav Belov, 9 years ago)

Sort by columnt, if click on heading

  • binaries/data/mods/public/gui/lobby/lobby.js

     
    11var g_ChatMessages = [];
    22var g_Name = "unknown";
    33var g_GameList = {};
     4var g_GameListSortBy = "default";
     5var g_GameListOrder = 1; // 1 for ascending sort, and -1 for descending
    46var g_specialKey = Math.random();
    57// This object looks like {"name":[numMessagesSinceReset, lastReset, timeBlocked]} when in use.
    68var g_spamMonitor = {};
     
    7476// Update functions
    7577////////////////////////////////////////////////////////////////////////////////////////////////
    7678
     79function updateOrderSelection()
     80{
     81    var newGameListSortBy = Engine.GetGUIObjectByName("gamesBox").selected_column;
     82    if (newGameListSortBy == g_GameListSortBy)
     83        g_GameListOrder = -g_GameListOrder;
     84    else
     85    {
     86        g_GameListSortBy = newGameListSortBy;
     87        g_GameListOrder = 1;
     88    }
     89
     90    // update the list of games
     91    updateGameList();
     92
     93    // Update info box about the game currently selected
     94    updateGameSelection();
     95}
     96
    7797function resetFilters()
    7898{
    7999    // Reset states of gui objects
     
    365385    // Sort the list of games to that games 'waiting' are displayed at the top, followed by 'init', followed by 'running'.
    366386    var gameStatuses = ['waiting', 'init', 'running'];
    367387    g_GameList.sort(function (a,b) {
    368         if (gameStatuses.indexOf(a.state) < gameStatuses.indexOf(b.state))
    369             return -1;
    370         else if (gameStatuses.indexOf(a.state) > gameStatuses.indexOf(b.state))
    371             return 1;
     388        if (g_GameListSortBy == 'name')
     389        {
     390            // Alphabetical comparison of names.
     391            if (a.name < b.name)
     392                return -g_GameListOrder;
     393            else if (a.name > b.name)
     394                return g_GameListOrder;
     395            return 0;
     396        }
     397        else if (g_GameListSortBy == 'mapName')
     398        {
     399            // Alphabetical comparison of map names.
     400            if (translate(a.niceMapName) < translate(b.niceMapName))
     401                return -g_GameListOrder;
     402            else if (translate(a.niceMapName) > translate(b.niceMapName))
     403                return g_GameListOrder;
     404            return 0;
     405        }
     406        else if (g_GameListSortBy == 'mapSize')
     407        {
     408            // Alphabetical comparison of map size.
     409            if (translatedMapSize(a.mapSize) < translatedMapSize(b.mapSize))
     410                return -g_GameListOrder;
     411            else if (translatedMapSize(a.mapSize) > translatedMapSize(b.mapSize))
     412                return g_GameListOrder;
     413            return 0;
     414        }
     415        else if (g_GameListSortBy == 'mapType')
     416        {
     417            // Numerical comparison of map types.
     418            if (a.mapType < b.mapType)
     419                return -g_GameListOrder;
     420            else if (a.mapType > b.mapType)
     421                return g_GameListOrder;
     422            return 0;
     423        }
     424        else if (g_GameListSortBy == 'nPlayers')
     425        {
     426            // Numerical comparison of player count ratio.
     427            if (a.nbp * b.tnbp < b.nbp * a.tnbp) // ratio a = a.nbp / a.tnbp, ratio b = b.nbp / b.tnbp
     428                return -g_GameListOrder;
     429            else if (a.nbp * b.tnbp > b.nbp * a.tnbp)
     430                return g_GameListOrder;
     431            return 0;
     432        }
     433        else
     434        {
     435            if (gameStatuses.indexOf(a.state) < gameStatuses.indexOf(b.state))
     436                return -1;
     437            else if (gameStatuses.indexOf(a.state) > gameStatuses.indexOf(b.state))
     438                return 1;
    372439
    373         // Alphabetical comparison of names as tiebreaker.
    374         if (a.name < b.name)
    375             return -1;
    376         else if (a.name > b.name)
    377             return 1;
    378         return 0;
     440            // Alphabetical comparison of names as tiebreaker.
     441            if (a.name < b.name)
     442                return -1;
     443            else if (a.name > b.name)
     444                return 1;
     445            return 0;
     446        }
    379447    });
    380448
    381449    var list_name = [];
  • binaries/data/mods/public/gui/lobby/lobby.xml

     
    169169
    170170        <!-- Middle panel: Filters, game list, chat box. -->
    171171        <object name="middlePanel" size="20%+5 5% 100%-255 97.2%">
    172             <object name="gamesBox" style="ModernList" type="olist" size="0 25 100% 48%" font="sans-stroke-13">
     172            <object name="gamesBox" style="ModernList" type="olist" sortable="true" size="0 25 100% 48%" font="sans-stroke-13">
    173173                <action on="SelectionChange">updateGameSelection();</action>
     174                <action on="SelectionColumnChange">updateOrderSelection();</action>
    174175                <def id="name" color="0 60 0" width="27%">
    175176                    <translatableAttribute id="heading">Name</translatableAttribute>
    176177                </def>