Blender V2.61 - r43446
|
00001 /* 00002 * Set scene/camera stuff 00003 * 00004 * 00005 * ***** BEGIN GPL LICENSE BLOCK ***** 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU General Public License 00009 * as published by the Free Software Foundation; either version 2 00010 * of the License, or (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software Foundation, 00019 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00020 * 00021 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00022 * All rights reserved. 00023 * 00024 * The Original Code is: all of this file. 00025 * 00026 * Contributor(s): none yet. 00027 * 00028 * ***** END GPL LICENSE BLOCK ***** 00029 */ 00030 00036 #include "SCA_IActuator.h" 00037 #include "KX_SceneActuator.h" 00038 #include <iostream> 00039 #include "KX_Scene.h" 00040 #include "KX_Camera.h" 00041 #include "KX_KetsjiEngine.h" 00042 00043 /* ------------------------------------------------------------------------- */ 00044 /* Native functions */ 00045 /* ------------------------------------------------------------------------- */ 00046 00047 KX_SceneActuator::KX_SceneActuator(SCA_IObject *gameobj, 00048 int mode, 00049 KX_Scene *scene, 00050 KX_KetsjiEngine* ketsjiEngine, 00051 const STR_String& nextSceneName, 00052 KX_Camera* camera) 00053 : SCA_IActuator(gameobj, KX_ACT_SCENE) 00054 { 00055 m_mode = mode; 00056 m_scene = scene; 00057 m_KetsjiEngine=ketsjiEngine; 00058 m_camera = camera; 00059 m_nextSceneName = nextSceneName; 00060 if (m_camera) 00061 m_camera->RegisterActuator(this); 00062 } /* End of constructor */ 00063 00064 00065 00066 KX_SceneActuator::~KX_SceneActuator() 00067 { 00068 if (m_camera) 00069 m_camera->UnregisterActuator(this); 00070 } /* end of destructor */ 00071 00072 00073 00074 CValue* KX_SceneActuator::GetReplica() 00075 { 00076 KX_SceneActuator* replica = new KX_SceneActuator(*this); 00077 replica->ProcessReplica(); 00078 return replica; 00079 } 00080 00081 void KX_SceneActuator::ProcessReplica() 00082 { 00083 if (m_camera) 00084 m_camera->RegisterActuator(this); 00085 SCA_IActuator::ProcessReplica(); 00086 } 00087 00088 bool KX_SceneActuator::UnlinkObject(SCA_IObject* clientobj) 00089 { 00090 if (clientobj == (SCA_IObject*)m_camera) 00091 { 00092 // this object is being deleted, we cannot continue to track it. 00093 m_camera = NULL; 00094 return true; 00095 } 00096 return false; 00097 } 00098 00099 void KX_SceneActuator::Relink(CTR_Map<CTR_HashedPtr, void*> *obj_map) 00100 { 00101 void **h_obj = (*obj_map)[m_camera]; 00102 if (h_obj) { 00103 if (m_camera) 00104 m_camera->UnregisterActuator(this); 00105 m_camera = (KX_Camera*)(*h_obj); 00106 m_camera->RegisterActuator(this); 00107 } 00108 } 00109 00110 00111 bool KX_SceneActuator::Update() 00112 { 00113 // bool result = false; /*unused*/ 00114 bool bNegativeEvent = IsNegativeEvent(); 00115 RemoveAllEvents(); 00116 00117 if (bNegativeEvent) 00118 return false; // do nothing on negative events 00119 00120 switch (m_mode) 00121 { 00122 case KX_SCENE_RESTART: 00123 { 00124 m_KetsjiEngine->ReplaceScene(m_scene->GetName(),m_scene->GetName()); 00125 break; 00126 } 00127 case KX_SCENE_SET_CAMERA: 00128 if (m_camera) 00129 { 00130 m_scene->SetActiveCamera(m_camera); 00131 } 00132 else 00133 { 00134 // if no camera is set and the parent object is a camera, use it as the camera 00135 SCA_IObject* parent = GetParent(); 00136 if (parent->GetGameObjectType()==SCA_IObject::OBJ_CAMERA) 00137 { 00138 m_scene->SetActiveCamera((KX_Camera*)parent); 00139 } 00140 } 00141 break; 00142 default: 00143 break; 00144 } 00145 00146 if (!m_nextSceneName.Length()) 00147 return false; 00148 00149 switch (m_mode) 00150 { 00151 case KX_SCENE_SET_SCENE: 00152 { 00153 m_KetsjiEngine->ReplaceScene(m_scene->GetName(),m_nextSceneName); 00154 break; 00155 } 00156 case KX_SCENE_ADD_FRONT_SCENE: 00157 { 00158 bool overlay=true; 00159 m_KetsjiEngine->ConvertAndAddScene(m_nextSceneName,overlay); 00160 break; 00161 } 00162 case KX_SCENE_ADD_BACK_SCENE: 00163 { 00164 bool overlay=false; 00165 m_KetsjiEngine->ConvertAndAddScene(m_nextSceneName,overlay); 00166 break; 00167 } 00168 case KX_SCENE_REMOVE_SCENE: 00169 { 00170 m_KetsjiEngine->RemoveScene(m_nextSceneName); 00171 break; 00172 } 00173 case KX_SCENE_SUSPEND: 00174 { 00175 m_KetsjiEngine->SuspendScene(m_nextSceneName); 00176 break; 00177 } 00178 case KX_SCENE_RESUME: 00179 { 00180 m_KetsjiEngine->ResumeScene(m_nextSceneName); 00181 break; 00182 } 00183 default: 00184 ; /* do nothing? this is an internal error !!! */ 00185 } 00186 00187 return false; 00188 } 00189 00190 00191 00192 /* returns a camera if the name is valid */ 00193 KX_Camera* KX_SceneActuator::FindCamera(const char *camName) 00194 { 00195 KX_SceneList* sl = m_KetsjiEngine->CurrentScenes(); 00196 STR_String name = STR_String(camName); 00197 KX_SceneList::iterator it = sl->begin(); 00198 KX_Camera* cam = NULL; 00199 00200 while ((it != sl->end()) && (!cam)) 00201 { 00202 cam = (*it)->FindCamera(name); 00203 it++; 00204 } 00205 00206 return cam; 00207 } 00208 00209 00210 00211 KX_Scene* KX_SceneActuator::FindScene(const char * sceneName) 00212 { 00213 return m_KetsjiEngine->FindScene(sceneName); 00214 } 00215 00216 00217 #ifdef WITH_PYTHON 00218 00219 /* ------------------------------------------------------------------------- */ 00220 /* Python functions */ 00221 /* ------------------------------------------------------------------------- */ 00222 00223 /* Integration hooks ------------------------------------------------------- */ 00224 PyTypeObject KX_SceneActuator::Type = { 00225 PyVarObject_HEAD_INIT(NULL, 0) 00226 "KX_SceneActuator", 00227 sizeof(PyObjectPlus_Proxy), 00228 0, 00229 py_base_dealloc, 00230 0, 00231 0, 00232 0, 00233 0, 00234 py_base_repr, 00235 0,0,0,0,0,0,0,0,0, 00236 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, 00237 0,0,0,0,0,0,0, 00238 Methods, 00239 0, 00240 0, 00241 &SCA_IActuator::Type, 00242 0,0,0,0,0,0, 00243 py_base_new 00244 }; 00245 00246 PyMethodDef KX_SceneActuator::Methods[] = 00247 { 00248 {NULL,NULL} //Sentinel 00249 }; 00250 00251 PyAttributeDef KX_SceneActuator::Attributes[] = { 00252 KX_PYATTRIBUTE_STRING_RW("scene",0,MAX_ID_NAME-2,true,KX_SceneActuator,m_nextSceneName), 00253 KX_PYATTRIBUTE_RW_FUNCTION("camera",KX_SceneActuator,pyattr_get_camera,pyattr_set_camera), 00254 KX_PYATTRIBUTE_BOOL_RW("useRestart", KX_SceneActuator, m_restart), 00255 KX_PYATTRIBUTE_INT_RW("mode", KX_SCENE_NODEF+1, KX_SCENE_MAX-1, true, KX_SceneActuator, m_mode), 00256 { NULL } //Sentinel 00257 }; 00258 00259 PyObject* KX_SceneActuator::pyattr_get_camera(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef) 00260 { 00261 KX_SceneActuator* actuator = static_cast<KX_SceneActuator*>(self); 00262 if (!actuator->m_camera) 00263 Py_RETURN_NONE; 00264 00265 return actuator->m_camera->GetProxy(); 00266 } 00267 00268 int KX_SceneActuator::pyattr_set_camera(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value) 00269 { 00270 KX_SceneActuator* actuator = static_cast<KX_SceneActuator*>(self); 00271 KX_Camera *camOb; 00272 00273 if (!ConvertPythonToCamera(value, &camOb, true, "actu.camera = value: KX_SceneActuator")) 00274 return PY_SET_ATTR_FAIL; 00275 00276 if (actuator->m_camera) 00277 actuator->m_camera->UnregisterActuator(actuator); 00278 00279 if(camOb==NULL) { 00280 actuator->m_camera= NULL; 00281 } 00282 else { 00283 actuator->m_camera = camOb; 00284 actuator->m_camera->RegisterActuator(actuator); 00285 } 00286 00287 return PY_SET_ATTR_SUCCESS; 00288 } 00289 00290 #endif // WITH_PYTHON 00291 00292 /* eof */