[solved] missing function IGUIElement::setParent()

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.

[solved] missing function IGUIElement::setParent()

Postby gerdb » Fri Jul 06, 2012 8:29 pm

hi,

how do i change the parent of a IGUIElement ?

im using svn4170, my C::B CodeCompletion found a function setParent() but searching IGUIElement.h revealed no such thing.

thx in advance

EDIT: would this be corrent?
cpp Code: Select all
 
    void setParent(IGUIElement* newParent)
    {
        remove();
       
        if (newParent)
        {
            newParent->addChild(this);
            Parent = newParent;
            // maybe recalculate size
        }  
        else
        {
            if (Environment)
            {
                Parent = Environment->getRootGUIElement();
                Parent->addChild(this);
            }
            else
            {
                // ERROR ?
                Parent = 0;
            }          
        }  
    }
 
Last edited by gerdb on Tue Jul 24, 2012 9:35 pm, edited 1 time in total.
gerdb
 
Posts: 168
Joined: Wed Dec 02, 2009 8:21 pm
Location: Dresden, Germany

Re: missing function IGUIElement::setParent()

Postby REDDemon » Fri Jul 06, 2012 8:59 pm

I think that if there is no parent the function should do nothing.
If a user wanna to remove a parent he can do

setParent(0).

as it is, your code will setup a unwanted parent (root), so can happen to users to see a unwanted element drawed.

also setting parent again is redundant since is already done in addChild(), and also calling remove().

cpp Code: Select all
 
virtual void setParent(IGUIElement *newparent)
{
    if(newParent)
    {
        newParent->addChild(this);
    }
}
 


I don't know why no one added "setParent" method. But is not really needed.

instead of doing
cpp Code: Select all
 
element1->setParent(element2);
//you can still do
element2->addChild(element1);
 


probably having only addChild will make the code more consistent and requires 1 less virtual method (in the case some user re-implement addChild, is also forced to re-implement setParent for keep consistency, so I think that could be one of the reasons that method hasn't been added before).
OpenGL is not hard. What you have to do is just explained in specifications. What is hard is dealing with poor OpenGL implementations.
User avatar
REDDemon
 
Posts: 831
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: missing function IGUIElement::setParent()

Postby CuteAlien » Fri Jul 06, 2012 9:38 pm

Good questions why it's missing - I hadn't noticed before. As IGUISceneNode has a setParent and addChild it probably makes sense adding it to IGUIElement as well. But I'll just put it on my todo for now as I don't have enough time currently to look into this (maybe it was to prevent ever having 0 as parent or something like that). On first view I'd say calling remove() first looks dangerous, wouldn't that drop the element?
IRC: #irrlicht on irc.freenode.net
My patches&stuff: http://www.michaelzeilfelder.de/irrlicht.htm
Games with Irrlicht: http://www.irrgheist.com/
News: http://www.reddit.com/r/irrlicht/
User avatar
CuteAlien
Admin
 
Posts: 5358
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany

Re: missing function IGUIElement::setParent()

Postby gerdb » Fri Jul 06, 2012 9:46 pm

thank you,

cpp Code: Select all
 
 newParent->addChild(this);
 


does the trick and

probably having only addChild will make the code more consistent and requires 1 less virtual method (in the case some user re-implement addChild, is also forced to re-implement setParent for keep consistency, so I think that could be one of the reasons that method hasn't been added before).


makes complete sense to me.

h.a.n.d. :wink:

EDIT:

cpp Code: Select all
 
 
virtual void setParent(IGUIElement *newparent)
{
    if(newParent)
    {
        newParent->addChild(this); // calls this->remove();
    }
    else
    {
        remove(); // Parent = 0; // Element is free and will not be drawn
    }
}
 
 
Last edited by gerdb on Fri Jul 06, 2012 10:12 pm, edited 3 times in total.
gerdb
 
Posts: 168
Joined: Wed Dec 02, 2009 8:21 pm
Location: Dresden, Germany

Re: missing function IGUIElement::setParent()

Postby REDDemon » Fri Jul 06, 2012 9:57 pm

don't call remove. is already called into addchild. and without grabbing first the element will be deleted.
OpenGL is not hard. What you have to do is just explained in specifications. What is hard is dealing with poor OpenGL implementations.
User avatar
REDDemon
 
Posts: 831
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: missing function IGUIElement::setParent()

Postby gerdb » Fri Jul 06, 2012 10:01 pm

sorry first post was wrong, but i have to call remove(),

when "newparent = 0", and hope that member this->Parent will be set to 0 through ( removeChild()::*it->Parent=0)
gerdb
 
Posts: 168
Joined: Wed Dec 02, 2009 8:21 pm
Location: Dresden, Germany

Re: missing function IGUIElement::setParent()

Postby REDDemon » Sat Jul 07, 2012 10:20 am

well :) you last snippet has "remove" before the "if" clause and that was not correct. Now that "remove" is the "else" branch it looks ok :)
OpenGL is not hard. What you have to do is just explained in specifications. What is hard is dealing with poor OpenGL implementations.
User avatar
REDDemon
 
Posts: 831
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)


Return to Bug reports

Who is online

Users browsing this forum: No registered users and 1 guest