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