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

126 lines
3.6 KiB
C++
Raw Normal View History

2011-08-14 04:09:11 +02:00
#include "uiwindow.h"
2011-08-15 16:06:15 +02:00
#include <framework/graphics/borderimage.h>
#include <framework/graphics/font.h>
2011-08-22 21:13:52 +02:00
#include <framework/graphics/graphics.h>
2011-08-15 16:06:15 +02:00
#include <framework/otml/otml.h>
2011-08-14 04:09:11 +02:00
void UIWindow::setup()
2011-08-14 04:09:11 +02:00
{
UIWidget::setup();
2011-08-14 04:09:11 +02:00
m_moving = false;
m_headHeight = 0;
m_headMargin = 0;
m_titleAlign = AlignCenter;
2011-08-14 04:09:11 +02:00
}
void UIWindow::render()
{
// draw window head
2011-08-21 21:43:05 +02:00
Rect headRect = getRect();
2011-08-14 04:09:11 +02:00
headRect.setHeight(m_headHeight);
if(m_headImage && m_headHeight > 0) {
2011-08-22 21:13:52 +02:00
g_graphics.bindColor(m_backgroundColor);
2011-08-14 04:09:11 +02:00
m_headImage->draw(headRect);
// draw window head text
Rect headTextRect = headRect;
if(m_titleAlign & AlignLeft)
headTextRect.addLeft(-m_headMargin);
else if(m_titleAlign & AlignRight)
headTextRect.addRight(-m_headMargin);
2011-08-20 22:30:41 +02:00
m_font->renderText(m_title, headTextRect, m_titleAlign, m_foregroundColor);
2011-04-16 21:46:31 +02:00
}
2011-08-14 04:09:11 +02:00
// draw window body
2011-08-21 21:43:05 +02:00
Rect bodyRect = getRect();
2011-08-14 04:09:11 +02:00
bodyRect.setTop(headRect.bottom() + 1);
if(m_bodyImage) {
g_graphics.bindColor(m_backgroundColor);
2011-08-14 04:09:11 +02:00
m_bodyImage->draw(bodyRect);
}
2011-08-14 04:09:11 +02:00
// render children
UIWidget::render();
}
void UIWindow::onStyleApply(const OTMLNodePtr& styleNode)
{
UIWidget::onStyleApply(styleNode);
for(OTMLNodePtr node : styleNode->children()) {
if(node->tag() == "head") {
if(OTMLNodePtr cnode = node->get("border-image"))
m_headImage = BorderImage::loadFromOTML(cnode);
m_headHeight = node->valueAt("height", m_headImage->getDefaultSize().height());
m_headMargin = node->valueAt("margin", 0);
m_titleAlign = fw::translateAlignment(node->valueAt("text align", std::string("center")));
}
else if(node->tag() == "body") {
if(OTMLNodePtr cnode = node->get("border-image"))
m_bodyImage = BorderImage::loadFromOTML(cnode);
}
else if(node->tag() == "title") {
setTitle(node->value());
}
}
}
2011-08-22 14:44:26 +02:00
void UIWindow::onGeometryUpdate(const Rect& oldRect, const Rect& newRect)
2011-08-14 04:09:11 +02:00
{
// bind window rect to parent rect
2011-08-22 14:44:26 +02:00
Rect boundRect = newRect;
2011-08-14 04:09:11 +02:00
UIWidgetPtr parent = getParent();
if(parent) {
2011-08-21 21:43:05 +02:00
Rect parentRect = parent->getRect();
2011-08-14 04:09:11 +02:00
if(boundRect.left() < parentRect.left())
boundRect.moveLeft(parentRect.left());
if(boundRect.top() < parentRect.top())
boundRect.moveTop(parentRect.top());
if(boundRect.bottom() > parentRect.bottom())
boundRect.moveBottom(parentRect.bottom());
if(boundRect.right() > parentRect.right())
boundRect.moveRight(parentRect.right());
}
2011-08-22 14:44:26 +02:00
if(boundRect != newRect)
2011-08-21 21:43:05 +02:00
setRect(boundRect);
2011-08-14 04:09:11 +02:00
}
void UIWindow::onFocusChange(bool focused, FocusReason reason)
2011-08-21 03:01:46 +02:00
{
// when a window is focused it goes to the top
if(focused) {
if(UIWidgetPtr parent = getParent())
parent->moveChildToTop(asUIWidget());
}
2011-08-21 03:01:46 +02:00
}
bool UIWindow::onMousePress(const Point& mousePos, MouseButton button)
2011-08-14 04:09:11 +02:00
{
if(!getChildByPos(mousePos)) {
2011-08-14 04:09:11 +02:00
m_moving = true;
2011-08-22 14:44:26 +02:00
m_movingReference = mousePos - getRect().topLeft();
return true;
}
return UIWidget::onMousePress(mousePos, button);
2011-08-14 04:09:11 +02:00
}
bool UIWindow::onMouseRelease(const Point& mousePos, MouseButton button)
2011-08-14 04:09:11 +02:00
{
2011-08-22 14:44:26 +02:00
if(m_moving) {
2011-08-14 04:09:11 +02:00
m_moving = false;
2011-08-22 14:44:26 +02:00
return true;
}
return UIWidget::onMouseRelease(mousePos, button);
2011-08-14 04:09:11 +02:00
}
2011-08-22 14:44:26 +02:00
bool UIWindow::onMouseMove(const Point& mousePos, const Point& mouseMoved)
2011-08-14 04:09:11 +02:00
{
2011-08-22 14:44:26 +02:00
if(m_moving) {
2011-08-22 21:13:52 +02:00
moveTo(mousePos - m_movingReference);
2011-08-22 14:44:26 +02:00
return true;
}
return UIWidget::onMouseMove(mousePos, mouseMoved);
2011-04-16 21:46:31 +02:00
}