implement icon property for UIButton

This commit is contained in:
Eduardo Bart 2011-11-16 15:58:42 -02:00
parent 71d9074fff
commit c584426f24
4 changed files with 24 additions and 43 deletions

3
TODO
View File

@ -13,4 +13,5 @@ scrollbar
make otui syntax more like css make otui syntax more like css
a real working border and background property in otui a real working border and background property in otui
setIcon() for buttons setIcon() for buttons
load state styles in order load state styles in order
grid layout

View File

@ -12,13 +12,9 @@ TopPanel
margin.top: 4 margin.top: 4
margin.left: 6 margin.left: 6
tooltip: Options tooltip: Options
icon: /core_styles/icons/settings.png
@onClick: Options.create() @onClick: Options.create()
UIWidget
size: 16 16
image: /core_styles/icons/settings.png
anchors.centerIn: parent
phantom: true
TopButton TopButton
id: enterGameButton id: enterGameButton
@ -26,6 +22,7 @@ TopPanel
anchors.left: prev.right anchors.left: prev.right
margin.left: 6 margin.left: 6
tooltip: Enter game with a character tooltip: Enter game with a character
icon: /core_styles/icons/login.png
@onClick: | @onClick: |
if Game.isOnline() then if Game.isOnline() then
CharacterList.show() CharacterList.show()
@ -33,32 +30,22 @@ TopPanel
EnterGame.show() EnterGame.show()
end end
UIWidget
size: 16 16
image: /core_styles/icons/login.png
anchors.centerIn: parent
phantom: true
TopButton TopButton
id: motdButton id: motdButton
anchors.top: prev.top anchors.top: prev.top
anchors.left: prev.right anchors.left: prev.right
margin.left: 6 margin.left: 6
tooltip: Message of the day tooltip: Message of the day
icon: /core_styles/icons/motd.png
@onClick: @onClick:
UIWidget
size: 16 16
image: /core_styles/icons/motd.png
anchors.centerIn: parent
phantom: true
TopButton TopButton
anchors.top: parent.top anchors.top: parent.top
anchors.right: parent.right anchors.right: parent.right
margin.top: 4 margin.top: 4
margin.right: 6 margin.right: 6
tooltip: Logout tooltip: Logout
icon: /core_styles/icons/logout.png
@onClick: | @onClick: |
if Game.isOnline() then if Game.isOnline() then
Game.logout(false) Game.logout(false)
@ -66,26 +53,15 @@ TopPanel
exit() exit()
end end
UIWidget
size: 16 16
image: /core_styles/icons/logout.png
anchors.centerIn: parent
phantom: true
TopButton TopButton
anchors.top: parent.top anchors.top: parent.top
anchors.right: prev.left anchors.right: prev.left
margin.top: 4 margin.top: 4
margin.right: 6 margin.right: 6
tooltip: About OTClient tooltip: About OTClient
icon: /core_styles/icons/about.png
@onClick: About.create() @onClick: About.create()
UIWidget
size: 16 16
image: /core_styles/icons/about.png
anchors.centerIn: parent
phantom: true
FrameCounter FrameCounter
anchors.top: parent.top anchors.top: parent.top
anchors.right: prev.left anchors.right: prev.left

View File

@ -26,19 +26,26 @@
#include <framework/otml/otmlnode.h> #include <framework/otml/otmlnode.h>
#include <framework/luascript/luainterface.h> #include <framework/luascript/luainterface.h>
#include <framework/graphics/graphics.h> #include <framework/graphics/graphics.h>
#include <framework/graphics/texture.h>
#include <framework/graphics/texturemanager.h>
void UIButton::setup() void UIButton::setup()
{ {
UIWidget::setup(); UIWidget::setup();
setFocusable(false); setFocusable(false);
// by default, all callbacks call lua fields
m_onClick = [this]() { this->callLuaField("onClick"); };
} }
void UIButton::render() void UIButton::render()
{ {
UIWidget::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; Rect textRect = m_rect;
textRect.translate(m_textOffset); textRect.translate(m_textOffset);
m_font->renderText(m_text, textRect, Fw::AlignCenter, m_foregroundColor); m_font->renderText(m_text, textRect, Fw::AlignCenter, m_foregroundColor);
@ -49,18 +56,18 @@ void UIButton::onStyleApply(const OTMLNodePtr& styleNode)
UIWidget::onStyleApply(styleNode); UIWidget::onStyleApply(styleNode);
for(OTMLNodePtr node : styleNode->children()) { for(OTMLNodePtr node : styleNode->children()) {
if(node->tag() == "text-offset") { if(node->tag() == "text-offset")
m_textOffset = node->value<Point>(); m_textOffset = node->value<Point>();
} else if(node->tag() == "text") { else if(node->tag() == "text")
m_text = node->value(); 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) void UIButton::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
{ {
if(isPressed()) { if(isPressed() && getRect().contains(mousePos)) {
if(m_onClick && getRect().contains(mousePos)) callLuaField("onClick");
m_onClick();
} }
} }

View File

@ -31,10 +31,7 @@ public:
virtual void setup(); virtual void setup();
virtual void render(); virtual void render();
void setOnClick(const SimpleCallback& onClick) { m_onClick = onClick; }
void setText(const std::string& text) { m_text = text; } void setText(const std::string& text) { m_text = text; }
SimpleCallback getOnClick() const { return m_onClick; }
std::string getText() const { return m_text; } std::string getText() const { return m_text; }
UIButtonPtr asUIButton() { return std::static_pointer_cast<UIButton>(shared_from_this()); } UIButtonPtr asUIButton() { return std::static_pointer_cast<UIButton>(shared_from_this()); }
@ -43,8 +40,8 @@ protected:
virtual void onStyleApply(const OTMLNodePtr& styleNode); virtual void onStyleApply(const OTMLNodePtr& styleNode);
virtual void onMouseRelease(const Point& mousePos, Fw::MouseButton button); virtual void onMouseRelease(const Point& mousePos, Fw::MouseButton button);
SimpleCallback m_onClick;
Point m_textOffset; Point m_textOffset;
TexturePtr m_icon;
std::string m_text; std::string m_text;
}; };