After adding a guielement as child to a new parent it will still be positioned relative to it's old parent. I would recommend to add a call to child->updateAbsolutePosition() within addChild, as this is probably not the behavior which is expected (edit: didn't work out - see below).
edit2: Calling updateAbsolutePosition() for the IGUIElement itself instead for the child would fix it. Unfortunately this does lead to several follow-up bugs as some other elements do not expect a call to updateAbsolutePosition() and will crash afterwards (happens for example in serialization, or CGUIScrollBar which will enter recursive calls to updateAbsolutePosition()) :-(
For now I will just call updateAbsolutePosition() myself each time after using addChild which will avoid the problem. But it does not feel correct...
edit3: Adding an 'updateposition' parameter to addChild which is per default false would work without breaking anything and would give the user of the function a hint what to do when the positions are wrong. Not sure if it's the best solution, but it would work without having to change too much.
[fixed] IGUIElement::addChild - would be nice to update pos
[fixed] IGUIElement::addChild - would be nice to update pos
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Removing the second if (Parent) block from the constructor and putting child->updateAbsolutePosition() in addChild seems to work.. I think.
I'll bug you about it on IRC tomorrow though
Patch-
I'll bug you about it on IRC tomorrow though
Patch-
Code: Select all
Index: IGUIElement.h
===================================================================
--- IGUIElement.h (revision 2264)
+++ IGUIElement.h (working copy)
@@ -44,14 +44,6 @@
if (parent)
parent->addChild(this);
- // if we succeeded in becoming a child
- if (Parent)
- {
- LastParentRect = Parent->getAbsolutePosition();
- AbsoluteRect += LastParentRect.UpperLeftCorner;
- AbsoluteClippingRect = AbsoluteRect;
- AbsoluteClippingRect.clipAgainst(Parent->AbsoluteClippingRect);
- }
}
@@ -398,15 +390,15 @@
{
if (child)
{
- child->grab();
+ child->grab(); // prevent destruction when removed
child->remove(); // remove from old parent
child->LastParentRect = getAbsolutePosition();
child->Parent = this;
Children.push_back(child);
+ child->updateAbsolutePosition();
}
}
-
//! Removes a child.
virtual void removeChild(IGUIElement* child)
{