Skip to content

Commit

Permalink
feat: implement sar_hud_bg
Browse files Browse the repository at this point in the history
  • Loading branch information
mlugg committed Jun 11, 2021
1 parent dd783c0 commit b10c858
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Features/Hud/Hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ void HudContext::DrawElement(const char* fmt, ...)
data);

++this->elements;

int width = surface->GetFontLength(this->font, "%s", data);
if (width > this->maxWidth) this->maxWidth = width;
}
void HudContext::DrawElementOnScreen(const int groupID, const float xPos, const float yPos, const char* fmt, ...)
{
Expand Down Expand Up @@ -129,6 +132,7 @@ void HudContext::Reset(int slot)
this->xPadding = sar_hud_default_padding_x.GetInt();
this->yPadding = sar_hud_default_padding_y.GetInt();
this->spacing = sar_hud_default_spacing.GetInt();
this->maxWidth = 0;

this->font = scheme->GetDefaultFont() + sar_hud_default_font_index.GetInt();
this->fontSize = surface->GetFontHeight(font);
Expand Down Expand Up @@ -316,7 +320,11 @@ HUD_ELEMENT2_NO_DISABLE(text, HudType_InGame | HudType_Paused | HudType_Menu | H
surface->DrawTxt(ctx->font, x, y, color, c.text.c_str());
x += pixLen;
}

++ctx->elements;

int width = x - ctx->xPadding;
if (width > ctx->maxWidth) ctx->maxWidth = width;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Features/Hud/Hud.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class HudContext {
int spacing = 0;
Color textColor = Color(255, 255, 255);
int elements = 0;
int maxWidth = 0;
std::array<int, 256> group { 0 };

public:
Expand Down
40 changes: 40 additions & 0 deletions src/Modules/VGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
REDECL(VGui::Paint);
REDECL(VGui::UpdateProgressBar);

Variable sar_hud_bg("sar_hud_bg", "0", "Enable the SAR HUD background.\n");

void VGui::Draw(Hud* const& hud)
{
if (hud->ShouldDraw()) {
Expand All @@ -29,9 +31,39 @@ void VGui::Draw(HudElement* const& element)
}
}

static void DrawHudBackground(int slot, HudContext &ctx) {
if (!sar_hud_bg.GetBool()) return;

int height =
ctx.elements == 0 ?
0 :
ctx.elements * ctx.fontSize + (ctx.elements - 1) * ctx.spacing + 4;

static int maxWidths[2][100];
memmove(maxWidths[slot], maxWidths[slot] + 1, sizeof maxWidths[slot] - sizeof maxWidths[slot][0]);
maxWidths[slot][99] = ctx.maxWidth;

int width = 0;

for (size_t i = 0; i < sizeof maxWidths[slot] / sizeof maxWidths[slot][0]; ++i) {
if (maxWidths[slot][i] > width) width = maxWidths[slot][i];
}

if (width % 5) width += 5 - width % 5;

if (width != 0) width += 4;

int x = ctx.xPadding - 2;
int y = ctx.yPadding - 2;

surface->DrawRect(Color{ 0, 0, 0, 192 }, x, y, x + width, y + height);
}

// CEngineVGui::Paint
DETOUR(VGui::Paint, PaintMode_t mode)
{
static HudContext lastCtx[2];

auto result = VGui::Paint(thisptr, mode);

surface->StartDrawing(surface->matsurface->ThisPtr());
Expand All @@ -42,21 +74,29 @@ DETOUR(VGui::Paint, PaintMode_t mode)

if (ctx->slot == 0) {
if (mode & PAINT_UIPANELS) {
DrawHudBackground(0, lastCtx[0]);

for (auto const& hud : vgui->huds) {
vgui->Draw(hud);
}

for (auto const& element : vgui->elements) {
vgui->Draw(element);
}

lastCtx[0] = *ctx;
}
} else if (ctx->slot == 1) {
DrawHudBackground(1, lastCtx[1]);

for (auto const& hud : vgui->huds) {
if (hud->drawSecondSplitScreen) {
vgui->Draw(hud);
}
}

lastCtx[1] = *ctx;

for (auto const& element : vgui->elements) {
if (element->drawSecondSplitScreen) {
vgui->Draw(element);
Expand Down

0 comments on commit b10c858

Please sign in to comment.