diff --git a/src/gui/gui2_element.cpp b/src/gui/gui2_element.cpp index 0c4e80c393..30115deca5 100644 --- a/src/gui/gui2_element.cpp +++ b/src/gui/gui2_element.cpp @@ -111,6 +111,31 @@ GuiElement* GuiElement::setMargins(float left, float top, float right, float bot return this; } +GuiElement* GuiElement::setParent(GuiContainer* new_parent) +{ + if (GuiContainer* old_owner = this->getOwner()) + { + // Works only if both the old owner and new parent are valid. + if (new_parent && old_owner != new_parent) + { + // Remove from old owner's children list. + old_owner->children.remove(this); + + // Add to new owner's children list. + new_parent->children.push_back(this); + + // Update owner pointer. + this->owner = new_parent; + } + else + LOG(Debug, "GuiElement::setParent called, but new parent is invalid."); + } + else + LOG(Debug, "GuiElement::setParent called, but old owner is invalid."); + + return this; +} + GuiElement* GuiElement::setPosition(float x, float y, sp::Alignment alignment) { layout.position.x = x; diff --git a/src/gui/gui2_element.h b/src/gui/gui2_element.h index 4831d4e0ba..55df1ad0a1 100644 --- a/src/gui/gui2_element.h +++ b/src/gui/gui2_element.h @@ -81,6 +81,9 @@ class GuiElement : public GuiContainer GuiContainer* getTopLevelContainer(); const string& getID() { return id; } + // Change this element's owner/container safely (removes from old owner and adds to new owner) + GuiElement* setParent(GuiContainer* new_owner); + //Have this GuiElement destroyed, but at a safe point&time in the code. (handled by the container) void destroy();