//---------------------------------------------------------------------------- SpatialPtr Node::SetChild(UInt i, Spatial* pChild) { // Some folks are under the impression that a node can have multiple // parents, the scene graph therefore being a DAG. That is not the case. // The parent-child relationships form a tree. This assertion is to let // folks know this and to warn them that a child is being kidnapped from // another parent. To be safe, you should really call DetachChild before // you reattach somewhere else with AttachChild or SetChild. If you do // call DetachChild first, be aware that the child might self-destruct. // If you want this not to happen, hang onto the child via a smart // pointer. For example, // // Node* pNode0 = WIRE_NEW Node; // Spatial* pChild0 = <...>; // pNode0->AttachChild(pChild0); // child at index 0 // Node* pNode1 = <...>; // // // This asserts because pChild0 already has a parent (pNode0). // pNode1->AttachChild(pChild0); // // // Instead do this and avoid the potential self-destruction of // // pChild0). // SpatialPtr spSaveChild = pNode0->GetChild(0); // pNode0->DetachChild(spSaveChild); // pNode1->AttachChild(spSaveChild); if (pChild) { WIRE_ASSERT(!pChild->GetParent()); } if (i < GetQuantity()) { // detach child currently in slot SpatialPtr spPreviousChild = mChildren[i]; if (spPreviousChild) { spPreviousChild->SetParent(0); } // attach new child to slot if (pChild) { pChild->SetParent(this); } mChildren[i] = pChild; return spPreviousChild; } // index out of range, increase array size and attach new child pChild->SetParent(this); mChildren.Append(pChild); return NULL; }
//---------------------------------------------------------------------------- SpatialPtr Node::DetachChildAt (int i) { if (0 <= i && i < m_kChild.GetQuantity()) { SpatialPtr spkChild = m_kChild[i]; if ( spkChild ) { // child exists in slot, detach it spkChild->SetParent(0); m_kChild[i] = 0; m_iUsed--; } return spkChild; } return 0; }
//---------------------------------------------------------------------------- SpatialPtr Node::DetachChildAt(UInt i) { if (i < GetQuantity()) { SpatialPtr spChild = mChildren[i]; if (spChild) { // child exists in slot, detach it spChild->SetParent(NULL); mChildren[i] = NULL; } return spChild; } return NULL; }
//---------------------------------------------------------------------------- SpatialPtr Node::SetChild (int i, Spatial* pkChild) { // Some folks are of the impression that a node can have multiple parents, // the scene graph therefore being a DAG. That is not the case. The // parent-child relationships form a tree. This assertion is to let folks // know this and to warn them that a child is being kidnapped from another // parent. To be safe, you should really call DetachChild before you // reattach somewhere else with AttachChild or SetChild. if (pkChild) { assert(!pkChild->GetParent()); } if (0 <= i && i < m_kChild.GetQuantity()) { // detach child currently in slot SpatialPtr spkPreviousChild = m_kChild[i]; if (spkPreviousChild) { spkPreviousChild->SetParent(0); m_iUsed--; } // attach new child to slot if (pkChild) { pkChild->SetParent(this); m_iUsed++; } m_kChild[i] = pkChild; return spkPreviousChild; } // index out of range, increase array size and attach new child pkChild->SetParent(this); m_kChild.SetElement(i,pkChild); m_iUsed++; return 0; }