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

126 lines
3.5 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
2011-08-22 14:44:26 +02:00
UIWindow::UIWindow()
2011-08-14 04:09:11 +02:00
{
m_moving = false;
}
UIWindowPtr UIWindow::create()
{
UIWindowPtr window(new UIWindow);
return window;
}
2011-08-22 14:44:26 +02:00
void UIWindow::onStyleApply(const OTMLNodePtr& styleNode)
2011-08-14 04:09:11 +02:00
{
2011-08-22 14:44:26 +02:00
UIWidget::onStyleApply(styleNode);
2011-08-14 04:09:11 +02:00
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-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
}
2011-08-22 14:44:26 +02:00
void UIWindow::onFocusChange(bool focused, UI::FocusReason reason)
2011-08-21 03:01:46 +02:00
{
// when a window is focused it goes to the top
if(UIWidgetPtr parent = getParent())
parent->moveChildToTop(asUIWidget());
}
2011-08-22 14:44:26 +02:00
bool UIWindow::onMousePress(const Point& mousePos, UI::MouseButton button)
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);
2011-08-22 14:44:26 +02:00
if(headRect.contains(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
}
2011-08-22 14:44:26 +02:00
bool UIWindow::onMouseRelease(const Point& mousePos, UI::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) {
move(mousePos - m_movingReference);
return true;
}
return UIWidget::onMouseMove(mousePos, mouseMoved);
2011-04-16 21:46:31 +02:00
}