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 // SPDX-License-Identifier: MIT
// //
// ES-DE // ES-DE Frontend
// ThemeData.cpp // ThemeData.cpp
// //
// Finds available themes on the file system and loads and parses these. // 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}, {"verticalAlignment", STRING},
{"color", COLOR}, {"color", COLOR},
{"backgroundColor", COLOR}, {"backgroundColor", COLOR},
{"backgroundMargins", NORMALIZED_PAIR},
{"letterCase", STRING}, {"letterCase", STRING},
{"lineSpacing", FLOAT}, {"lineSpacing", FLOAT},
{"opacity", FLOAT}, {"opacity", FLOAT},

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
// //
// ES-DE // ES-DE Frontend
// ThemeData.h // ThemeData.h
// //
// Finds available themes on the file system and loads and parses these. // Finds available themes on the file system and loads and parses these.

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
// //
// ES-DE // ES-DE Frontend
// TextComponent.cpp // TextComponent.cpp
// //
// Displays text. // Displays text.
@ -18,6 +18,7 @@ TextComponent::TextComponent()
, mRenderer {Renderer::getInstance()} , mRenderer {Renderer::getInstance()}
, mColor {0x000000FF} , mColor {0x000000FF}
, mBgColor {0x00000000} , mBgColor {0x00000000}
, mBackgroundMargins {0.0f, 0.0f}
, mColorOpacity {1.0f} , mColorOpacity {1.0f}
, mBgColorOpacity {0.0f} , mBgColorOpacity {0.0f}
, mRenderBackground {false} , mRenderBackground {false}
@ -63,6 +64,7 @@ TextComponent::TextComponent(const std::string& text,
, mRenderer {Renderer::getInstance()} , mRenderer {Renderer::getInstance()}
, mColor {0x000000FF} , mColor {0x000000FF}
, mBgColor {0x00000000} , mBgColor {0x00000000}
, mBackgroundMargins {0.0f, 0.0f}
, mColorOpacity {1.0f} , mColorOpacity {1.0f}
, mBgColorOpacity {0.0f} , mBgColorOpacity {0.0f}
, mRenderBackground {false} , mRenderBackground {false}
@ -249,8 +251,9 @@ void TextComponent::render(const glm::mat4& parentTrans)
auto renderFunc = [this](glm::mat4 trans, bool secondPass) { auto renderFunc = [this](glm::mat4 trans, bool secondPass) {
if (mRenderBackground && !secondPass) if (mRenderBackground && !secondPass)
mRenderer->drawRect(0.0f, 0.0f, mSize.x, mSize.y, mBgColor, mBgColor, false, mRenderer->drawRect(-mBackgroundMargins.x, 0.0f,
mOpacity * mThemeOpacity, mDimming); mSize.x + mBackgroundMargins.x + mBackgroundMargins.y, mSize.y,
mBgColor, mBgColor, false, mOpacity * mThemeOpacity, mDimming);
if (mTextCache) { if (mTextCache) {
const float textHeight {mTextCache->metrics.size.y}; const float textHeight {mTextCache->metrics.size.y};
float yOff {0.0f}; float yOff {0.0f};
@ -765,6 +768,12 @@ void TextComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
if (properties & LINE_SPACING && elem->has("lineSpacing")) if (properties & LINE_SPACING && elem->has("lineSpacing"))
setLineSpacing(glm::clamp(elem->get<float>("lineSpacing"), 0.5f, 3.0f)); 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)); 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. // 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 // SPDX-License-Identifier: MIT
// //
// ES-DE // ES-DE Frontend
// TextComponent.h // TextComponent.h
// //
// Displays text. // Displays text.
@ -155,6 +155,7 @@ private:
std::string mDefaultValue; std::string mDefaultValue;
unsigned int mColor; unsigned int mColor;
unsigned int mBgColor; unsigned int mBgColor;
glm::vec2 mBackgroundMargins;
float mColorOpacity; float mColorOpacity;
float mBgColorOpacity; float mBgColorOpacity;
bool mRenderBackground; bool mRenderBackground;