From f1642393a4cad0e0463a679908ed5029ad9dfbb5 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Sun, 2 Jun 2024 22:30:41 +0200 Subject: [PATCH] Added a 'backgroundMargins' property to the text element --- es-core/src/ThemeData.cpp | 3 ++- es-core/src/ThemeData.h | 2 +- es-core/src/components/TextComponent.cpp | 15 ++++++++++++--- es-core/src/components/TextComponent.h | 3 ++- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/es-core/src/ThemeData.cpp b/es-core/src/ThemeData.cpp index 76e149d0f..63ec61972 100644 --- a/es-core/src/ThemeData.cpp +++ b/es-core/src/ThemeData.cpp @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// ES-DE +// ES-DE Frontend // ThemeData.cpp // // Finds available themes on the file system and loads and parses these. @@ -438,6 +438,7 @@ std::map> {"verticalAlignment", STRING}, {"color", COLOR}, {"backgroundColor", COLOR}, + {"backgroundMargins", NORMALIZED_PAIR}, {"letterCase", STRING}, {"lineSpacing", FLOAT}, {"opacity", FLOAT}, diff --git a/es-core/src/ThemeData.h b/es-core/src/ThemeData.h index d6ed87fa3..f118ddc17 100644 --- a/es-core/src/ThemeData.h +++ b/es-core/src/ThemeData.h @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// ES-DE +// ES-DE Frontend // ThemeData.h // // Finds available themes on the file system and loads and parses these. diff --git a/es-core/src/components/TextComponent.cpp b/es-core/src/components/TextComponent.cpp index 715a70045..6e55a2f06 100644 --- a/es-core/src/components/TextComponent.cpp +++ b/es-core/src/components/TextComponent.cpp @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// ES-DE +// ES-DE Frontend // TextComponent.cpp // // Displays text. @@ -18,6 +18,7 @@ TextComponent::TextComponent() , mRenderer {Renderer::getInstance()} , mColor {0x000000FF} , mBgColor {0x00000000} + , mBackgroundMargins {0.0f, 0.0f} , mColorOpacity {1.0f} , mBgColorOpacity {0.0f} , mRenderBackground {false} @@ -63,6 +64,7 @@ TextComponent::TextComponent(const std::string& text, , mRenderer {Renderer::getInstance()} , mColor {0x000000FF} , mBgColor {0x00000000} + , mBackgroundMargins {0.0f, 0.0f} , mColorOpacity {1.0f} , mBgColorOpacity {0.0f} , mRenderBackground {false} @@ -249,8 +251,9 @@ void TextComponent::render(const glm::mat4& parentTrans) auto renderFunc = [this](glm::mat4 trans, bool secondPass) { if (mRenderBackground && !secondPass) - mRenderer->drawRect(0.0f, 0.0f, mSize.x, mSize.y, mBgColor, mBgColor, false, - mOpacity * mThemeOpacity, mDimming); + mRenderer->drawRect(-mBackgroundMargins.x, 0.0f, + mSize.x + mBackgroundMargins.x + mBackgroundMargins.y, mSize.y, + mBgColor, mBgColor, false, mOpacity * mThemeOpacity, mDimming); if (mTextCache) { const float textHeight {mTextCache->metrics.size.y}; float yOff {0.0f}; @@ -765,6 +768,12 @@ 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 837c8c92e..bfc66efba 100644 --- a/es-core/src/components/TextComponent.h +++ b/es-core/src/components/TextComponent.h @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// ES-DE +// ES-DE Frontend // TextComponent.h // // Displays text. @@ -155,6 +155,7 @@ private: std::string mDefaultValue; unsigned int mColor; unsigned int mBgColor; + glm::vec2 mBackgroundMargins; float mColorOpacity; float mBgColorOpacity; bool mRenderBackground;