From c584426f24fbf2fb81d9af485a843c74dbe04a7f Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Wed, 16 Nov 2011 15:58:42 -0200 Subject: [PATCH] implement icon property for UIButton --- TODO | 3 ++- modules/topmenu/topmenu.otui | 34 +++++----------------------------- src/framework/ui/uibutton.cpp | 25 ++++++++++++++++--------- src/framework/ui/uibutton.h | 5 +---- 4 files changed, 24 insertions(+), 43 deletions(-) diff --git a/TODO b/TODO index 4adafe45..c3fc4d94 100644 --- a/TODO +++ b/TODO @@ -13,4 +13,5 @@ scrollbar make otui syntax more like css a real working border and background property in otui setIcon() for buttons -load state styles in order \ No newline at end of file +load state styles in order +grid layout \ No newline at end of file diff --git a/modules/topmenu/topmenu.otui b/modules/topmenu/topmenu.otui index 3e5422b9..72e58f47 100644 --- a/modules/topmenu/topmenu.otui +++ b/modules/topmenu/topmenu.otui @@ -12,13 +12,9 @@ TopPanel margin.top: 4 margin.left: 6 tooltip: Options + icon: /core_styles/icons/settings.png @onClick: Options.create() - UIWidget - size: 16 16 - image: /core_styles/icons/settings.png - anchors.centerIn: parent - phantom: true TopButton id: enterGameButton @@ -26,6 +22,7 @@ TopPanel anchors.left: prev.right margin.left: 6 tooltip: Enter game with a character + icon: /core_styles/icons/login.png @onClick: | if Game.isOnline() then CharacterList.show() @@ -33,32 +30,22 @@ TopPanel EnterGame.show() end - UIWidget - size: 16 16 - image: /core_styles/icons/login.png - anchors.centerIn: parent - phantom: true - TopButton id: motdButton anchors.top: prev.top anchors.left: prev.right margin.left: 6 tooltip: Message of the day + icon: /core_styles/icons/motd.png @onClick: - UIWidget - size: 16 16 - image: /core_styles/icons/motd.png - anchors.centerIn: parent - phantom: true - TopButton anchors.top: parent.top anchors.right: parent.right margin.top: 4 margin.right: 6 tooltip: Logout + icon: /core_styles/icons/logout.png @onClick: | if Game.isOnline() then Game.logout(false) @@ -66,26 +53,15 @@ TopPanel exit() end - UIWidget - size: 16 16 - image: /core_styles/icons/logout.png - anchors.centerIn: parent - phantom: true - TopButton anchors.top: parent.top anchors.right: prev.left margin.top: 4 margin.right: 6 tooltip: About OTClient + icon: /core_styles/icons/about.png @onClick: About.create() - UIWidget - size: 16 16 - image: /core_styles/icons/about.png - anchors.centerIn: parent - phantom: true - FrameCounter anchors.top: parent.top anchors.right: prev.left diff --git a/src/framework/ui/uibutton.cpp b/src/framework/ui/uibutton.cpp index f8ffe945..8a807f16 100644 --- a/src/framework/ui/uibutton.cpp +++ b/src/framework/ui/uibutton.cpp @@ -26,19 +26,26 @@ #include #include #include +#include +#include void UIButton::setup() { UIWidget::setup(); setFocusable(false); - - // by default, all callbacks call lua fields - m_onClick = [this]() { this->callLuaField("onClick"); }; } void UIButton::render() { UIWidget::render(); + + if(m_icon) { + Rect iconRect; + iconRect.setSize(m_icon->getSize()); + iconRect.moveCenter(m_rect.center()); + g_graphics.drawTexturedRect(iconRect, m_icon); + } + Rect textRect = m_rect; textRect.translate(m_textOffset); m_font->renderText(m_text, textRect, Fw::AlignCenter, m_foregroundColor); @@ -49,18 +56,18 @@ void UIButton::onStyleApply(const OTMLNodePtr& styleNode) UIWidget::onStyleApply(styleNode); for(OTMLNodePtr node : styleNode->children()) { - if(node->tag() == "text-offset") { + if(node->tag() == "text-offset") m_textOffset = node->value(); - } else if(node->tag() == "text") { + else if(node->tag() == "text") m_text = node->value(); - } + else if(node->tag() == "icon") + m_icon = g_textures.getTexture(node->value()); } } void UIButton::onMouseRelease(const Point& mousePos, Fw::MouseButton button) { - if(isPressed()) { - if(m_onClick && getRect().contains(mousePos)) - m_onClick(); + if(isPressed() && getRect().contains(mousePos)) { + callLuaField("onClick"); } } diff --git a/src/framework/ui/uibutton.h b/src/framework/ui/uibutton.h index e7a6d743..950f3ee8 100644 --- a/src/framework/ui/uibutton.h +++ b/src/framework/ui/uibutton.h @@ -31,10 +31,7 @@ public: virtual void setup(); virtual void render(); - void setOnClick(const SimpleCallback& onClick) { m_onClick = onClick; } void setText(const std::string& text) { m_text = text; } - - SimpleCallback getOnClick() const { return m_onClick; } std::string getText() const { return m_text; } UIButtonPtr asUIButton() { return std::static_pointer_cast(shared_from_this()); } @@ -43,8 +40,8 @@ protected: virtual void onStyleApply(const OTMLNodePtr& styleNode); virtual void onMouseRelease(const Point& mousePos, Fw::MouseButton button); - SimpleCallback m_onClick; Point m_textOffset; + TexturePtr m_icon; std::string m_text; };