Skip to content

Instantly share code, notes, and snippets.

@Kolesias123
Created December 31, 2014 04:13
Show Gist options
  • Save Kolesias123/f198ebcd8382feb74fcb to your computer and use it in GitHub Desktop.
Save Kolesias123/f198ebcd8382feb74fcb to your computer and use it in GitHub Desktop.
//========= Copyright 1996-2005, Valve Corporation, All rights reserved. ============//
#include "cbase.h"
#include "hud.h"
#include "hudelement.h"
#include "hud_numericdisplay.h"
#include <vgui_controls/Panel.h>
#include "c_in_player.h"
#include "iclientmode.h"
#ifdef APOCALYPSE
#include "c_ap_player.h"
#include "item_object.h"
#endif
#include "vgui_controls/controls.h"
#include "vgui/ISurface.h"
#include <vgui/ILocalize.h>
#include "IVRenderView.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//====================================================================
// Muestra un HUD super básico para el Inventario
//====================================================================
class CHudInventory : public CHudElement, public vgui::Panel
{
public:
DECLARE_CLASS_SIMPLE( CHudInventory, vgui::Panel );
CHudInventory( const char *pElementName );
virtual void Paint();
protected:
CPanelAnimationVar( vgui::HFont, m_hTextFont, "TextFont", "Default" );
};
DECLARE_HUDELEMENT( CHudInventory );
//====================================================================
// Constructor
//====================================================================
CHudInventory::CHudInventory( const char *pElementName ) : CHudElement( pElementName ), BaseClass(NULL, "HudInventory")
{
// Esto va en el HUD del cliente
vgui::Panel *pParent = GetClientMode()->GetViewport();
SetParent( pParent );
// No mostrar mientras estemos muertos
SetHiddenBits( HIDEHUD_PLAYERDEAD );
}
//====================================================================
// Pintamos: Mostramos en la pantalla
//====================================================================
void CHudInventory::Paint()
{
C_IN_Player *pPlayer = C_IN_Player::GetLocalInPlayer();
if ( !pPlayer )
return;
// ¡No tiene inventario!
if ( !pPlayer->GetInventory() )
return;
int y = 5;
// Por cada slot en el inventario
for ( int i = 0; i < MAX_INVENTORY_ITEMS; i++ )
{
CItemObject *pItem = dynamic_cast<CItemObject*>( pPlayer->GetInventory()->GetItem(i) );
// No hay ningún objeto en este slot
if ( !pItem )
continue;
// Cadena
// Ejemplo: 1. #ItemBandage
const char *pLabel = VarArgs( "%i - %s", i, pItem->ObjectName() );
wchar_t *wLabel = new wchar_t[strlen(pLabel)+1];
// Convertimos [pLabel] a un [wchar_t] (necesario para el vgui)
V_UTF8ToUnicode( pLabel, wLabel, strlen(pLabel)+1 );
// Establecemos la fuente, color de letra, posición y después pintamos las letras
vgui::surface()->DrawSetTextFont( m_hTextFont );
vgui::surface()->DrawSetTextColor( GetFgColor() );
vgui::surface()->DrawSetTextPos( 0, y );
vgui::surface()->DrawUnicodeString( wLabel );
// El siguiente ira más abajo
y += 15;
}
}
//====================================================================
// MACRO: Sirve para crear con una sola línea de código un
// contador.
//====================================================================
#define CREATE_HUD_RESOURCE( _classname, _userFunc, _labelName, _playerClass ) \
class C##_classname : public CHudElement, public CHudNumericDisplay \
{ \
public:\
DECLARE_CLASS_SIMPLE( C##_classname, CHudNumericDisplay );\
C##_classname( const char *pElementName );\
virtual void Init();\
virtual void OnThink();\
protected:\
int m_iValue;\
};\
DECLARE_HUDELEMENT( C##_classname );\
C##_classname::C##_classname( const char *pElementName ) : CHudElement( pElementName ), CHudNumericDisplay( NULL, #_classname ) {\
SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD );\
}\
void C##_classname::Init() \
{ \
SetLabelText( _labelName ); \
SetDisplayValue( -1 ); \
} \
void C##_classname::OnThink() \
{ \
_playerClass *pPlayer = dynamic_cast<_playerClass *>( CBasePlayer::GetLocalPlayer() );\
\
if ( !pPlayer )\
return;\
\
int actualValue = MAX( pPlayer->##_userFunc(), 0 );\
\
if ( actualValue == m_iValue )\
return;\
\
m_iValue = actualValue;\
SetDisplayValue( m_iValue );\
}
//====================================================================
//====================================================================
CREATE_HUD_RESOURCE( HudHealth, GetHealth, L"Salud:", C_IN_Player );
CREATE_HUD_RESOURCE( HudBlood, GetBloodLevel, L"Sangre:", C_AP_Player );
CREATE_HUD_RESOURCE( HudHungry, GetHungryLevel, L"Hambre:", C_AP_Player );
CREATE_HUD_RESOURCE( HudThrist, GetThirstLevel, L"Sed:", C_AP_Player );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment