Added margin support to TextComponent for abbreviated text.

This commit is contained in:
Leon Styhre 2021-01-05 16:52:39 +01:00
parent dc9c49438f
commit 8ecc50b4ce
2 changed files with 10 additions and 3 deletions

View file

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

View file

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