mirror of
https://github.com/edubart/otclient.git
synced 2025-10-16 04:24:54 +02:00
chat line wrapping
* rework UIWidget text wrapping * implement auto wrap * fixes in console
This commit is contained in:
@@ -294,14 +294,15 @@ void Application::registerLuaFunctions()
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getImageBorderLeft", &UIWidget::getImageBorderLeft);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("resizeToText", &UIWidget::resizeToText);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("clearText", &UIWidget::clearText);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("wrapText", &UIWidget::wrapText);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setText", &UIWidget::setText);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setTextAlign", &UIWidget::setTextAlign);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setTextOffset", &UIWidget::setTextOffset);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setTextWrap", &UIWidget::setTextWrap);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setFont", &UIWidget::setFont);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getText", &UIWidget::getText);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getTextAlign", &UIWidget::getTextAlign);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getTextOffset", &UIWidget::getTextOffset);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getTextWrap", &UIWidget::getTextWrap);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getFont", &UIWidget::getFont);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getTextSize", &UIWidget::getTextSize);
|
||||
|
||||
@@ -502,6 +503,7 @@ void Application::registerLuaFunctions()
|
||||
g_lua.bindClassStaticFunction("g_ui", "createWidgetFromOTML", std::bind(&UIManager::createWidgetFromOTML, &g_ui, _1, _2));
|
||||
g_lua.bindClassStaticFunction("g_ui", "getRootWidget", std::bind(&UIManager::getRootWidget, &g_ui));
|
||||
g_lua.bindClassStaticFunction("g_ui", "getDraggingWidget", std::bind(&UIManager::getDraggingWidget, &g_ui));
|
||||
g_lua.bindClassStaticFunction("g_ui", "getPressedWidget", std::bind(&UIManager::getPressedWidget, &g_ui));
|
||||
g_lua.bindClassStaticFunction("g_ui", "setDebugBoxesDrawing", std::bind(&UIManager::setDebugBoxesDrawing, &g_ui, _1));
|
||||
g_lua.bindClassStaticFunction("g_ui", "isDrawingDebugBoxes", std::bind(&UIManager::setDebugBoxesDrawing, &g_ui, _1));
|
||||
|
||||
|
@@ -1251,13 +1251,14 @@ void UIWidget::onStyleApply(const std::string& styleName, const OTMLNodePtr& sty
|
||||
void UIWidget::onGeometryChange(const Rect& oldRect, const Rect& newRect)
|
||||
{
|
||||
callLuaField("onGeometryChange", oldRect, newRect);
|
||||
|
||||
if(m_textWrap && oldRect.size() != newRect.size())
|
||||
updateText();
|
||||
}
|
||||
|
||||
void UIWidget::onLayoutUpdate()
|
||||
{
|
||||
callLuaField("onLayoutUpdate");
|
||||
if(UIWidgetPtr parent = getParent())
|
||||
parent->onLayoutUpdate();
|
||||
}
|
||||
|
||||
void UIWidget::onFocusChange(bool focused, Fw::FocusReason reason)
|
||||
|
@@ -436,6 +436,7 @@ public:
|
||||
// text related
|
||||
private:
|
||||
void initText();
|
||||
void updateText();
|
||||
void parseTextStyle(const OTMLNodePtr& styleNode);
|
||||
|
||||
Boolean<true> m_textMustRecache;
|
||||
@@ -449,25 +450,31 @@ protected:
|
||||
virtual void onFontChange(const std::string& font);
|
||||
|
||||
std::string m_text;
|
||||
std::string m_drawText;
|
||||
Fw::AlignmentFlag m_textAlign;
|
||||
Point m_textOffset;
|
||||
Boolean<false> m_textWrap;
|
||||
Boolean<false> m_textAutoResize;
|
||||
FontPtr m_font;
|
||||
|
||||
public:
|
||||
void resizeToText() { setSize(getTextSize()); }
|
||||
void clearText() { setText(""); }
|
||||
void wrapText();
|
||||
|
||||
void setText(const std::string& text);
|
||||
void setTextAlign(Fw::AlignmentFlag align) { m_textAlign = align; m_textMustRecache = true; }
|
||||
void setTextOffset(const Point& offset) { m_textOffset = offset; m_textMustRecache = true; }
|
||||
void setTextAlign(Fw::AlignmentFlag align) { m_textAlign = align; updateText(); }
|
||||
void setTextOffset(const Point& offset) { m_textOffset = offset; updateText(); }
|
||||
void setTextWrap(bool textWrap) { m_textWrap = textWrap; updateText(); }
|
||||
void setTextAutoResize(bool textAutoResize) { m_textAutoResize = textAutoResize; updateText(); }
|
||||
void setFont(const std::string& fontName);
|
||||
|
||||
std::string getText() { return m_text; }
|
||||
std::string getDrawText() { return m_drawText; }
|
||||
Fw::AlignmentFlag getTextAlign() { return m_textAlign; }
|
||||
Point getTextOffset() { return m_textOffset; }
|
||||
bool getTextWrap() { return m_textWrap; }
|
||||
std::string getFont() { return m_font->getName(); }
|
||||
Size getTextSize() { return m_font->calculateTextRectSize(m_text); }
|
||||
Size getTextSize() { return m_font->calculateTextRectSize(m_drawText); }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@@ -33,6 +33,29 @@ void UIWidget::initText()
|
||||
m_textCoordsBuffer.enableHardwareCaching();
|
||||
}
|
||||
|
||||
void UIWidget::updateText()
|
||||
{
|
||||
if(m_textWrap && m_rect.isValid())
|
||||
m_drawText = m_font->wrapText(m_text, getWidth() - m_textOffset.x);
|
||||
else
|
||||
m_drawText = m_text;
|
||||
|
||||
// update rect size
|
||||
if(!m_rect.isValid()) {
|
||||
Size textSize = getTextSize();
|
||||
Size newSize = getSize();
|
||||
if(newSize.width() <= 0)
|
||||
newSize.setWidth(textSize.width());
|
||||
if(newSize.height() <= 0)
|
||||
newSize.setHeight(textSize.height());
|
||||
setSize(newSize);
|
||||
} else if(m_textAutoResize) {
|
||||
setSize(getTextSize());
|
||||
}
|
||||
|
||||
m_textMustRecache = true;
|
||||
}
|
||||
|
||||
void UIWidget::parseTextStyle(const OTMLNodePtr& styleNode)
|
||||
{
|
||||
for(const OTMLNodePtr& node : styleNode->children()) {
|
||||
@@ -42,6 +65,10 @@ void UIWidget::parseTextStyle(const OTMLNodePtr& styleNode)
|
||||
setTextAlign(Fw::translateAlignment(node->value()));
|
||||
else if(node->tag() == "text-offset")
|
||||
setTextOffset(node->value<Point>());
|
||||
else if(node->tag() == "text-wrap")
|
||||
setTextWrap(node->value<bool>());
|
||||
else if(node->tag() == "text-auto-resize")
|
||||
setTextAutoResize(node->value<bool>());
|
||||
else if(node->tag() == "font")
|
||||
setFont(node->value());
|
||||
}
|
||||
@@ -49,7 +76,7 @@ void UIWidget::parseTextStyle(const OTMLNodePtr& styleNode)
|
||||
|
||||
void UIWidget::drawText(const Rect& screenCoords)
|
||||
{
|
||||
if(m_text.length() == 0 || m_color.aF() == 0.0f)
|
||||
if(m_drawText.length() == 0 || m_color.aF() == 0.0f)
|
||||
return;
|
||||
|
||||
if(screenCoords != m_textCachedScreenCoords || m_textMustRecache) {
|
||||
@@ -58,7 +85,7 @@ void UIWidget::drawText(const Rect& screenCoords)
|
||||
|
||||
m_textCoordsBuffer.clear();
|
||||
|
||||
m_font->calculateDrawTextCoords(m_textCoordsBuffer, m_text, screenCoords.translated(m_textOffset), m_textAlign);
|
||||
m_font->calculateDrawTextCoords(m_textCoordsBuffer, m_drawText, screenCoords.translated(m_textOffset), m_textAlign);
|
||||
}
|
||||
|
||||
g_painter.setColor(m_color);
|
||||
@@ -75,11 +102,6 @@ void UIWidget::onFontChange(const std::string& font)
|
||||
callLuaField("onFontChange", font);
|
||||
}
|
||||
|
||||
void UIWidget::wrapText()
|
||||
{
|
||||
setText(m_font->wrapText(m_text, getWidth() - m_textOffset.x));
|
||||
}
|
||||
|
||||
void UIWidget::setText(const std::string& text)
|
||||
{
|
||||
if(m_text == text)
|
||||
@@ -87,25 +109,14 @@ void UIWidget::setText(const std::string& text)
|
||||
|
||||
std::string oldText = m_text;
|
||||
m_text = text;
|
||||
|
||||
// update rect size
|
||||
if(!m_rect.isValid()) {
|
||||
Size textSize = m_font->calculateTextRectSize(m_text);
|
||||
Size newSize = getSize();
|
||||
if(newSize.width() <= 0)
|
||||
newSize.setWidth(textSize.width());
|
||||
if(newSize.height() <= 0)
|
||||
newSize.setHeight(textSize.height());
|
||||
setSize(newSize);
|
||||
}
|
||||
updateText();
|
||||
|
||||
onTextChange(text, oldText);
|
||||
m_textMustRecache = true;
|
||||
}
|
||||
|
||||
void UIWidget::setFont(const std::string& fontName)
|
||||
{
|
||||
m_font = g_fonts.getFont(fontName);
|
||||
updateText();
|
||||
onFontChange(fontName);
|
||||
m_textMustRecache = true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user