Skip to content

Commit

Permalink
Add vscript SetHudElementVisible
Browse files Browse the repository at this point in the history
  • Loading branch information
samisalreadytaken committed Nov 1, 2022
1 parent 2b612a2 commit a4c78a0
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sp/src/game/client/hudelement.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class CHudElement : public CGameEventListener
// 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 );
Expand Down
83 changes: 83 additions & 0 deletions sp/src/game/client/mapbase/vscript_vgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

#include "view.h"

#include "hudelement.h"

#include "vscript_vgui.h"
#include "vscript_vgui.nut"

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand All @@ -3426,7 +3460,54 @@ 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, "" )
{
for ( int i = 0; i < gHUD.m_HudList.Size(); i++ )
{
Msg( "%s\n", gHUD.m_HudList[i]->GetName() );
}
}
#endif


class CScriptIInput
Expand Down Expand Up @@ -3693,6 +3774,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", "" );

Expand Down

0 comments on commit a4c78a0

Please sign in to comment.