Skip to content

Commit

Permalink
Refactoring skin refresh, fix skin list empty on Android initially
Browse files Browse the repository at this point in the history
  • Loading branch information
Robyt3 committed Dec 1, 2024
1 parent 3277223 commit c9de8f8
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/game/client/components/menus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1815,7 +1815,7 @@ void CMenus::RenderPopupFullscreen(CUIRect Screen)
if(m_pClient->m_Skins7.SaveSkinfile(m_SkinNameInput.GetString(), m_Dummy))
{
m_Popup = POPUP_NONE;
m_SkinListNeedsUpdate = true;
m_SkinList7LastRefreshTime = std::nullopt;
}
else
PopupMessage(Localize("Error"), Localize("Unable to save the skin"), Localize("Ok"), POPUP_SAVE_SKIN);
Expand Down
6 changes: 4 additions & 2 deletions src/game/client/components/menus.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <chrono>
#include <deque>
#include <optional>
#include <unordered_set>
#include <vector>

Expand Down Expand Up @@ -94,8 +95,10 @@ class CMenus : public CComponent
void DoJoystickAxisPicker(CUIRect View);
void DoJoystickBar(const CUIRect *pRect, float Current, float Tolerance, bool Active);

bool m_SkinListNeedsUpdate = false;
std::optional<std::chrono::nanoseconds> m_SkinListLastRefreshTime;
bool m_SkinListScrollToSelected = false;
std::optional<std::chrono::nanoseconds> m_SkinList7LastRefreshTime;
std::optional<std::chrono::nanoseconds> m_SkinPartsList7LastRefreshTime;

int m_DirectionQuadContainerIndex;

Expand Down Expand Up @@ -668,7 +671,6 @@ class CMenus : public CComponent

virtual void OnStateChange(int NewState, int OldState) override;
virtual void OnWindowResize() override;
virtual void OnRefreshSkins() override;
virtual void OnReset() override;
virtual void OnRender() override;
virtual bool OnInput(const IInput::CEvent &Event) override;
Expand Down
15 changes: 4 additions & 11 deletions src/game/client/components/menus_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,6 @@ struct CUISkin
bool operator==(const char *pOther) const { return !str_comp_nocase(m_pSkin->GetName(), pOther); }
};

void CMenus::OnRefreshSkins()
{
m_SkinListNeedsUpdate = true;
}

void CMenus::Con_AddFavoriteSkin(IConsole::IResult *pResult, void *pUserData)
{
auto *pSelf = (CMenus *)pUserData;
Expand Down Expand Up @@ -713,10 +708,9 @@ void CMenus::RenderSettingsTee(CUIRect MainView)
static CListBox s_ListBox;

// be nice to the CPU
static std::chrono::nanoseconds s_SkinLastRefreshTime = m_pClient->m_Skins.LastRefreshTime();
if(m_SkinListNeedsUpdate || m_SkinFavoritesChanged || s_SkinLastRefreshTime != m_pClient->m_Skins.LastRefreshTime())
if(m_SkinFavoritesChanged || !m_SkinListLastRefreshTime.has_value() || m_SkinListLastRefreshTime.value() != m_pClient->m_Skins.LastRefreshTime())
{
s_SkinLastRefreshTime = m_pClient->m_Skins.LastRefreshTime();
m_SkinListLastRefreshTime = m_pClient->m_Skins.LastRefreshTime();
s_vSkinList.clear();
s_vSkinListHelper.clear();
s_vFavoriteSkinListHelper.clear();
Expand Down Expand Up @@ -756,7 +750,6 @@ void CMenus::RenderSettingsTee(CUIRect MainView)
std::sort(s_vFavoriteSkinListHelper.begin(), s_vFavoriteSkinListHelper.end());
s_vSkinList = s_vFavoriteSkinListHelper;
s_vSkinList.insert(s_vSkinList.end(), s_vSkinListHelper.begin(), s_vSkinListHelper.end());
m_SkinListNeedsUpdate = false;
}

int OldSelected = -1;
Expand Down Expand Up @@ -820,7 +813,7 @@ void CMenus::RenderSettingsTee(CUIRect MainView)
{
m_SkinFavorites.emplace(pSkinToBeDraw->GetName());
}
m_SkinListNeedsUpdate = true;
m_SkinListLastRefreshTime = std::nullopt;
}
}
}
Expand All @@ -835,7 +828,7 @@ void CMenus::RenderSettingsTee(CUIRect MainView)
static CLineInput s_SkinFilterInput(g_Config.m_ClSkinFilterString, sizeof(g_Config.m_ClSkinFilterString));
if(Ui()->DoEditBox_Search(&s_SkinFilterInput, &QuickSearch, 14.0f, !Ui()->IsPopupOpen() && m_pClient->m_GameConsole.IsClosed()))
{
m_SkinListNeedsUpdate = true;
m_SkinListLastRefreshTime = std::nullopt;
}

static CButtonContainer s_SkinDatabaseButton;
Expand Down
20 changes: 6 additions & 14 deletions src/game/client/components/menus_settings7.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ void CMenus::RenderSettingsTee7(CUIRect MainView)
static CLineInput s_SkinFilterInput(g_Config.m_ClSkinFilterString, sizeof(g_Config.m_ClSkinFilterString));
if(Ui()->DoEditBox_Search(&s_SkinFilterInput, &QuickSearch, 14.0f, !Ui()->IsPopupOpen() && m_pClient->m_GameConsole.IsClosed()))
{
m_SkinListNeedsUpdate = true;
m_SkinPartListNeedsUpdate = true;
m_SkinList7LastRefreshTime = std::nullopt;
m_SkinPartsList7LastRefreshTime = std::nullopt;
}

static CButtonContainer s_DirectoryButton;
Expand Down Expand Up @@ -317,14 +317,11 @@ void CMenus::RenderSkinSelection7(CUIRect MainView)
static float s_LastSelectionTime = -10.0f;
static std::vector<const CSkins7::CSkin *> s_vpSkinList;
static CListBox s_ListBox;
static size_t s_SkinCount = 0;

const std::vector<CSkins7::CSkin> &vCurrentSkins = GameClient()->m_Skins7.GetSkins();
if(m_SkinListNeedsUpdate || vCurrentSkins.size() != s_SkinCount)
if(!m_SkinList7LastRefreshTime.has_value() || m_SkinList7LastRefreshTime.value() != m_SkinList7LastRefreshTime)
{
s_vpSkinList.clear();
s_SkinCount = vCurrentSkins.size();
for(const CSkins7::CSkin &Skin : vCurrentSkins)
for(const CSkins7::CSkin &Skin : GameClient()->m_Skins7.GetSkins())
{
if((Skin.m_Flags & CSkins7::SKINFLAG_SPECIAL) != 0)
continue;
Expand All @@ -333,7 +330,6 @@ void CMenus::RenderSkinSelection7(CUIRect MainView)

s_vpSkinList.emplace_back(&Skin);
}
m_SkinListNeedsUpdate = false;
}

m_pSelectedSkin = nullptr;
Expand Down Expand Up @@ -405,15 +401,12 @@ void CMenus::RenderSkinPartSelection7(CUIRect MainView)
{
static std::vector<const CSkins7::CSkinPart *> s_paList[protocol7::NUM_SKINPARTS];
static CListBox s_ListBox;
static size_t s_aSkinPartCount[protocol7::NUM_SKINPARTS] = {0};
for(int Part = 0; Part < protocol7::NUM_SKINPARTS; Part++)
{
const std::vector<CSkins7::CSkinPart> &vCurrentSkinParts = GameClient()->m_Skins7.GetSkinParts(Part);
if(m_SkinPartListNeedsUpdate || vCurrentSkinParts.size() != s_aSkinPartCount[Part])
if(!m_SkinList7LastRefreshTime.has_value() || m_SkinList7LastRefreshTime.value() != GameClient()->m_Skins7.LastRefreshTime())
{
s_paList[Part].clear();
s_aSkinPartCount[Part] = vCurrentSkinParts.size();
for(const CSkins7::CSkinPart &SkinPart : vCurrentSkinParts)
for(const CSkins7::CSkinPart &SkinPart : GameClient()->m_Skins7.GetSkinParts(Part))
{
if((SkinPart.m_Flags & CSkins7::SKINFLAG_SPECIAL) != 0)
continue;
Expand All @@ -425,7 +418,6 @@ void CMenus::RenderSkinPartSelection7(CUIRect MainView)
}
}
}
m_SkinPartListNeedsUpdate = false;

static int s_OldSelected = -1;
s_ListBox.DoBegin(&MainView);
Expand Down
2 changes: 2 additions & 0 deletions src/game/client/components/skins7.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ void CSkins7::OnInit()
LoadXmasHat();
LoadBotDecoration();
GameClient()->m_Menus.RenderLoading(Localize("Loading DDNet Client"), Localize("Loading skin files"), 0);

m_LastRefreshTime = time_get_nanoseconds();
}

void CSkins7::InitPlaceholderSkinParts()
Expand Down
4 changes: 4 additions & 0 deletions src/game/client/components/skins7.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <game/generated/protocol.h>
#include <game/generated/protocol7.h>

#include <chrono>
#include <vector>

class CSkins7 : public CComponent
Expand Down Expand Up @@ -66,6 +67,8 @@ class CSkins7 : public CComponent
int Sizeof() const override { return sizeof(*this); }
void OnInit() override;

std::chrono::nanoseconds LastRefreshTime() const { return m_LastRefreshTime; }

const std::vector<CSkin> &GetSkins() const;
const std::vector<CSkinPart> &GetSkinParts(int Part) const;
const CSkinPart *FindSkinPartOrNullptr(int Part, const char *pName, bool AllowSpecialPart) const;
Expand All @@ -87,6 +90,7 @@ class CSkins7 : public CComponent

private:
int m_ScanningPart;
std::chrono::nanoseconds m_LastRefreshTime;

std::vector<CSkinPart> m_avSkinParts[protocol7::NUM_SKINPARTS];
CSkinPart m_aPlaceholderSkinParts[protocol7::NUM_SKINPARTS];
Expand Down

0 comments on commit c9de8f8

Please sign in to comment.