Blender  V2.59
KX_ParentActuator.cpp
Go to the documentation of this file.
00001 /*
00002  * Set or remove an objects parent
00003  *
00004  * $Id: KX_ParentActuator.cpp 36523 2011-05-06 20:18:42Z blendix $
00005  *
00006  * ***** BEGIN GPL LICENSE BLOCK *****
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * as published by the Free Software Foundation; either version 2
00011  * of the License, or (at your option) any later version. The Blender
00012  * Foundation also sells licenses for use in proprietary software under
00013  * the Blender License.  See http://www.blender.org/BL/ for information
00014  * about this.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00024  *
00025  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00026  * All rights reserved.
00027  *
00028  * The Original Code is: all of this file.
00029  *
00030  * Contributor(s): none yet.
00031  *
00032  * ***** END GPL LICENSE BLOCK *****
00033  */
00034 
00040 #include "KX_ParentActuator.h"
00041 #include "KX_GameObject.h"
00042 #include "KX_PythonInit.h"
00043 
00044 #include "PyObjectPlus.h" 
00045 
00046 /* ------------------------------------------------------------------------- */
00047 /* Native functions                                                          */
00048 /* ------------------------------------------------------------------------- */
00049 
00050 KX_ParentActuator::KX_ParentActuator(SCA_IObject *gameobj, 
00051                                                                          int mode,
00052                                                                          bool addToCompound,
00053                                                                          bool ghost,
00054                                                                          SCA_IObject *ob)
00055         : SCA_IActuator(gameobj, KX_ACT_PARENT),
00056           m_mode(mode),
00057           m_addToCompound(addToCompound),
00058           m_ghost(ghost),
00059           m_ob(ob)
00060 {
00061         if (m_ob)
00062                 m_ob->RegisterActuator(this);
00063 } 
00064 
00065 
00066 
00067 KX_ParentActuator::~KX_ParentActuator()
00068 {
00069         if (m_ob)
00070                 m_ob->UnregisterActuator(this);
00071 } 
00072 
00073 
00074 
00075 CValue* KX_ParentActuator::GetReplica()
00076 {
00077         KX_ParentActuator* replica = new KX_ParentActuator(*this);
00078         // replication just copy the m_base pointer => common random generator
00079         replica->ProcessReplica();
00080         return replica;
00081 }
00082 
00083 void KX_ParentActuator::ProcessReplica()
00084 {
00085         if (m_ob)
00086                 m_ob->RegisterActuator(this);
00087         SCA_IActuator::ProcessReplica();
00088 }
00089 
00090 
00091 bool KX_ParentActuator::UnlinkObject(SCA_IObject* clientobj)
00092 {
00093         if (clientobj == m_ob)
00094         {
00095                 // this object is being deleted, we cannot continue to track it.
00096                 m_ob = NULL;
00097                 return true;
00098         }
00099         return false;
00100 }
00101 
00102 void KX_ParentActuator::Relink(CTR_Map<CTR_HashedPtr, void*> *obj_map)
00103 {
00104         void **h_obj = (*obj_map)[m_ob];
00105         if (h_obj) {
00106                 if (m_ob)
00107                         m_ob->UnregisterActuator(this);
00108                 m_ob = (SCA_IObject*)(*h_obj);
00109                 m_ob->RegisterActuator(this);
00110         }
00111 }
00112 
00113 
00114 
00115 bool KX_ParentActuator::Update()
00116 {
00117         bool bNegativeEvent = IsNegativeEvent();
00118         RemoveAllEvents();
00119 
00120         if (bNegativeEvent)
00121                 return false; // do nothing on negative events
00122 
00123         KX_GameObject *obj = (KX_GameObject*) GetParent();
00124         KX_Scene *scene = KX_GetActiveScene();
00125         switch (m_mode) {
00126                 case KX_PARENT_SET:
00127                         if (m_ob)
00128                                 obj->SetParent(scene, (KX_GameObject*)m_ob, m_addToCompound, m_ghost);
00129                         break;
00130                 case KX_PARENT_REMOVE:
00131                         obj->RemoveParent(scene);
00132                         break;
00133         };
00134         
00135         return false;
00136 }
00137 
00138 #ifdef WITH_PYTHON
00139 
00140 /* ------------------------------------------------------------------------- */
00141 /* Python functions                                                          */
00142 /* ------------------------------------------------------------------------- */
00143 
00144 /* Integration hooks ------------------------------------------------------- */
00145 PyTypeObject KX_ParentActuator::Type = {
00146         PyVarObject_HEAD_INIT(NULL, 0)
00147         "KX_ParentActuator",
00148         sizeof(PyObjectPlus_Proxy),
00149         0,
00150         py_base_dealloc,
00151         0,
00152         0,
00153         0,
00154         0,
00155         py_base_repr,
00156         0,0,0,0,0,0,0,0,0,
00157         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00158         0,0,0,0,0,0,0,
00159         Methods,
00160         0,
00161         0,
00162         &SCA_IActuator::Type,
00163         0,0,0,0,0,0,
00164         py_base_new
00165 };
00166 
00167 PyMethodDef KX_ParentActuator::Methods[] = {
00168         {NULL,NULL} //Sentinel
00169 };
00170 
00171 PyAttributeDef KX_ParentActuator::Attributes[] = {
00172         KX_PYATTRIBUTE_RW_FUNCTION("object", KX_ParentActuator, pyattr_get_object, pyattr_set_object),
00173         KX_PYATTRIBUTE_INT_RW("mode", KX_PARENT_NODEF+1, KX_PARENT_MAX-1, true, KX_ParentActuator, m_mode),
00174         KX_PYATTRIBUTE_BOOL_RW("compound", KX_ParentActuator, m_addToCompound),
00175         KX_PYATTRIBUTE_BOOL_RW("ghost", KX_ParentActuator, m_ghost),
00176         { NULL }        //Sentinel
00177 };
00178 
00179 PyObject* KX_ParentActuator::pyattr_get_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
00180 {
00181         KX_ParentActuator* actuator = static_cast<KX_ParentActuator*>(self);
00182         if (!actuator->m_ob)    
00183                 Py_RETURN_NONE;
00184         else
00185                 return actuator->m_ob->GetProxy();
00186 }
00187 
00188 int KX_ParentActuator::pyattr_set_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
00189 {
00190         KX_ParentActuator* actuator = static_cast<KX_ParentActuator*>(self);
00191         KX_GameObject *gameobj;
00192                 
00193         if (!ConvertPythonToGameObject(value, &gameobj, true, "actuator.object = value: KX_ParentActuator"))
00194                 return PY_SET_ATTR_FAIL; // ConvertPythonToGameObject sets the error
00195                 
00196         if (actuator->m_ob != NULL)
00197                 actuator->m_ob->UnregisterActuator(actuator);   
00198 
00199         actuator->m_ob = (SCA_IObject*) gameobj;
00200                 
00201         if (actuator->m_ob)
00202                 actuator->m_ob->RegisterActuator(actuator);
00203                 
00204         return PY_SET_ATTR_SUCCESS;
00205 }
00206 
00207 #endif // WITH_PYTHON
00208 
00209 /* eof */