svn merge -r 14236:14505 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / gameengine / Ketsji / KX_SceneActuator.cpp
1 /**
2 * Set scene/camera stuff
3 *
4 * $Id$
5 *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
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 LICENSE BLOCK *****
30 */
31
32 #include "SCA_IActuator.h"
33 #include "KX_SceneActuator.h"
34 #include <iostream>
35 #include "KX_Scene.h"
36 #include "KX_Camera.h"
37 #include "KX_KetsjiEngine.h"
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 /* ------------------------------------------------------------------------- */
44 /* Native functions                                                          */
45 /* ------------------------------------------------------------------------- */
46
47 KX_SceneActuator::KX_SceneActuator(SCA_IObject *gameobj, 
48                                                                    int mode,
49                                                                    KX_Scene *scene,
50                                                                    KX_KetsjiEngine* ketsjiEngine,
51                                                                    const STR_String& nextSceneName,
52                                                                    KX_Camera* camera,
53                                                                    PyTypeObject* T)
54                                                                    : SCA_IActuator(gameobj, T)
55 {
56         m_mode = mode;
57         m_scene  = scene;
58         m_KetsjiEngine=ketsjiEngine;
59         m_camera = camera;
60         m_nextSceneName = nextSceneName;
61 } /* End of constructor */
62
63
64
65 KX_SceneActuator::~KX_SceneActuator()
66
67         // there's nothing to be done here, really....
68 } /* end of destructor */
69
70
71
72 CValue* KX_SceneActuator::GetReplica()
73 {
74         KX_SceneActuator* replica = new KX_SceneActuator(*this);
75         replica->ProcessReplica();
76         // this will copy properties and so on...
77         CValue::AddDataToReplica(replica);
78         
79         return replica;
80 }
81
82
83
84 bool KX_SceneActuator::Update()
85 {
86         // bool result = false; /*unused*/
87         bool bNegativeEvent = IsNegativeEvent();
88         RemoveAllEvents();
89
90         if (bNegativeEvent)
91                 return false; // do nothing on negative events
92
93         switch (m_mode)
94         {
95         case KX_SCENE_RESTART:
96                 {
97                         m_KetsjiEngine->ReplaceScene(m_scene->GetName(),m_scene->GetName());
98                         break;
99                 }
100         case KX_SCENE_SET_CAMERA:
101                 if (m_camera)
102                 {
103                         m_scene->SetActiveCamera(m_camera);
104                 }
105                 else
106                 {
107                         // if no camera is set and the parent object is a camera, use it as the camera
108                         SCA_IObject* parent = GetParent();
109                         if (parent->isA(&KX_Camera::Type))
110                         {
111                                 m_scene->SetActiveCamera((KX_Camera*)parent);
112                         }
113                 }
114                 break;
115         default:
116                 break;
117         }
118         
119         if (!m_nextSceneName.Length())
120                 return false;
121         
122         switch (m_mode)
123         {
124         case KX_SCENE_SET_SCENE:
125                 {
126                         m_KetsjiEngine->ReplaceScene(m_scene->GetName(),m_nextSceneName);
127                         break;
128                 }
129         case KX_SCENE_ADD_FRONT_SCENE:
130                 {
131                         bool overlay=true;
132                         m_KetsjiEngine->ConvertAndAddScene(m_nextSceneName,overlay);
133                         break;
134                 }
135         case KX_SCENE_ADD_BACK_SCENE:
136                 {
137                         bool overlay=false;
138                         m_KetsjiEngine->ConvertAndAddScene(m_nextSceneName,overlay);
139                         break;
140                 }
141         case KX_SCENE_REMOVE_SCENE:
142                 {
143                         m_KetsjiEngine->RemoveScene(m_nextSceneName);
144                         break;
145                 }
146         case KX_SCENE_SUSPEND:
147                 {
148                         m_KetsjiEngine->SuspendScene(m_nextSceneName);
149                         break;
150                 }
151         case KX_SCENE_RESUME:
152                 {
153                         m_KetsjiEngine->ResumeScene(m_nextSceneName);
154                         break;
155                 }
156         default:
157                 ; /* do nothing? this is an internal error !!! */
158         }
159         
160         return false;
161 }
162
163
164
165 /*  returns a camera if the name is valid */
166 KX_Camera* KX_SceneActuator::FindCamera(char *camName)
167 {
168         KX_SceneList* sl = m_KetsjiEngine->CurrentScenes();
169         STR_String name = STR_String(camName);
170         KX_SceneList::iterator it = sl->begin();
171         KX_Camera* cam = NULL;
172
173         while ((it != sl->end()) && (!cam))
174         {
175                 cam = (*it)->FindCamera(name);
176                 it++;
177         }
178
179         return cam;
180 }
181
182
183
184 KX_Scene* KX_SceneActuator::FindScene(char * sceneName)
185 {
186         return m_KetsjiEngine->FindScene(sceneName);
187 }
188
189
190
191
192 /* ------------------------------------------------------------------------- */
193 /* Python functions                                                          */
194 /* ------------------------------------------------------------------------- */
195
196 /* Integration hooks ------------------------------------------------------- */
197 PyTypeObject KX_SceneActuator::Type = {
198         PyObject_HEAD_INIT(&PyType_Type)
199                 0,
200                 "KX_SceneActuator",
201                 sizeof(KX_SceneActuator),
202                 0,
203                 PyDestructor,
204                 0,
205                 __getattr,
206                 __setattr,
207                 0, //&MyPyCompare,
208                 __repr,
209                 0, //&cvalue_as_number,
210                 0,
211                 0,
212                 0,
213                 0
214 };
215
216
217
218 PyParentObject KX_SceneActuator::Parents[] =
219 {
220         &KX_SceneActuator::Type,
221                 &SCA_IActuator::Type,
222                 &SCA_ILogicBrick::Type,
223                 &CValue::Type,
224                 NULL
225 };
226
227
228
229 PyMethodDef KX_SceneActuator::Methods[] =
230 {
231         {"setUseRestart", (PyCFunction) KX_SceneActuator::sPySetUseRestart, METH_VARARGS, SetUseRestart_doc},
232         {"setScene",      (PyCFunction) KX_SceneActuator::sPySetScene, METH_VARARGS, SetScene_doc},
233         {"setCamera",     (PyCFunction) KX_SceneActuator::sPySetCamera, METH_VARARGS, SetCamera_doc},
234         {"getUseRestart", (PyCFunction) KX_SceneActuator::sPyGetUseRestart, METH_VARARGS, GetUseRestart_doc},
235         {"getScene",      (PyCFunction) KX_SceneActuator::sPyGetScene, METH_VARARGS, GetScene_doc},
236         {"getCamera",     (PyCFunction) KX_SceneActuator::sPyGetCamera, METH_VARARGS, GetCamera_doc},
237         {NULL,NULL} //Sentinel
238 };
239
240
241
242 PyObject* KX_SceneActuator::_getattr(const STR_String& attr)
243 {
244         _getattr_up(SCA_IActuator);
245 }
246
247
248
249 /* 2. setUseRestart--------------------------------------------------------- */
250 char KX_SceneActuator::SetUseRestart_doc[] = 
251 "setUseRestart(flag)\n"
252 "\t- flag: 0 or 1.\n"
253 "\tSet flag to 1 to restart the scene.\n" ;
254 PyObject* KX_SceneActuator::PySetUseRestart(PyObject* self, 
255                                                                                         PyObject* args, 
256                                                                                         PyObject* kwds)
257 {
258         int boolArg;
259         
260         if (!PyArg_ParseTuple(args, "i", &boolArg))
261         {
262                 return NULL;
263         }
264         
265         m_restart = boolArg != 0;
266         
267         Py_Return;
268 }
269
270
271
272 /* 3. getUseRestart:                                                         */
273 char KX_SceneActuator::GetUseRestart_doc[] = 
274 "getUseRestart()\n"
275 "\tReturn whether the scene will be restarted.\n" ;
276 PyObject* KX_SceneActuator::PyGetUseRestart(PyObject* self, 
277                                                                                         PyObject* args, 
278                                                                                         PyObject* kwds)
279 {
280         return PyInt_FromLong(!(m_restart == 0));
281 }
282
283
284
285 /* 4. set scene------------------------------------------------------------- */
286 char KX_SceneActuator::SetScene_doc[] = 
287 "setScene(scene)\n"
288 "\t- scene: string\n"
289 "\tSet the name of scene the actuator will switch to.\n" ;
290 PyObject* KX_SceneActuator::PySetScene(PyObject* self, 
291                                                                            PyObject* args, 
292                                                                            PyObject* kwds)
293 {
294         /* one argument: a scene, ignore the rest */
295         char *scene_name;
296
297         if(!PyArg_ParseTuple(args, "s", &scene_name))
298         {
299                 return NULL;
300         }
301
302         /* Scene switch is done by name. */
303         m_nextSceneName = scene_name;
304
305         Py_Return;
306 }
307
308
309
310 /* 5. getScene:                                                              */
311 char KX_SceneActuator::GetScene_doc[] = 
312 "getScene()\n"
313 "\tReturn the name of the scene the actuator wants to switch to.\n" ;
314 PyObject* KX_SceneActuator::PyGetScene(PyObject* self, 
315                                                                            PyObject* args, 
316                                                                            PyObject* kwds)
317 {
318         return PyString_FromString(m_nextSceneName);
319 }
320
321
322
323 /* 6. set camera------------------------------------------------------------ */
324 char KX_SceneActuator::SetCamera_doc[] = 
325 "setCamera(camera)\n"
326 "\t- camera: string\n"
327 "\tSet the camera to switch to.\n" ;
328 PyObject* KX_SceneActuator::PySetCamera(PyObject* self, 
329                                                                                 PyObject* args, 
330                                                                                 PyObject* kwds)
331 {
332         PyObject *cam;
333         if (PyArg_ParseTuple(args, "O!", &KX_Camera::Type, &cam))
334         {
335                 m_camera = (KX_Camera*) cam;
336                 Py_Return;
337         }
338         PyErr_Clear();
339
340         /* one argument: a scene, ignore the rest */
341         char *camName;
342         if(!PyArg_ParseTuple(args, "s", &camName))
343         {
344                 return NULL;
345         }
346
347         KX_Camera *camOb = FindCamera(camName);
348         if (camOb) m_camera = camOb;
349
350         Py_Return;
351 }
352
353
354
355 /* 7. getCamera:                                                             */
356 char KX_SceneActuator::GetCamera_doc[] = 
357 "getCamera()\n"
358 "\tReturn the name of the camera to switch to.\n" ;
359 PyObject* KX_SceneActuator::PyGetCamera(PyObject* self, 
360                                                                                 PyObject* args, 
361                                                                                 PyObject* kwds)
362 {
363         return PyString_FromString(m_camera->GetName());
364 }
365 /* eof */