CHudFadingObject: Difference between revisions
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 ...) |
TomEdwards (talk | contribs) (Cleanup) |
||
Line 1: | Line 1: | ||
'''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 == | |||
= | <source lang="cpp"> | ||
//----------------------------------------------------------------------------- | //----------------------------------------------------------------------------- | ||
// Purpose: | // Purpose: | ||
Line 47: | Line 46: | ||
#endif | #endif | ||
</ | </source> | ||
==hud_basefadingobject.cpp== | |||
< | == hud_basefadingobject.cpp == | ||
<source lang="cpp"> | |||
#include "cbase.h" | #include "cbase.h" | ||
#include "hud.h" | #include "hud.h" | ||
Line 116: | Line 117: | ||
SetDisplayValue(GetValue()); | SetDisplayValue(GetValue()); | ||
} | } | ||
</ | </source> | ||
== | == Example == | ||
< | |||
<source lang="cpp"> | |||
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============// | //========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============// | ||
// | // | ||
Line 193: | Line 195: | ||
return temp; | return temp; | ||
} | } | ||
</ | </source> | ||
===animation=== | === HUD animation script === | ||
< | |||
<source lang="cpp"> | |||
event FadeOutGameInfo | event FadeOutGameInfo | ||
{ | { | ||
Line 205: | Line 208: | ||
{ | { | ||
Animate HudGameplayInfo Alpha "255" Linear 0.0 1.5 | Animate HudGameplayInfo Alpha "255" Linear 0.0 1.5 | ||
}</ | }</source> | ||
[[Category:Free source code]] |
Revision as of 09:57, 15 April 2009
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
}