Skip to content

Commit

Permalink
Merge pull request ddnet#8825 from KebsCS/pr-ghost-sorting
Browse files Browse the repository at this point in the history
Add ghost list sorting
  • Loading branch information
Robyt3 authored Aug 27, 2024
2 parents 7c8f9b4 + 906f75c commit 14f1b36
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/engine/shared/config_variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ MACRO_CONFIG_INT(BrDemoSort, br_demo_sort, 0, 0, 2, CFGFLAG_SAVE | CFGFLAG_CLIEN
MACRO_CONFIG_INT(BrDemoSortOrder, br_demo_sort_order, 0, 0, 1, CFGFLAG_SAVE | CFGFLAG_CLIENT, "Sorting order in demo browser")
MACRO_CONFIG_INT(BrDemoFetchInfo, br_demo_fetch_info, 0, 0, 1, CFGFLAG_SAVE | CFGFLAG_CLIENT, "Whether to auto fetch demo infos on refresh")

MACRO_CONFIG_INT(GhSort, gh_sort, 1, 0, 2, CFGFLAG_SAVE | CFGFLAG_CLIENT, "Sorting column in ghost list")
MACRO_CONFIG_INT(GhSortOrder, gh_sort_order, 0, 0, 1, CFGFLAG_SAVE | CFGFLAG_CLIENT, "Sorting order in ghost list")

MACRO_CONFIG_INT(SndBufferSize, snd_buffer_size, 512, 128, 32768, CFGFLAG_SAVE | CFGFLAG_CLIENT, "Sound buffer size (may cause delay if large)")
MACRO_CONFIG_INT(SndRate, snd_rate, 48000, 5512, 384000, CFGFLAG_SAVE | CFGFLAG_CLIENT, "Sound mixing rate")
MACRO_CONFIG_INT(SndEnable, snd_enable, 1, 0, 1, CFGFLAG_SAVE | CFGFLAG_CLIENT, "Sound enable")
Expand Down
9 changes: 9 additions & 0 deletions src/game/client/components/menus.h
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,14 @@ class CMenus : public CComponent
bool HasFile() const { return m_aFilename[0]; }
};

enum
{
GHOST_SORT_NONE = -1,
GHOST_SORT_NAME,
GHOST_SORT_TIME,
GHOST_SORT_DATE,
};

std::vector<CGhostItem> m_vGhosts;

std::chrono::nanoseconds m_GhostPopulateStartTime{0};
Expand All @@ -776,6 +784,7 @@ class CMenus : public CComponent
CGhostItem *GetOwnGhost();
void UpdateOwnGhost(CGhostItem Item);
void DeleteGhostItem(int Index);
void SortGhostlist();

bool CanDisplayWarning() const;

Expand Down
51 changes: 42 additions & 9 deletions src/game/client/components/menus_ingame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ void CMenus::GhostlistPopulate()
m_vGhosts.clear();
m_GhostPopulateStartTime = time_get_nanoseconds();
Storage()->ListDirectoryInfo(IStorage::TYPE_ALL, m_pClient->m_Ghost.GetGhostDir(), GhostlistFetchCallback, this);
std::sort(m_vGhosts.begin(), m_vGhosts.end());
SortGhostlist();

CGhostItem *pOwnGhost = 0;
for(auto &Ghost : m_vGhosts)
Expand Down Expand Up @@ -1000,6 +1000,7 @@ void CMenus::UpdateOwnGhost(CGhostItem Item)
Item.m_Date = std::time(0);
Item.m_Failed = false;
m_vGhosts.insert(std::lower_bound(m_vGhosts.begin(), m_vGhosts.end(), Item), Item);
SortGhostlist();
}

void CMenus::DeleteGhostItem(int Index)
Expand All @@ -1009,6 +1010,22 @@ void CMenus::DeleteGhostItem(int Index)
m_vGhosts.erase(m_vGhosts.begin() + Index);
}

void CMenus::SortGhostlist()
{
if(g_Config.m_GhSort == GHOST_SORT_NAME)
std::stable_sort(m_vGhosts.begin(), m_vGhosts.end(), [](const CGhostItem &Left, const CGhostItem &Right) {
return g_Config.m_GhSortOrder ? (str_comp(Left.m_aPlayer, Right.m_aPlayer) > 0) : (str_comp(Left.m_aPlayer, Right.m_aPlayer) < 0);
});
else if(g_Config.m_GhSort == GHOST_SORT_TIME)
std::stable_sort(m_vGhosts.begin(), m_vGhosts.end(), [](const CGhostItem &Left, const CGhostItem &Right) {
return g_Config.m_GhSortOrder ? (Left.m_Time > Right.m_Time) : (Left.m_Time < Right.m_Time);
});
else if(g_Config.m_GhSort == GHOST_SORT_DATE)
std::stable_sort(m_vGhosts.begin(), m_vGhosts.end(), [](const CGhostItem &Left, const CGhostItem &Right) {
return g_Config.m_GhSortOrder ? (Left.m_Date > Right.m_Date) : (Left.m_Date < Right.m_Date);
});
}

void CMenus::RenderGhost(CUIRect MainView)
{
// render background
Expand All @@ -1029,10 +1046,12 @@ void CMenus::RenderGhost(CUIRect MainView)
Headers.Draw(ColorRGBA(1, 1, 1, 0.25f), IGraphics::CORNER_T, 5.0f);
Headers.VSplitRight(20.0f, &Headers, 0);

struct CColumn
class CColumn
{
public:
const char *m_pCaption;
int m_Id;
int m_Sort;
float m_Width;
CUIRect m_Rect;
};
Expand All @@ -1046,11 +1065,11 @@ void CMenus::RenderGhost(CUIRect MainView)
};

static CColumn s_aCols[] = {
{"", -1, 2.0f, {0}},
{"", COL_ACTIVE, 30.0f, {0}},
{Localizable("Name"), COL_NAME, 200.0f, {0}},
{Localizable("Time"), COL_TIME, 90.0f, {0}},
{Localizable("Date"), COL_DATE, 150.0f, {0}},
{"", -1, GHOST_SORT_NONE, 2.0f, {0}},
{"", COL_ACTIVE, GHOST_SORT_NONE, 30.0f, {0}},
{Localizable("Name"), COL_NAME, GHOST_SORT_NAME, 200.0f, {0}},
{Localizable("Time"), COL_TIME, GHOST_SORT_TIME, 90.0f, {0}},
{Localizable("Date"), COL_DATE, GHOST_SORT_DATE, 150.0f, {0}},
};

int NumCols = std::size(s_aCols);
Expand All @@ -1065,8 +1084,22 @@ void CMenus::RenderGhost(CUIRect MainView)
}

// do headers
for(int i = 0; i < NumCols; i++)
DoButton_GridHeader(&s_aCols[i].m_Id, Localize(s_aCols[i].m_pCaption), 0, &s_aCols[i].m_Rect);
for(const auto &Col : s_aCols)
{
if(DoButton_GridHeader(&Col.m_Id, Localize(Col.m_pCaption), g_Config.m_GhSort == Col.m_Sort, &Col.m_Rect))
{
if(Col.m_Sort != GHOST_SORT_NONE)
{
if(g_Config.m_GhSort == Col.m_Sort)
g_Config.m_GhSortOrder ^= 1;
else
g_Config.m_GhSortOrder = 0;
g_Config.m_GhSort = Col.m_Sort;

SortGhostlist();
}
}
}

View.Draw(ColorRGBA(0, 0, 0, 0.15f), 0, 0);

Expand Down

0 comments on commit 14f1b36

Please sign in to comment.