aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/client/hud_posture.cpp
blob: 0e76cf272383cdaecb7e97066d15b65ee0720d99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"
#include "hud.h"
#include "hudelement.h"
#include "hud_numericdisplay.h"
#include "hud_macros.h"
#include "iclientmode.h"
#include <vgui_controls/AnimationController.h>
#include <vgui/ISurface.h>
#include <vgui/ILocalize.h>
#include <vgui_controls/Panel.h>
#include <vgui/IVGui.h>


using namespace vgui;

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

#define HUD_POSTURE_UPDATES_PER_SECOND	10
#define HUD_POSTURE_FADE_TIME 0.4f
#define CROUCHING_CHARACTER_INDEX 92  // index of the crouching dude in the TTF font

//-----------------------------------------------------------------------------
// Purpose: Shows the sprint power bar
//-----------------------------------------------------------------------------
class CHudPosture : public CHudElement, public vgui::Panel
{
	DECLARE_CLASS_SIMPLE( CHudPosture, vgui::Panel );

public:
	CHudPosture( const char *pElementName );
	bool			ShouldDraw( void );

#ifdef _X360 	// if not xbox 360, don't waste code space on this
	virtual void	Init( void );
	virtual void	Reset( void );
	virtual void	OnTick( void );

protected:
	virtual void	Paint();

	float	m_duckTimeout; /// HUD_POSTURE_FADE_TIME after the last known time the player was ducking

private:

	CPanelAnimationVar( vgui::HFont, m_hFont, "Font", "WeaponIconsSmall" );
	CPanelAnimationVarAliasType( float, m_IconX, "icon_xpos", "4", "proportional_float" );
	CPanelAnimationVarAliasType( float, m_IconY, "icon_ypos", "4", "proportional_float" );

	enum { NOT_FADING, 
		   FADING_UP, 
		   FADING_DOWN
	} m_kIsFading;
#endif
};	


DECLARE_HUDELEMENT( CHudPosture );


namespace
{
	// Don't pass a null pPlayer. Doesn't check for it.
	inline bool PlayerIsDucking(C_BasePlayer *pPlayer)
	{
		return  pPlayer->m_Local.m_bDucked &&		 // crouching 
				pPlayer->GetGroundEntity() != NULL ; // but not jumping
	}
}

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CHudPosture::CHudPosture( const char *pElementName ) : CHudElement( pElementName ), BaseClass( NULL, "HudPosture" )
{
	vgui::Panel *pParent = g_pClientMode->GetViewport();
	SetParent( pParent );

	SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );

	if( IsX360() )
	{
		vgui::ivgui()->AddTickSignal( GetVPanel(), (1000/HUD_POSTURE_UPDATES_PER_SECOND) );
	}
}

//-----------------------------------------------------------------------------
// Purpose: Save CPU cycles by letting the HUD system early cull
// costly traversal.  Called per frame, return true if thinking and 
// painting need to occur.
//-----------------------------------------------------------------------------
bool CHudPosture::ShouldDraw()
{
#ifdef _X360
	return ( m_duckTimeout >= gpGlobals->curtime &&
		CHudElement::ShouldDraw() );
#else
	return false;
#endif
}

#ifdef _X360

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudPosture::Init( void )
{
	m_duckTimeout = 0.0f;
	m_kIsFading = NOT_FADING;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudPosture::Reset( void )
{
	Init();
}

void CHudPosture::OnTick( void )
{
	C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
	if (!pPlayer)
		return;

	if ( PlayerIsDucking(pPlayer) )
	{
		m_duckTimeout = gpGlobals->curtime + HUD_POSTURE_FADE_TIME; // kick the timer forward
		if (GetAlpha() < 255)
		{
			// if not fully faded in, and not fading in, start fading in.
			if (m_kIsFading != FADING_UP)
			{
				m_kIsFading = FADING_UP;
				GetAnimationController()->RunAnimationCommand( this, "alpha", 255, 0, HUD_POSTURE_FADE_TIME, vgui::AnimationController::INTERPOLATOR_SIMPLESPLINE );
			}
		}
		else
		{
			m_kIsFading = NOT_FADING;
		}
	}	
	else // player is not ducking
	{
		if (GetAlpha() > 0)
		{
			// if not faded out or fading out, fade out.
			if (m_kIsFading != FADING_DOWN)
			{
				m_kIsFading = FADING_DOWN;
				GetAnimationController()->RunAnimationCommand( this, "alpha", 0, 0, HUD_POSTURE_FADE_TIME, vgui::AnimationController::INTERPOLATOR_SIMPLESPLINE );
			}
		}
		else
		{
			m_kIsFading = NOT_FADING;
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: draws the posture elements we want.
//-----------------------------------------------------------------------------
void CHudPosture::Paint()
{
	C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
	if ( !pPlayer )
		return;

	SetPaintBackgroundEnabled( true );

	Color clr;
	clr = gHUD.m_clrNormal;
	clr[3] = 255;

	// Pick the duck character
	wchar_t duck_char = CROUCHING_CHARACTER_INDEX;

	surface()->DrawSetTextFont( m_hFont );
	surface()->DrawSetTextColor( clr );
	surface()->DrawSetTextPos( m_IconX, m_IconY );
	surface()->DrawUnicodeChar( duck_char );
}

#endif