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