2011-08-28 15:17:58 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
|
|
|
|
*
|
|
|
|
* 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;
|
2011-11-03 13:48:48 +01:00
|
|
|
m_movePolicy = DONT_MOVE;
|
2011-11-17 18:43:41 +01:00
|
|
|
m_titleAlign = Fw::AlignCenter;
|
2011-08-26 17:06:52 +02:00
|
|
|
m_headHeight = 0;
|
2011-11-11 21:26:10 +01:00
|
|
|
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-08-26 17:06:52 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
void UIWindow::onStyleApply(const OTMLNodePtr& styleNode)
|
|
|
|
{
|
|
|
|
UIWidget::onStyleApply(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")
|
2011-08-26 17:06:52 +02:00
|
|
|
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") {
|
2011-11-03 13:48:48 +01:00
|
|
|
if(node->value() == "free")
|
|
|
|
m_movePolicy = FREE_MOVE;
|
|
|
|
else if(node->value() == "free updated")
|
|
|
|
m_movePolicy = FREE_UPDATED_MOVE;
|
|
|
|
else
|
|
|
|
m_movePolicy = DONT_MOVE;
|
|
|
|
}
|
2011-08-26 17:06:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-06 05:12:13 +01:00
|
|
|
boundRect.bound(parentRect);
|
2011-08-14 04:09:11 +02:00
|
|
|
}
|
2011-08-14 16:09:26 +02:00
|
|
|
|
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-28 18:02:26 +02:00
|
|
|
bool UIWindow::onMousePress(const Point& mousePos, Fw::MouseButton button)
|
2011-08-14 04:09:11 +02:00
|
|
|
{
|
2011-11-03 13:48:48 +01:00
|
|
|
if(m_movePolicy != DONT_MOVE) {
|
2011-11-03 13:17:10 +01:00
|
|
|
UIWidgetPtr clickedChild = getChildByPos(mousePos);
|
2011-11-03 17:14:40 +01:00
|
|
|
//FIXME: recursively check for non phantom children
|
2011-11-03 13:17:10 +01:00
|
|
|
if(!clickedChild || clickedChild->isPhantom()) {
|
|
|
|
m_moving = true;
|
|
|
|
m_movingReference = mousePos - getRect().topLeft();
|
2011-11-11 21:26:10 +01:00
|
|
|
m_oldIndex = getParent()->getChildIndex(asUIWidget());
|
|
|
|
m_oldPos = getPosition();
|
|
|
|
getParent()->moveChildToTop(asUIWidget());
|
2011-11-03 13:17:10 +01:00
|
|
|
}
|
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) {
|
2011-11-11 21:26:10 +01:00
|
|
|
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
|
|
|
}
|
2011-08-29 04:03:31 +02:00
|
|
|
|
2011-12-03 22:41:37 +01:00
|
|
|
bool UIWindow::onKeyPress(uchar keyCode, std::string keyText, int keyboardModifiers)
|
2011-08-29 04:03:31 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2011-12-03 22:41:37 +01:00
|
|
|
return UIWidget::onKeyPress(keyCode, keyText, keyboardModifiers);
|
2011-08-29 04:03:31 +02:00
|
|
|
}
|