Skip to content

Instantly share code, notes, and snippets.

@untodesu
Created December 16, 2019 17:10
Show Gist options
  • Save untodesu/bf2ff90f32d191da07b1c6750d41dcc3 to your computer and use it in GitHub Desktop.
Save untodesu/bf2ff90f32d191da07b1c6750d41dcc3 to your computer and use it in GitHub Desktop.
#include "cbase.h"
#include "hud_display.h"
#include "iclientmode.h"
#include <Color.h>
#include <KeyValues.h>
#include <vgui/ISurface.h>
#include <vgui/ISystem.h>
#include <vgui/IVGui.h>
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// Purpose: constructor
//-----------------------------------------------------------------------------
CHudDisplay::CHudDisplay(vgui::Panel *parent, const char *name, char *num_fmt, char *dummy_fmt, char *label) : vgui::Panel(parent, name), m_szNumberFormat(num_fmt), m_szDummyFormat(dummy_fmt), m_szLabel(label), m_pLabel(NULL)
{
vgui::Panel *pParent = g_pClientMode->GetViewport();
SetParent(pParent);
m_iValue = 0;
m_bShouldDisplay = true;
memset(m_pNumbers, 0, sizeof(CHudTexture *) * 10);
memset(m_pDummies, 0, sizeof(CHudTexture *) * 2);
}
//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
void CHudDisplay::SetDisplayValue(int value)
{
m_iValue = value;
}
//-----------------------------------------------------------------------------
// Purpose: draw goods on background
//-----------------------------------------------------------------------------
void CHudDisplay::PaintBackground(void)
{
BaseClass::PaintBackground();
Color bg = GetFgColor();
bg[3] /= 10;
PaintDummies(digit_xpos, digit_ypos, bg);
}
//-----------------------------------------------------------------------------
// Purpose: draw goods on foreground
//-----------------------------------------------------------------------------
void CHudDisplay::Paint(void)
{
if(m_bShouldDisplay) {
PaintLabel(label_xpos, label_ypos, GetFgColor());
PaintNumbers(digit_xpos, digit_ypos, m_iValue, GetFgColor());
}
}
//-----------------------------------------------------------------------------
// Purpose: draw a label texture on the panel
//-----------------------------------------------------------------------------
void CHudDisplay::PaintLabel(int xpos, int ypos, Color color)
{
//You can disable drawing of label, just pass NULL to constructor!
if(m_szLabel) {
//Check for need to load sprites
bool needsLoad = !m_pLabel;
//Load them if need
if(needsLoad) {
m_pLabel = gHUD.GetIcon(m_szLabel);
//If cannot load - do error
if(!m_pLabel) {
Warning("Couldn't load sprite called '%s'! HUD element will not draw!\n", m_szLabel);
return;
}
}
//Finally draw
float scale = (label_height / (float)m_pLabel->Height());
int width = (m_pLabel->Width() * scale);
int height = (m_pLabel->Height() * scale);
m_pLabel->DrawSelf(xpos, ypos, width, height, color);
}
}
//-----------------------------------------------------------------------------
// Purpose: draw a three dulls on the panel
//-----------------------------------------------------------------------------
void CHudDisplay::PaintDummies(int xpos, int ypos, Color color)
{
//Check for need to load sprites
bool needsLoad = false;
for(int i = 0; i < 2; i++) {
if(!m_pDummies[i]) {
needsLoad = true;
break;
}
}
//Load them if need
if(needsLoad) {
char name[16] = { 0 };
// 1 to 2
for(int i = 0; i < 2; i++) {
sprintf(name, (m_szDummyFormat) ? m_szDummyFormat : HUD_DUMMY_FMT, i + 1);
m_pDummies[i] = gHUD.GetIcon(name);
//If cannot load - do error
if(!m_pDummies[i]) {
Warning("Couldn't load sprite called '%s'! HUD element will not draw!\n", name);
return;
}
}
}
//Finally draw
float scale = (digit_height / (float)m_pDummies[0]->Height());
int width = (m_pDummies[0]->Width() * scale);
int height = (m_pDummies[0]->Height() * scale);
//Draw 100's dull
m_pDummies[0]->DrawSelf(xpos, ypos, width, height, color);
xpos += width;
//Draw 10's dull
m_pDummies[1]->DrawSelf(xpos, ypos, width, height, color);
xpos += width;
m_pDummies[0]->DrawSelf(xpos, ypos, width, height, color);
// xpos += width;
}
//-----------------------------------------------------------------------------
// Purpose: draw three digits on the panel
//-----------------------------------------------------------------------------
void CHudDisplay::PaintNumbers(int xpos, int ypos, int value, Color color)
{
//Check for need to load sprites
bool needsLoad = false;
for(int i = 0; i < 10; i++) {
if(!m_pNumbers[i]) {
needsLoad = true;
break;
}
}
//Load them if need
if(needsLoad) {
char name[16] = { 0 };
// 1 to 2
for(int i = 0; i < 10; i++) {
sprintf(name, (m_szNumberFormat) ? m_szNumberFormat : HUD_NUMBER_FMT, i);
m_pNumbers[i] = gHUD.GetIcon(name);
//If cannot load - do error
if(!m_pNumbers[i]) {
Warning("Couldn't load sprite called '%s'! HUD element will not draw!\n", name);
return;
}
}
}
//Finally draw
float scale = (digit_height / (float)m_pNumbers[0]->Height());
int width = (m_pNumbers[0]->Width() * scale);
int height = (m_pNumbers[0]->Height() * scale);
//Now, if the value is NON-ZERO, draw actual numbers
//Any negative value will result only dulls!
if(m_iValue >= 0) {
//Draw 100's
if(m_iValue >= 100) {
int k = (m_iValue / 100);
m_pNumbers[k]->DrawSelf(xpos, ypos, width, height, color);
}
xpos += width;
//Draw 10's
if(m_iValue >= 10) {
int k = (m_iValue % 100) / 10;
m_pNumbers[k]->DrawSelf(xpos, ypos, width, height, color);
}
xpos += width;
//Draw ones
int k = (m_iValue % 10);
m_pNumbers[k]->DrawSelf(xpos, ypos, width, height, color);
// xpos += width;
}
}
//=============================================================================//
//
// Purpose: Draws sprite display with dulls and label.
//
//=============================================================================//
#ifndef HUD_DISPLAY_H
#define HUD_DISPLAY_H
#ifdef _WIN32
#pragma once
#endif
#include "hud.h"
#include "hud_numericdisplay.h"
//Compatibility with LeakNet hud_textures.txt
#define HUD_NUMBER_FMT "number_%d"
#define HUD_NUMBER_FMT_S "number_%d_small"
#define HUD_DUMMY_FMT "number_dummy%d"
#define HUD_DUMMY_FMT_S "number_dummy%d_small"
#define HUD_LABEL_HEALTH "health_label"
#define HUD_LABEL_AMMO "ammo_label"
#define HUD_LABEL_HEV "battery_label"
class CHudDisplay : public vgui::Panel {
DECLARE_CLASS_SIMPLE(CHudDisplay, vgui::Panel);
public:
CHudDisplay(vgui::Panel *parent, const char *name, char *num_fmt, char *dummy_fmt, char *label);
void SetDisplayValue(int value);
void SetShouldDisplayValue(bool state);
protected:
virtual void PaintBackground(void);
virtual void Paint();
void PaintLabel(int xpos, int ypos, Color color);
void PaintDummies(int xpos, int ypos, Color color);
void PaintNumbers(int xpos, int ypos, int value, Color color);
CPanelAnimationVar(float, m_flAlphaOverride, "Alpha", "255");
CPanelAnimationVar(float, m_flBlur, "Blur", "0");
CPanelAnimationVarAliasType(float, label_xpos, "label_xpos", "0", "proportional_float");
CPanelAnimationVarAliasType(float, label_ypos, "label_ypos", "0", "proportional_float");
CPanelAnimationVarAliasType(float, label_height, "label_height", "0", "proportional_float");
CPanelAnimationVarAliasType(float, digit_xpos, "digit_xpos", "0", "proportional_float");
CPanelAnimationVarAliasType(float, digit_ypos, "digit_ypos", "0", "proportional_float");
CPanelAnimationVarAliasType(float, digit_height, "digit_height", "16", "proportional_float");
private:
CHudTexture *m_pNumbers[10];
CHudTexture *m_pDummies[2];
CHudTexture *m_pLabel;
char *m_szNumberFormat;
char *m_szDummyFormat;
char *m_szLabel;
int m_iValue;
bool m_bShouldDisplay;
};
#endif //HUD_DISPLAY_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment