⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vgui_healthbar.cpp

📁 hl2 source code. Do not use it illegal.
💻 CPP
字号:
//======== (C) Copyright 1999, 2000 Valve, L.L.C. All rights reserved. ========
//
// The copyright to the contents herein is the property of Valve, L.L.C.
// The contents may be used and/or copied only with the written permission of
// Valve, L.L.C., or in accordance with the terms and conditions stipulated in
// the agreement/contract under which the contents have been supplied.
//
// 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 );
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -