From 94fa8bf5f72511589de502593478f46443a1c2d3 Mon Sep 17 00:00:00 2001 From: Arkadiusz Lach Date: Mon, 28 Oct 2024 23:42:19 +0100 Subject: [PATCH] Text overflow improved * OTUI property "text-overflow" now takes 2 values "length character" (eg. "text-overflow: 13 [...]" will result in cutting long text and adding [...] at the end) * OTUI property "text-overflow-length" takes number value to cut text that is longer than the value * OTUI property "text-overflow-character" takes string value and adds at the end of overflowed text, default is "..." * Updated container style with new property --- data/styles/40-container.otui | 2 +- src/framework/luafunctions.cpp | 2 ++ src/framework/ui/uiwidget.h | 6 ++++-- src/framework/ui/uiwidgettext.cpp | 26 +++++++++++++++++++------- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/data/styles/40-container.otui b/data/styles/40-container.otui index 5ec47b1..b538337 100644 --- a/data/styles/40-container.otui +++ b/data/styles/40-container.otui @@ -5,7 +5,7 @@ PageButton < Button ContainerWindow < MiniWindow height: 150 - text-overflow: 18 + text-overflow-length: 18 &save: true &containerWindow: true diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index adcf29a..f9eda50 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -730,6 +730,8 @@ void Application::registerLuaFunctions() g_lua.bindClassMemberFunction("setEventListener", &UIWidget::setEventListener); g_lua.bindClassMemberFunction("removeEventListener", &UIWidget::removeEventListener); g_lua.bindClassMemberFunction("hasEventListener", &UIWidget::hasEventListener); + g_lua.bindClassMemberFunction("setTextOverflowLength", &UIWidget::setTextOverflowLength); + g_lua.bindClassMemberFunction("setTextOverflowCharacter", &UIWidget::setTextOverflowCharacter); // UILayout g_lua.registerClass(); diff --git a/src/framework/ui/uiwidget.h b/src/framework/ui/uiwidget.h index fb3ff8a..e172a04 100644 --- a/src/framework/ui/uiwidget.h +++ b/src/framework/ui/uiwidget.h @@ -550,7 +550,8 @@ class UIWidget : public LuaObject std::vector> m_textColors; std::vector> m_drawTextColors; stdext::boolean m_shadow; - uint16 m_textOverflow; + uint16 m_textOverflowLength; + std::string m_textOverflowCharacter; std::vector> m_rectToWord; @@ -569,7 +570,8 @@ class UIWidget : public LuaObject void setTextOnlyUpperCase(bool textOnlyUpperCase) { m_textOnlyUpperCase = textOnlyUpperCase; setText(m_text); } void setFont(const std::string& fontName); void setShadow(bool shadow) { m_shadow = shadow; } - void setTextOverflow(uint16 overflow) { m_textOverflow = overflow; updateText(); } + void setTextOverflowLength(uint16 length) { m_textOverflowLength = length; updateText(); } + void setTextOverflowCharacter(std::string character) { m_textOverflowCharacter = character; updateText(); } std::string getText() { return m_text; } std::string getDrawText() { return m_drawText; } diff --git a/src/framework/ui/uiwidgettext.cpp b/src/framework/ui/uiwidgettext.cpp index 6626f6b..47b90c0 100644 --- a/src/framework/ui/uiwidgettext.cpp +++ b/src/framework/ui/uiwidgettext.cpp @@ -30,19 +30,20 @@ void UIWidget::initText() { m_font = g_fonts.getDefaultFont(); m_textAlign = Fw::AlignCenter; - m_textOverflow = 0; + m_textOverflowLength = 0; + m_textOverflowCharacter = "..."; } void UIWidget::updateText() { if (m_textWrap && m_rect.isValid()) { - if (m_textOverflow > 0 && m_text.length() > m_textOverflow) - m_drawText = m_font->wrapText(m_text.substr(0, m_textOverflow - 3) + "...", getWidth() - m_textOffset.x, &m_drawTextColors); + if (m_textOverflowLength > 0 && m_text.length() > m_textOverflowLength) + m_drawText = m_font->wrapText(m_text.substr(0, m_textOverflowLength - m_textOverflowCharacter.length()) + m_textOverflowCharacter, getWidth() - m_textOffset.x, &m_drawTextColors); else m_drawText = m_font->wrapText(m_text, getWidth() - m_textOffset.x, &m_drawTextColors); } else { - if (m_textOverflow > 0 && m_text.length() > m_textOverflow) - m_drawText = m_text.substr(0, m_textOverflow - 3) + "..."; + if (m_textOverflowLength > 0 && m_text.length() > m_textOverflowLength) + m_drawText = m_text.substr(0, m_textOverflowLength - m_textOverflowCharacter.length()) + m_textOverflowCharacter; else m_drawText = m_text; } @@ -87,8 +88,19 @@ void UIWidget::parseTextStyle(const OTMLNodePtr& styleNode) setFont(node->value()); else if (node->tag() == "shadow") setShadow(node->value()); - else if (node->tag() == "text-overflow") - setTextOverflow(node->value()); + else if (node->tag() == "text-overflow") { + auto split = stdext::split(node->value(true), " "); + if (split.size() == 2) { + setTextOverflowLength(stdext::safe_cast(g_ui.getOTUIVarSafe(split[0]))); + setTextOverflowCharacter(g_ui.getOTUIVarSafe(split[1])); + } + else + throw OTMLException(node, "text-overflow param must have its length followed by its character (eg. \"13 ...\")"); + } + else if (node->tag() == "text-overflow-length") + setTextOverflowLength(node->value()); + else if (node->tag() == "text-overflow-character") + setTextOverflowCharacter(node->value<>()); } }