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

155 lines
5.3 KiB
C++
Raw Normal View History

2011-08-28 15:17:58 +02:00
/*
2012-01-02 17:58:37 +01:00
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
2011-08-28 15:17:58 +02:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2011-08-14 04:09:11 +02:00
#include "uiwindow.h"
2011-11-16 00:47:32 +01:00
#include "uitranslator.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
2011-11-17 18:43:41 +01:00
UIWindow::UIWindow()
2011-08-14 04:09:11 +02:00
{
m_moving = false;
m_movePolicy = DONT_MOVE;
2011-11-17 18:43:41 +01:00
m_titleAlign = Fw::AlignCenter;
m_headHeight = 0;
m_oldIndex = -1;
2011-08-14 04:09:11 +02:00
}
void UIWindow::render()
{
2011-10-29 01:56:45 +02:00
// render children
UIWidget::render();
2011-08-14 04:09:11 +02:00
2011-10-29 01:56:45 +02:00
// draw window head text
Rect headTextRect = m_rect;
2011-11-17 22:41:02 +01:00
headTextRect.addTop(-m_headTextOffset.y);
2011-10-29 01:56:45 +02:00
headTextRect.setHeight(m_headHeight);
if(m_titleAlign & Fw::AlignLeft)
2011-11-17 22:41:02 +01:00
headTextRect.addLeft(-m_headTextOffset.x);
2011-10-29 01:56:45 +02:00
else if(m_titleAlign & Fw::AlignRight)
2011-11-17 22:41:02 +01:00
headTextRect.addRight(-m_headTextOffset.x);
2011-10-29 01:56:45 +02:00
else {
2011-11-17 22:41:02 +01:00
headTextRect.addLeft(-m_headTextOffset.x);
headTextRect.addRight(-m_headTextOffset.x);
}
2011-10-29 01:56:45 +02:00
m_font->renderText(m_title, headTextRect, m_titleAlign, m_foregroundColor);
2011-08-14 04:09:11 +02:00
}
void UIWindow::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode)
{
UIWidget::onStyleApply(styleName, styleNode);
for(OTMLNodePtr node : styleNode->children()) {
2011-11-17 22:41:02 +01:00
if(node->tag() == "head-height")
2011-10-29 01:56:45 +02:00
m_headHeight = node->value<int>();
2011-11-17 22:41:02 +01:00
else if(node->tag() == "head-text-offset")
m_headTextOffset = node->value<Point>();
2011-10-29 01:56:45 +02:00
else if(node->tag() == "title")
setTitle(node->value());
2011-11-17 22:41:02 +01:00
else if(node->tag() == "head-text-align")
2011-10-29 01:56:45 +02:00
m_titleAlign = Fw::translateAlignment(node->value());
2011-11-17 22:41:02 +01:00
else if(node->tag() == "move-policy") {
if(node->value() == "free")
m_movePolicy = FREE_MOVE;
else if(node->value() == "free updated")
m_movePolicy = FREE_UPDATED_MOVE;
else
m_movePolicy = DONT_MOVE;
}
}
}
2012-01-05 03:42:17 +01:00
void UIWindow::onGeometryChange(const Rect& oldRect, const Rect& newRect)
2011-08-14 04:09:11 +02:00
{
2012-01-02 23:09:49 +01:00
bindRectToParent();
if(newRect == getRect())
UIWidget::onGeometryChange(oldRect, newRect);
2011-08-14 04:09:11 +02:00
}
bool UIWindow::onMousePress(const Point& mousePos, Fw::MouseButton button)
2011-08-14 04:09:11 +02:00
{
2012-01-05 03:42:17 +01:00
if(m_movePolicy != DONT_MOVE && button == Fw::MouseLeftButton) {
UIWidgetPtr clickedChild = getChildByPos(mousePos);
2011-11-03 17:14:40 +01:00
//FIXME: recursively check for non phantom children
if(!clickedChild || clickedChild->isPhantom()) {
m_moving = true;
m_movingReference = mousePos - getRect().topLeft();
m_oldIndex = getParent()->getChildIndex(asUIWidget());
m_oldPos = getPos();
getParent()->moveChildToTop(asUIWidget());
}
2011-08-22 14:44:26 +02:00
}
return UIWidget::onMousePress(mousePos, button);
2011-08-14 04:09:11 +02:00
}
2011-11-13 06:11:47 +01:00
void UIWindow::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
2011-08-14 04:09:11 +02:00
{
2011-08-22 14:44:26 +02:00
if(m_moving) {
if(m_movePolicy == FREE_UPDATED_MOVE) {
UIWidgetPtr parent = getParent();
// restore position before move
parent->moveChildToIndex(asUIWidget(), m_oldIndex);
moveTo(m_oldPos);
// calculate new index
int newIndex;
for(newIndex=parent->getChildCount();newIndex>1;--newIndex) {
UIWidgetPtr child = parent->getChildByIndex(newIndex);
if(mousePos.y >= child->getRect().top())
break;
}
// set the new index
parent->moveChildToIndex(asUIWidget(), newIndex);
updateParentLayout();
}
2011-08-14 04:09:11 +02:00
m_moving = false;
2011-08-22 14:44:26 +02:00
}
2011-11-13 06:11:47 +01:00
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
}
bool UIWindow::onKeyPress(uchar keyCode, std::string keyText, int keyboardModifiers)
{
if(keyboardModifiers == Fw::KeyboardNoModifier) {
if(keyCode == Fw::KeyReturn || keyCode == Fw::KeyEnter) {
if(callLuaField<bool>("onEnter"))
return true;
} else if(keyCode == Fw::KeyEscape) {
if(callLuaField<bool>("onEscape"))
return true;
}
}
return UIWidget::onKeyPress(keyCode, keyText, keyboardModifiers);
}