From 42ffb0b554736a9666303ded0ffc6ec16d34f905 Mon Sep 17 00:00:00 2001 From: hzqst <113660872@qq.com> Date: Sat, 17 Feb 2024 21:15:17 +0800 Subject: [PATCH] Fix a bug that ClientUI layout in CounterStrike was broken. --- Plugins/VGUI2Extension/ClientVGUI.cpp | 92 +++++++++++++++++++----- Plugins/VGUI2Extension/SchemeHook.cpp | 97 ++++++++------------------ Plugins/VGUI2Extension/SurfaceHook.cpp | 54 ++++++++------ 3 files changed, 139 insertions(+), 104 deletions(-) diff --git a/Plugins/VGUI2Extension/ClientVGUI.cpp b/Plugins/VGUI2Extension/ClientVGUI.cpp index aece7929..925adfec 100644 --- a/Plugins/VGUI2Extension/ClientVGUI.cpp +++ b/Plugins/VGUI2Extension/ClientVGUI.cpp @@ -30,6 +30,8 @@ extern vgui::ISurface* g_pSurface; extern vgui::ISurface_HL25* g_pSurface_HL25; bool g_IsNativeClientVGUI2 = false; +bool g_IsNativeClientUIHDProportional = false; + IClientVGUI* g_pClientVGUI = NULL; CounterStrikeViewport* g_pCounterStrikeViewport = NULL; @@ -162,31 +164,56 @@ void __fastcall ClientVGUI_Panel_Init(vgui::Panel* pthis, int dummy, int x, int } } +void CSBuyMenu_ActivateInternal(vgui::Panel* pthis) +{ + pthis->MoveToFront(); + pthis->RequestFocus(); + pthis->SetVisible(true); + pthis->SetEnabled(true); + vgui::surface()->SetMinimized(pthis->GetVPanel(), false); + + int screenW, screenH; + vgui::surface()->GetScreenSize(screenW, screenH); + + pthis->SetPos(0, 0); + pthis->SetSize(screenW, screenH); + + int wide2 = vgui::scheme()->GetAlteredProportionalScaledValue(640); + int tall2 = vgui::scheme()->GetAlteredProportionalScaledValue(480); + + int offsetX = (screenW - wide2) / 2; + int offsetY = (screenH - tall2) / 2; + + int offset = min(offsetX, offsetY); + + pthis->SetPos(offset, offset); +} + void __fastcall CSBuyMenu_Activate(vgui::Panel* pthis, int dummy) { if (DpiManagerInternal()->IsHighDpiSupportEnabled()) { - pthis->MoveToFront(); - pthis->RequestFocus(); - pthis->SetVisible(true); - pthis->SetEnabled(true); - vgui::surface()->SetMinimized(pthis->GetVPanel(), false); + if (g_IsNativeClientUIHDProportional) + { + auto original = g_pVGuiSurface2->IsForcingHDProportional(); - int screenW, screenH; - vgui::surface()->GetScreenSize(screenW, screenH); + g_pVGuiSurface2->SetForcingHDProportional(true); - pthis->SetPos(0, 0); - pthis->SetSize(screenW, screenH); + CSBuyMenu_ActivateInternal(pthis); - int wide2 = vgui::scheme()->GetAlteredProportionalScaledValue(640); - int tall2 = vgui::scheme()->GetAlteredProportionalScaledValue(480); + g_pVGuiSurface2->SetForcingHDProportional(original); - int offsetX = (screenW - wide2) / 2; - int offsetY = (screenH - tall2) / 2; + } + else + { + auto original = g_pVGuiSurface2->IsForcingHDProportional(); - int offset = min(offsetX, offsetY); + g_pVGuiSurface2->SetForcingHDProportional(false); - pthis->SetPos(offset, offset); + CSBuyMenu_ActivateInternal(pthis); + + g_pVGuiSurface2->SetForcingHDProportional(original); + } } else { @@ -401,7 +428,7 @@ void __fastcall ClientVGUI_LoadControlSettings(vgui::Panel* pthis, int dummy, co gPrivateFuncs.ClientVGUI_LoadControlSettings(pthis, 0, controlResourceName, pathID); } -void ClientVGUI_KeyValues_FitToFullScreen(KeyValues* pControlKeyValues) +void ClientVGUI_KeyValues_FitToFullScreenInternal(KeyValues* pControlKeyValues) { int xpos = pControlKeyValues->GetInt("xpos"); int ypos = pControlKeyValues->GetInt("ypos"); @@ -422,6 +449,30 @@ void ClientVGUI_KeyValues_FitToFullScreen(KeyValues* pControlKeyValues) pControlKeyValues->SetInt("ypos", ypos); } +void ClientVGUI_KeyValues_FitToFullScreen(KeyValues* pControlKeyValues) +{ + if (g_IsNativeClientUIHDProportional) + { + auto original = g_pVGuiSurface2->IsForcingHDProportional(); + + g_pVGuiSurface2->SetForcingHDProportional(true); + + ClientVGUI_KeyValues_FitToFullScreenInternal(pControlKeyValues); + + g_pVGuiSurface2->SetForcingHDProportional(original); + } + else + { + auto original = g_pVGuiSurface2->IsForcingHDProportional(); + + g_pVGuiSurface2->SetForcingHDProportional(false); + + ClientVGUI_KeyValues_FitToFullScreenInternal(pControlKeyValues); + + g_pVGuiSurface2->SetForcingHDProportional(original); + } +} + void ClientVGUI_KeyValues_LoadFromFile_CounterStrike(KeyValues* pthis, const char* resourceName, const char* pathId) { if (DpiManagerInternal()->IsHighDpiSupportEnabled()) @@ -687,6 +738,15 @@ void CClientVGUIProxy::Initialize(CreateInterfaceFn *factories, int count) return; } + if (gEngfuncs.CheckParm("clientui_use_hdp", NULL)) + { + g_IsNativeClientUIHDProportional = true; + } + else if (gEngfuncs.CheckParm("clientui_no_hdp", NULL)) + { + g_IsNativeClientUIHDProportional = false; + } + VGUI2ExtensionInternal()->ClientVGUI_Initialize(factories, count); } diff --git a/Plugins/VGUI2Extension/SchemeHook.cpp b/Plugins/VGUI2Extension/SchemeHook.cpp index efc342b1..4444fa8f 100644 --- a/Plugins/VGUI2Extension/SchemeHook.cpp +++ b/Plugins/VGUI2Extension/SchemeHook.cpp @@ -9,6 +9,8 @@ using namespace vgui; #define CLIENTUI_USE_640_480_PROPBASE +extern bool g_IsNativeClientUIHDProportional; + extern vgui::ISurface2* g_pVGuiSurface2; extern vgui::ISchemeManager2 * g_pVGuiSchemeManager2; extern vgui::ISchemeManager * g_pSchemeManager; @@ -88,52 +90,40 @@ void CSchemeManagerProxy::Shutdown(bool full) int CSchemeManagerProxy::GetProportionalScaledValue(int normalizedValue) { - bool bNoHDProportional = false; - #ifdef CLIENTUI_USE_640_480_PROPBASE auto retaddr = (PUCHAR)_ReturnAddress(); - if (retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) - { - bNoHDProportional = true; - } -#endif - - if (bNoHDProportional) + if (g_pVGuiSurface2->IsForcingHDProportional() && !g_IsNativeClientUIHDProportional && retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) { - auto originalHDProportional = g_pVGuiSurface2->IsForcingHDProportional(); g_pVGuiSurface2->SetForcingHDProportional(false); auto r = g_pVGuiSchemeManager2->GetProportionalScaledValue(normalizedValue); - g_pVGuiSurface2->SetForcingHDProportional(originalHDProportional); + g_pVGuiSurface2->SetForcingHDProportional(true); + return r; } +#endif return g_pVGuiSchemeManager2->GetProportionalScaledValue(normalizedValue); } int CSchemeManagerProxy::GetProportionalNormalizedValue(int scaledValue) { - bool bNoHDProportional = false; #ifdef CLIENTUI_USE_640_480_PROPBASE auto retaddr = (PUCHAR)_ReturnAddress(); - if (retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) - { - bNoHDProportional = true; - } -#endif - if (bNoHDProportional) + if (g_pVGuiSurface2->IsForcingHDProportional() && !g_IsNativeClientUIHDProportional && retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) { - auto originalHDProportional = g_pVGuiSurface2->IsForcingHDProportional(); g_pVGuiSurface2->SetForcingHDProportional(false); auto r = g_pVGuiSchemeManager2->GetProportionalNormalizedValue(scaledValue); - g_pVGuiSurface2->SetForcingHDProportional(originalHDProportional); + g_pVGuiSurface2->SetForcingHDProportional(true); + return r; } +#endif return g_pVGuiSchemeManager2->GetProportionalNormalizedValue(scaledValue); } @@ -210,127 +200,100 @@ void CSchemeManagerProxy_HL25::Shutdown(bool full) int CSchemeManagerProxy_HL25::GetProportionalScaledValue(int normalizedValue) { - bool bNoHDProportional = false; #ifdef CLIENTUI_USE_640_480_PROPBASE auto retaddr = (PUCHAR)_ReturnAddress(); - if (retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) - { - bNoHDProportional = true; - } -#endif - if (bNoHDProportional) + if (g_pVGuiSurface2->IsForcingHDProportional() && !g_IsNativeClientUIHDProportional && retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) { - auto originalHDProportional = g_pVGuiSurface2->IsForcingHDProportional(); g_pVGuiSurface2->SetForcingHDProportional(false); auto r = g_pVGuiSchemeManager2->GetProportionalScaledValue(normalizedValue); - g_pVGuiSurface2->SetForcingHDProportional(originalHDProportional); + g_pVGuiSurface2->SetForcingHDProportional(true); + return r; } +#endif return g_pVGuiSchemeManager2->GetProportionalScaledValue(normalizedValue); } int CSchemeManagerProxy_HL25::GetProportionalNormalizedValue(int scaledValue) { - bool bNoHDProportional = false; - #ifdef CLIENTUI_USE_640_480_PROPBASE auto retaddr = (PUCHAR)_ReturnAddress(); - if (retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) - { - bNoHDProportional = true; - } -#endif - - if (bNoHDProportional) + if (g_pVGuiSurface2->IsForcingHDProportional() && !g_IsNativeClientUIHDProportional && retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) { - auto originalHDProportional = g_pVGuiSurface2->IsForcingHDProportional(); g_pVGuiSurface2->SetForcingHDProportional(false); auto r = g_pVGuiSchemeManager2->GetProportionalNormalizedValue(scaledValue); - g_pVGuiSurface2->SetForcingHDProportional(originalHDProportional); + g_pVGuiSurface2->SetForcingHDProportional(true); + return r; } +#endif return g_pVGuiSchemeManager2->GetProportionalNormalizedValue(scaledValue); } float CSchemeManagerProxy_HL25::GetProportionalScale(void) { - bool bNoHDProportional = false; #ifdef CLIENTUI_USE_640_480_PROPBASE auto retaddr = (PUCHAR)_ReturnAddress(); - if (retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) + if (g_pVGuiSurface2->IsForcingHDProportional() && !g_IsNativeClientUIHDProportional && retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) { - bNoHDProportional = true; - } -#endif - if (bNoHDProportional) - { - auto originalHDProportional = g_pVGuiSurface2->IsForcingHDProportional(); g_pVGuiSurface2->SetForcingHDProportional(false); auto r = g_pVGuiSchemeManager2->GetProportionalScale(); - g_pVGuiSurface2->SetForcingHDProportional(originalHDProportional); + g_pVGuiSurface2->SetForcingHDProportional(true); + return r; } +#endif return g_pVGuiSchemeManager2->GetProportionalScale(); } int CSchemeManagerProxy_HL25::GetHDProportionalScaledValue(int normalizedValue) { - bool bNoHDProportional = false; #ifdef CLIENTUI_USE_640_480_PROPBASE auto retaddr = (PUCHAR)_ReturnAddress(); - if (retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) + if (g_pVGuiSurface2->IsForcingHDProportional() && !g_IsNativeClientUIHDProportional && retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) { - bNoHDProportional = true; - } -#endif - if (bNoHDProportional) - { - auto originalHDProportional = g_pVGuiSurface2->IsForcingHDProportional(); g_pVGuiSurface2->SetForcingHDProportional(false); auto r = g_pVGuiSchemeManager2->GetHDProportionalScaledValue(normalizedValue); - g_pVGuiSurface2->SetForcingHDProportional(originalHDProportional); + g_pVGuiSurface2->SetForcingHDProportional(true); + return r; } +#endif return g_pVGuiSchemeManager2->GetHDProportionalScaledValue(normalizedValue); } int CSchemeManagerProxy_HL25::GetHDProportionalNormalizedValue(int normalizedValue) { - bool bNoHDProportional = false; #ifdef CLIENTUI_USE_640_480_PROPBASE auto retaddr = (PUCHAR)_ReturnAddress(); - if (retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) + if (g_pVGuiSurface2->IsForcingHDProportional() && !g_IsNativeClientUIHDProportional && retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) { - bNoHDProportional = true; - } -#endif - if (bNoHDProportional) - { - auto originalHDProportional = g_pVGuiSurface2->IsForcingHDProportional(); g_pVGuiSurface2->SetForcingHDProportional(false); auto r = g_pVGuiSchemeManager2->GetHDProportionalNormalizedValue(normalizedValue); - g_pVGuiSurface2->SetForcingHDProportional(originalHDProportional); + g_pVGuiSurface2->SetForcingHDProportional(true); + return r; } +#endif return g_pVGuiSchemeManager2->GetHDProportionalNormalizedValue(normalizedValue); } diff --git a/Plugins/VGUI2Extension/SurfaceHook.cpp b/Plugins/VGUI2Extension/SurfaceHook.cpp index d13b8b80..92eeb8a5 100644 --- a/Plugins/VGUI2Extension/SurfaceHook.cpp +++ b/Plugins/VGUI2Extension/SurfaceHook.cpp @@ -13,7 +13,7 @@ #include "Cursor.h" #include -#define CLIENTUI_USE_640_480_PROPBASE +extern bool g_IsNativeClientUIHDProportional; extern IEngineSurface *staticSurface; extern IEngineSurface_HL25 *staticSurface_HL25; @@ -813,18 +813,23 @@ void CSurfaceProxy::GetAbsoluteWindowBounds(int &x, int &y, int &wide, int &tall void CSurfaceProxy::GetProportionalBase(int &width, int &height) { - bool bNoHDProportional = false; - -#ifdef CLIENTUI_USE_640_480_PROPBASE auto retaddr = (PUCHAR)_ReturnAddress(); - if (retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) + if (g_bIsForcingHDProportional && !g_IsNativeClientUIHDProportional && retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) { - bNoHDProportional = true; + if (g_iProportionalBaseWidth && g_iProportionalBaseHeight) + { + width = g_iProportionalBaseWidth; + height = g_iProportionalBaseHeight; + } + else + { + m_pfnGetProportionalBase(g_pSurface, 0, width, height); + } + return; } -#endif - if (!bNoHDProportional && g_bIsForcingHDProportional && g_iProportionalBaseWidthHD && g_iProportionalBaseHeightHD) + if (g_bIsForcingHDProportional && g_iProportionalBaseWidthHD && g_iProportionalBaseHeightHD) { width = g_iProportionalBaseWidthHD; height = g_iProportionalBaseHeightHD; @@ -835,10 +840,11 @@ void CSurfaceProxy::GetProportionalBase(int &width, int &height) { width = g_iProportionalBaseWidth; height = g_iProportionalBaseHeight; - return; } - - m_pfnGetProportionalBase(g_pSurface, 0, width, height); + else + { + m_pfnGetProportionalBase(g_pSurface, 0, width, height); + } } void CSurfaceProxy::CalculateMouseVisible(void) @@ -1686,18 +1692,23 @@ void CSurfaceProxy_HL25::GetAbsoluteWindowBounds(int &x, int &y, int &wide, int void CSurfaceProxy_HL25::GetProportionalBase(int &width, int &height) { - bool bNoHDProportional = false; - auto retaddr = (PUCHAR)_ReturnAddress(); -#ifdef CLIENTUI_USE_640_480_PROPBASE - if (retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) + if (g_bIsForcingHDProportional && !g_IsNativeClientUIHDProportional && retaddr > g_dwClientTextBase && retaddr < (PUCHAR)g_dwClientTextBase + g_dwClientTextSize) { - bNoHDProportional = true; + if (g_iProportionalBaseWidth && g_iProportionalBaseHeight) + { + width = g_iProportionalBaseWidth; + height = g_iProportionalBaseHeight; + } + else + { + m_pfnGetProportionalBase(g_pSurface_HL25, 0, width, height); + } + return; } -#endif - if (!bNoHDProportional && g_bIsForcingHDProportional && g_iProportionalBaseWidthHD && g_iProportionalBaseHeightHD) + if (g_bIsForcingHDProportional && g_iProportionalBaseWidthHD && g_iProportionalBaseHeightHD) { width = g_iProportionalBaseWidthHD; height = g_iProportionalBaseHeightHD; @@ -1708,10 +1719,11 @@ void CSurfaceProxy_HL25::GetProportionalBase(int &width, int &height) { width = g_iProportionalBaseWidth; height = g_iProportionalBaseHeight; - return; } - - m_pfnGetProportionalBase(g_pSurface_HL25, 0, width, height); + else + { + m_pfnGetProportionalBase(g_pSurface_HL25, 0, width, height); + } } void CSurfaceProxy_HL25::CalculateMouseVisible(void)