summaryrefslogtreecommitdiff
path: root/game/client/tf2/vgui_healthbar.cpp
blob: 83e97680f1fd287a2103a6a6d0401e506fb003e7 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $Workfile:     $
// $Date:         $
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "vgui_HealthBar.h"
#include "CommanderOverlay.h"
#include <vgui_controls/Controls.h>
#include <vgui/ISurface.h>

//-----------------------------------------------------------------------------
// Purpose: 
// Output : 
//-----------------------------------------------------------------------------
CHealthBarPanel::CHealthBarPanel( vgui::Panel *pParent ) : vgui::Panel(pParent, "CHealthBarPanel" )
{
	SetHealth( 0.0f );
	SetPaintBackgroundEnabled( false );
	SetVertical( false );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CHealthBarPanel::~CHealthBarPanel( void )
{
}

//-----------------------------------------------------------------------------
// Purpose: Parse values from the file
//-----------------------------------------------------------------------------
bool CHealthBarPanel::Init( KeyValues* pInitData )
{
	if (!pInitData)
		return false;

	if (!ParseRGBA(pInitData, "okcolor", m_Ok ))
		return false;

	if (!ParseRGBA(pInitData, "badcolor", m_Bad ))
		return false;

	int x, y, w, h;
	if (!ParseRect(pInitData, "position", x, y, w, h ))
		return false;
	SetPos( x, y );
	SetSize( w, h );
	SetCursor( NULL );

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHealthBarPanel::SetGoodColor( int r, int g, int b, int a )
{
	m_Ok.SetColor( r,g,b,a );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHealthBarPanel::SetBadColor( int r, int g, int b, int a )
{
	m_Bad.SetColor( r,g,b,a );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHealthBarPanel::SetHealth( float health )
{
	m_Health = health;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHealthBarPanel::SetVertical( bool bVertical )
{
	m_bVertical = bVertical;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHealthBarPanel::Paint( void )
{
	int w, h;
	GetSize( w, h );
	
	float frac;
	frac = MIN( 1.0, m_Health );
	frac = MAX( 0.0, m_Health );

	vgui::surface()->DrawSetColor( 255,255,255,255 );
	vgui::surface()->DrawOutlinedRect( 0,0, w,h );

	int drawwidth = frac * w;
	int drawheight = frac * h;

	// Draw the Ok section
	vgui::surface()->DrawSetColor( m_Ok[0], m_Ok[1], m_Ok[2], m_Ok[3] );
	if ( m_bVertical )
	{
		vgui::surface()->DrawFilledRect( 0, h - drawheight, w, h );
	}
	else
	{
		vgui::surface()->DrawFilledRect( 0, 0, drawwidth, h );
	}

	vgui::surface()->DrawSetColor( m_Bad[0], m_Bad[1], m_Bad[2], m_Bad[3] );
	// Draw the Bad section
	if ( m_bVertical )
	{
		if (h != drawheight)
		{
			vgui::surface()->DrawFilledRect( 0, 0, w, h - drawheight );
		}
	}
	else
	{
		if (w != drawwidth)
		{
			vgui::surface()->DrawFilledRect( drawwidth, 0, w, h );
		}
	}
}