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

124 lines
3.4 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>
#include <framework/otml/otml.h>
2011-08-14 04:09:11 +02:00
UIWindow::UIWindow(): UIWidget(UITypeWindow)
{
m_moving = false;
}
UIWindowPtr UIWindow::create()
{
UIWindowPtr window(new UIWindow);
window->setStyle("Window");
return window;
}
void UIWindow::loadStyleFromOTML(const OTMLNodePtr& styleNode)
{
UIWidget::loadStyleFromOTML(styleNode);
if(OTMLNodePtr headNode = styleNode->get("head")) {
if(OTMLNodePtr node = headNode->get("border-image"))
m_headImage = BorderImage::loadFromOTML(node);
m_headHeight = headNode->valueAt("height", m_headImage->getDefaultSize().height());
m_headMargin = headNode->valueAt("margin", 0);
m_titleAlign = fw::translateAlignment(headNode->valueAt("text align", std::string("center")));
2011-08-14 04:09:11 +02:00
} else {
m_headHeight = 0;
m_headMargin = 0;
m_titleAlign = AlignCenter;
}
if(OTMLNodePtr bodyNode = styleNode->get("body")) {
if(OTMLNodePtr node = bodyNode->get("border-image"))
m_bodyImage = BorderImage::loadFromOTML(node);
}
m_title = styleNode->valueAt("title", fw::empty_string);
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) {
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)
m_bodyImage->draw(bodyRect);
// render children
UIWidget::render();
}
2011-08-21 21:43:05 +02:00
void UIWindow::onRectUpdate(UIRectUpdateEvent& event)
2011-08-14 04:09:11 +02:00
{
// bind window rect to parent rect
Rect boundRect = event.rect();
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());
}
if(boundRect != event.rect())
2011-08-21 21:43:05 +02:00
setRect(boundRect);
2011-08-14 04:09:11 +02:00
}
2011-08-21 03:01:46 +02:00
void UIWindow::onFocusChange(UIFocusEvent& event)
{
// when a window is focused it goes to the top
if(UIWidgetPtr parent = getParent())
parent->moveChildToTop(asUIWidget());
}
void UIWindow::onMousePress(UIMouseEvent& event)
2011-08-14 04:09:11 +02:00
{
2011-08-21 21:43:05 +02:00
Rect headRect = getRect();
2011-08-14 04:09:11 +02:00
headRect.setHeight(m_headHeight);
if(headRect.contains(event.pos())) {
2011-08-14 04:09:11 +02:00
m_moving = true;
2011-08-21 21:43:05 +02:00
m_movingReference = event.pos() - getRect().topLeft();
} else
2011-08-14 20:02:28 +02:00
UIWidget::onMousePress(event);
2011-08-14 04:09:11 +02:00
}
void UIWindow::onMouseRelease(UIMouseEvent& event)
2011-08-14 04:09:11 +02:00
{
if(m_moving)
m_moving = false;
else
2011-08-14 20:02:28 +02:00
UIWidget::onMouseRelease(event);
2011-08-14 04:09:11 +02:00
}
void UIWindow::onMouseMove(UIMouseEvent& event)
2011-08-14 04:09:11 +02:00
{
if(m_moving)
move(event.pos() - m_movingReference);
else
2011-08-14 20:02:28 +02:00
UIWidget::onMouseMove(event);
2011-04-16 21:46:31 +02:00
}