Commit acfc8b2d authored by Michele Bosi's avatar Michele Bosi

ActorTree class interface and docs update.

parent 6fd58fbf
......@@ -37,30 +37,37 @@ using namespace vl;
//-----------------------------------------------------------------------------
void ActorTree::eraseAllChildren()
{
for(unsigned i=0;i<mChildren.size(); ++i)
mChildren[i]->setParent(NULL);
for( size_t i = 0; i < mChildren.size(); ++i ) {
mChildren[i]->setParent( NULL );
}
mChildren.clear();
}
//-----------------------------------------------------------------------------
void ActorTree::addChild(ActorTreeAbstract* node)
{
if (node->parent())
{
if ( node->parent() ) {
vl::Log::error("ActorTreeAbstract::addChild(node): 'node' already has a parent.\n");
return;
}
node->setParent(this);
mChildren.push_back(node);
node->setParent( this );
mChildren.push_back( node );
}
//-----------------------------------------------------------------------------
void ActorTree::addChildOnce(ActorTreeAbstract* node)
{
if ( findChild( node ) == -1 ) {
addChild( node );
}
}
//-----------------------------------------------------------------------------
int ActorTree::findChild(ActorTreeAbstract* node)
{
int pos = -1;
std::vector< ref<ActorTreeAbstract> >::iterator it = std::find( mChildren.begin(), mChildren.end(), node );
if ( it != mChildren.end() ) {
pos = (int)( it - mChildren.begin() );
return (int)( it - mChildren.begin() );
} else {
return -1;
}
return pos;
}
//-----------------------------------------------------------------------------
bool ActorTree::eraseChild(ActorTreeAbstract* node)
......@@ -76,20 +83,20 @@ bool ActorTree::eraseChild(ActorTreeAbstract* node)
//-----------------------------------------------------------------------------
void ActorTree::setChild(int i, ActorTreeAbstract* node)
{
if (node->parent())
{
if ( node->parent() ) {
vl::Log::error("ActorTreeAbstract::setChild(int,node): 'node' already has a parent.\n");
return;
}
mChildren[i]->setParent(NULL);
mChildren[i]->setParent( NULL );
mChildren[i] = node;
mChildren[i]->setParent(this);
node->setParent( this );
}
//-----------------------------------------------------------------------------
void ActorTree::eraseChild(int i, int count)
{
for(int j=i; j<i+count; ++j)
mChildren[j]->setParent(NULL);
mChildren.erase(mChildren.begin()+i,mChildren.begin()+i+count);
for( int j = i; j < i + count; ++j ) {
mChildren[j]->setParent( NULL );
}
mChildren.erase( mChildren.begin() + i, mChildren.begin() + i + count );
}
//-----------------------------------------------------------------------------
......@@ -37,14 +37,14 @@
namespace vl
{
/**
* The ActorTree class implements a generic tree whose nodes contain Actor[s]. Each node of the tree can have any number of children.
* The ActorTree class implements a generic tree whose nodes contain Actors. Each node of the tree can have any number of children.
*
* Use this class when you want to have direct control over how the Actor[s] are
* Use this class when you want to have direct control over how the Actors are
* grouped together or to implement specific space partitioning schemes like
* BSP trees, Quadtrees, Octrees etc. For example you can use the ActorTree class
* to build a quadtree by assigning 4 children per node and filling them appropriately.
* Of course you can also use an ActorTree to group a set of Actor[s] based on any other
* principle. For example animated Actor[s] are usually kept in separate "flat" trees
* Of course you can also use an ActorTree to group a set of Actors based on any other
* principle. For example animated Actors are usually kept in separate "flat" trees
* (for example all in a single node) as the computational cost of rebuilding every frame
* the BSP tree, Quadtree, Octree etc. might be to high.
*
......@@ -73,14 +73,25 @@ namespace vl
virtual ActorTreeAbstract* child(int i) { return mChildren[i].get(); }
virtual const ActorTreeAbstract* child(int i) const { return mChildren[i].get(); }
//! Adds a child node to the current node
void addChild(ActorTreeAbstract* node);
//! Adds a child node to the current node if not already present
void addChildOnce(ActorTreeAbstract* node);
//! Sets the i-th child node to be `node`
void setChild(int i, ActorTreeAbstract* node);
//! Returns the index of the given node or -1 if not found
int findChild(ActorTreeAbstract* node);
//! Removes the given node
bool eraseChild(ActorTreeAbstract* node);
void setChild(int i, ActorTreeAbstract* node);
void eraseChild(int i, int count=1);
//! Removes `count` nodes starting from the i-th included
void eraseChild(int i, int count = 1);
//! Removes all the child nodes
void eraseAllChildren();
public:
//! The list of child nodes.
const std::vector< ref<ActorTreeAbstract> >& children() const { return mChildren; }
protected:
std::vector< ref<ActorTreeAbstract> > mChildren;
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment