From d0add1e07fee08c570f149f58fe9289e1327b7aa Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Fri, 23 Sep 2022 13:35:32 +0100 Subject: [PATCH] Change: Ignore offsets when drawing GUI sprite. --- src/gfx_func.h | 1 + src/widget.cpp | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/gfx_func.h b/src/gfx_func.h index c4e21792bd..f6018c8a59 100644 --- a/src/gfx_func.h +++ b/src/gfx_func.h @@ -90,6 +90,7 @@ void RedrawScreenRect(int left, int top, int right, int bottom); void GfxScroll(int left, int top, int width, int height, int xo, int yo); Dimension GetSpriteSize(SpriteID sprid, Point *offset = nullptr, ZoomLevel zoom = ZOOM_LVL_GUI); +Dimension GetScaledSpriteSize(SpriteID sprid); /* widget.cpp */ void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = nullptr); void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = nullptr, ZoomLevel zoom = ZOOM_LVL_GUI); std::unique_ptr DrawSpriteToRgbaBuffer(SpriteID spriteId, ZoomLevel zoom = ZOOM_LVL_GUI); diff --git a/src/widget.cpp b/src/widget.cpp index e864846ae8..5cee21f4d3 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -72,6 +72,19 @@ static inline Dimension ScaleGUITrad(const Dimension &dim) return {(uint)ScaleGUITrad(dim.width), (uint)ScaleGUITrad(dim.height)}; } +/** + * Scale sprite size for GUI. + * Offset is ignored. + */ +Dimension GetScaledSpriteSize(SpriteID sprid) +{ + Point offset; + Dimension d = GetSpriteSize(sprid, &offset, ZOOM_LVL_OUT_4X); + d.width -= offset.x; + d.height -= offset.y; + return ScaleGUITrad(d); +} + /** * Set up pre-scaled versions of Widget Dimensions. */ @@ -318,6 +331,17 @@ void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, Fra } } +void DrawSpriteIgnorePadding(const Rect &r, SpriteID img, int clicked, StringAlignment align) +{ + Point offset; + Dimension d = GetSpriteSize(img, &offset); + d.width -= offset.x; + d.height -= offset.y; + + Point p = GetAlignedPosition(r, d, align); + DrawSprite(img, PAL_NONE, p.x + clicked - offset.x, p.y + clicked - offset.y); +} + /** * Draw an image button. * @param r Rectangle of the button. @@ -333,9 +357,7 @@ static inline void DrawImageButtons(const Rect &r, WidgetType type, Colours colo DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, (clicked) ? FR_LOWERED : FR_NONE); if ((type & WWT_MASK) == WWT_IMGBTN_2 && clicked) img++; // Show different image when clicked for #WWT_IMGBTN_2. - Dimension d = GetSpriteSize(img); - Point p = GetAlignedPosition(r, d, align); - DrawSprite(img, PAL_NONE, p.x + clicked, p.y + clicked); + DrawSpriteIgnorePadding(r, img, clicked, align); } /** @@ -645,9 +667,12 @@ static inline void DrawResizeBox(const Rect &r, Colours colour, bool at_left, bo static inline void DrawCloseBox(const Rect &r, Colours colour) { if (colour != COLOUR_WHITE) DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_NONE); - Dimension d = GetSpriteSize(SPR_CLOSEBOX); - int s = UnScaleGUI(1); /* Offset to account for shadow of SPR_CLOSEBOX */ - DrawSprite(SPR_CLOSEBOX, (colour != COLOUR_WHITE ? TC_BLACK : TC_SILVER) | (1U << PALETTE_TEXT_RECOLOUR), CenterBounds(r.left, r.right, d.width - s), CenterBounds(r.top, r.bottom, d.height - s)); + Point offset; + Dimension d = GetSpriteSize(SPR_CLOSEBOX, &offset); + d.width -= offset.x; + d.height -= offset.y; + int s = ScaleGUITrad(1); /* Offset to account for shadow of SPR_CLOSEBOX */ + DrawSprite(SPR_CLOSEBOX, (colour != COLOUR_WHITE ? TC_BLACK : TC_SILVER) | (1U << PALETTE_TEXT_RECOLOUR), CenterBounds(r.left, r.right, d.width - s) - offset.x, CenterBounds(r.top, r.bottom, d.height - s) - offset.y); } /**