mirror of
https://github.com/edubart/otclient.git
synced 2025-04-30 17:49:21 +02:00
117 lines
3.2 KiB
C++
117 lines
3.2 KiB
C++
#include "uiwindow.h"
|
|
#include <graphics/borderimage.h>
|
|
#include <graphics/font.h>
|
|
#include <otml/otml.h>
|
|
|
|
UIWindow::UIWindow(): UIWidget(UITypeWindow)
|
|
{
|
|
m_moving = false;
|
|
setFocusable(true);
|
|
}
|
|
|
|
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->readAt("height", m_headImage->getDefaultSize().height());
|
|
m_headMargin = headNode->readAt("margin", 0);
|
|
m_titleAlign = parseAlignment(headNode->readAt("text align", std::string("center")));
|
|
} 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->readAt("title", aux::empty_string);
|
|
}
|
|
|
|
void UIWindow::render()
|
|
{
|
|
// draw window head
|
|
Rect headRect = getGeometry();
|
|
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);
|
|
getFont()->renderText(m_title, headTextRect, m_titleAlign, getColor());
|
|
}
|
|
|
|
// draw window body
|
|
Rect bodyRect = getGeometry();
|
|
bodyRect.setTop(headRect.bottom() + 1);
|
|
if(m_bodyImage)
|
|
m_bodyImage->draw(bodyRect);
|
|
|
|
// render children
|
|
UIWidget::render();
|
|
}
|
|
|
|
void UIWindow::onGeometryUpdate()
|
|
{
|
|
UIWidget::onGeometryUpdate();
|
|
|
|
// bind window rect to parent rect
|
|
Rect boundRect = getGeometry();
|
|
UIWidgetPtr parent = getParent();
|
|
if(parent) {
|
|
Rect parentRect = parent->getGeometry();
|
|
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 != getGeometry())
|
|
setGeometry(boundRect);
|
|
}
|
|
|
|
void UIWindow::onMousePress(const UIMouseEvent& event)
|
|
{
|
|
UIWidget::onMousePress(event);
|
|
Rect headRect = getGeometry();
|
|
headRect.setHeight(m_headHeight);
|
|
if(headRect.contains(event.mousePos)) {
|
|
m_moving = true;
|
|
m_movingReference = event.mousePos - getGeometry().topLeft();
|
|
}
|
|
}
|
|
|
|
void UIWindow::onMouseRelease(const UIMouseEvent& event)
|
|
{
|
|
UIWidget::onMouseRelease(event);
|
|
if(m_moving)
|
|
m_moving = false;
|
|
}
|
|
|
|
void UIWindow::onMouseMove(const UIMouseEvent& event)
|
|
{
|
|
UIWidget::onMouseMove(event);
|
|
if(m_moving)
|
|
move(event.mousePos - m_movingReference);
|
|
}
|