From 760e1e6dd5233f439d86ef5b496b715b992cbb55 Mon Sep 17 00:00:00 2001 From: samisalreadytaken <46823719+samisalreadytaken@users.noreply.github.com> Date: Tue, 1 Nov 2022 15:28:00 +0300 Subject: [PATCH] Add vscript SetHudElementVisible --- sp/src/game/client/hudelement.h | 3 + sp/src/game/client/mapbase/vscript_vgui.cpp | 99 +++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/sp/src/game/client/hudelement.h b/sp/src/game/client/hudelement.h index b903de5d..824cb975 100644 --- a/sp/src/game/client/hudelement.h +++ b/sp/src/game/client/hudelement.h @@ -58,6 +58,9 @@ public: // Hidden bits. // HIDEHUD_ flags that note when this element should be hidden in the HUD virtual void SetHiddenBits( int iBits ); +#ifdef MAPBASE_VSCRIPT + int GetHiddenBits() const { return m_iHiddenBits; } +#endif bool IsParentedToClientDLLRootPanel() const; void SetParentedToClientDLLRootPanel( bool parented ); diff --git a/sp/src/game/client/mapbase/vscript_vgui.cpp b/sp/src/game/client/mapbase/vscript_vgui.cpp index 61354093..bed046f0 100644 --- a/sp/src/game/client/mapbase/vscript_vgui.cpp +++ b/sp/src/game/client/mapbase/vscript_vgui.cpp @@ -49,6 +49,8 @@ #include "view.h" +#include "hudelement.h" + #include "vscript_vgui.h" #include "vscript_vgui.nut" @@ -3329,6 +3331,18 @@ END_SCRIPTDESC() //============================================================== +struct hudelementcache_t +{ + CUtlConstString name; + int bits; +}; +CUtlVector< hudelementcache_t > m_HudElementCache; + +// Check if hud elements were changed in this level to shortcut on level shutdown +bool m_bHudVisiblityChangedThisLevel = false; + + + class CScriptVGUI : public CAutoGameSystem { public: @@ -3403,6 +3417,26 @@ void CScriptVGUI::LevelShutdownPostEntity() surface()->DestroyTextureID( g_ScriptTextureIDs[i] ); } g_ScriptTextureIDs.Purge(); + + // + // Reset hud element visibility + // + if ( m_bHudVisiblityChangedThisLevel ) + { + m_bHudVisiblityChangedThisLevel = false; + + FOR_EACH_VEC( m_HudElementCache, i ) + { + const hudelementcache_t &cache = m_HudElementCache[i]; + Assert( !cache.name.IsEmpty() ); + CHudElement *elem = gHUD.FindElement( cache.name ); + Assert( elem ); + if ( elem ) + { + elem->SetHiddenBits( cache.bits ); + } + } + } } void CScriptVGUI::Shutdown() @@ -3426,9 +3460,72 @@ void CScriptVGUI::Shutdown() } g_ScriptFonts.Purge(); + + m_HudElementCache.Purge(); } +void SetHudElementVisible( const char *name, bool state ) +{ + CHudElement *elem = gHUD.FindElement( name ); + if ( !elem ) + return; + + int iOldBits = -2; + + FOR_EACH_VEC( m_HudElementCache, i ) + { + const hudelementcache_t &cache = m_HudElementCache[i]; + if ( !V_stricmp( cache.name, name ) ) + { + iOldBits = cache.bits; + break; + } + } + + if ( iOldBits == -2 ) + { + if ( state ) // no change + return; + + // First time setting the visibility of this element, save the original bits + hudelementcache_t &cache = m_HudElementCache.Element( m_HudElementCache.AddToTail() ); + cache.name.Set( name ); + cache.bits = elem->GetHiddenBits(); + } + + elem->SetHiddenBits( state ? iOldBits : -1 ); + + m_bHudVisiblityChangedThisLevel = true; +} + +#ifdef _DEBUG +CON_COMMAND( dump_hud_elements, "" ) +{ + int size = gHUD.m_HudList.Size(); + + CUtlVector< const char* > list( 0, size ); + + for ( int i = 0; i < size; i++ ) + { + list.AddToTail( gHUD.m_HudList[i]->GetName() ); + } + + struct _cmp + { + static int __cdecl fn( const char * const *a, const char * const *b ) { return strcmp( *a, *b ); } + }; + + list.Sort( _cmp::fn ); + + for ( int i = 0; i < size; i++ ) + { + Msg( "%s\n", list[i] ); + } +} +#endif + + class CScriptIInput { public: @@ -3693,6 +3790,8 @@ vgui::HFont GetScriptFont( const char *name, bool proportional ) void RegisterScriptVGUI() { + ScriptRegisterFunction( g_pScriptVM, SetHudElementVisible, "" ); + ScriptRegisterFunctionNamed( g_pScriptVM, ScriptXRES, "XRES", "" ); ScriptRegisterFunctionNamed( g_pScriptVM, ScriptYRES, "YRES", "" );