scripting and UI improvements

This commit is contained in:
Eduardo Bart
2011-05-01 15:47:35 -03:00
parent 3960240b8e
commit 32a8ed3871
23 changed files with 424 additions and 293 deletions

View File

@@ -29,19 +29,22 @@
void UIContainer::internalOnDestroy()
{
// destroy children
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
(*it)->setParent(UIContainerPtr());
(*it)->destroy();
}
m_children.clear();
//logTraceDebug(getId());
// root container must not call internalDestroy
if(asUIContainer() != getRootContainer())
UIElement::internalOnDestroy();
// clear additional references
m_lockedElements.clear();
m_focusedElement.reset();
// destroy children
while(m_children.size() > 0) {
UIElementPtr element = m_children.back(); //hold reference
element->internalOnDestroy();
}
UIElement::internalOnDestroy();
}
UIContainerPtr& UIContainer::getRootContainer()
UIContainerPtr& UIContainer::getRoot()
{
static UIContainerPtr rootContainer;
if(!rootContainer) {
@@ -59,22 +62,14 @@ void UIContainer::addChild(UIElementPtr child)
void UIContainer::removeChild(UIElementPtr child)
{
// first check if its really a child
bool removed = false;
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
if((*it) == child) {
removed = true;
m_children.erase(it);
break;
}
}
assert(removed);
// defocus if needed
if(m_focusedElement == child)
setFocusedElement(UIElementPtr());
// remove from children list
m_children.remove(child);
// child must have this container as parent
assert(child->getParent() == asUIContainer());
child->setParent(UIContainerPtr());
}
@@ -82,19 +77,23 @@ UIElementPtr UIContainer::getChildById(const std::string& id)
{
if(getId() == id)
return asUIElement();
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
if((*it)->getId() == id)
return (*it);
foreach(const UIElementPtr& child, m_children) {
if(child->getId() == id)
return child;
}
return UIElementPtr();
}
UIElementPtr UIContainer::getChildByPos(const Point& pos)
{
for(auto it = m_children.rbegin(); it != m_children.rend(); ++it) {
if((*it)->getRect().contains(pos))
return (*it);
const UIElementPtr& element = (*it);
if(element->getRect().contains(pos))
return element;
}
return UIElementPtr();
}
@@ -103,50 +102,42 @@ UIElementPtr UIContainer::recursiveGetChildById(const std::string& id)
if(getId() == id)
return asUIElement();
UIElementPtr element;
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
element = (*it);
if(element->getId() == id) {
foreach(const UIElementPtr& element, m_children) {
if(element->getId() == id)
return element;
} else {
else {
UIContainerPtr container = element->asUIContainer();
if(container) {
element = container->recursiveGetChildById(id);
if(element)
return element;
UIElementPtr element2 = container->recursiveGetChildById(id);
if(element2)
return element2;
}
}
}
return UIElementPtr();
}
void UIContainer::pushChildToTop(const UIElementPtr& child)
{
bool removed = false;
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
if((*it) == child) {
removed = true;
m_children.erase(it);
break;
}
}
if(removed) {
auto it = std::find(m_children.begin(), m_children.end(), child);
if(it != m_children.end()) {
m_children.erase(it);
m_children.push_back(child);
}
}
void UIContainer::onLoad()
{
for(auto it = m_children.begin(); it != m_children.end(); ++it)
(*it)->onLoad();
foreach(const UIElementPtr& child, m_children)
child->onLoad();
UIElement::onLoad();
}
void UIContainer::render()
{
UIElement::render();
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
const UIElementPtr& child = (*it);
foreach(const UIElementPtr& child, m_children) {
if(child->isVisible())
child->render();
}
@@ -155,8 +146,7 @@ void UIContainer::render()
void UIContainer::onInputEvent(const InputEvent& event)
{
UIElementPtr focusedElement = m_focusedElement;
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
const UIElementPtr& child = (*it);
foreach(const UIElementPtr& child, m_children) {
bool shouldFire = false;
// events should pass only when element is visible and enabled
@@ -191,25 +181,19 @@ void UIContainer::onInputEvent(const InputEvent& event)
void UIContainer::focusNextElement()
{
UIElementPtr element;
auto focusedIt = std::find(m_children.begin(), m_children.end(), m_focusedElement);
if(focusedIt != m_children.end()) {
for(auto it = ++focusedIt; it != m_children.end(); ++it) {
const UIElementPtr& child = (*it);
if(child->isFocusable()) {
element = child;
break;
}
}
}
if(!element) {
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
const UIElementPtr& child = (*it);
std::list<UIElementPtr> rotatedChildren(m_children);
auto focusedIt = std::find(rotatedChildren.begin(), rotatedChildren.end(), m_focusedElement);
if(focusedIt != rotatedChildren.end()) {
std::rotate(rotatedChildren.begin(), focusedIt, rotatedChildren.end());
rotatedChildren.pop_front();
foreach(const UIElementPtr& child, rotatedChildren) {
if(child->isFocusable()) {
element = child;
break;
}
}
}
if(element)
setFocusedElement(element);
}
@@ -237,28 +221,38 @@ void UIContainer::setFocusedElement(UIElementPtr focusedElement)
bool UIContainer::lockElement(UIElementPtr element)
{
bool found = false;
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
if((*it) == element) {
(*it)->setEnabled(true);
found = true;
break;
}
}
if(found) {
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
if((*it) != element)
(*it)->setEnabled(false);
if(std::find(m_children.begin(), m_children.end(), element) != m_children.end()) {
m_lockedElements.remove(element);
m_lockedElements.push_front(element);
foreach(const UIElementPtr& child, m_children) {
if(child != element)
child->setEnabled(false);
else
child->setEnabled(true);
}
return true;
}
return false;
}
void UIContainer::unlockElement()
bool UIContainer::unlockElement(UIElementPtr element)
{
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
(*it)->setEnabled(true);
auto it = std::find(m_lockedElements.begin(), m_lockedElements.end(), element);
if(it != m_lockedElements.end()) {
m_lockedElements.erase(it);
UIElementPtr newLockedElement;
if(m_lockedElements.size() > 0)
newLockedElement = m_lockedElements.front();
foreach(const UIElementPtr& child, m_children) {
if(newLockedElement) {
if(child == newLockedElement)
child->setEnabled(true);
else
child->setEnabled(false);
} else
child->setEnabled(true);
}
return true;
}
return false;
}

View File

@@ -57,7 +57,7 @@ public:
/// Disable all children except the specified element
bool lockElement(UIElementPtr element);
/// Renable all children
void unlockElement();
bool unlockElement(UIElementPtr element);
/// Focus next element
void focusNextElement();
@@ -72,13 +72,14 @@ public:
virtual const char *getScriptableName() const { return "UIContainer"; }
/// Get root container (the container that contains everything)
static UIContainerPtr& getRootContainer();
static UIContainerPtr& getRoot();
protected:
virtual void internalOnDestroy();
private:
std::list<UIElementPtr> m_children;
std::list<UIElementPtr> m_lockedElements;
UIElementPtr m_focusedElement;
};

View File

@@ -40,24 +40,34 @@ UIElement::UIElement(UI::EElementType type) :
}
UIElement::~UIElement()
{
//logTraceDebug(getId());
}
void UIElement::destroy()
{
if(m_onDestroyCallback)
g_dispatcher.addTask(boost::bind(m_onDestroyCallback, asUIElement()));
if(getParent()) {
// schedule removal from parent
g_dispatcher.addTask(boost::bind(&UIContainer::removeChild, getParent(), asUIElement()));
}
// schedule internal destroy
//logTraceDebug(getId());
g_dispatcher.addTask(boost::bind(&UIElement::internalOnDestroy, asUIElement()));
}
void UIElement::internalOnDestroy()
{
setVisible(false);
setEnabled(false);
//logTraceDebug(getId());
UIElementPtr me = asUIElement();
if(m_onDestroyCallback)
m_onDestroyCallback(me);
// remove from parent
if(getParent()) {
getParent()->removeChild(me);
}
// check for leaks, the number of references must be always 2 here
assert(asUIElement().use_count() == 2);
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));
}
}
void UIElement::setSkin(const UIElementSkinPtr& skin)

View File

@@ -47,10 +47,11 @@ class UIElement : public UILayout
{
public:
UIElement(UI::EElementType type = UI::Element);
virtual ~UIElement() { }
virtual ~UIElement();
/// Destroy this element by removing it from its parent
void destroy();
virtual void internalOnDestroy();
/// Draw element
virtual void render();
@@ -92,11 +93,7 @@ public:
void setOnDestroy(const UIElementCallback& onDestroyCallback) { m_onDestroyCallback = onDestroyCallback; }
void setOnLoad(const UIElementCallback& onLoadCallback) { m_onLoadCallback = onLoadCallback; }
protected:
virtual void internalOnDestroy();
private:
UI::EElementType m_type;
UIContainerWeakPtr m_parent;
UIElementSkinPtr m_skin;

View File

@@ -27,6 +27,7 @@
#include <ui/ui.h>
#include <ui/uiloader.h>
#include <script/luascript.h>
#include <script/luafunctions.h>
UIElementPtr UILoader::createElementFromId(const std::string& id)
{
@@ -216,7 +217,7 @@ void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node)
if(funcRef != LUA_REFNIL) {
g_lua.pushClassInstance(element);
g_lua.pushFunction(funcRef);
g_lua.lua_UIElement_setOnLoad();
lua_UIElement_setOnLoad();
} else
throw YAML::Exception(cnode.GetMark(), "failed to parse lua script");
}
@@ -227,7 +228,7 @@ void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node)
if(funcRef != LUA_REFNIL) {
g_lua.pushClassInstance(element);
g_lua.pushFunction(funcRef);
g_lua.lua_UIElement_setOnDestroy();
lua_UIElement_setOnDestroy();
} else
throw YAML::Exception(cnode.GetMark(), "failed to parse lua script");
}
@@ -286,7 +287,7 @@ void UILoader::loadElementAnchor(const UIElementPtr& element, EAnchorType type,
if(relativeElementId == "parent" && element->getParent()) {
relativeElement = element->getParent()->asUILayout();
} else if(relativeElementId == "root") {
relativeElement = UIContainer::getRootContainer();
relativeElement = UIContainer::getRoot();
} else {
UIElementPtr tmp = element->backwardsGetElementById(relativeElementId);
if(tmp)
@@ -311,7 +312,7 @@ void UILoader::loadButton(const UIButtonPtr& button, const YAML::Node& node)
if(funcRef != LUA_REFNIL) {
g_lua.pushClassInstance(button);
g_lua.pushFunction(funcRef);
g_lua.lua_UIButton_setOnClick();
lua_UIButton_setOnClick();
} else {
throw YAML::Exception(node["onClick"].GetMark(), "failed to parse lua script");
}

View File

@@ -34,7 +34,7 @@ class UILoader
{
public:
/// Loads an UIElement and it's children from a YAML file
static UIElementPtr loadFile(const std::string& file, const UIContainerPtr& parent = UIContainer::getRootContainer());
static UIElementPtr loadFile(const std::string& file, const UIContainerPtr& parent = UIContainer::getRoot());
private:
/// Detect element type and create it