Skip to content

Commit

Permalink
Text overflow improved
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Oen44 committed Oct 28, 2024
1 parent ee530d9 commit 94fa8bf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion data/styles/40-container.otui
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PageButton < Button

ContainerWindow < MiniWindow
height: 150
text-overflow: 18
text-overflow-length: 18
&save: true
&containerWindow: true

Expand Down
2 changes: 2 additions & 0 deletions src/framework/luafunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,8 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<UIWidget>("setEventListener", &UIWidget::setEventListener);
g_lua.bindClassMemberFunction<UIWidget>("removeEventListener", &UIWidget::removeEventListener);
g_lua.bindClassMemberFunction<UIWidget>("hasEventListener", &UIWidget::hasEventListener);
g_lua.bindClassMemberFunction<UIWidget>("setTextOverflowLength", &UIWidget::setTextOverflowLength);
g_lua.bindClassMemberFunction<UIWidget>("setTextOverflowCharacter", &UIWidget::setTextOverflowCharacter);

// UILayout
g_lua.registerClass<UILayout>();
Expand Down
6 changes: 4 additions & 2 deletions src/framework/ui/uiwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,8 @@ class UIWidget : public LuaObject
std::vector<std::pair<int, Color>> m_textColors;
std::vector<std::pair<int, Color>> m_drawTextColors;
stdext::boolean<false> m_shadow;
uint16 m_textOverflow;
uint16 m_textOverflowLength;
std::string m_textOverflowCharacter;

std::vector<std::pair<Rect, std::string>> m_rectToWord;

Expand All @@ -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; }
Expand Down
26 changes: 19 additions & 7 deletions src/framework/ui/uiwidgettext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -87,8 +88,19 @@ void UIWidget::parseTextStyle(const OTMLNodePtr& styleNode)
setFont(node->value());
else if (node->tag() == "shadow")
setShadow(node->value<bool>());
else if (node->tag() == "text-overflow")
setTextOverflow(node->value<uint16>());
else if (node->tag() == "text-overflow") {
auto split = stdext::split(node->value(true), " ");
if (split.size() == 2) {
setTextOverflowLength(stdext::safe_cast<uint16>(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<uint16>());
else if (node->tag() == "text-overflow-character")
setTextOverflowCharacter(node->value<>());
}
}

Expand Down

0 comments on commit 94fa8bf

Please sign in to comment.