From c8c601bcc97648248fcdb2ce64282f5ca004d6d1 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Mon, 3 Jun 2024 17:27:00 +0200 Subject: [PATCH] Added a 'backgroundCornerRadius' property to the text element --- es-core/src/ThemeData.cpp | 1 + es-core/src/components/TextComponent.cpp | 38 ++++++++++++++++++------ es-core/src/components/TextComponent.h | 1 + es-core/src/renderers/Renderer.cpp | 10 +++++-- es-core/src/renderers/Renderer.h | 9 +++--- 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/es-core/src/ThemeData.cpp b/es-core/src/ThemeData.cpp index ca8857c5d..b22a06178 100644 --- a/es-core/src/ThemeData.cpp +++ b/es-core/src/ThemeData.cpp @@ -440,6 +440,7 @@ std::map> {"color", COLOR}, {"backgroundColor", COLOR}, {"backgroundMargins", NORMALIZED_PAIR}, + {"backgroundCornerRadius", FLOAT}, {"letterCase", STRING}, {"lineSpacing", FLOAT}, {"opacity", FLOAT}, diff --git a/es-core/src/components/TextComponent.cpp b/es-core/src/components/TextComponent.cpp index 6e55a2f06..bb09627f1 100644 --- a/es-core/src/components/TextComponent.cpp +++ b/es-core/src/components/TextComponent.cpp @@ -19,6 +19,7 @@ TextComponent::TextComponent() , mColor {0x000000FF} , mBgColor {0x00000000} , mBackgroundMargins {0.0f, 0.0f} + , mBackgroundCornerRadius {0.0f} , mColorOpacity {1.0f} , mBgColorOpacity {0.0f} , mRenderBackground {false} @@ -65,6 +66,7 @@ TextComponent::TextComponent(const std::string& text, , mColor {0x000000FF} , mBgColor {0x00000000} , mBackgroundMargins {0.0f, 0.0f} + , mBackgroundCornerRadius {0.0f} , mColorOpacity {1.0f} , mBgColorOpacity {0.0f} , mRenderBackground {false} @@ -251,9 +253,21 @@ void TextComponent::render(const glm::mat4& parentTrans) auto renderFunc = [this](glm::mat4 trans, bool secondPass) { if (mRenderBackground && !secondPass) - mRenderer->drawRect(-mBackgroundMargins.x, 0.0f, - mSize.x + mBackgroundMargins.x + mBackgroundMargins.y, mSize.y, - mBgColor, mBgColor, false, mOpacity * mThemeOpacity, mDimming); + if (mBackgroundMargins.x > 0.0f) { + trans = glm::translate(trans, glm::vec3 {-mBackgroundMargins.x, 0.0f, 0.0f}); + mRenderer->setMatrix(trans); + } + + mRenderer->drawRect(0.0f, 0.0f, mSize.x + mBackgroundMargins.x + mBackgroundMargins.y, + mSize.y, mBgColor, mBgColor, false, mOpacity * mThemeOpacity, mDimming, + Renderer::BlendFactor::SRC_ALPHA, + Renderer::BlendFactor::ONE_MINUS_SRC_ALPHA, mBackgroundCornerRadius); + + if (mBackgroundMargins.x > 0.0f) { + trans = glm::translate(trans, glm::vec3 {mBackgroundMargins.x, 0.0f, 0.0f}); + mRenderer->setMatrix(trans); + } + if (mTextCache) { const float textHeight {mTextCache->metrics.size.y}; float yOff {0.0f}; @@ -721,6 +735,18 @@ void TextComponent::applyTheme(const std::shared_ptr& theme, } } + if (elem->has("backgroundMargins")) { + const glm::vec2 backgroundMargins { + glm::clamp(elem->get("backgroundMargins"), 0.0f, 0.5f)}; + mBackgroundMargins = backgroundMargins * Renderer::getScreenWidth(); + } + + if (elem->has("backgroundCornerRadius")) { + mBackgroundCornerRadius = + glm::clamp(elem->get("backgroundCornerRadius"), 0.0f, 0.5f) * + mRenderer->getScreenWidth(); + } + if (properties & LETTER_CASE && elem->has("letterCaseSystemNameSuffix")) { const std::string& letterCase {elem->get("letterCaseSystemNameSuffix")}; if (letterCase == "uppercase") { @@ -768,12 +794,6 @@ void TextComponent::applyTheme(const std::shared_ptr& theme, if (properties & LINE_SPACING && elem->has("lineSpacing")) setLineSpacing(glm::clamp(elem->get("lineSpacing"), 0.5f, 3.0f)); - if (elem->has("backgroundMargins")) { - const glm::vec2 backgroundMargins { - glm::clamp(elem->get("backgroundMargins"), 0.0f, 0.5f)}; - mBackgroundMargins = backgroundMargins * Renderer::getScreenWidth(); - } - setFont(Font::getFromTheme(elem, properties, mFont, maxHeight)); // We need to do this after setting the font as the scroll speed is calculated from its size. diff --git a/es-core/src/components/TextComponent.h b/es-core/src/components/TextComponent.h index bfc66efba..68ff7ece8 100644 --- a/es-core/src/components/TextComponent.h +++ b/es-core/src/components/TextComponent.h @@ -156,6 +156,7 @@ private: unsigned int mColor; unsigned int mBgColor; glm::vec2 mBackgroundMargins; + float mBackgroundCornerRadius; float mColorOpacity; float mBgColorOpacity; bool mRenderBackground; diff --git a/es-core/src/renderers/Renderer.cpp b/es-core/src/renderers/Renderer.cpp index 33624d07c..2a51ad3e1 100644 --- a/es-core/src/renderers/Renderer.cpp +++ b/es-core/src/renderers/Renderer.cpp @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// ES-DE +// ES-DE Frontend // Renderer.cpp // // Generic rendering functions. @@ -466,7 +466,8 @@ void Renderer::drawRect(const float x, const float opacity, const float dimming, const BlendFactor srcBlendFactor, - const BlendFactor dstBlendFactor) + const BlendFactor dstBlendFactor, + const float cornerRadius) { Vertex vertices[4]; @@ -494,6 +495,11 @@ void Renderer::drawRect(const float x, vertices->opacity = opacity; vertices->dimming = dimming; + if (cornerRadius > 0.0f) { + vertices->shaderFlags = vertices->shaderFlags | Renderer::ShaderFlags::ROUNDED_CORNERS; + vertices->cornerRadius = cornerRadius; + } + bindTexture(0, 0); drawTriangleStrips(vertices, 4, srcBlendFactor, dstBlendFactor); } diff --git a/es-core/src/renderers/Renderer.h b/es-core/src/renderers/Renderer.h index 4ec4d1dfa..7d624df06 100644 --- a/es-core/src/renderers/Renderer.h +++ b/es-core/src/renderers/Renderer.h @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// ES-DE +// ES-DE Frontend // Renderer.h // // Generic rendering functions. @@ -174,10 +174,11 @@ public: const unsigned int color, const unsigned int colorEnd, const bool horizontalGradient = false, - const float opacity = 1.0, - const float dimming = 1.0, + const float opacity = 1.0f, + const float dimming = 1.0f, const BlendFactor srcBlendFactor = BlendFactor::SRC_ALPHA, - const BlendFactor dstBlendFactor = BlendFactor::ONE_MINUS_SRC_ALPHA); + const BlendFactor dstBlendFactor = BlendFactor::ONE_MINUS_SRC_ALPHA, + const float cornerRadius = 0.0f); const glm::mat4& getProjectionMatrix() { return mProjectionMatrix; } const glm::mat4& getProjectionMatrixNormal() { return mProjectionMatrixNormal; }