CHudFadingObject: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(New page: ==What is it?== Baseclass of 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 ...)
 
m (clean up, added orphan, deadend tags)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
==What is it?==
{{Multiple issues|
Baseclass of 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.
{{Dead end|date=January 2024}}
{{Orphan|date=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==
== hud_basefadingobject.h ==
<pre>
 
<source lang="cpp">
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose:
// Purpose:
Line 47: Line 51:


#endif
#endif
</pre>
</source>
==hud_basefadingobject.cpp==
 
<pre>
== hud_basefadingobject.cpp ==
 
<source lang="cpp">
#include "cbase.h"
#include "cbase.h"
#include "hud.h"
#include "hud.h"
Line 65: Line 71:
// memdbgon must be the last include file in a .cpp file!!!
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#include "tier0/memdbgon.h"


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
Line 116: Line 121:
SetDisplayValue(GetValue());
SetDisplayValue(GetValue());
}
}
</pre>
</source>
 
== Example ==


==example==
<source lang="cpp">
<pre>
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
//
Line 139: Line 145:


#include "hud_basefadingobject.h"
#include "hud_basefadingobject.h"


// memdbgon must be the last include file in a .cpp file!!!
// memdbgon must be the last include file in a .cpp file!!!
Line 193: Line 198:
return temp;
return temp;
}
}
</pre>
</source>
 
=== HUD animation script ===


===animation===
<source lang="cpp">
<pre>
event FadeOutGameInfo
event FadeOutGameInfo
{
{
Line 205: Line 211:
{
{
Animate HudGameplayInfo Alpha "255" Linear 0.0 1.5
Animate HudGameplayInfo Alpha "255" Linear 0.0 1.5
}</pre>
}</source>
 
[[Category:Free source code|H]]

Latest revision as of 08:49, 21 January 2024

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
}