Insertion

Another popular scene graph for Java had a nice insertion feature. Inserting a node makes the children of the parent the children of the inserted node and would become the sole child of the parent node until further additions.



Prior to insertion:

parent±child

          -child2

          -child3



Became this after insertion:

parent±insertedNode±child

                                -child2

                                -child3



Just found my self wanting to do this. And is was kinda handy in the other scene graph.


Seems reasonable and straight forward. Shouldn't be that difficult to add.

I guess I hadn't ever thought of doing that.  If I had a node to insert I simply made the changes so it became the parent of the children and then attached it as a child to the node I wanted it attached to.



I can see where this might come in handy, but I never thought about doing it.

Any thoughts on a clear way to name such a method?  Insert is too ambiguous…

Attach and detach have child as a prefix. Follow the trend maybe?


insertChild(Spatial child);

Should the method signature include something to specify a "before" or "after" insertion?  Or should it always be assumed that the insert is "after" the node the call is being made against?



My question is due to the fact that sometimes you will operating ON the node that you might want to create a new parent for, in which case you'd want to insert before the current node.  This is a convenience decision, I suppose, as it's not very hard to call myNode.getParent().insertChild() but you might save some typing.

Something like:



insertChild(Spatial child, Boolean before)



That might not be the best way to handle it though since it's not hard to do even assuming the insert always takes place after the node being called against.



Bah, I think I just talked myself out of it.  Nevermind, it's early on a Monday.

I would say rather than add the boolean… it could be overloaded (I'll defer to renanse on this), but I would do:



insertChildBefore(Spatial child)

insertChildAfter(Spatial child)



thx,



timo

stupid me but… wouldn't be the "child before" actually be the parent? :stuck_out_tongue:

not stupid you… that's correct, the child before is the parent.

sfera said:

stupid me but... wouldn't be the "child before" actually be the parent? :P


Should the methods simply then be:

insertChild(Spatial child) - inserts after the node it's called against becoming the singular child of the node.  All previous children become the children of the newly insertyed node.

insertParent(Spatial parent) - inserts before the node it's called against.  The inserted node becomes the parent of the node it's called against and it becomes the singular child of the "previous" parent node.

I would agree… something like that



If it had been any earlier in the morning I probably would have suggested insertStepChild or something. :stuck_out_tongue:

gerbildrop said:

If it had been any earlier in the morning I probably would have suggested insertStepChild or something. :P


public BloodyMess insertRedHeadedStepChild(Amphibian darkfrog) {

Maybe it's just me but insert child really sounds like I'm adding a child amongst the midst of my other children, similar to ArrayList.insert…  Perhaps insert generation or something…  I dunno.

insertContainer?

Perhaps an insertParent method in Spatial and a putChildrenUpForAdoption in Node that calls insertParent on all its children?  :slight_smile:



Or maybe name that second one something else…

inject?

renanse said:
putChildrenUpForAdoption


Now that is funny.  XD

inheritChildren(Spatial newParent)?



Causes newParent to become the parent of all the current node's children?



I think none of the suggestions so far really captures the fact that what you really are doing IS an "insert".

Interpose?

chirstius said:

But this does make me think that something like insertBefore() and insertAfter() might be more clear as it doesn't have "Child" or "Parent" in it and similar convention is used for DOM manipulation.


Yes, I follow your line of reasoning.  I guess insertBefore and insertAfter, with supporting javadoc, should do the trick.  Anyone against that?