From b10c858068361965e0082a771d794f5306614bd2 Mon Sep 17 00:00:00 2001 From: mlugg Date: Sat, 12 Jun 2021 00:28:19 +0100 Subject: [PATCH] feat: implement sar_hud_bg --- src/Features/Hud/Hud.cpp | 8 ++++++++ src/Features/Hud/Hud.hpp | 1 + src/Modules/VGui.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/src/Features/Hud/Hud.cpp b/src/Features/Hud/Hud.cpp index fbc91ffe1..04f28a57e 100644 --- a/src/Features/Hud/Hud.cpp +++ b/src/Features/Hud/Hud.cpp @@ -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, ...) { @@ -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); @@ -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; } } } diff --git a/src/Features/Hud/Hud.hpp b/src/Features/Hud/Hud.hpp index c96486335..179208009 100644 --- a/src/Features/Hud/Hud.hpp +++ b/src/Features/Hud/Hud.hpp @@ -48,6 +48,7 @@ class HudContext { int spacing = 0; Color textColor = Color(255, 255, 255); int elements = 0; + int maxWidth = 0; std::array group { 0 }; public: diff --git a/src/Modules/VGui.cpp b/src/Modules/VGui.cpp index cb083bd09..d90003bce 100644 --- a/src/Modules/VGui.cpp +++ b/src/Modules/VGui.cpp @@ -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()) { @@ -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()); @@ -42,6 +74,8 @@ 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); } @@ -49,14 +83,20 @@ DETOUR(VGui::Paint, PaintMode_t mode) 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);