chat line wrapping

* rework UIWidget text wrapping
* implement auto wrap
* fixes in console
This commit is contained in:
Eduardo Bart 2012-03-25 19:14:00 -03:00
parent ccf55132a1
commit 532e8e3e39
11 changed files with 65 additions and 32 deletions

View File

@ -105,7 +105,6 @@ function ModuleManager.updateModuleInfo(moduleName)
moduleManagerWindow:recursiveGetChildById('moduleName'):setText(name) moduleManagerWindow:recursiveGetChildById('moduleName'):setText(name)
moduleManagerWindow:recursiveGetChildById('moduleDescription'):setText(description) moduleManagerWindow:recursiveGetChildById('moduleDescription'):setText(description)
moduleManagerWindow:recursiveGetChildById('moduleDescription'):wrapText()
moduleManagerWindow:recursiveGetChildById('moduleAuthor'):setText(author) moduleManagerWindow:recursiveGetChildById('moduleAuthor'):setText(author)
moduleManagerWindow:recursiveGetChildById('moduleWebsite'):setText(website) moduleManagerWindow:recursiveGetChildById('moduleWebsite'):setText(website)
moduleManagerWindow:recursiveGetChildById('moduleVersion'):setText(version) moduleManagerWindow:recursiveGetChildById('moduleVersion'):setText(version)

View File

@ -85,6 +85,7 @@ MainWindow
ModuleValueLabel ModuleValueLabel
id: moduleDescription id: moduleDescription
height: 100 height: 100
text-wrap: true
//ModuleInfoLabel //ModuleInfoLabel
// text: Autoload // text: Autoload

View File

@ -16,6 +16,10 @@ function Mouse.restoreCursor()
g_window.restoreMouseCursor() g_window.restoreMouseCursor()
end end
function Mouse.isPressed()
return g_ui.getPressedWidget() == nil
end
function Mouse.bindAutoPress(widget, callback) function Mouse.bindAutoPress(widget, callback)
connect(widget, { onMousePress = function(widget, mousePos, mouseButton) connect(widget, { onMousePress = function(widget, mousePos, mouseButton)
callback() callback()

View File

@ -21,7 +21,7 @@ end
local function onWidgetHoverChange(widget, hovered) local function onWidgetHoverChange(widget, hovered)
if hovered then if hovered then
if widget.tooltip then if widget.tooltip and not Mouse.isPressed() then
ToolTip.display(widget.tooltip) ToolTip.display(widget.tooltip)
currentHoveredWidget = widget currentHoveredWidget = widget
end end

View File

@ -229,8 +229,14 @@ function Console.clear()
channels = {} channels = {}
channels[0] = 'Default' channels[0] = 'Default'
consoleTabBar:getTab('Default').tabPanel:destroyChildren() consoleTabBar:getTab('Default').tabPanel:getChildById('consoleBuffer'):destroyChildren()
consoleTabBar:getTab('Server Log').tabPanel:destroyChildren() consoleTabBar:getTab('Server Log').tabPanel:getChildById('consoleBuffer'):destroyChildren()
local npcTab = consoleTabBar:getTab('NPCs')
if npcTab then
consoleTabBar:removeTab(npcTab)
end
consoleLineEdit:clearText() consoleLineEdit:clearText()
if channelsWindow then if channelsWindow then

View File

@ -2,6 +2,9 @@ ConsoleLabel < UILabel
font: verdana-11px-antialised font: verdana-11px-antialised
height: 14 height: 14
color: yellow color: yellow
margin-left: 2
text-wrap: true
text-auto-resize: true
ConsoleTabBar < TabBar ConsoleTabBar < TabBar
ConsoleTabBarPanel < TabBarPanel ConsoleTabBarPanel < TabBarPanel

View File

@ -37,7 +37,6 @@ local function displayMessage(msgtype, msg, time)
if msgtype.wrap then if msgtype.wrap then
label:setWidth(label:getParent():getWidth()) label:setWidth(label:getParent():getWidth())
label:wrapText()
label:setHeight(label:getTextSize().height) label:setHeight(label:getTextSize().height)
else else
label:resizeToText() label:resizeToText()

View File

@ -294,14 +294,15 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<UIWidget>("getImageBorderLeft", &UIWidget::getImageBorderLeft); g_lua.bindClassMemberFunction<UIWidget>("getImageBorderLeft", &UIWidget::getImageBorderLeft);
g_lua.bindClassMemberFunction<UIWidget>("resizeToText", &UIWidget::resizeToText); g_lua.bindClassMemberFunction<UIWidget>("resizeToText", &UIWidget::resizeToText);
g_lua.bindClassMemberFunction<UIWidget>("clearText", &UIWidget::clearText); 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>("setText", &UIWidget::setText);
g_lua.bindClassMemberFunction<UIWidget>("setTextAlign", &UIWidget::setTextAlign); g_lua.bindClassMemberFunction<UIWidget>("setTextAlign", &UIWidget::setTextAlign);
g_lua.bindClassMemberFunction<UIWidget>("setTextOffset", &UIWidget::setTextOffset); 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>("setFont", &UIWidget::setFont);
g_lua.bindClassMemberFunction<UIWidget>("getText", &UIWidget::getText); g_lua.bindClassMemberFunction<UIWidget>("getText", &UIWidget::getText);
g_lua.bindClassMemberFunction<UIWidget>("getTextAlign", &UIWidget::getTextAlign); g_lua.bindClassMemberFunction<UIWidget>("getTextAlign", &UIWidget::getTextAlign);
g_lua.bindClassMemberFunction<UIWidget>("getTextOffset", &UIWidget::getTextOffset); 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>("getFont", &UIWidget::getFont);
g_lua.bindClassMemberFunction<UIWidget>("getTextSize", &UIWidget::getTextSize); 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", "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", "getRootWidget", std::bind(&UIManager::getRootWidget, &g_ui));
g_lua.bindClassStaticFunction("g_ui", "getDraggingWidget", std::bind(&UIManager::getDraggingWidget, &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", "setDebugBoxesDrawing", std::bind(&UIManager::setDebugBoxesDrawing, &g_ui, _1));
g_lua.bindClassStaticFunction("g_ui", "isDrawingDebugBoxes", std::bind(&UIManager::setDebugBoxesDrawing, &g_ui, _1)); g_lua.bindClassStaticFunction("g_ui", "isDrawingDebugBoxes", std::bind(&UIManager::setDebugBoxesDrawing, &g_ui, _1));

View File

@ -1251,13 +1251,14 @@ void UIWidget::onStyleApply(const std::string& styleName, const OTMLNodePtr& sty
void UIWidget::onGeometryChange(const Rect& oldRect, const Rect& newRect) void UIWidget::onGeometryChange(const Rect& oldRect, const Rect& newRect)
{ {
callLuaField("onGeometryChange", oldRect, newRect); callLuaField("onGeometryChange", oldRect, newRect);
if(m_textWrap && oldRect.size() != newRect.size())
updateText();
} }
void UIWidget::onLayoutUpdate() void UIWidget::onLayoutUpdate()
{ {
callLuaField("onLayoutUpdate"); callLuaField("onLayoutUpdate");
if(UIWidgetPtr parent = getParent())
parent->onLayoutUpdate();
} }
void UIWidget::onFocusChange(bool focused, Fw::FocusReason reason) void UIWidget::onFocusChange(bool focused, Fw::FocusReason reason)

View File

@ -436,6 +436,7 @@ public:
// text related // text related
private: private:
void initText(); void initText();
void updateText();
void parseTextStyle(const OTMLNodePtr& styleNode); void parseTextStyle(const OTMLNodePtr& styleNode);
Boolean<true> m_textMustRecache; Boolean<true> m_textMustRecache;
@ -449,25 +450,31 @@ protected:
virtual void onFontChange(const std::string& font); virtual void onFontChange(const std::string& font);
std::string m_text; std::string m_text;
std::string m_drawText;
Fw::AlignmentFlag m_textAlign; Fw::AlignmentFlag m_textAlign;
Point m_textOffset; Point m_textOffset;
Boolean<false> m_textWrap;
Boolean<false> m_textAutoResize;
FontPtr m_font; FontPtr m_font;
public: public:
void resizeToText() { setSize(getTextSize()); } void resizeToText() { setSize(getTextSize()); }
void clearText() { setText(""); } void clearText() { setText(""); }
void wrapText();
void setText(const std::string& text); void setText(const std::string& text);
void setTextAlign(Fw::AlignmentFlag align) { m_textAlign = align; m_textMustRecache = true; } void setTextAlign(Fw::AlignmentFlag align) { m_textAlign = align; updateText(); }
void setTextOffset(const Point& offset) { m_textOffset = offset; m_textMustRecache = true; } 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); void setFont(const std::string& fontName);
std::string getText() { return m_text; } std::string getText() { return m_text; }
std::string getDrawText() { return m_drawText; }
Fw::AlignmentFlag getTextAlign() { return m_textAlign; } Fw::AlignmentFlag getTextAlign() { return m_textAlign; }
Point getTextOffset() { return m_textOffset; } Point getTextOffset() { return m_textOffset; }
bool getTextWrap() { return m_textWrap; }
std::string getFont() { return m_font->getName(); } std::string getFont() { return m_font->getName(); }
Size getTextSize() { return m_font->calculateTextRectSize(m_text); } Size getTextSize() { return m_font->calculateTextRectSize(m_drawText); }
}; };
#endif #endif

View File

@ -33,6 +33,29 @@ void UIWidget::initText()
m_textCoordsBuffer.enableHardwareCaching(); 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) void UIWidget::parseTextStyle(const OTMLNodePtr& styleNode)
{ {
for(const OTMLNodePtr& node : styleNode->children()) { for(const OTMLNodePtr& node : styleNode->children()) {
@ -42,6 +65,10 @@ void UIWidget::parseTextStyle(const OTMLNodePtr& styleNode)
setTextAlign(Fw::translateAlignment(node->value())); setTextAlign(Fw::translateAlignment(node->value()));
else if(node->tag() == "text-offset") else if(node->tag() == "text-offset")
setTextOffset(node->value<Point>()); 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") else if(node->tag() == "font")
setFont(node->value()); setFont(node->value());
} }
@ -49,7 +76,7 @@ void UIWidget::parseTextStyle(const OTMLNodePtr& styleNode)
void UIWidget::drawText(const Rect& screenCoords) 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; return;
if(screenCoords != m_textCachedScreenCoords || m_textMustRecache) { if(screenCoords != m_textCachedScreenCoords || m_textMustRecache) {
@ -58,7 +85,7 @@ void UIWidget::drawText(const Rect& screenCoords)
m_textCoordsBuffer.clear(); 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); g_painter.setColor(m_color);
@ -75,11 +102,6 @@ void UIWidget::onFontChange(const std::string& font)
callLuaField("onFontChange", font); callLuaField("onFontChange", font);
} }
void UIWidget::wrapText()
{
setText(m_font->wrapText(m_text, getWidth() - m_textOffset.x));
}
void UIWidget::setText(const std::string& text) void UIWidget::setText(const std::string& text)
{ {
if(m_text == text) if(m_text == text)
@ -87,25 +109,14 @@ void UIWidget::setText(const std::string& text)
std::string oldText = m_text; std::string oldText = m_text;
m_text = text; m_text = text;
updateText();
// 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);
}
onTextChange(text, oldText); onTextChange(text, oldText);
m_textMustRecache = true;
} }
void UIWidget::setFont(const std::string& fontName) void UIWidget::setFont(const std::string& fontName)
{ {
m_font = g_fonts.getFont(fontName); m_font = g_fonts.getFont(fontName);
updateText();
onFontChange(fontName); onFontChange(fontName);
m_textMustRecache = true;
} }