[fixed] IGUIElement::addChild - would be nice to update pos

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

[fixed] IGUIElement::addChild - would be nice to update pos

Post by CuteAlien »

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.
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
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

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-

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)
 	{
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Post Reply