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