mirror of
https://github.com/edubart/otclient.git
synced 2025-10-14 19:44:54 +02:00
implement icon property for UIButton
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user