Added a 'backgroundMargins' property to the text element

This commit is contained in:
Leon Styhre 2024-06-02 22:30:41 +02:00
parent 5dc9eff312
commit f1642393a4
4 changed files with 17 additions and 6 deletions

View file

@ -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<std::string, std::map<std::string, ThemeData::ElementPropertyType>>
{"verticalAlignment", STRING},
{"color", COLOR},
{"backgroundColor", COLOR},
{"backgroundMargins", NORMALIZED_PAIR},
{"letterCase", STRING},
{"lineSpacing", FLOAT},
{"opacity", FLOAT},

View file

@ -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.

View file

@ -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<ThemeData>& theme,
if (properties & LINE_SPACING && elem->has("lineSpacing"))
setLineSpacing(glm::clamp(elem->get<float>("lineSpacing"), 0.5f, 3.0f));
if (elem->has("backgroundMargins")) {
const glm::vec2 backgroundMargins {
glm::clamp(elem->get<glm::vec2>("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.

View file

@ -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;