From 6f7153bf71b088a3ebb5cab3a3e1919903744b6d Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 30 Nov 2023 18:10:07 +0000 Subject: [PATCH] Codechange: Make a generic DrawRectOutline function from DrawOutline. (#11524) This allows drawing an outline from Rect, not just constrained to a Widget's Rect. And reduces duplication a little. --- src/gfx.cpp | 15 +++++++++++++++ src/gfx_func.h | 1 + src/industry_gui.cpp | 5 +---- src/widget.cpp | 8 ++------ 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/gfx.cpp b/src/gfx.cpp index b261845f0d..aed5aa3d3e 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -451,6 +451,21 @@ void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3) GfxDrawLineUnscaled(x + dx3, y + dy3, x + dx3 + dx2, y + dy3 + dy2, colour); } +/** + * Draw the outline of a Rect + * @param r Rect to draw. + * @param colour Colour of the outline. + * @param width Width of the outline. + * @param dash Length of dashes for dashed lines. 0 means solid lines. + */ +void DrawRectOutline(const Rect &r, int colour, int width, int dash) +{ + GfxDrawLine(r.left, r.top, r.right, r.top, colour, width, dash); + GfxDrawLine(r.left, r.top, r.left, r.bottom, colour, width, dash); + GfxDrawLine(r.right, r.top, r.right, r.bottom, colour, width, dash); + GfxDrawLine(r.left, r.bottom, r.right, r.bottom, colour, width, dash); +} + /** * Set the colour remap to be for the given colour. * @param colour the new colour of the remap. diff --git a/src/gfx_func.h b/src/gfx_func.h index c0610e43ea..2e747d3170 100644 --- a/src/gfx_func.h +++ b/src/gfx_func.h @@ -103,6 +103,7 @@ void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectM void GfxFillPolygon(const std::vector &shape, int colour, FillRectMode mode = FILLRECT_OPAQUE); void GfxDrawLine(int left, int top, int right, int bottom, int colour, int width = 1, int dash = 0); void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3); +void DrawRectOutline(const Rect &r, int colour, int width = 1, int dash = 0); /* Versions of DrawString/DrawStringMultiLine that accept a Rect instead of separate left, right, top and bottom parameters. */ static inline int DrawString(const Rect &r, std::string_view str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL) diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 39f2404445..328824f0df 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -2146,10 +2146,7 @@ struct CargoesField { int ypos1 = ypos + vert_inter_industry_space / 2; int ypos2 = ypos + normal_height - 1 - vert_inter_industry_space / 2; int xpos2 = xpos + industry_width - 1; - GfxDrawLine(xpos, ypos1, xpos2, ypos1, INDUSTRY_LINE_COLOUR); - GfxDrawLine(xpos, ypos1, xpos, ypos2, INDUSTRY_LINE_COLOUR); - GfxDrawLine(xpos, ypos2, xpos2, ypos2, INDUSTRY_LINE_COLOUR); - GfxDrawLine(xpos2, ypos1, xpos2, ypos2, INDUSTRY_LINE_COLOUR); + DrawRectOutline({xpos, ypos1, xpos2, ypos2}, INDUSTRY_LINE_COLOUR); ypos += (normal_height - GetCharacterHeight(FS_NORMAL)) / 2; if (this->u.industry.ind_type < NUM_INDUSTRYTYPES) { const IndustrySpec *indsp = GetIndustrySpec(this->u.industry.ind_type); diff --git a/src/widget.cpp b/src/widget.cpp index d5f35d6d21..20d010d787 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -922,15 +922,11 @@ int Window::SortButtonWidth() bool _draw_widget_outlines; -void DrawOutline(const Window *, const NWidgetBase *wid) +static void DrawOutline(const Window *, const NWidgetBase *wid) { if (!_draw_widget_outlines || wid->current_x == 0 || wid->current_y == 0) return; - Rect r = wid->GetCurrentRect(); - GfxDrawLine(r.left, r.top, r.right, r.top, PC_WHITE, 1, 4); - GfxDrawLine(r.left, r.top, r.left, r.bottom, PC_WHITE, 1, 4); - GfxDrawLine(r.right, r.top, r.right, r.bottom, PC_WHITE, 1, 4); - GfxDrawLine(r.left, r.bottom, r.right, r.bottom, PC_WHITE, 1, 4); + DrawRectOutline(wid->GetCurrentRect(), PC_WHITE, 1, 4); } /**