Fixed an issue where text element vertical auto-sizing did not work correctly.

This commit is contained in:
Leon Styhre 2022-09-23 19:40:39 +02:00
parent 1839dfc31a
commit 00b71c7b27
3 changed files with 14 additions and 8 deletions

View file

@ -336,9 +336,9 @@ void GuiComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
const std::string& element,
unsigned int properties)
{
glm::vec2 scale {getParent() ?
getParent()->getSize() :
glm::vec2 {Renderer::getScreenWidth(), Renderer::getScreenHeight()}};
const glm::vec2 scale {getParent() ?
getParent()->getSize() :
glm::vec2 {Renderer::getScreenWidth(), Renderer::getScreenHeight()}};
const ThemeData::ThemeElement* elem {theme->getElement(view, element, "")};
if (!elem)

View file

@ -29,6 +29,7 @@ TextComponent::TextComponent()
, mLineSpacing {1.5f}
, mNoTopMargin {false}
, mSelectable {false}
, mVerticalAutoSizing {false}
{
}
@ -55,6 +56,7 @@ TextComponent::TextComponent(const std::string& text,
, mLineSpacing {1.5f}
, mNoTopMargin {false}
, mSelectable {false}
, mVerticalAutoSizing {false}
{
setFont(font);
setColor(color);
@ -275,6 +277,9 @@ void TextComponent::calculateExtent()
void TextComponent::onTextChanged()
{
if (!mVerticalAutoSizing)
mVerticalAutoSizing = (mSize.x != 0.0f && mSize.y == 0.0f);
calculateExtent();
if (!mFont || mText.empty() || mSize.x == 0.0f || mSize.y == 0.0f) {
@ -325,15 +330,15 @@ void TextComponent::onTextChanged()
text, glm::vec2 {}, mColor, mSize.x, mHorizontalAlignment, mLineSpacing, mNoTopMargin));
}
else if (isMultiline && text.size() && !isScrollable) {
const std::string wrappedText {
f->wrapText(text, mSize.x, mSize.y - lineHeight, mLineSpacing)};
const std::string wrappedText {f->wrapText(
text, mSize.x, (mVerticalAutoSizing ? 0.0f : mSize.y - lineHeight), mLineSpacing)};
mTextCache = std::shared_ptr<TextCache>(f->buildTextCache(wrappedText, glm::vec2 {}, mColor,
mSize.x, mHorizontalAlignment,
mLineSpacing, mNoTopMargin));
}
else {
mTextCache = std::shared_ptr<TextCache>(
f->buildTextCache(f->wrapText(text, mSize.x), glm::vec2 {}, mColor, mSize.x,
f->buildTextCache(f->wrapText(text, mSize.x), glm::vec2 {0.0f, 0.0f}, mColor, mSize.x,
mHorizontalAlignment, mLineSpacing, mNoTopMargin));
}

View file

@ -29,8 +29,8 @@ public:
const std::shared_ptr<Font>& font,
unsigned int color = 0x000000FF,
Alignment align = ALIGN_LEFT,
glm::vec3 pos = {},
glm::vec2 size = {},
glm::vec3 pos = {0.0f, 0.0f, 0.0f},
glm::vec2 size = {0.0f, 0.0f},
unsigned int bgcolor = 0x00000000);
void setFont(const std::shared_ptr<Font>& font);
@ -116,6 +116,7 @@ private:
float mLineSpacing;
bool mNoTopMargin;
bool mSelectable;
bool mVerticalAutoSizing;
};
#endif // ES_CORE_COMPONENTS_TEXT_COMPONENT_H