#include "uibutton.h" #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(); Rect textRect = m_rect; textRect.translate(m_textTranslate); m_font->renderText(m_text, textRect, AlignCenter, m_foregroundColor); } void UIButton::onStyleApply(const OTMLNodePtr& styleNode) { UIWidget::onStyleApply(styleNode); for(OTMLNodePtr node : styleNode->children()) { if(node->tag() == "text-translate") { m_textTranslate = node->value(); } 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()); } } } bool UIButton::onMouseRelease(const Point& mousePos, MouseButton button) { if(isPressed()) { if(m_onClick && getRect().contains(mousePos)) m_onClick(); return true; } return false; }