[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.
Post Reply
gerdb
Posts: 194
Joined: Wed Dec 02, 2009 8:21 pm
Location: Dresden, Germany

[solved] missing function IGUIElement::setParent()

Post by gerdb »

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?

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.
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: missing function IGUIElement::setParent()

Post by REDDemon »

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().

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

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).
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: missing function IGUIElement::setParent()

Post by CuteAlien »

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.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
gerdb
Posts: 194
Joined: Wed Dec 02, 2009 8:21 pm
Location: Dresden, Germany

Re: missing function IGUIElement::setParent()

Post by gerdb »

thank you,

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:

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.
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: missing function IGUIElement::setParent()

Post by REDDemon »

don't call remove. is already called into addchild. and without grabbing first the element will be deleted.
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
gerdb
Posts: 194
Joined: Wed Dec 02, 2009 8:21 pm
Location: Dresden, Germany

Re: missing function IGUIElement::setParent()

Post by gerdb »

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)
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: missing function IGUIElement::setParent()

Post by REDDemon »

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 :)
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Post Reply