2011-04-08 07:10:00 +02:00
|
|
|
/* The MIT License
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010 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-04-17 21:14:24 +02:00
|
|
|
#include <prerequisites.h>
|
2011-04-17 00:06:42 +02:00
|
|
|
#include <core/dispatcher.h>
|
2011-04-17 21:14:24 +02:00
|
|
|
#include <graphics/graphics.h>
|
|
|
|
#include <ui/uielement.h>
|
|
|
|
#include <ui/uiskins.h>
|
|
|
|
#include <ui/uielementskin.h>
|
|
|
|
#include <ui/uicontainer.h>
|
2011-04-09 19:18:50 +02:00
|
|
|
|
2011-05-09 22:38:01 +02:00
|
|
|
UIElement::UIElement(UI::ElementType type) :
|
|
|
|
Scriptable(),
|
2011-04-09 19:18:50 +02:00
|
|
|
m_type(type),
|
2011-04-09 21:07:35 +02:00
|
|
|
m_visible(true),
|
2011-05-09 22:38:01 +02:00
|
|
|
m_enabled(true),
|
2011-05-13 01:24:57 +02:00
|
|
|
m_mouseOver(false),
|
2011-05-09 22:38:01 +02:00
|
|
|
m_marginLeft(0),
|
|
|
|
m_marginRight(0),
|
|
|
|
m_marginTop(0),
|
|
|
|
m_marginBottom(0)
|
2011-04-09 01:20:44 +02:00
|
|
|
{
|
2011-04-16 18:08:55 +02:00
|
|
|
|
2011-04-09 01:20:44 +02:00
|
|
|
}
|
|
|
|
|
2011-05-01 20:47:35 +02:00
|
|
|
UIElement::~UIElement()
|
|
|
|
{
|
|
|
|
//logTraceDebug(getId());
|
|
|
|
}
|
|
|
|
|
2011-05-09 22:38:01 +02:00
|
|
|
void UIElement::destroyLater()
|
2011-04-17 00:06:42 +02:00
|
|
|
{
|
2011-05-01 20:47:35 +02:00
|
|
|
//logTraceDebug(getId());
|
2011-05-09 22:38:01 +02:00
|
|
|
g_dispatcher.addTask(boost::bind(&UIElement::destroy, asUIElement()));
|
2011-04-22 15:49:46 +02:00
|
|
|
}
|
|
|
|
|
2011-05-09 22:38:01 +02:00
|
|
|
void UIElement::destroy()
|
2011-04-22 15:49:46 +02:00
|
|
|
{
|
2011-05-01 20:47:35 +02:00
|
|
|
//logTraceDebug(getId());
|
|
|
|
|
|
|
|
UIElementPtr me = asUIElement();
|
2011-05-03 00:48:41 +02:00
|
|
|
callLuaTableField("onDestroy");
|
2011-05-01 20:47:35 +02:00
|
|
|
|
|
|
|
// remove from parent
|
2011-05-09 22:38:01 +02:00
|
|
|
if(getParent())
|
2011-05-01 20:47:35 +02:00
|
|
|
getParent()->removeChild(me);
|
|
|
|
|
2011-05-02 06:13:57 +02:00
|
|
|
// free script stuff
|
2011-05-03 00:48:41 +02:00
|
|
|
releaseLuaTableRef();
|
2011-05-02 06:13:57 +02:00
|
|
|
|
2011-05-09 22:38:01 +02:00
|
|
|
g_dispatcher.addTask(boost::bind(&UIElement::destroyCheck, me));
|
2011-05-02 06:13:57 +02:00
|
|
|
}
|
|
|
|
|
2011-05-09 22:38:01 +02:00
|
|
|
void UIElement::destroyCheck()
|
2011-05-02 06:13:57 +02:00
|
|
|
{
|
|
|
|
//logTraceDebug(getId());
|
|
|
|
|
|
|
|
UIElementPtr me = asUIElement();
|
2011-04-22 15:49:46 +02:00
|
|
|
// check for leaks, the number of references must be always 2 here
|
2011-05-01 20:47:35 +02:00
|
|
|
if(me.use_count() != 2 && me != UIContainer::getRoot()) {
|
|
|
|
flogWarning("destroyed element with id '%s', but it still have %d references left", getId() % (me.use_count()-2));
|
|
|
|
}
|
2011-04-17 00:06:42 +02:00
|
|
|
}
|
|
|
|
|
2011-05-09 22:38:01 +02:00
|
|
|
void UIElement::setSize(const Size& size)
|
|
|
|
{
|
|
|
|
Rect rect = getRect();
|
|
|
|
if(rect.isValid())
|
|
|
|
rect.setSize(size);
|
|
|
|
else
|
|
|
|
rect = Rect(0, 0, size);
|
|
|
|
setRect(rect);
|
|
|
|
getLayout()->recalculateElementLayout(asUIElement());
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIElement::setRect(const Rect& rect)
|
|
|
|
{
|
|
|
|
if(rect != m_rect) {
|
|
|
|
m_rect = rect;
|
|
|
|
|
|
|
|
// rect updated, recalculate children layout
|
|
|
|
getLayout()->recalculateChildrenLayout(asUIElement());
|
|
|
|
|
|
|
|
onRectUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-17 21:14:24 +02:00
|
|
|
void UIElement::setSkin(const UIElementSkinPtr& skin)
|
2011-04-09 01:20:44 +02:00
|
|
|
{
|
2011-04-16 18:08:55 +02:00
|
|
|
m_skin = skin;
|
2011-04-17 02:36:58 +02:00
|
|
|
if(skin)
|
2011-04-17 00:06:42 +02:00
|
|
|
skin->apply(this);
|
2011-04-09 01:20:44 +02:00
|
|
|
}
|
|
|
|
|
2011-04-23 05:28:23 +02:00
|
|
|
void UIElement::onLoad()
|
|
|
|
{
|
2011-05-03 00:48:41 +02:00
|
|
|
g_dispatcher.addTask(boost::bind(&Scriptable::callLuaTableField, shared_from_this(), "onLoad"));
|
2011-04-23 05:28:23 +02:00
|
|
|
}
|
|
|
|
|
2011-04-09 01:20:44 +02:00
|
|
|
void UIElement::render()
|
|
|
|
{
|
|
|
|
if(m_skin)
|
|
|
|
m_skin->draw(this);
|
2011-04-11 22:06:03 +02:00
|
|
|
//g_graphics.drawBoundingRect(getRect());
|
2011-04-09 01:20:44 +02:00
|
|
|
}
|
2011-04-10 23:12:42 +02:00
|
|
|
|
|
|
|
UIElementPtr UIElement::backwardsGetElementById(const std::string& id)
|
|
|
|
{
|
2011-05-09 22:38:01 +02:00
|
|
|
if(getId() == id || id == "self")
|
2011-04-10 23:12:42 +02:00
|
|
|
return asUIElement();
|
|
|
|
|
2011-05-09 22:38:01 +02:00
|
|
|
if(id == "parent")
|
|
|
|
return getParent();
|
|
|
|
|
|
|
|
if(id == "root")
|
|
|
|
return UIContainer::getRoot();
|
2011-04-10 23:12:42 +02:00
|
|
|
|
2011-05-09 22:38:01 +02:00
|
|
|
UIElementPtr element;
|
2011-04-10 23:12:42 +02:00
|
|
|
if(asUIContainer()) {
|
|
|
|
element = asUIContainer()->recursiveGetChildById(id);
|
|
|
|
if(element)
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(getParent())
|
|
|
|
element = getParent()->backwardsGetElementById(id);
|
|
|
|
return element;
|
|
|
|
}
|
2011-04-16 21:46:31 +02:00
|
|
|
|
|
|
|
void UIElement::moveTo(Point pos)
|
|
|
|
{
|
|
|
|
Rect newRect = getRect();
|
|
|
|
newRect.moveTo(pos);
|
|
|
|
|
|
|
|
// bound newRect to parent rect
|
|
|
|
UIContainerPtr parent = getParent();
|
|
|
|
if(parent) {
|
|
|
|
Rect parentRect = parent->getRect();
|
|
|
|
if(newRect.left() < parentRect.left())
|
|
|
|
newRect.moveLeft(parentRect.left());
|
|
|
|
if(newRect.top() < parentRect.top())
|
|
|
|
newRect.moveTop(parentRect.top());
|
|
|
|
if(newRect.bottom() > parentRect.bottom())
|
|
|
|
newRect.moveBottom(parentRect.bottom());
|
|
|
|
if(newRect.right() > parentRect.right())
|
|
|
|
newRect.moveRight(parentRect.right());
|
|
|
|
}
|
|
|
|
setRect(newRect);
|
|
|
|
}
|
2011-05-03 00:48:41 +02:00
|
|
|
|
|
|
|
void UIElement::setParent(UIContainerPtr parent)
|
|
|
|
{
|
|
|
|
UIElementPtr me = asUIElement();
|
|
|
|
UIContainerPtr oldParent = m_parent.lock();
|
|
|
|
m_parent.reset();
|
|
|
|
if(oldParent && oldParent->hasChild(me)) {
|
|
|
|
oldParent->removeChild(me);
|
|
|
|
}
|
|
|
|
if(parent) {
|
|
|
|
m_parent = UIContainerWeakPtr(parent);
|
|
|
|
if(!parent->hasChild(me))
|
|
|
|
parent->addChild(me);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UIElement::isFocused() const
|
|
|
|
{
|
|
|
|
if(UIContainerPtr parent = m_parent.lock())
|
|
|
|
return (parent->getFocusedElement() == shared_from_this());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIElement::setFocused(bool focused)
|
|
|
|
{
|
|
|
|
if(UIContainerPtr parent = m_parent.lock()) {
|
|
|
|
if(focused) {
|
|
|
|
parent->setFocusedElement(asUIElement());
|
|
|
|
} else if(parent->getFocusedElement() == asUIElement()) {
|
|
|
|
parent->setFocusedElement(UIElementPtr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-09 22:38:01 +02:00
|
|
|
|
|
|
|
UILayoutPtr UIElement::getLayout() const
|
|
|
|
{
|
|
|
|
if(m_layout)
|
|
|
|
return m_layout;
|
|
|
|
else if(getParent())
|
|
|
|
return getParent()->getLayout();
|
|
|
|
return UILayoutPtr();
|
|
|
|
}
|