Blender V2.61 - r43446

KX_ParentActuator.cpp

Go to the documentation of this file.
00001 /*
00002  * Set or remove an objects parent
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 "KX_ParentActuator.h"
00037 #include "KX_GameObject.h"
00038 #include "KX_PythonInit.h"
00039 
00040 #include "PyObjectPlus.h" 
00041 
00042 /* ------------------------------------------------------------------------- */
00043 /* Native functions                                                          */
00044 /* ------------------------------------------------------------------------- */
00045 
00046 KX_ParentActuator::KX_ParentActuator(SCA_IObject *gameobj, 
00047                                      int mode,
00048                                      bool addToCompound,
00049                                      bool ghost,
00050                                      SCA_IObject *ob)
00051     : SCA_IActuator(gameobj, KX_ACT_PARENT),
00052       m_mode(mode),
00053       m_addToCompound(addToCompound),
00054       m_ghost(ghost),
00055       m_ob(ob)
00056 {
00057     if (m_ob)
00058         m_ob->RegisterActuator(this);
00059 } 
00060 
00061 
00062 
00063 KX_ParentActuator::~KX_ParentActuator()
00064 {
00065     if (m_ob)
00066         m_ob->UnregisterActuator(this);
00067 } 
00068 
00069 
00070 
00071 CValue* KX_ParentActuator::GetReplica()
00072 {
00073     KX_ParentActuator* replica = new KX_ParentActuator(*this);
00074     // replication just copy the m_base pointer => common random generator
00075     replica->ProcessReplica();
00076     return replica;
00077 }
00078 
00079 void KX_ParentActuator::ProcessReplica()
00080 {
00081     if (m_ob)
00082         m_ob->RegisterActuator(this);
00083     SCA_IActuator::ProcessReplica();
00084 }
00085 
00086 
00087 bool KX_ParentActuator::UnlinkObject(SCA_IObject* clientobj)
00088 {
00089     if (clientobj == m_ob)
00090     {
00091         // this object is being deleted, we cannot continue to track it.
00092         m_ob = NULL;
00093         return true;
00094     }
00095     return false;
00096 }
00097 
00098 void KX_ParentActuator::Relink(CTR_Map<CTR_HashedPtr, void*> *obj_map)
00099 {
00100     void **h_obj = (*obj_map)[m_ob];
00101     if (h_obj) {
00102         if (m_ob)
00103             m_ob->UnregisterActuator(this);
00104         m_ob = (SCA_IObject*)(*h_obj);
00105         m_ob->RegisterActuator(this);
00106     }
00107 }
00108 
00109 
00110 
00111 bool KX_ParentActuator::Update()
00112 {
00113     bool bNegativeEvent = IsNegativeEvent();
00114     RemoveAllEvents();
00115 
00116     if (bNegativeEvent)
00117         return false; // do nothing on negative events
00118 
00119     KX_GameObject *obj = (KX_GameObject*) GetParent();
00120     KX_Scene *scene = KX_GetActiveScene();
00121     switch (m_mode) {
00122         case KX_PARENT_SET:
00123             if (m_ob)
00124                 obj->SetParent(scene, (KX_GameObject*)m_ob, m_addToCompound, m_ghost);
00125             break;
00126         case KX_PARENT_REMOVE:
00127             obj->RemoveParent(scene);
00128             break;
00129     };
00130     
00131     return false;
00132 }
00133 
00134 #ifdef WITH_PYTHON
00135 
00136 /* ------------------------------------------------------------------------- */
00137 /* Python functions                                                          */
00138 /* ------------------------------------------------------------------------- */
00139 
00140 /* Integration hooks ------------------------------------------------------- */
00141 PyTypeObject KX_ParentActuator::Type = {
00142     PyVarObject_HEAD_INIT(NULL, 0)
00143     "KX_ParentActuator",
00144     sizeof(PyObjectPlus_Proxy),
00145     0,
00146     py_base_dealloc,
00147     0,
00148     0,
00149     0,
00150     0,
00151     py_base_repr,
00152     0,0,0,0,0,0,0,0,0,
00153     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00154     0,0,0,0,0,0,0,
00155     Methods,
00156     0,
00157     0,
00158     &SCA_IActuator::Type,
00159     0,0,0,0,0,0,
00160     py_base_new
00161 };
00162 
00163 PyMethodDef KX_ParentActuator::Methods[] = {
00164     {NULL,NULL} //Sentinel
00165 };
00166 
00167 PyAttributeDef KX_ParentActuator::Attributes[] = {
00168     KX_PYATTRIBUTE_RW_FUNCTION("object", KX_ParentActuator, pyattr_get_object, pyattr_set_object),
00169     KX_PYATTRIBUTE_INT_RW("mode", KX_PARENT_NODEF+1, KX_PARENT_MAX-1, true, KX_ParentActuator, m_mode),
00170     KX_PYATTRIBUTE_BOOL_RW("compound", KX_ParentActuator, m_addToCompound),
00171     KX_PYATTRIBUTE_BOOL_RW("ghost", KX_ParentActuator, m_ghost),
00172     { NULL }    //Sentinel
00173 };
00174 
00175 PyObject* KX_ParentActuator::pyattr_get_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
00176 {
00177     KX_ParentActuator* actuator = static_cast<KX_ParentActuator*>(self);
00178     if (!actuator->m_ob)    
00179         Py_RETURN_NONE;
00180     else
00181         return actuator->m_ob->GetProxy();
00182 }
00183 
00184 int KX_ParentActuator::pyattr_set_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
00185 {
00186     KX_ParentActuator* actuator = static_cast<KX_ParentActuator*>(self);
00187     KX_GameObject *gameobj;
00188         
00189     if (!ConvertPythonToGameObject(value, &gameobj, true, "actuator.object = value: KX_ParentActuator"))
00190         return PY_SET_ATTR_FAIL; // ConvertPythonToGameObject sets the error
00191         
00192     if (actuator->m_ob != NULL)
00193         actuator->m_ob->UnregisterActuator(actuator);   
00194 
00195     actuator->m_ob = (SCA_IObject*) gameobj;
00196         
00197     if (actuator->m_ob)
00198         actuator->m_ob->RegisterActuator(actuator);
00199         
00200     return PY_SET_ATTR_SUCCESS;
00201 }
00202 
00203 #endif // WITH_PYTHON
00204 
00205 /* eof */