From 3230095ceaadd775347655475167548f15c09708 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Mon, 9 Jan 2012 16:45:28 -0200 Subject: [PATCH] add ui padding --- modules/addon_terminal/commands.lua | 7 ++- modules/addon_terminal/terminal.lua | 37 +++++--------- modules/game_console/console.lua | 2 +- modules/game_skills/skills.otui | 2 +- modules/game_textmessage/textmessage.lua | 2 +- modules/game_viplist/viplist.lua | 8 +-- src/framework/luafunctions.cpp | 6 ++- src/framework/math/rect.h | 1 + src/framework/ui/uianchorlayout.cpp | 16 +++--- src/framework/ui/uicheckbox.cpp | 2 +- src/framework/ui/uilineedit.cpp | 2 +- src/framework/ui/uimanager.h | 3 ++ src/framework/ui/uiprogressbar.cpp | 2 +- src/framework/ui/uiverticallayout.cpp | 10 ++-- src/framework/ui/uiwidget.cpp | 64 +++++++++++++++++++++--- src/framework/ui/uiwidget.h | 19 +++++-- src/framework/ui/uiwindow.cpp | 2 +- 17 files changed, 126 insertions(+), 59 deletions(-) diff --git a/modules/addon_terminal/commands.lua b/modules/addon_terminal/commands.lua index e2c0c8be..d17238a7 100644 --- a/modules/addon_terminal/commands.lua +++ b/modules/addon_terminal/commands.lua @@ -2,4 +2,9 @@ function dumpWidgets() for i=1,rootWidget:getChildCount() do print(rootWidget:getChildByIndex(i):getId()) end -end \ No newline at end of file +end + +function drawDebugBoxes(enable) + if enable == nil then enable = true end + g_ui.setDebugBoxesDrawing(enable) +end diff --git a/modules/addon_terminal/terminal.lua b/modules/addon_terminal/terminal.lua index b182a8a4..43896b17 100644 --- a/modules/addon_terminal/terminal.lua +++ b/modules/addon_terminal/terminal.lua @@ -82,29 +82,11 @@ local function completeCommand() end end -local function onCommandLineKeyPress(widget, keyCode, keyText, keyboardModifiers) - if keyboardModifiers == KeyboardNoModifier then - -- execute current command - if keyCode == KeyReturn or keyCode == KeyEnter then - local currentCommand = commandLineEdit:getText() - Terminal.executeCommand(currentCommand) - commandLineEdit:clearText() - return true - -- navigate history up - elseif keyCode == KeyUp then - navigateCommand(1) - return true - -- navigate history down - elseif keyCode == KeyDown then - navigateCommand(-1) - return true - -- complete command - elseif keyCode == KeyTab then - completeCommand() - return true - end - end - return false +local function doCommand() + local currentCommand = commandLineEdit:getText() + Terminal.executeCommand(currentCommand) + commandLineEdit:clearText() + return true end local function onLog(level, message, time) @@ -128,7 +110,11 @@ function Terminal.init() Hotkeys.bind('Ctrl+T', Terminal.toggle) commandLineEdit = terminalWidget:getChildById('commandLineEdit') - connect(commandLineEdit, { onKeyPress = onCommandLineKeyPress }) + Hotkeys.bind('Up', function() navigateCommand(1) end, commandLineEdit) + Hotkeys.bind('Down', function() navigateCommand(-1) end, commandLineEdit) + Hotkeys.bind('Tab', completeCommand, commandLineEdit) + Hotkeys.bind('Enter', doCommand, commandLineEdit) + Hotkeys.bind('Return', doCommand, commandLineEdit) terminalBuffer = terminalWidget:getChildById('terminalBuffer') Logger.setOnLog(onLog) @@ -171,7 +157,7 @@ function Terminal.addLine(text, color) local label = createWidget('TerminalLabel', terminalBuffer) label:setId('terminalLabel' .. numLines) label:setText(text) - label:setForegroundColor(color) + label:setColor(color) -- delete old lines if needed if numLines > MaxLogLines then @@ -182,6 +168,7 @@ function Terminal.addLine(text, color) end function Terminal.executeCommand(command) + if command == nil or #command == 0 then return end -- detect and convert commands with simple syntax local realCommand if commandEnv[command] then diff --git a/modules/game_console/console.lua b/modules/game_console/console.lua index 1f8fba04..234f39c5 100644 --- a/modules/game_console/console.lua +++ b/modules/game_console/console.lua @@ -41,7 +41,7 @@ function Console.addText(text, color) local label = createWidget('ConsoleLabel', consoleBuffer) label:setText(text) - label:setForegroundColor(color) + label:setColor(color) end -- hooked events diff --git a/modules/game_skills/skills.otui b/modules/game_skills/skills.otui index 39793cfd..246f1f0f 100644 --- a/modules/game_skills/skills.otui +++ b/modules/game_skills/skills.otui @@ -17,10 +17,10 @@ SkillValueLabel < GameLabel id: value font: verdana-11px-monochrome text-align: topright - width: 64 anchors.right: parent.right anchors.top: parent.top anchors.bottom: parent.bottom + anchors.left: prev.left SkillPercentPanel < UIProgressBar id: percent diff --git a/modules/game_textmessage/textmessage.lua b/modules/game_textmessage/textmessage.lua index 21319c3d..b58baf5b 100644 --- a/modules/game_textmessage/textmessage.lua +++ b/modules/game_textmessage/textmessage.lua @@ -46,7 +46,7 @@ local function displayMessage(msgtype, msg, time) label:setVisible(true) label:setText(msg) label:setStyle(style) - label:setForegroundColor(msgtype.color) + label:setColor(msgtype.color) if not time then time = math.max(#msg * 75, 3000) diff --git a/modules/game_viplist/viplist.lua b/modules/game_viplist/viplist.lua index 4009ff75..fc0b5c59 100644 --- a/modules/game_viplist/viplist.lua +++ b/modules/game_viplist/viplist.lua @@ -37,9 +37,9 @@ function Game.onAddVip(id, name, online) label:setText(name) if online then - label:setForegroundColor('#00ff00') + label:setColor('#00ff00') else - label:setForegroundColor('#ff0000') + label:setColor('#ff0000') end label.vipOnline = online @@ -50,9 +50,9 @@ function Game.onVipStateChange(id, online) local label = vipList:getChildById('vip' .. id) if online then - label:setForegroundColor('#00ff00') + label:setColor('#00ff00') else - label:setForegroundColor('#ff0000') + label:setColor('#ff0000') end label.vipOnline = online diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index 577e6f91..73c9af97 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -67,7 +67,7 @@ void Application::registerLuaFunctions() g_lua.bindClassMemberFunction("setIcon", &UIWidget::setIcon); g_lua.bindClassMemberFunction("setOpacity", &UIWidget::setOpacity); g_lua.bindClassMemberFunction("setBackgroundColor", &UIWidget::setBackgroundColor); - g_lua.bindClassMemberFunction("setForegroundColor", &UIWidget::setForegroundColor); + g_lua.bindClassMemberFunction("setColor", &UIWidget::setColor); g_lua.bindClassMemberFunction("setMarginTop", &UIWidget::setMarginTop); g_lua.bindClassMemberFunction("setMarginRight", &UIWidget::setMarginRight); g_lua.bindClassMemberFunction("setMarginBottom", &UIWidget::setMarginBottom); @@ -128,7 +128,7 @@ void Application::registerLuaFunctions() g_lua.bindClassMemberFunction("getY", &UIWidget::getY); g_lua.bindClassMemberFunction("getWidth", &UIWidget::getWidth); g_lua.bindClassMemberFunction("getHeight", &UIWidget::getHeight); - g_lua.bindClassMemberFunction("getForegroundColor", &UIWidget::getForegroundColor); + g_lua.bindClassMemberFunction("getColor", &UIWidget::getColor); g_lua.bindClassMemberFunction("getBackgroundColor", &UIWidget::getBackgroundColor); g_lua.bindClassMemberFunction("getOpacity", &UIWidget::getOpacity); g_lua.bindClassMemberFunction("getMarginTop", &UIWidget::getMarginTop); @@ -297,6 +297,8 @@ void Application::registerLuaFunctions() g_lua.bindClassStaticFunction("g_ui", "getStyleClass", std::bind(&UIManager::getStyleClass, &g_ui, _1)); g_lua.bindClassStaticFunction("g_ui", "loadUI", std::bind(&UIManager::loadUI, &g_ui, _1, _2)); g_lua.bindClassStaticFunction("g_ui", "getRootWidget", std::bind(&UIManager::getRootWidget, &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)); // FontManager g_lua.registerStaticClass("g_fonts"); diff --git a/src/framework/math/rect.h b/src/framework/math/rect.h index ac3debe7..ecbc9320 100644 --- a/src/framework/math/rect.h +++ b/src/framework/math/rect.h @@ -87,6 +87,7 @@ public: void addTop(T add) { y1 -= add; } void addRight(T add) { x2 += add; } void addBottom(T add) { y2 += add; } + void add(T top, T right, T bottom, T left) { x1 -= left; y1 -= top; x2 += right; y2 += bottom; } void translate(T x, T y) { x1 += x; y1 += y; x2 += x; y2 += y; } void translate(const TPoint &p) { x1 += p.x; y1 += p.y; x2 += p.x; y2 += p.y; } diff --git a/src/framework/ui/uianchorlayout.cpp b/src/framework/ui/uianchorlayout.cpp index 0b834342..102d164a 100644 --- a/src/framework/ui/uianchorlayout.cpp +++ b/src/framework/ui/uianchorlayout.cpp @@ -122,25 +122,29 @@ void UIAnchorLayout::updateWidget(const UIWidgetPtr& widget, UIAnchorGroup& anch } // determine hooked widget edge point + Rect hookedWidgetRect = hookedWidget->getRect(); + if(hookedWidget == parentWidget) + hookedWidgetRect = parentWidget->getChildrenRect(); + int point = 0; switch(anchor.getHookedEdge()) { case Fw::AnchorLeft: - point = hookedWidget->getRect().left(); + point = hookedWidgetRect.left(); break; case Fw::AnchorRight: - point = hookedWidget->getRect().right(); + point = hookedWidgetRect.right(); break; case Fw::AnchorTop: - point = hookedWidget->getRect().top(); + point = hookedWidgetRect.top(); break; case Fw::AnchorBottom: - point = hookedWidget->getRect().bottom(); + point = hookedWidgetRect.bottom(); break; case Fw::AnchorHorizontalCenter: - point = hookedWidget->getRect().horizontalCenter(); + point = hookedWidgetRect.horizontalCenter(); break; case Fw::AnchorVerticalCenter: - point = hookedWidget->getRect().verticalCenter(); + point = hookedWidgetRect.verticalCenter(); break; default: // must never happens diff --git a/src/framework/ui/uicheckbox.cpp b/src/framework/ui/uicheckbox.cpp index be090755..ab480aae 100644 --- a/src/framework/ui/uicheckbox.cpp +++ b/src/framework/ui/uicheckbox.cpp @@ -48,7 +48,7 @@ void UICheckBox::render() if(m_text.length()) { Rect textRect(m_rect); textRect.setTopLeft(textRect.topLeft() + m_textOffset); - m_font->renderText(m_text, textRect, m_textAlign, m_foregroundColor); + m_font->renderText(m_text, textRect, m_textAlign, m_color); } } diff --git a/src/framework/ui/uilineedit.cpp b/src/framework/ui/uilineedit.cpp index 7720b48b..a0df09b5 100644 --- a/src/framework/ui/uilineedit.cpp +++ b/src/framework/ui/uilineedit.cpp @@ -50,7 +50,7 @@ void UILineEdit::renderSelf() int textLength = m_text.length(); const TexturePtr& texture = m_font->getTexture(); - g_painter.setColor(m_foregroundColor); + g_painter.setColor(m_color); for(int i=0;i m_drawDebugBoxes; std::map m_styles; }; diff --git a/src/framework/ui/uiprogressbar.cpp b/src/framework/ui/uiprogressbar.cpp index 185837dd..6373a233 100644 --- a/src/framework/ui/uiprogressbar.cpp +++ b/src/framework/ui/uiprogressbar.cpp @@ -35,7 +35,7 @@ void UIProgressBar::render() { UIWidget::render(); - g_painter.setColor(m_foregroundColor); + g_painter.setColor(m_color); g_painter.drawBoundingRect(m_rect, 1); Rect fillRect = m_rect.expanded(-1); diff --git a/src/framework/ui/uiverticallayout.cpp b/src/framework/ui/uiverticallayout.cpp index b9fd0008..548c0b74 100644 --- a/src/framework/ui/uiverticallayout.cpp +++ b/src/framework/ui/uiverticallayout.cpp @@ -82,7 +82,8 @@ void UIVerticalLayout::internalUpdate() if(m_alignBottom) std::reverse(widgets.begin(), widgets.end()); - Point pos = (m_alignBottom) ? parentWidget->getRect().bottomLeft() : parentWidget->getPos(); + Rect childrenRect = parentWidget->getChildrenRect(); + Point pos = (m_alignBottom) ? childrenRect.bottomLeft() : childrenRect.topLeft(); int prefferedHeight = 0; int gap; @@ -98,12 +99,12 @@ void UIVerticalLayout::internalUpdate() if(widget->isSizeFixed()) { // center it - pos.x = parentWidget->getX() + (parentWidget->getWidth() - (widget->getMarginLeft() + widget->getWidth() + widget->getMarginRight()))/2; + pos.x = childrenRect.left() + (childrenRect.width() - (widget->getMarginLeft() + widget->getWidth() + widget->getMarginRight()))/2; pos.x = std::max(pos.x, parentWidget->getX()); } else { // expand width - size.setWidth(parentWidget->getWidth() - (widget->getMarginLeft() + widget->getMarginRight())); - pos.x = parentWidget->getX() + (parentWidget->getWidth() - size.width())/2; + size.setWidth(childrenRect.width() - (widget->getMarginLeft() + widget->getMarginRight())); + pos.x = childrenRect.left() + (childrenRect.width() - size.width())/2; } widget->setRect(Rect(pos, size)); @@ -115,6 +116,7 @@ void UIVerticalLayout::internalUpdate() } prefferedHeight -= m_spacing; + prefferedHeight += parentWidget->getPaddingTop() + parentWidget->getPaddingBottom(); if(m_fitParent && prefferedHeight != parentWidget->getHeight()) { // must set the preffered width later diff --git a/src/framework/ui/uiwidget.cpp b/src/framework/ui/uiwidget.cpp index 456ec278..5d85b44e 100644 --- a/src/framework/ui/uiwidget.cpp +++ b/src/framework/ui/uiwidget.cpp @@ -42,9 +42,10 @@ UIWidget::UIWidget() m_font = g_fonts.getDefaultFont(); m_opacity = 255; m_marginTop = m_marginRight = m_marginBottom = m_marginLeft = 0; + m_paddingTop = m_paddingRight = m_paddingBottom = m_paddingLeft = 0; //m_backgroundColor = Fw::alpha; m_backgroundColor = Fw::white; - m_foregroundColor = Fw::white; + m_color = Fw::white; m_textAlign = Fw::AlignCenter; // generate an unique id, this is need because anchored layouts find widgets by id @@ -113,8 +114,10 @@ void UIWidget::renderChildren() child->render(); // debug draw box - //g_painter.setColor(Fw::green); - //g_painter.drawBoundingRect(child->getRect()); + if(g_ui.isDrawingDebugBoxes()) { + g_painter.setColor(Fw::green); + g_painter.drawBoundingRect(child->getRect()); + } //g_fonts.getDefaultFont()->renderText(child->getId(), child->getPos() + Point(2, 0), Fw::red); g_painter.setOpacity(oldOpacity); @@ -164,11 +167,11 @@ void UIWidget::drawIcon(const Rect& screenCoords) void UIWidget::drawText(const Rect& screenCoords) { - g_painter.setColor(m_foregroundColor); - if(m_text.length() > 0 && m_foregroundColor.a() > 0) { + g_painter.setColor(m_color); + if(m_text.length() > 0 && m_color.a() > 0) { Rect textRect = screenCoords; textRect.translate(m_textOffset); - m_font->renderText(m_text, textRect, m_textAlign, m_foregroundColor); + m_font->renderText(m_text, textRect, m_textAlign, m_color); } } @@ -403,6 +406,13 @@ bool UIWidget::hasChild(const UIWidgetPtr& child) return false; } +Rect UIWidget::getChildrenRect() +{ + Rect rect = m_rect; + rect.add(-m_paddingTop, -m_paddingRight, -m_paddingBottom, -m_paddingLeft); + return rect; +} + UIWidgetPtr UIWidget::getRootParent() { if(UIWidgetPtr parent = getParent()) @@ -1031,7 +1041,7 @@ void UIWidget::onStyleApply(const std::string& styleName, const OTMLNodePtr& sty else if(node->tag() == "font") setFont(node->value()); else if(node->tag() == "color") - setForegroundColor(node->value()); + setColor(node->value()); else if(node->tag() == "background-color") setBackgroundColor(node->value()); else if(node->tag() == "opacity") @@ -1102,6 +1112,46 @@ void UIWidget::onStyleApply(const std::string& styleName, const OTMLNodePtr& sty setMarginLeft(margin); } } + else if(node->tag() == "padding-top") + setPaddingTop(node->value()); + else if(node->tag() == "padding-right") + setPaddingRight(node->value()); + else if(node->tag() == "padding-bottom") + setPaddingBottom(node->value()); + else if(node->tag() == "padding-left") + setPaddingLeft(node->value()); + else if(node->tag() == "padding") { + std::string paddingDesc = node->value(); + std::vector split; + boost::split(split, paddingDesc, boost::is_any_of(std::string(" "))); + if(split.size() == 4) { + setPaddingTop(Fw::safeCast(split[0])); + setPaddingRight(Fw::safeCast(split[1])); + setPaddingBottom(Fw::safeCast(split[2])); + setPaddingLeft(Fw::safeCast(split[3])); + } else if(split.size() == 3) { + int paddingTop = Fw::safeCast(split[0]); + int paddingHorizontal = Fw::safeCast(split[1]); + int paddingBottom = Fw::safeCast(split[2]); + setPaddingTop(paddingTop); + setPaddingRight(paddingHorizontal); + setPaddingBottom(paddingBottom); + setPaddingLeft(paddingHorizontal); + } else if(split.size() == 2) { + int paddingVertical = Fw::safeCast(split[0]); + int paddingHorizontal = Fw::safeCast(split[1]); + setPaddingTop(paddingVertical); + setPaddingRight(paddingHorizontal); + setPaddingBottom(paddingVertical); + setPaddingLeft(paddingHorizontal); + } else if(split.size() == 1) { + int padding = Fw::safeCast(split[0]); + setPaddingTop(padding); + setPaddingRight(padding); + setPaddingBottom(padding); + setPaddingLeft(padding); + } + } // layouts else if(node->tag() == "layout") { std::string layoutType; diff --git a/src/framework/ui/uiwidget.h b/src/framework/ui/uiwidget.h index b6abbc17..5dff0960 100644 --- a/src/framework/ui/uiwidget.h +++ b/src/framework/ui/uiwidget.h @@ -72,11 +72,15 @@ public: void setIcon(const std::string& iconFile); void setOpacity(int opacity) { m_opacity = opacity; } void setBackgroundColor(const Color& color) { m_backgroundColor = color; } - void setForegroundColor(const Color& color) { m_foregroundColor = color; } + void setColor(const Color& color) { m_color = color; } void setMarginTop(int margin) { m_marginTop = margin; updateParentLayout(); } void setMarginRight(int margin) { m_marginRight = margin; updateParentLayout(); } void setMarginBottom(int margin) { m_marginBottom = margin; updateParentLayout(); } void setMarginLeft(int margin) { m_marginLeft = margin; updateParentLayout(); } + void setPaddingTop(int padding) { m_paddingTop = padding; updateLayout(); } + void setPaddingRight(int padding) { m_paddingRight = padding; updateLayout(); } + void setPaddingBottom(int padding) { m_paddingBottom = padding; updateLayout(); } + void setPaddingLeft(int padding) { m_paddingLeft = padding; updateLayout(); } void setText(const std::string& text); void setTextAlign(Fw::AlignmentFlag align) { m_textAlign = align; } void setTextOffset(const Point& offset) { m_textOffset = offset; } @@ -133,17 +137,22 @@ public: Point getPos() { return m_rect.topLeft(); } Size getSize() { return m_rect.size(); } Rect getRect() { return m_rect; } + Rect getChildrenRect(); int getX() { return m_rect.x(); } int getY() { return m_rect.y(); } int getWidth() { return m_rect.width(); } int getHeight() { return m_rect.height(); } - Color getForegroundColor() { return m_foregroundColor; } + Color getColor() { return m_color; } Color getBackgroundColor() { return m_backgroundColor; } int getOpacity() { return m_opacity; } int getMarginTop() { return m_marginTop; } int getMarginRight() { return m_marginRight; } int getMarginBottom() { return m_marginBottom; } int getMarginLeft() { return m_marginLeft; } + int getPaddingTop() { return m_paddingTop; } + int getPaddingRight() { return m_paddingRight; } + int getPaddingBottom() { return m_paddingBottom; } + int getPaddingLeft() { return m_paddingLeft; } std::string getText() { return m_text; } Fw::AlignmentFlag getTextAlign() { return m_textAlign; } Point getTextOffset() { return m_textOffset; } @@ -244,13 +253,17 @@ protected: TexturePtr m_icon; FontPtr m_font; Color m_backgroundColor; - Color m_foregroundColor; + Color m_color; int m_states; int m_opacity; int m_marginTop; int m_marginRight; int m_marginBottom; int m_marginLeft; + int m_paddingTop; + int m_paddingRight; + int m_paddingBottom; + int m_paddingLeft; std::string m_text; Point m_textOffset; Fw::AlignmentFlag m_textAlign; diff --git a/src/framework/ui/uiwindow.cpp b/src/framework/ui/uiwindow.cpp index 93eb5a5b..7e68fc0c 100644 --- a/src/framework/ui/uiwindow.cpp +++ b/src/framework/ui/uiwindow.cpp @@ -53,7 +53,7 @@ void UIWindow::render() headTextRect.addLeft(-m_headTextOffset.x); headTextRect.addRight(-m_headTextOffset.x); } - m_font->renderText(m_title, headTextRect, m_titleAlign, m_foregroundColor); + m_font->renderText(m_title, headTextRect, m_titleAlign, m_color); } void UIWindow::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode)