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

101 lines
3.0 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
2011-08-14 04:09:11 +02:00
UIButton::UIButton(): UIWidget(UITypeButton)
2011-04-08 11:50:26 +02:00
{
2011-08-14 04:09:11 +02:00
m_state = ButtonUp;
2011-08-21 03:01:46 +02:00
m_focusable = false;
2011-08-14 04:09:11 +02:00
// by default, all callbacks call lua fields
m_onClick = [this]() { this->callLuaField("onClick"); };
}
UIButtonPtr UIButton::create()
{
UIButtonPtr button(new UIButton);
return button;
}
void UIButton::loadStyleFromOTML(const OTMLNodePtr& styleNode)
{
UIWidget::loadStyleFromOTML(styleNode);
for(int i=0; i<3; ++i) {
m_statesStyle[i].image = m_image;
2011-08-20 22:30:41 +02:00
m_statesStyle[i].color = m_backgroundColor;
m_statesStyle[i].foregroundColor = m_foregroundColor;
2011-08-14 04:09:11 +02:00
m_statesStyle[i].textTranslate = Point(0,0);
}
if(OTMLNodePtr node = styleNode->get("state.up"))
loadStateStyle(m_statesStyle[ButtonUp], node);
if(OTMLNodePtr node = styleNode->get("state.hover"))
loadStateStyle(m_statesStyle[ButtonHover], node);
if(OTMLNodePtr node = styleNode->get("state.down"))
loadStateStyle(m_statesStyle[ButtonDown], node);
m_text = styleNode->valueAt("text", fw::empty_string);
2011-08-14 04:09:11 +02:00
if(OTMLNodePtr node = styleNode->get("onClick")) {
2011-08-20 22:30:41 +02:00
g_lua.loadFunction(node->value(), "@" + node->source() + "[" + node->tag() + "]");
luaSetField(node->tag());
2011-08-14 04:09:11 +02:00
}
}
void UIButton::loadStateStyle(ButtonStateStyle& stateStyle, const OTMLNodePtr& stateStyleNode)
{
if(OTMLNodePtr node = stateStyleNode->get("border-image"))
stateStyle.image = BorderImage::loadFromOTML(node);
if(OTMLNodePtr node = stateStyleNode->get("image"))
stateStyle.image = Image::loadFromOTML(node);
stateStyle.textTranslate = stateStyleNode->valueAt("text-translate", Point());
2011-08-20 22:30:41 +02:00
stateStyle.color = stateStyleNode->valueAt("font-color", m_foregroundColor);
stateStyle.color = stateStyleNode->valueAt("color", m_backgroundColor);
2011-08-14 04:09:11 +02:00
}
void UIButton::render()
{
2011-08-20 22:30:41 +02:00
UIWidget::render();
2011-08-14 04:09:11 +02:00
const ButtonStateStyle& currentStyle = m_statesStyle[m_state];
2011-08-21 21:43:05 +02:00
Rect textRect = getRect();
2011-08-14 04:09:11 +02:00
2011-08-20 22:30:41 +02:00
if(currentStyle.image) {
g_graphics.bindColor(currentStyle.color);
2011-08-14 04:09:11 +02:00
currentStyle.image->draw(textRect);
2011-08-20 22:30:41 +02:00
}
2011-08-14 04:09:11 +02:00
textRect.translate(currentStyle.textTranslate);
2011-08-20 22:30:41 +02:00
m_font->renderText(m_text, textRect, AlignCenter, currentStyle.foregroundColor);
2011-08-14 04:09:11 +02:00
}
void UIButton::onHoverChange(UIHoverEvent& event)
2011-08-14 04:09:11 +02:00
{
if(event.mouseEnter() && m_state == ButtonUp)
2011-08-14 04:09:11 +02:00
m_state = ButtonHover;
else if(m_state == ButtonHover)
m_state = ButtonUp;
2011-08-14 04:09:11 +02:00
}
void UIButton::onMousePress(UIMouseEvent& event)
2011-08-14 04:09:11 +02:00
{
if(event.button() == MouseLeftButton) {
2011-08-14 04:09:11 +02:00
m_state = ButtonDown;
} else
event.ignore();
2011-08-14 04:09:11 +02:00
}
void UIButton::onMouseRelease(UIMouseEvent& event)
2011-08-14 04:09:11 +02:00
{
if(m_state == ButtonDown) {
2011-08-21 21:43:05 +02:00
if(m_onClick && getRect().contains(event.pos()))
2011-08-14 04:09:11 +02:00
m_onClick();
m_state = (isHovered() && isEnabled()) ? ButtonHover : ButtonUp;
} else
event.ignore();
2011-04-08 11:50:26 +02:00
}