tibia-client/src/framework/ui/uibutton.cpp

50 lines
1.3 KiB
C++
Raw Normal View History

2011-08-14 04:09:11 +02:00
#include "uibutton.h"
2011-08-15 16:06:15 +02:00
#include <framework/graphics/borderimage.h>
#include <framework/graphics/font.h>
#include <framework/otml/otmlnode.h>
#include <framework/luascript/luainterface.h>
2011-08-20 22:30:41 +02:00
#include <framework/graphics/graphics.h>
2011-04-08 07:10:00 +02:00
void UIButton::setup()
2011-04-08 11:50:26 +02:00
{
UIWidget::setup();
setFocusable(false);
2011-08-14 04:09:11 +02:00
// by default, all callbacks call lua fields
m_onClick = [this]() { this->callLuaField("onClick"); };
}
void UIButton::render()
{
2011-08-20 22:30:41 +02:00
UIWidget::render();
Rect textRect = m_rect;
textRect.translate(m_textTranslate);
m_font->renderText(m_text, textRect, AlignCenter, m_foregroundColor);
2011-08-14 04:09:11 +02:00
}
void UIButton::onStyleApply(const OTMLNodePtr& styleNode)
2011-08-14 04:09:11 +02:00
{
UIWidget::onStyleApply(styleNode);
2011-08-14 04:09:11 +02:00
for(OTMLNodePtr node : styleNode->children()) {
if(node->tag() == "text-translate") {
m_textTranslate = node->value<Point>();
} else if(node->tag() == "text") {
m_text = node->value();
} else if(node->tag() == "onClick") {
g_lua.loadFunction(node->value(), "@" + node->source() + "[" + node->tag() + "]");
luaSetField(node->tag());
}
2011-08-22 14:44:26 +02:00
}
2011-08-14 04:09:11 +02:00
}
bool UIButton::onMouseRelease(const Point& mousePos, MouseButton button)
2011-08-14 04:09:11 +02:00
{
if(isPressed()) {
2011-08-22 14:44:26 +02:00
if(m_onClick && getRect().contains(mousePos))
2011-08-14 04:09:11 +02:00
m_onClick();
2011-08-22 14:44:26 +02:00
return true;
}
return false;
2011-04-08 11:50:26 +02:00
}