Visualization Library 2.0.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
ActorTree.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include <vlGraphics/ActorTree.hpp>
33 #include <vlCore/Log.hpp>
34 
35 using namespace vl;
36 
37 //-----------------------------------------------------------------------------
39 {
40  for( size_t i = 0; i < mChildren.size(); ++i ) {
41  mChildren[i]->setParent( NULL );
42  }
43  mChildren.clear();
44 }
45 //-----------------------------------------------------------------------------
47 {
48  if ( node->parent() ) {
49  vl::Log::error("ActorTreeAbstract::addChild(node): 'node' already has a parent.\n");
50  return;
51  }
52  node->setParent( this );
53  mChildren.push_back( node );
54 }
55 //-----------------------------------------------------------------------------
57 {
58  if ( findChild( node ) == -1 ) {
59  addChild( node );
60  }
61 }
62 //-----------------------------------------------------------------------------
64 {
65  std::vector< ref<ActorTreeAbstract> >::iterator it = std::find( mChildren.begin(), mChildren.end(), node );
66  if ( it != mChildren.end() ) {
67  return (int)( it - mChildren.begin() );
68  } else {
69  return -1;
70  }
71 }
72 //-----------------------------------------------------------------------------
74 {
75  int pos = findChild( node );
76  if ( pos != -1 ) {
77  eraseChild( pos );
78  return true;
79  } else {
80  return false;
81  }
82 }
83 //-----------------------------------------------------------------------------
85 {
86  if ( node->parent() ) {
87  vl::Log::error("ActorTreeAbstract::setChild(int,node): 'node' already has a parent.\n");
88  return;
89  }
90  mChildren[i]->setParent( NULL );
91  mChildren[i] = node;
92  node->setParent( this );
93 }
94 //-----------------------------------------------------------------------------
95 void ActorTree::eraseChild(int i, int count)
96 {
97  for( int j = i; j < i + count; ++j ) {
98  mChildren[j]->setParent( NULL );
99  }
100  mChildren.erase( mChildren.begin() + i, mChildren.begin() + i + count );
101 }
102 //-----------------------------------------------------------------------------
void addChildOnce(ActorTreeAbstract *node)
Adds a child node to the current node if not already present.
Definition: ActorTree.cpp:56
const ActorTreeAbstract * parent() const
Returns the parent of a node.
int findChild(ActorTreeAbstract *node)
Returns the index of the given node or -1 if not found.
Definition: ActorTree.cpp:63
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
Definition: Log.cpp:165
std::vector< ref< ActorTreeAbstract > > mChildren
Definition: ActorTree.hpp:95
Visualization Library main namespace.
void addChild(ActorTreeAbstract *node)
Adds a child node to the current node.
Definition: ActorTree.cpp:46
The ActorTreeAbstract class implements the interface of a generic tree containing Actors in its nodes...
#define NULL
Definition: OpenGLDefs.hpp:81
void setParent(ActorTreeAbstract *p)
For internal use only.
void setChild(int i, ActorTreeAbstract *node)
Sets the i-th child node to be node
Definition: ActorTree.cpp:84
bool eraseChild(ActorTreeAbstract *node)
Removes the given node.
Definition: ActorTree.cpp:73
void eraseAllChildren()
Removes all the child nodes.
Definition: ActorTree.cpp:38