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

View File

@@ -26,19 +26,26 @@
#include <framework/otml/otmlnode.h>
#include <framework/luascript/luainterface.h>
#include <framework/graphics/graphics.h>
#include <framework/graphics/texture.h>
#include <framework/graphics/texturemanager.h>
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<Point>();
} 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");
}
}

View File

@@ -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<UIButton>(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;
};