Filling in branch from trunk
[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()
88 {
89         // bool result = false; /*unused*/
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_CAMERA:
104                 if (m_camera)
105                 {
106                         m_scene->SetActiveCamera(m_camera);
107                 }
108                 break;
109         default:
110                 break;
111         }
112         
113         if (!m_nextSceneName.Length())
114                 return false;
115         
116         switch (m_mode)
117         {
118         case KX_SCENE_SET_SCENE:
119                 {
120                         m_KetsjiEngine->ReplaceScene(m_scene->GetName(),m_nextSceneName);
121                         break;
122                 }
123         case KX_SCENE_ADD_FRONT_SCENE:
124                 {
125                         bool overlay=true;
126                         m_KetsjiEngine->ConvertAndAddScene(m_nextSceneName,overlay);
127                         break;
128                 }
129         case KX_SCENE_ADD_BACK_SCENE:
130                 {
131                         bool overlay=false;
132                         m_KetsjiEngine->ConvertAndAddScene(m_nextSceneName,overlay);
133                         break;
134                 }
135         case KX_SCENE_REMOVE_SCENE:
136                 {
137                         m_KetsjiEngine->RemoveScene(m_nextSceneName);
138                         break;
139                 }
140         case KX_SCENE_SUSPEND:
141                 {
142                         m_KetsjiEngine->SuspendScene(m_nextSceneName);
143                         break;
144                 }
145         case KX_SCENE_RESUME:
146                 {
147                         m_KetsjiEngine->ResumeScene(m_nextSceneName);
148                         break;
149                 }
150         default:
151                 ; /* do nothing? this is an internal error !!! */
152         }
153         
154         return false;
155 }
156
157
158
159 /*  returns a camera if the name is valid */
160 KX_Camera* KX_SceneActuator::FindCamera(char *camName)
161 {
162         KX_SceneList* sl = m_KetsjiEngine->CurrentScenes();
163         STR_String name = STR_String(camName);
164         KX_SceneList::iterator it = sl->begin();
165         KX_Camera* cam = NULL;
166
167         while ((it != sl->end()) && (!cam))
168         {
169                 cam = (*it)->FindCamera(name);
170                 it++;
171         }
172
173         return cam;
174 }
175
176
177
178 KX_Scene* KX_SceneActuator::FindScene(char * sceneName)
179 {
180         return m_KetsjiEngine->FindScene(sceneName);
181 }
182
183
184
185
186 /* ------------------------------------------------------------------------- */
187 /* Python functions                                                          */
188 /* ------------------------------------------------------------------------- */
189
190 /* Integration hooks ------------------------------------------------------- */
191 PyTypeObject KX_SceneActuator::Type = {
192         PyObject_HEAD_INIT(&PyType_Type)
193                 0,
194                 "KX_SceneActuator",
195                 sizeof(KX_SceneActuator),
196                 0,
197                 PyDestructor,
198                 0,
199                 __getattr,
200                 __setattr,
201                 0, //&MyPyCompare,
202                 __repr,
203                 0, //&cvalue_as_number,
204                 0,
205                 0,
206                 0,
207                 0
208 };
209
210
211
212 PyParentObject KX_SceneActuator::Parents[] =
213 {
214         &KX_SceneActuator::Type,
215                 &SCA_IActuator::Type,
216                 &SCA_ILogicBrick::Type,
217                 &CValue::Type,
218                 NULL
219 };
220
221
222
223 PyMethodDef KX_SceneActuator::Methods[] =
224 {
225         {"setUseRestart", (PyCFunction) KX_SceneActuator::sPySetUseRestart, METH_VARARGS, SetUseRestart_doc},
226         {"setScene",      (PyCFunction) KX_SceneActuator::sPySetScene, METH_VARARGS, SetScene_doc},
227         {"setCamera",     (PyCFunction) KX_SceneActuator::sPySetCamera, METH_VARARGS, SetCamera_doc},
228         {"getUseRestart", (PyCFunction) KX_SceneActuator::sPyGetUseRestart, METH_VARARGS, GetUseRestart_doc},
229         {"getScene",      (PyCFunction) KX_SceneActuator::sPyGetScene, METH_VARARGS, GetScene_doc},
230         {"getCamera",     (PyCFunction) KX_SceneActuator::sPyGetCamera, METH_VARARGS, GetCamera_doc},
231         {NULL,NULL} //Sentinel
232 };
233
234
235
236 PyObject* KX_SceneActuator::_getattr(const STR_String& attr)
237 {
238         _getattr_up(SCA_IActuator);
239 }
240
241
242
243 /* 2. setUseRestart--------------------------------------------------------- */
244 char KX_SceneActuator::SetUseRestart_doc[] = 
245 "setUseRestart(flag)\n"
246 "\t- flag: 0 or 1.\n"
247 "\tSet flag to 1 to restart the scene.\n" ;
248 PyObject* KX_SceneActuator::PySetUseRestart(PyObject* self, 
249                                                                                         PyObject* args, 
250                                                                                         PyObject* kwds)
251 {
252         int boolArg;
253         
254         if (!PyArg_ParseTuple(args, "i", &boolArg))
255         {
256                 return NULL;
257         }
258         
259         m_restart = boolArg != 0;
260         
261         Py_Return;
262 }
263
264
265
266 /* 3. getUseRestart:                                                         */
267 char KX_SceneActuator::GetUseRestart_doc[] = 
268 "getUseRestart()\n"
269 "\tReturn whether the scene will be restarted.\n" ;
270 PyObject* KX_SceneActuator::PyGetUseRestart(PyObject* self, 
271                                                                                         PyObject* args, 
272                                                                                         PyObject* kwds)
273 {
274         return PyInt_FromLong(!(m_restart == 0));
275 }
276
277
278
279 /* 4. set scene------------------------------------------------------------- */
280 char KX_SceneActuator::SetScene_doc[] = 
281 "setScene(scene)\n"
282 "\t- scene: string\n"
283 "\tSet the name of scene the actuator will switch to.\n" ;
284 PyObject* KX_SceneActuator::PySetScene(PyObject* self, 
285                                                                            PyObject* args, 
286                                                                            PyObject* kwds)
287 {
288         /* one argument: a scene, ignore the rest */
289         char *scene_name;
290
291         if(!PyArg_ParseTuple(args, "s", &scene_name))
292         {
293                 return NULL;
294         }
295
296         /* Scene switch is done by name. */
297         m_nextSceneName = scene_name;
298
299         Py_Return;
300 }
301
302
303
304 /* 5. getScene:                                                              */
305 char KX_SceneActuator::GetScene_doc[] = 
306 "getScene()\n"
307 "\tReturn the name of the scene the actuator wants to switch to.\n" ;
308 PyObject* KX_SceneActuator::PyGetScene(PyObject* self, 
309                                                                            PyObject* args, 
310                                                                            PyObject* kwds)
311 {
312         return PyString_FromString(m_nextSceneName);
313 }
314
315
316
317 /* 6. set camera------------------------------------------------------------ */
318 char KX_SceneActuator::SetCamera_doc[] = 
319 "setCamera(camera)\n"
320 "\t- camera: string\n"
321 "\tSet the camera to switch to.\n" ;
322 PyObject* KX_SceneActuator::PySetCamera(PyObject* self, 
323                                                                                 PyObject* args, 
324                                                                                 PyObject* kwds)
325 {
326         PyObject *cam;
327         if (PyArg_ParseTuple(args, "O!", &KX_Camera::Type, &cam))
328         {
329                 m_camera = (KX_Camera*) cam;
330                 Py_Return;
331         }
332         PyErr_Clear();
333
334         /* one argument: a scene, ignore the rest */
335         char *camName;
336         if(!PyArg_ParseTuple(args, "s", &camName))
337         {
338                 return NULL;
339         }
340
341         KX_Camera *camOb = FindCamera(camName);
342         if (camOb) m_camera = camOb;
343
344         Py_Return;
345 }
346
347
348
349 /* 7. getCamera:                                                             */
350 char KX_SceneActuator::GetCamera_doc[] = 
351 "getCamera()\n"
352 "\tReturn the name of the camera to switch to.\n" ;
353 PyObject* KX_SceneActuator::PyGetCamera(PyObject* self, 
354                                                                                 PyObject* args, 
355                                                                                 PyObject* kwds)
356 {
357         return PyString_FromString(m_camera->GetName());
358 }
359 /* eof */