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-04-09 01:20:44 +02:00
|
|
|
UIElement::UIElement(UI::EElementType type) :
|
2011-04-10 22:40:44 +02:00
|
|
|
UILayout(),
|
2011-04-09 19:18:50 +02:00
|
|
|
m_type(type),
|
2011-04-09 21:07:35 +02:00
|
|
|
m_visible(true),
|
2011-04-11 06:08:56 +02:00
|
|
|
m_enabled(true),
|
|
|
|
m_focused(false)
|
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-04-17 00:06:42 +02:00
|
|
|
void UIElement::destroy()
|
|
|
|
{
|
2011-05-01 20:47:35 +02:00
|
|
|
//logTraceDebug(getId());
|
2011-04-23 05:28:23 +02:00
|
|
|
g_dispatcher.addTask(boost::bind(&UIElement::internalOnDestroy, asUIElement()));
|
2011-04-22 15:49:46 +02:00
|
|
|
}
|
|
|
|
|
2011-04-23 05:28:23 +02:00
|
|
|
void UIElement::internalOnDestroy()
|
2011-04-22 15:49:46 +02:00
|
|
|
{
|
2011-05-01 20:47:35 +02:00
|
|
|
//logTraceDebug(getId());
|
|
|
|
|
|
|
|
UIElementPtr me = asUIElement();
|
|
|
|
if(m_onDestroyCallback)
|
|
|
|
m_onDestroyCallback(me);
|
|
|
|
|
|
|
|
// remove from parent
|
|
|
|
if(getParent()) {
|
|
|
|
getParent()->removeChild(me);
|
|
|
|
}
|
|
|
|
|
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-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()
|
|
|
|
{
|
|
|
|
if(m_onLoadCallback)
|
|
|
|
g_dispatcher.addTask(boost::bind(m_onLoadCallback, asUIElement()));
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if(getId() == id)
|
|
|
|
return asUIElement();
|
|
|
|
|
|
|
|
UIElementPtr element;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|