move UIWindow to lua

This commit is contained in:
Eduardo Bart
2012-01-10 21:13:38 -02:00
parent a1374baee1
commit 8ad88c4070
23 changed files with 57 additions and 254 deletions

View File

@@ -189,7 +189,6 @@ SET(framework_SOURCES ${framework_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/ui/uiwidgettext.cpp
${CMAKE_CURRENT_LIST_DIR}/ui/uiwidgetbasestyle.cpp
${CMAKE_CURRENT_LIST_DIR}/ui/uilineedit.cpp
${CMAKE_CURRENT_LIST_DIR}/ui/uiwindow.cpp
${CMAKE_CURRENT_LIST_DIR}/ui/uianchorlayout.cpp
${CMAKE_CURRENT_LIST_DIR}/ui/uiverticallayout.cpp
${CMAKE_CURRENT_LIST_DIR}/ui/uilayout.cpp

View File

@@ -307,12 +307,6 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<UILineEdit>("isAlwaysActive", &UILineEdit::isAlwaysActive);
g_lua.bindClassMemberFunction<UILineEdit>("isTextHidden", &UILineEdit::isTextHidden);
// UIWindow
g_lua.registerClass<UIWindow, UIWidget>();
g_lua.bindClassStaticFunction<UIWindow>("create", []{ return UIWindowPtr(new UIWindow); } );
g_lua.bindClassMemberFunction<UIWindow>("getTitle", &UIWindow::getTitle);
g_lua.bindClassMemberFunction<UIWindow>("setTitle", &UIWindow::setTitle);
// UIFrameCounter
g_lua.registerClass<UIFrameCounter, UIWidget>();
g_lua.bindClassStaticFunction<UIFrameCounter>("create", []{ return UIFrameCounterPtr(new UIFrameCounter); } );

View File

@@ -29,7 +29,6 @@ class UIManager;
class UIWidget;
class UILineEdit;
class UIFrameCounter;
class UIWindow;
class UILayout;
class UIVerticalLayout;
class UIAnchorLayout;
@@ -39,7 +38,6 @@ typedef std::weak_ptr<UIWidget> UIWidgetWeakPtr;
typedef std::shared_ptr<UILineEdit> UILineEditPtr;
typedef std::shared_ptr<UIFrameCounter> UIFrameCounterPtr;
typedef std::shared_ptr<UIWindow> UIWindowPtr;
typedef std::shared_ptr<UILayout> UILayoutPtr;
typedef std::shared_ptr<UIVerticalLayout> UIVerticalLayoutPtr;
typedef std::shared_ptr<UIAnchorLayout> UIAnchorLayoutPtr;

View File

@@ -26,7 +26,6 @@
#include "uimanager.h"
#include "uiwidget.h"
#include "uilineedit.h"
#include "uiwindow.h"
#include "uiframecounter.h"
#include "uilayout.h"
#include "uiverticallayout.h"

View File

@@ -722,7 +722,6 @@ int UIWidget::getChildIndex(const UIWidgetPtr& child)
Rect UIWidget::getChildrenRect()
{
Rect rect = m_rect;
rect.expand(-m_borderWidth.top, -m_borderWidth.right, -m_borderWidth.bottom, -m_borderWidth.left);
rect.expand(-m_padding.top, -m_padding.right, -m_padding.bottom, -m_padding.left);
return rect;
}

View File

@@ -34,9 +34,7 @@ void UIWidget::initText()
void UIWidget::parseTextStyle(const OTMLNodePtr& styleNode)
{
for(const OTMLNodePtr& node : styleNode->children()) {
if(node->tag() == "icon")
setIcon(node->value());
else if(node->tag() == "text")
if(node->tag() == "text")
setText(node->value());
else if(node->tag() == "text-align")
setTextAlign(Fw::translateAlignment(node->value()));

View File

@@ -1,153 +0,0 @@
/*
* Copyright (c) 2010-2012 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.
*/
#include "uiwindow.h"
#include "uitranslator.h"
#include <framework/graphics/font.h>
#include <framework/graphics/graphics.h>
#include <framework/otml/otml.h>
UIWindow::UIWindow()
{
m_moving = false;
m_movePolicy = DONT_MOVE;
m_titleAlign = Fw::AlignCenter;
m_headHeight = 0;
m_oldIndex = -1;
}
void UIWindow::draw()
{
// render children
UIWidget::draw();
// draw window head text
Rect headTextRect = m_rect;
headTextRect.expandTop(-m_headTextOffset.y);
headTextRect.setHeight(m_headHeight);
if(m_titleAlign & Fw::AlignLeft)
headTextRect.expandLeft(-m_headTextOffset.x);
else if(m_titleAlign & Fw::AlignRight)
headTextRect.expandRight(-m_headTextOffset.x);
else {
headTextRect.expandLeft(-m_headTextOffset.x);
headTextRect.expandRight(-m_headTextOffset.x);
}
m_font->renderText(m_title, headTextRect, m_titleAlign, m_color);
}
void UIWindow::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode)
{
UIWidget::onStyleApply(styleName, styleNode);
for(OTMLNodePtr node : styleNode->children()) {
if(node->tag() == "head-height")
m_headHeight = node->value<int>();
else if(node->tag() == "head-text-offset")
m_headTextOffset = node->value<Point>();
else if(node->tag() == "title")
setTitle(node->value());
else if(node->tag() == "head-text-align")
m_titleAlign = Fw::translateAlignment(node->value());
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;
}
}
}
void UIWindow::onGeometryChange(const Rect& oldRect, const Rect& newRect)
{
bindRectToParent();
if(newRect == getRect())
UIWidget::onGeometryChange(oldRect, newRect);
}
bool UIWindow::onMousePress(const Point& mousePos, Fw::MouseButton button)
{
if(m_movePolicy != DONT_MOVE && button == Fw::MouseLeftButton) {
UIWidgetPtr clickedChild = getChildByPos(mousePos);
//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());
}
}
return UIWidget::onMousePress(mousePos, button);
}
void UIWindow::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
{
if(m_moving) {
if(m_movePolicy == FREE_UPDATED_MOVE) {
UIWidgetPtr parent = getParent();
// restore position before move
parent->moveChildToIndex(asUIWidget(), m_oldIndex);
setPos(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();
}
m_moving = false;
}
UIWidget::onMouseRelease(mousePos, button);
}
bool UIWindow::onMouseMove(const Point& mousePos, const Point& mouseMoved)
{
if(m_moving) {
setPos(mousePos - m_movingReference);
return true;
}
return UIWidget::onMouseMove(mousePos, mouseMoved);
}
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);
}

View File

@@ -1,63 +0,0 @@
/*
* Copyright (c) 2010-2012 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.
*/
#ifndef UIWINDOW_H
#define UIWINDOW_H
#include "uiwidget.h"
class UIWindow : public UIWidget
{
enum MovePolicy {
DONT_MOVE = 0,
FREE_MOVE,
FREE_UPDATED_MOVE
};
public:
UIWindow();
virtual void draw();
void setTitle(const std::string& title) { m_title = title; }
std::string getTitle() const { return m_title; }
protected:
virtual void onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode);
virtual void onGeometryChange(const Rect& oldRect, const Rect& newRect);
virtual bool onMousePress(const Point& mousePos, Fw::MouseButton button);
virtual void onMouseRelease(const Point& mousePos, Fw::MouseButton button);
virtual bool onMouseMove(const Point& mousePos, const Point& mouseMoved);
virtual bool onKeyPress(uchar keyCode, std::string keyText, int keyboardModifiers);
private:
std::string m_title;
bool m_moving;
MovePolicy m_movePolicy;
Fw::AlignmentFlag m_titleAlign;
Point m_headTextOffset;
Point m_movingReference;
Point m_oldPos;
int m_oldIndex;
int m_headHeight;
};
#endif