Added a 'backgroundCornerRadius' property to the text element

This commit is contained in:
Leon Styhre 2024-06-03 17:27:00 +02:00
parent fd40eb7545
commit c8c601bcc9
5 changed files with 44 additions and 15 deletions

View file

@ -440,6 +440,7 @@ std::map<std::string, std::map<std::string, ThemeData::ElementPropertyType>>
{"color", COLOR},
{"backgroundColor", COLOR},
{"backgroundMargins", NORMALIZED_PAIR},
{"backgroundCornerRadius", FLOAT},
{"letterCase", STRING},
{"lineSpacing", FLOAT},
{"opacity", FLOAT},

View file

@ -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<ThemeData>& theme,
}
}
if (elem->has("backgroundMargins")) {
const glm::vec2 backgroundMargins {
glm::clamp(elem->get<glm::vec2>("backgroundMargins"), 0.0f, 0.5f)};
mBackgroundMargins = backgroundMargins * Renderer::getScreenWidth();
}
if (elem->has("backgroundCornerRadius")) {
mBackgroundCornerRadius =
glm::clamp(elem->get<float>("backgroundCornerRadius"), 0.0f, 0.5f) *
mRenderer->getScreenWidth();
}
if (properties & LETTER_CASE && elem->has("letterCaseSystemNameSuffix")) {
const std::string& letterCase {elem->get<std::string>("letterCaseSystemNameSuffix")};
if (letterCase == "uppercase") {
@ -768,12 +794,6 @@ 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

@ -156,6 +156,7 @@ private:
unsigned int mColor;
unsigned int mBgColor;
glm::vec2 mBackgroundMargins;
float mBackgroundCornerRadius;
float mColorOpacity;
float mBgColorOpacity;
bool mRenderBackground;

View file

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

View file

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