From 0bf0a4a1eff69fc78f27635e006e07c849d5bff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 3 Jul 2023 22:54:25 +0200 Subject: [PATCH] UIContext: Add an easy way to draw drop shadows under rectangular things --- Common/Math/geom2d.h | 3 +++ Common/UI/Context.cpp | 11 +++++++++++ Common/UI/Context.h | 3 +++ UI/OnScreenDisplay.cpp | 4 +--- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Common/Math/geom2d.h b/Common/Math/geom2d.h index 3d2223c36e..f319d7c79d 100644 --- a/Common/Math/geom2d.h +++ b/Common/Math/geom2d.h @@ -69,6 +69,9 @@ struct Bounds { Bounds Expand(float xAmount, float yAmount) const { return Bounds(x - xAmount, y - yAmount, w + xAmount * 2, h + yAmount * 2); } + Bounds Expand(float left, float top, float right, float bottom) const { + return Bounds(x - left, y - top, w + left + right, h + top + bottom); + } Bounds Offset(float xAmount, float yAmount) const { return Bounds(x + xAmount, y + yAmount, w, h); } diff --git a/Common/UI/Context.cpp b/Common/UI/Context.cpp index cb62571bf0..6e4e277f07 100644 --- a/Common/UI/Context.cpp +++ b/Common/UI/Context.cpp @@ -313,6 +313,17 @@ void UIContext::FillRect(const UI::Drawable &drawable, const Bounds &bounds) { } } +void UIContext::DrawRectDropShadow(const Bounds &bounds, float radius, float alpha, uint32_t color) { + if (alpha <= 0.0f) + return; + + color = colorAlpha(color, alpha); + + // Bias the shadow downwards a bit. + Bounds shadowBounds = bounds.Expand(radius, 0.5 * radius, radius, 1.5 * radius); + Draw()->DrawImage4Grid(theme->dropShadow4Grid, shadowBounds.x, shadowBounds.y, shadowBounds.x2(), shadowBounds.y2(), color, radius * (1.0f / 24.0f) * 2.0f); +} + void UIContext::DrawImageVGradient(ImageID image, uint32_t color1, uint32_t color2, const Bounds &bounds) { uidrawbuffer_->DrawImageStretchVGradient(image, bounds.x, bounds.y, bounds.x2(), bounds.y2(), color1, color2); } diff --git a/Common/UI/Context.h b/Common/UI/Context.h index 1e2767d181..f8c409c287 100644 --- a/Common/UI/Context.h +++ b/Common/UI/Context.h @@ -77,6 +77,8 @@ public: void SetTintSaturation(float tint, float sat); + // High level drawing functions. They generally assume the default texture to be bounds. + void SetFontStyle(const UI::FontStyle &style); const UI::FontStyle &GetFontStyle() { return *fontStyle_; } void SetFontScale(float scaleX, float scaleY); @@ -88,6 +90,7 @@ public: void DrawTextRect(const char *str, const Bounds &bounds, uint32_t color, int align = 0); void DrawTextShadowRect(const char *str, const Bounds &bounds, uint32_t color, int align = 0); void FillRect(const UI::Drawable &drawable, const Bounds &bounds); + void DrawRectDropShadow(const Bounds &bounds, float radius, float alpha, uint32_t color = 0); void DrawImageVGradient(ImageID image, uint32_t color1, uint32_t color2, const Bounds &bounds); // in dps, like dp_xres and dp_yres diff --git a/UI/OnScreenDisplay.cpp b/UI/OnScreenDisplay.cpp index 9d921b666f..51d633940d 100644 --- a/UI/OnScreenDisplay.cpp +++ b/UI/OnScreenDisplay.cpp @@ -99,9 +99,7 @@ static void RenderNotice(UIContext &dc, Bounds bounds, float height1, NoticeLeve uint32_t foreGround = whiteAlpha(alpha); - Bounds shadowBounds = bounds.Expand(10.0f); - - dc.Draw()->DrawImage4Grid(dc.theme->dropShadow4Grid, shadowBounds.x, shadowBounds.y + 4.0f, shadowBounds.x2(), shadowBounds.y2(), alphaMul(0xFF000000, 0.9f * alpha), 1.0f); + dc.DrawRectDropShadow(bounds, 12.0f, 0.7f * alpha); dc.FillRect(background, bounds);