From 96cc8c58300ae05e3fbb03d74e8e87e6752dd8bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Wed, 18 Sep 2024 17:36:37 +0200 Subject: [PATCH] Move `CMenus::RandomSkin` function to `CSkins::RandomizeSkin` This function is independent from the menus and this also makes it consistent with the `CSkins7::RandomizeSkin` function. --- src/game/client/components/menus.h | 1 - src/game/client/components/menus_settings.cpp | 47 +------------------ src/game/client/components/skins.cpp | 42 +++++++++++++++++ src/game/client/components/skins.h | 1 + 4 files changed, 45 insertions(+), 46 deletions(-) diff --git a/src/game/client/components/menus.h b/src/game/client/components/menus.h index e08a5fa11..c9797eb37 100644 --- a/src/game/client/components/menus.h +++ b/src/game/client/components/menus.h @@ -95,7 +95,6 @@ class CMenus : public CComponent void DoJoystickBar(const CUIRect *pRect, float Current, float Tolerance, bool Active); bool m_SkinListNeedsUpdate = false; - void RandomSkin(); // menus_settings_assets.cpp public: diff --git a/src/game/client/components/menus_settings.cpp b/src/game/client/components/menus_settings.cpp index db89a67d2..3176b38f5 100644 --- a/src/game/client/components/menus_settings.cpp +++ b/src/game/client/components/menus_settings.cpp @@ -404,50 +404,6 @@ void CMenus::OnRefreshSkins() m_SkinListNeedsUpdate = true; } -void CMenus::RandomSkin() -{ - static const float s_aSchemes[] = {1.0f / 2.0f, 1.0f / 3.0f, 1.0f / -3.0f, 1.0f / 12.0f, 1.0f / -12.0f}; // complementary, triadic, analogous - const bool UseCustomColor = !m_Dummy ? g_Config.m_ClPlayerUseCustomColor : g_Config.m_ClDummyUseCustomColor; - if(UseCustomColor) - { - float GoalSat = random_float(0.3f, 1.0f); - float MaxBodyLht = 1.0f - GoalSat * GoalSat; // max allowed lightness before we start losing saturation - - ColorHSLA Body; - Body.h = random_float(); - Body.l = random_float(0.0f, MaxBodyLht); - Body.s = clamp(GoalSat * GoalSat / (1.0f - Body.l), 0.0f, 1.0f); - - ColorHSLA Feet; - Feet.h = std::fmod(Body.h + s_aSchemes[rand() % std::size(s_aSchemes)], 1.0f); - Feet.l = random_float(); - Feet.s = clamp(GoalSat * GoalSat / (1.0f - Feet.l), 0.0f, 1.0f); - - unsigned *pColorBody = !m_Dummy ? &g_Config.m_ClPlayerColorBody : &g_Config.m_ClDummyColorBody; - unsigned *pColorFeet = !m_Dummy ? &g_Config.m_ClPlayerColorFeet : &g_Config.m_ClDummyColorFeet; - - *pColorBody = Body.Pack(false); - *pColorFeet = Feet.Pack(false); - } - - const size_t SkinNameSize = !m_Dummy ? sizeof(g_Config.m_ClPlayerSkin) : sizeof(g_Config.m_ClDummySkin); - char aRandomSkinName[24]; - str_copy(aRandomSkinName, "default", SkinNameSize); - if(!m_pClient->m_Skins.GetSkinsUnsafe().empty()) - { - do - { - auto it = m_pClient->m_Skins.GetSkinsUnsafe().begin(); - std::advance(it, rand() % m_pClient->m_Skins.GetSkinsUnsafe().size()); - str_copy(aRandomSkinName, (*it).second->GetName(), SkinNameSize); - } while(!str_comp(aRandomSkinName, "x_ninja") || !str_comp(aRandomSkinName, "x_spec")); - } - char *pSkinName = !m_Dummy ? g_Config.m_ClPlayerSkin : g_Config.m_ClDummySkin; - str_copy(pSkinName, aRandomSkinName, SkinNameSize); - - SetNeedSendInfo(); -} - void CMenus::Con_AddFavoriteSkin(IConsole::IResult *pResult, void *pUserData) { auto *pSelf = (CMenus *)pUserData; @@ -673,7 +629,8 @@ void CMenus::RenderSettingsTee(CUIRect MainView) TextRender()->SetRenderFlags(ETextRenderFlags::TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH | ETextRenderFlags::TEXT_RENDER_FLAG_NO_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_Y_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_PIXEL_ALIGMENT | ETextRenderFlags::TEXT_RENDER_FLAG_NO_OVERSIZE); if(DoButton_Menu(&s_RandomSkinButton, s_apDice[s_CurrentDie], 0, &RandomSkinButton, nullptr, IGraphics::CORNER_ALL, 5.0f, -0.2f)) { - RandomSkin(); + GameClient()->m_Skins.RandomizeSkin(m_Dummy); + SetNeedSendInfo(); s_CurrentDie = rand() % std::size(s_apDice); } TextRender()->SetRenderFlags(0); diff --git a/src/game/client/components/skins.cpp b/src/game/client/components/skins.cpp index 221a88059..9e66d0651 100644 --- a/src/game/client/components/skins.cpp +++ b/src/game/client/components/skins.cpp @@ -433,3 +433,45 @@ const CSkin *CSkins::FindImpl(const char *pName) return nullptr; } + +void CSkins::RandomizeSkin(int Dummy) +{ + static const float s_aSchemes[] = {1.0f / 2.0f, 1.0f / 3.0f, 1.0f / -3.0f, 1.0f / 12.0f, 1.0f / -12.0f}; // complementary, triadic, analogous + const bool UseCustomColor = Dummy ? g_Config.m_ClDummyUseCustomColor : g_Config.m_ClPlayerUseCustomColor; + if(UseCustomColor) + { + float GoalSat = random_float(0.3f, 1.0f); + float MaxBodyLht = 1.0f - GoalSat * GoalSat; // max allowed lightness before we start losing saturation + + ColorHSLA Body; + Body.h = random_float(); + Body.l = random_float(0.0f, MaxBodyLht); + Body.s = clamp(GoalSat * GoalSat / (1.0f - Body.l), 0.0f, 1.0f); + + ColorHSLA Feet; + Feet.h = std::fmod(Body.h + s_aSchemes[rand() % std::size(s_aSchemes)], 1.0f); + Feet.l = random_float(); + Feet.s = clamp(GoalSat * GoalSat / (1.0f - Feet.l), 0.0f, 1.0f); + + unsigned *pColorBody = Dummy ? &g_Config.m_ClDummyColorBody : &g_Config.m_ClPlayerColorBody; + unsigned *pColorFeet = Dummy ? &g_Config.m_ClDummyColorFeet : &g_Config.m_ClPlayerColorFeet; + + *pColorBody = Body.Pack(false); + *pColorFeet = Feet.Pack(false); + } + + const size_t SkinNameSize = Dummy ? sizeof(g_Config.m_ClDummySkin) : sizeof(g_Config.m_ClPlayerSkin); + char aRandomSkinName[24]; + str_copy(aRandomSkinName, "default", SkinNameSize); + if(!m_pClient->m_Skins.GetSkinsUnsafe().empty()) + { + do + { + auto it = m_pClient->m_Skins.GetSkinsUnsafe().begin(); + std::advance(it, rand() % m_pClient->m_Skins.GetSkinsUnsafe().size()); + str_copy(aRandomSkinName, (*it).second->GetName(), SkinNameSize); + } while(!str_comp(aRandomSkinName, "x_ninja") || !str_comp(aRandomSkinName, "x_spec")); + } + char *pSkinName = Dummy ? g_Config.m_ClDummySkin : g_Config.m_ClPlayerSkin; + str_copy(pSkinName, aRandomSkinName, SkinNameSize); +} diff --git a/src/game/client/components/skins.h b/src/game/client/components/skins.h index ecab4112f..cf648f6ad 100644 --- a/src/game/client/components/skins.h +++ b/src/game/client/components/skins.h @@ -66,6 +66,7 @@ public: std::unordered_map> &GetSkinsUnsafe() { return m_Skins; } const CSkin *FindOrNullptr(const char *pName, bool IgnorePrefix = false); const CSkin *Find(const char *pName); + void RandomizeSkin(int Dummy); bool IsDownloadingSkins() { return m_DownloadingSkins; }