CHudFadingObject

From Valve Developer Community
Jump to navigation Jump to search
Wikipedia - Letter.png
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)
Dead End - Icon.png
This article has no Wikipedia icon links to other VDC articles. Please help improve this article by adding links Wikipedia icon that are relevant to the context within the existing text.
January 2024

CHudFadingObject is a baseclass for fading numeric elements.(fade in hud element when called or value changed for a certain amount of time then fade out). I used it to clear up the non essential elements such as ammo/game mode/etc.

hud_basefadingobject.h

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------

#ifndef HUD_FADINGOBJECT_H
#define HUD_FADINGOBJECT_H
#ifdef _WIN32
#pragma once
#endif

#include "hud_numericdisplay.h"

class CHudFadingObject : public CHudNumericDisplay
{
	DECLARE_CLASS_SIMPLE( CHudFadingObject, CHudNumericDisplay );

public:
	CHudFadingObject(vgui::Panel *parent, const char *name);

private:
	//OVERRIDE THESE
	virtual void FadeIn();
	virtual void FadeOut();

	virtual int  GetValue();

	virtual float Visible_Duration() 
	{ return 6.0f; }
	//OVERRIDE THESE

	virtual void OnThink( void );
	virtual void Reset( void );

protected:
	void Activate();

	float	m_flExpireTime;
	bool	m_bShow;
	int		m_iLastValue;
};

#endif

hud_basefadingobject.cpp

#include "cbase.h"
#include "hud.h"
#include "hudelement.h"
#include "hud_macros.h"
#include "iclientmode.h"
#include "hl2mp_gamerules.h"

#include "vgui_controls/AnimationController.h"
#include "vgui/ILocalize.h"

#include "hud_basefadingobject.h"
using namespace vgui;

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

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CHudFadingObject::CHudFadingObject(vgui::Panel *parent, const char *name) : BaseClass(parent, name)
{
	m_bShow = false;
	m_flExpireTime = 0.0f;
	m_iLastValue = 0;
}

void CHudFadingObject::Reset( void )
{Activate();}

void CHudFadingObject::FadeIn()
{/*play corresponding animation*/}

void CHudFadingObject::FadeOut()
{/*play corresponding animation*/}

void CHudFadingObject::Activate()
{
	m_iLastValue = GetValue(); 
	FadeIn();
	m_flExpireTime = gpGlobals->curtime + Visible_Duration() ;
	m_bShow = true;
}

int CHudFadingObject::GetValue()
{
	return 0;
}

//-----------------------------------------------------------------------------
void CHudFadingObject::OnThink( void )
{
	if(!IsVisible())
	return;

	if ( m_bShow && (m_flExpireTime <= gpGlobals->curtime) )
	{
		m_bShow = false;
		FadeOut();
	}

	if (GetValue() != m_iLastValue)
		Activate();
	
	SetDisplayValue(GetValue());
}

Example

//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//
#include "cbase.h"
#include "hud.h"
#include "hudelement.h"
#include "hud_macros.h"
#include "hud_numericdisplay.h"
#include "iclientmode.h"
#include "hl2mp_gamerules.h"

#include "vgui_controls/AnimationController.h"
#include "vgui/ILocalize.h"

#include "hud_basefadingobject.h"

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

//-----------------------------------------------------------------------------
// Purpose: Displays the amount of tickets
//-----------------------------------------------------------------------------
class CHudGameplayInfo : public CHudElement, public CHudFadingObject
{
	DECLARE_CLASS_SIMPLE( CHudGameplayInfo, CHudFadingObject );

public:
	CHudGameplayInfo( const char *pElementName );

private:

	void FadeIn();
	void FadeOut();
	int  GetValue();
	float Visible_Duration() 
	{ return 3.0f; }
};

DECLARE_HUDELEMENT( CHudGameplayInfo );

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CHudGameplayInfo::CHudGameplayInfo( const char *pElementName ) : CHudElement( pElementName ), CHudFadingObject(NULL, "HudGameplayInfo")
{
	SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );
}

void CHudGameplayInfo::FadeIn()
{g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "FadeInGameInfo" );}

void CHudGameplayInfo::FadeOut()
{g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "FadeOutGameInfo" );}

int CHudGameplayInfo::GetValue()
{
	int temp = 0;

	C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
	if(!pPlayer)return temp;

	if (pPlayer->GetTeamNumber() == TEAM_COMBINE)
	temp = HL2MPRules()->m_iCTTickets;
	else if (pPlayer->GetTeamNumber() == TEAM_REBELS)
	temp = HL2MPRules()->m_iTTickets;

	return temp;
}

HUD animation script

event FadeOutGameInfo
{
	Animate HudGameplayInfo Alpha		"0" 		Linear 0.0 1.5
}

event FadeInGameInfo
{
	Animate HudGameplayInfo Alpha		"255" 		Linear 0.0 1.5
}