From 8ecc50b4ce848113ce2b8673d1116d79e23e3aa7 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Tue, 5 Jan 2021 16:52:39 +0100 Subject: [PATCH] Added margin support to TextComponent for abbreviated text. --- es-core/src/components/TextComponent.cpp | 9 +++++++-- es-core/src/components/TextComponent.h | 4 +++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/es-core/src/components/TextComponent.cpp b/es-core/src/components/TextComponent.cpp index 9291e5531..5224965fb 100644 --- a/es-core/src/components/TextComponent.cpp +++ b/es-core/src/components/TextComponent.cpp @@ -23,6 +23,7 @@ TextComponent::TextComponent( mVerticalAlignment(ALIGN_CENTER), mLineSpacing(1.5f), mBgColor(0), + mMargin(0.0f), mRenderBackground(false) { } @@ -35,7 +36,8 @@ TextComponent::TextComponent( Alignment align, Vector3f pos, Vector2f size, - unsigned int bgcolor) + unsigned int bgcolor, + float margin) : GuiComponent(window), mFont(nullptr), mUppercase(false), @@ -45,6 +47,7 @@ TextComponent::TextComponent( mVerticalAlignment(ALIGN_CENTER), mLineSpacing(1.5f), mBgColor(0), + mMargin(margin), mRenderBackground(false) { setFont(font); @@ -230,8 +233,10 @@ void TextComponent::onTextChanged() // Abbreviate text. const std::string abbrev = "..."; Vector2f abbrevSize = f->sizeText(abbrev); + // mMargin adds a margin around the text if it's abbreviated. + float marginAdjustedSize = mSize.x() - (mSize.x() * mMargin); - while (text.size() && size.x() + abbrevSize.x() > mSize.x()) { + while (text.size() && size.x() + abbrevSize.x() > marginAdjustedSize) { size_t newSize = Utils::String::prevCursor(text, text.size()); text.erase(newSize, text.size() - newSize); size = f->sizeText(text); diff --git a/es-core/src/components/TextComponent.h b/es-core/src/components/TextComponent.h index c49379bf3..91e0cf303 100644 --- a/es-core/src/components/TextComponent.h +++ b/es-core/src/components/TextComponent.h @@ -33,7 +33,8 @@ public: Alignment align = ALIGN_LEFT, Vector3f pos = Vector3f::Zero(), Vector2f size = Vector2f::Zero(), - unsigned int bgcolor = 0x00000000); + unsigned int bgcolor = 0x00000000, + float margin = 0.0f); void setFont(const std::shared_ptr& font); void setUppercase(bool uppercase); @@ -80,6 +81,7 @@ private: unsigned int mBgColor; unsigned char mColorOpacity; unsigned char mBgColorOpacity; + float mMargin; bool mRenderBackground; bool mUppercase;