resolved conflict state with HEAD r14096
[blender.git] / source / gameengine / SceneGraph / SG_Node.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 #include "SG_Node.h"
33 #include "SG_ParentRelation.h"
34 #include <algorithm>
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 using namespace std;
41
42
43 SG_Node::SG_Node(
44         void* clientobj,
45         void* clientinfo,
46         SG_Callbacks callbacks
47
48 )
49         : SG_Spatial(clientobj,clientinfo,callbacks),
50         m_SGparent(NULL)
51 {
52 }
53
54 SG_Node::SG_Node(
55         const SG_Node & other
56 ) :
57         SG_Spatial(other),
58         m_children(other.m_children),
59         m_SGparent(other.m_SGparent)
60 {
61         // nothing to do
62 }
63
64 SG_Node::~SG_Node()
65 {
66 }
67
68
69 SG_Node* SG_Node::GetSGReplica()
70 {
71         SG_Node* replica = new SG_Node(*this);
72         if (replica == NULL) return NULL;
73
74         ProcessSGReplica(replica);
75         
76         return replica;
77 }
78
79         void 
80 SG_Node::
81 ProcessSGReplica(
82         SG_Node* replica
83 ){
84         // Apply the replication call back function.
85         ActivateReplicationCallback(replica);
86
87         // clear the replica node of it's parent.
88         static_cast<SG_Node*>(replica)->m_SGparent = NULL;
89
90         if (m_children.begin() != m_children.end())
91         {
92                 // if this node has children, the replica has too, so clear and clone children
93                 replica->ClearSGChildren();
94         
95                 NodeList::iterator childit;
96                 for (childit = m_children.begin();childit!=m_children.end();++childit)
97                 {
98                         replica->AddChild((*childit)->GetSGReplica());
99                 }
100         }
101 }
102
103
104         void 
105 SG_Node::
106 Destruct()
107 {
108         // Not entirely sure what Destruct() expects to happen.
109         // I think it probably means just to call the DestructionCallback
110         // in the right order on all the children - rather than free any memory
111         
112         // We'll delete m_parent_relation now anyway.
113         
114         delete(m_parent_relation);
115         m_parent_relation = NULL;               
116
117         if (m_children.begin() != m_children.end())
118         {
119                 NodeList::iterator childit;
120                 for (childit = m_children.begin();childit!=m_children.end();++childit)
121                 {
122                         // call the SG_Node destruct method on each of our children }-)
123                         (*childit)->Destruct();
124                 }
125         }
126
127         ActivateDestructionCallback();
128 }
129
130
131         SG_Node*                        
132 SG_Node::
133 GetSGParent(
134 ) const { 
135         return m_SGparent;
136 }
137
138         void                            
139 SG_Node::
140 SetSGParent(
141         SG_Node* parent
142 ){
143         m_SGparent = parent;
144 }
145
146 const 
147         SG_Node*        
148 SG_Node::
149 GetRootSGParent(
150 ) const {
151         return (m_SGparent ? (const SG_Node*) m_SGparent->GetRootSGParent() : (const SG_Node*) this);
152 }
153
154
155         void 
156 SG_Node::
157 DisconnectFromParent(
158 ){
159         if (m_SGparent)
160         {
161                 m_SGparent->RemoveChild(this);
162                 m_SGparent = NULL;
163         }
164
165 }
166
167
168
169 void SG_Node::AddChild(SG_Node* child)
170 {
171         m_children.push_back(child);
172         child->SetSGParent(this); // this way ?
173 }
174
175 void SG_Node::RemoveChild(SG_Node* child)
176 {
177         NodeList::iterator childfound = find(m_children.begin(),m_children.end(),child);
178
179         if (childfound != m_children.end())
180         {
181                 m_children.erase(childfound);
182         }
183 }
184
185
186
187 void SG_Node::UpdateWorldData(double time)
188 {
189         //if (!GetSGParent())
190         //      return;
191
192         if (UpdateSpatialData(GetSGParent(),time))
193                 ActivateUpdateTransformCallback();
194
195         // update children's worlddata
196         for (NodeList::iterator it = m_children.begin();it!=m_children.end();++it)
197         {
198                 (*it)->UpdateWorldData(time);
199         }
200 }
201
202
203 NodeList& SG_Node::GetSGChildren()
204 {
205         return this->m_children;
206 }
207
208
209 const NodeList& SG_Node::GetSGChildren() const
210 {
211         return this->m_children;
212 }
213
214
215 void SG_Node::ClearSGChildren()
216 {
217         m_children.clear();
218 }
219
220
221
222 void SG_Node::SetSimulatedTime(double time,bool recurse)
223 {
224
225         // update the controllers of this node.
226         SetControllerTime(time);
227
228         // update children's simulate time.
229         if (recurse)
230         {
231                 for (NodeList::iterator it = m_children.begin();it!=m_children.end();++it)
232                 {
233                         (*it)->SetSimulatedTime(time,recurse);
234                 }
235         }
236 }
237
238
239