VGUI Health bar: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 4: | Line 4: | ||
==Code== | ==Code== | ||
First create a '''hud_hull.cpp''' in your CLIENT part your code.And paste this to hud_hull.cpp | First create a '''hud_hull.cpp''' in your CLIENT part your code.And paste this to hud_hull.cpp | ||
#include " hud_hull .h" # include " | <big>'''hud_hull.cpp'''</big> | ||
<pre> | |||
#include "cbase.h" | |||
#include "hud.h" | |||
#include "hud_hull. h" | |||
#include "hud_macros.h" | |||
#include "c_sdk_player.h" | |||
#include "iclientmode.h" | |||
using namespace vgui; | |||
#include " | #include "tier0/memdbgon.h" | ||
DECLARE_HUDELEMENT (CHudHull); | |||
# | # define HULL_INIT 80 | ||
//------------------------------------------------------------------------ | |||
// Purpose: Constructor | |||
//------------------------------------------------------------------------ | |||
CHudHull:: CHudHull (const char * pElementName) : | |||
CHudElement (pElementName), BaseClass (NULL, "HudHull") | |||
{ | |||
vgui:: Panel * pParent = g_pClientMode-> GetViewport (); | |||
SetParent (pParent); | |||
SetHiddenBits (HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT); | |||
} | |||
//------------------------------------------------------------------------ //------------------------------------------------ ------------------------ | //------------------------------------------------------------------------ | ||
// Purpose: | |||
//------------------------------------------------------------------------ | |||
void CHudHull:: Init() | |||
{ | |||
m_flHull = HULL_INIT; | |||
m_nHullLow = -1; | |||
SetBgColor (Color (0,0,0,128)); | |||
} | |||
//------------------------------------------------------------------------ // | //------------------------------------------------------------------------ | ||
// Purpose: | |||
//----------------------------------------------------------------------- | |||
CHudHull:: | void CHudHull:: Reset (void) | ||
{ | |||
Init(); | |||
} | |||
//------------------------------------------------------------------------ | |||
// Purpose: | |||
//------------------------------------------------------------------------ | |||
void CHudHull:: OnThink (void) | |||
{ | |||
float newHull = 0; | |||
C_BasePlayer * local = C_BasePlayer:: GetLocalPlayer (); | |||
if (!local) | |||
return; | |||
// newHull = local->GetHull(); | // Never below zero | ||
// newHull = local->GetHull(); newHull = local->GetHealth(); | |||
// DevMsg("Sheild at is at: %f\n",newShield); | |||
// Only update the fade if we've changed health | |||
if (newHull == m_flHull) | |||
return; | |||
m_flHull = newHull; | |||
} | |||
//------------------------------------------------------------------------ | |||
// Purpose: draws the power bar | |||
//------------------------------------------------------------------------ | |||
{ | void CHudHull::Paint() | ||
{ | |||
// Get bar chunks | |||
int chunkCount = m_flBarWidth / (m_flBarChunkWidth + m_flBarChunkGap); | |||
int enabledChunks = (int)((float)chunkCount * (m_flHull / 100.0f) + 0.5f ); | |||
// Draw the suit power bar | |||
surface()->DrawSetColor (m_HullColor); | |||
int xpos = m_flBarInsetX, ypos = m_flBarInsetY; | |||
} | for (int i = 0; i < enabledChunks; i++) | ||
{ | |||
surface()->DrawFilledRect(xpos, ypos, xpos + m_flBarChunkWidth, ypos + m_flBarHeight); | |||
xpos + = (m_flBarChunkWidth + m_flBarChunkGap); | |||
} | |||
// Draw the exhausted portion of the bar. | |||
surface()->DrawSetColor(Color(m_HullColor [0], m_HullColor [1], m_HullColor [2], m_iHullDisabledAlpha)); | |||
for (int i = enabledChunks; i < chunkCount; i++) | |||
{ | |||
surface()->DrawFilledRect(xpos, ypos, xpos + m_flBarChunkWidth, ypos + m_flBarHeight); | |||
xpos += (m_flBarChunkWidth + m_flBarChunkGap); | |||
} | |||
// | // Draw our name | ||
surface()->DrawSetTextFont(m_hTextFont); | |||
surface()->DrawSetTextColor(m_HullColor); | |||
surface()->DrawSetTextPos(text_xpos, text_ypos); | |||
//- | //wchar_t *tempString = vgui::localize()->Find("#Valve_Hud_AUX_POWER"); | ||
surface()->DrawPrintText(L"HULL", wcslen(L"HULL")); | |||
} | |||
</pre> | |||
} | |||
</ | |||
==Then make a hud_hull.h file and put this in it== | ==Then make a hud_hull.h file and put this in it== | ||
# | <big>'''hud_hull.h'''</big> | ||
<pre> | |||
#if !defined HUD_HULL_H | |||
#define HUD_HULL_H | |||
#pragma once # | #ifdef _WIN32 | ||
#pragma once | |||
#endif | |||
# | #include "hudelement.h" | ||
#include "hud_numericdisplay.h" | |||
//----------------------------------------------------------------------------- | |||
// Purpose: Shows the hull bar | |||
//----------------------------------------------------------------------------- | |||
class CHudHull : public CHudElement, public vgui::Panel | |||
{ | |||
DECLARE_CLASS_SIMPLE(CHudHull, vgui::Panel); | |||
public: | |||
CHudHull(const char * pElementName); | |||
virtual void Init (void); | |||
virtual void Reset (void); | |||
virtual void OnThink (void); | |||
protected: | |||
virtual void Paint(); | |||
private: | |||
CPanelAnimationVar(Color, m_HullColor, "HullColor", "255 0 0 255"); | |||
CPanelAnimationVar(int, m_iHullDisabledAlpha, "HullDisabledAlpha", "50"); | |||
CPanelAnimationVarAliasType(float, m_flBarInsetX, "BarInsetX", "26", "proportional_float"); | |||
CPanelAnimationVarAliasType(float, m_flBarInsetY, "BarInsetY", "3", "proportional_float"); | |||
CPanelAnimationVarAliasType(float, m_flBarWidth, "BarWidth", "84", "proportional_float"); | |||
CPanelAnimationVarAliasType(float, m_flBarHeight, "BarHeight", "4", "proportional_float"); | |||
CPanelAnimationVarAliasType(float, m_flBarChunkWidth, "BarChunkWidth", "2", "proportional_float"); | |||
CPanelAnimationVarAliasType(float, m_flBarChunkGap, "BarChunkGap", "1", "proportional_float"); | |||
CPanelAnimationVar(vgui::HFont, m_hTextFont, "TextFont", "HUDBarText"); | |||
CPanelAnimationVarAliasType(float, text_xpos, "text_xpos", "2", "proportional_float"); | |||
CPanelAnimationVarAliasType(float, text_ypos, "text_ypos", "2", "proportional_float"); | |||
CPanelAnimationVarAliasType(float, text2_xpos, "text2_xpos", "8", "proportional_float"); | |||
CPanelAnimationVarAliasType(float, text2_ypos, "text2_ypos", "40", "proportional_float"); | |||
CPanelAnimationVarAliasType(float, text2_gap, "text2_gap", "10", "proportional_float"); | |||
float m_flHull; | |||
int m_nHullLow; | |||
}; | |||
#endif // HUD_SUITPOWER_H | |||
</pre> | |||
==Then put this in HudLayout.res== | |||
<big>'''HudLayout.res'''</big> | |||
<pre> | |||
HudHull | HudHull | ||
| Line 289: | Line 212: | ||
} ) | } ) | ||
</pre> | |||
[[Category:Programming]] [[Category:VGUI]] | [[Category:Programming]] [[Category:VGUI]] | ||
Revision as of 10:56, 16 February 2009
VGUI health bar
Well the VGUI Image Progress bar didn't help me mutch because i had problems with updating the Health bar.Buti found on a one russian website a Russian tutorial on how to make a VGUI Health Bar.This tutorial is purely a Copy N Paste tutorial.And it works on both EP1 and OB engines.
Code
First create a hud_hull.cpp in your CLIENT part your code.And paste this to hud_hull.cpp
hud_hull.cpp
#include "cbase.h"
#include "hud.h"
#include "hud_hull. h"
#include "hud_macros.h"
#include "c_sdk_player.h"
#include "iclientmode.h"
using namespace vgui;
#include "tier0/memdbgon.h"
DECLARE_HUDELEMENT (CHudHull);
# define HULL_INIT 80
//------------------------------------------------------------------------
// Purpose: Constructor
//------------------------------------------------------------------------
CHudHull:: CHudHull (const char * pElementName) :
CHudElement (pElementName), BaseClass (NULL, "HudHull")
{
vgui:: Panel * pParent = g_pClientMode-> GetViewport ();
SetParent (pParent);
SetHiddenBits (HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT);
}
//------------------------------------------------------------------------
// Purpose:
//------------------------------------------------------------------------
void CHudHull:: Init()
{
m_flHull = HULL_INIT;
m_nHullLow = -1;
SetBgColor (Color (0,0,0,128));
}
//------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------
void CHudHull:: Reset (void)
{
Init();
}
//------------------------------------------------------------------------
// Purpose:
//------------------------------------------------------------------------
void CHudHull:: OnThink (void)
{
float newHull = 0;
C_BasePlayer * local = C_BasePlayer:: GetLocalPlayer ();
if (!local)
return;
// Never below zero
// newHull = local->GetHull(); newHull = local->GetHealth();
// DevMsg("Sheild at is at: %f\n",newShield);
// Only update the fade if we've changed health
if (newHull == m_flHull)
return;
m_flHull = newHull;
}
//------------------------------------------------------------------------
// Purpose: draws the power bar
//------------------------------------------------------------------------
void CHudHull::Paint()
{
// Get bar chunks
int chunkCount = m_flBarWidth / (m_flBarChunkWidth + m_flBarChunkGap);
int enabledChunks = (int)((float)chunkCount * (m_flHull / 100.0f) + 0.5f );
// Draw the suit power bar
surface()->DrawSetColor (m_HullColor);
int xpos = m_flBarInsetX, ypos = m_flBarInsetY;
for (int i = 0; i < enabledChunks; i++)
{
surface()->DrawFilledRect(xpos, ypos, xpos + m_flBarChunkWidth, ypos + m_flBarHeight);
xpos + = (m_flBarChunkWidth + m_flBarChunkGap);
}
// Draw the exhausted portion of the bar.
surface()->DrawSetColor(Color(m_HullColor [0], m_HullColor [1], m_HullColor [2], m_iHullDisabledAlpha));
for (int i = enabledChunks; i < chunkCount; i++)
{
surface()->DrawFilledRect(xpos, ypos, xpos + m_flBarChunkWidth, ypos + m_flBarHeight);
xpos += (m_flBarChunkWidth + m_flBarChunkGap);
}
// Draw our name
surface()->DrawSetTextFont(m_hTextFont);
surface()->DrawSetTextColor(m_HullColor);
surface()->DrawSetTextPos(text_xpos, text_ypos);
//wchar_t *tempString = vgui::localize()->Find("#Valve_Hud_AUX_POWER");
surface()->DrawPrintText(L"HULL", wcslen(L"HULL"));
}
Then make a hud_hull.h file and put this in it
hud_hull.h
#if !defined HUD_HULL_H
#define HUD_HULL_H
#ifdef _WIN32
#pragma once
#endif
#include "hudelement.h"
#include "hud_numericdisplay.h"
//-----------------------------------------------------------------------------
// Purpose: Shows the hull bar
//-----------------------------------------------------------------------------
class CHudHull : public CHudElement, public vgui::Panel
{
DECLARE_CLASS_SIMPLE(CHudHull, vgui::Panel);
public:
CHudHull(const char * pElementName);
virtual void Init (void);
virtual void Reset (void);
virtual void OnThink (void);
protected:
virtual void Paint();
private:
CPanelAnimationVar(Color, m_HullColor, "HullColor", "255 0 0 255");
CPanelAnimationVar(int, m_iHullDisabledAlpha, "HullDisabledAlpha", "50");
CPanelAnimationVarAliasType(float, m_flBarInsetX, "BarInsetX", "26", "proportional_float");
CPanelAnimationVarAliasType(float, m_flBarInsetY, "BarInsetY", "3", "proportional_float");
CPanelAnimationVarAliasType(float, m_flBarWidth, "BarWidth", "84", "proportional_float");
CPanelAnimationVarAliasType(float, m_flBarHeight, "BarHeight", "4", "proportional_float");
CPanelAnimationVarAliasType(float, m_flBarChunkWidth, "BarChunkWidth", "2", "proportional_float");
CPanelAnimationVarAliasType(float, m_flBarChunkGap, "BarChunkGap", "1", "proportional_float");
CPanelAnimationVar(vgui::HFont, m_hTextFont, "TextFont", "HUDBarText");
CPanelAnimationVarAliasType(float, text_xpos, "text_xpos", "2", "proportional_float");
CPanelAnimationVarAliasType(float, text_ypos, "text_ypos", "2", "proportional_float");
CPanelAnimationVarAliasType(float, text2_xpos, "text2_xpos", "8", "proportional_float");
CPanelAnimationVarAliasType(float, text2_ypos, "text2_ypos", "40", "proportional_float");
CPanelAnimationVarAliasType(float, text2_gap, "text2_gap", "10", "proportional_float");
float m_flHull;
int m_nHullLow;
};
#endif // HUD_SUITPOWER_H
Then put this in HudLayout.res
HudLayout.res
HudHull
{ (
"fieldName" "HudHull" "fieldName" "HudHull"
"visible" "1" "visible" "1"
"enabled" "1" "enabled" "1"
"xpos" "12" "xpos" "12"
"ypos" "420" "ypos" "420"
"wide" "120" "wide" "120"
"tall" "10" "tall" "10"
"text_xpos" "5" "text_xpos" "5"
"text_ypos" "2" "text_ypos" "2"
"TextColor" "0 255 0 80" "TextColor" "0 255 0 80"
"PaintBackgroundType" "2" "PaintBackgroundType" "2"
} )