Skip to content

Commit

Permalink
Move version display out into it's own HUD panel.
Browse files Browse the repository at this point in the history
This pulls version from a version.txt file now too. If we exclude that, it won't get displayed, aaand there's a cvar for toggling it now (not archived for now). Yay.
  • Loading branch information
hogsy committed Oct 6, 2022
1 parent 36ddd15 commit ff4f2ec
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 9 deletions.
1 change: 1 addition & 0 deletions mp/src/game/client/client_hl2mp.vpc
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ $Project "Client"
$File fortress/hud_technologytreedoc.h
$File fortress/hud_timer.cpp
$File fortress/hud_timer.h
$File fortress/hud_version.cpp
$File fortress/hud_vehicle_role.cpp
$File fortress/hud_vehicle_role.h
$File fortress/hud_weaponselection.cpp
Expand Down
7 changes: 0 additions & 7 deletions mp/src/game/client/fortress/clientmode_tfnormal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,11 @@ ClientModeTFNormal::Viewport::Viewport() :
#endif

SetScheme( vgui::scheme()->LoadSchemeFromFileEx( enginevgui->GetPanel( PANEL_CLIENTDLL ), "resource/teamscheme.res", "TeamScheme" ) );

versionLabel = new vgui::Label( this, "VersionLabel", "PRE-ALPHA (v0.0.1.3)" );
versionLabel->SetContentAlignment( vgui::Label::Alignment::a_center );
versionLabel->SetWide( GetWide() );
versionLabel->SetPaintBackgroundEnabled( false );
}

ClientModeTFNormal::Viewport::~Viewport() {}

void ClientModeTFNormal::Viewport::Paint() {
versionLabel->SetWide( GetWide() );

BaseClass::Paint();
}

Expand Down
2 changes: 0 additions & 2 deletions mp/src/game/client/fortress/clientmode_tfnormal.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ DECLARE_CLASS( ClientModeTFNormal, ClientModeTFBase );
vgui::HCursor m_CursorCommander;
vgui::HCursor m_CursorRightMouseMove;
// hogsy end

vgui::Label *versionLabel;
};

virtual void Update() override;
Expand Down
122 changes: 122 additions & 0 deletions mp/src/game/client/fortress/hud_version.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
"Invasion" project by TalonBrave.info, Copyright (C) 2014-2022 (see README.md for list of contributors)
*/

#include "cbase.h"
#include "hud.h"
#include "hudelement.h"
#include "vgui/ISurface.h"
#include "vgui_controls/Panel.h"
#include "vgui_controls/Label.h"
#include "vgui_controls/Controls.h"
#include "clientmode.h"
#include "filesystem.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

class CHudVersion : public CHudElement, public vgui::Panel
{
DECLARE_CLASS_SIMPLE( CHudVersion, vgui::Panel );

public:
CHudVersion( const char *elementName );
~CHudVersion();

private:
void Init() override;
void Paint() override;
void OnThink() override;
void ApplySchemeSettings( vgui::IScheme *scheme ) override;

char versionLabel[ 128 ];
vgui::HFont labelFont;

static const char *GetVersionPath() { return "version.txt"; }
};

static ConVar showVersion( "show_version", "1" );

DECLARE_HUDELEMENT( CHudVersion );

CHudVersion::CHudVersion( const char *elementName ) : CHudElement( elementName ), vgui::Panel( nullptr, "HudVersion" )
{
Panel *pParent = g_pClientMode->GetViewport();
SetParent( pParent );

SetActive( true );
SetPaintBorderEnabled( true );

SetHiddenBits( HIDEHUD_ALL );

versionLabel[ 0 ] = '\0';
}

CHudVersion::~CHudVersion()
{
}

void CHudVersion::Init()
{
CHudElement::Init();

CUtlBuffer buffer;
if ( !filesystem->ReadFile( GetVersionPath(), "GAME", buffer ) )
{
Msg( "Failed to fetch \"%s\", will not display version.\n", GetVersionPath() );
return;
}

const char *str = ( char * ) buffer.Base();
assert( *str != '\0' );
if ( *str == '\0' )
Error( "Invalid version provided under \"%s\"!\n", GetVersionPath() );

char *end;
unsigned int versionMajor = strtoul( str, &end, 10 );
unsigned int versionMinor = strtoul( end, &end, 10 );
unsigned int versionPatch = strtoul( end, NULL, 10 );

V_snprintf( versionLabel, sizeof( versionLabel ), "v%u.%u.%u (Pre-Alpha)", versionMajor, versionMinor, versionPatch );

SetVisible( true );
}

void CHudVersion::Paint()
{
if ( *versionLabel == '\0' )
return;

vgui::HScheme scheme = vgui::scheme()->GetScheme( "ClientScheme" );
vgui::HFont font = vgui::scheme()->GetIScheme( scheme )->GetFont( "CommentaryDefault" );
if ( !font )
font = vgui::scheme()->GetIScheme( scheme )->GetFont( "Default" );

vgui::surface()->DrawSetTextFont( font );
vgui::surface()->DrawSetTextColor( Color( 100, 255, 100, GetAlpha() ) );

int pixels = UTIL_ComputeStringWidth( font, versionLabel );
vgui::surface()->DrawSetTextPos( ( GetWide() - pixels ) / 2, 22 );

char *p = versionLabel;
while ( *p )
{
vgui::surface()->DrawUnicodeChar( *p );
p++;
}
}

void CHudVersion::OnThink()
{
SetVisible( showVersion.GetBool() );
}

void CHudVersion::ApplySchemeSettings( vgui::IScheme *scheme )
{
BaseClass::ApplySchemeSettings( scheme );

SetPaintBackgroundEnabled( true );
SetPaintBackgroundType( 2 );

SetBgColor( Color( 0, 0, 0, 255 ) );
}

0 comments on commit ff4f2ec

Please sign in to comment.