Blender V2.61 - r43446

SCA_IObject.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00032 #include <iostream>
00033 #include <algorithm>
00034 
00035 #include "SCA_IObject.h"
00036 #include "SCA_ISensor.h"
00037 #include "SCA_IController.h"
00038 #include "SCA_IActuator.h"
00039 #include "MT_Point3.h"
00040 #include "ListValue.h"
00041 
00042 MT_Point3 SCA_IObject::m_sDummy=MT_Point3(0,0,0);
00043 SG_QList SCA_IObject::m_activeBookmarkedControllers;
00044 
00045 SCA_IObject::SCA_IObject():
00046     CValue(),
00047     m_initState(0),
00048     m_state(0),
00049     m_firstState(NULL)
00050 {
00051     m_suspended = false;
00052 }
00053 
00054 SCA_IObject::~SCA_IObject()
00055 {
00056     SCA_SensorList::iterator its;
00057     for (its = m_sensors.begin(); !(its == m_sensors.end()); ++its)
00058     {
00059         //Use Delete for sensor to ensure proper cleaning
00060         (*its)->Delete();
00061         //((CValue*)(*its))->Release();
00062     }
00063     SCA_ControllerList::iterator itc; 
00064     for (itc = m_controllers.begin(); !(itc == m_controllers.end()); ++itc)
00065     {
00066         //Use Delete for controller to ensure proper cleaning (expression controller)
00067         (*itc)->Delete();
00068         //((CValue*)(*itc))->Release();
00069     }
00070     SCA_ActuatorList::iterator ita;
00071     for (ita = m_registeredActuators.begin(); !(ita==m_registeredActuators.end()); ++ita)
00072     {
00073         (*ita)->UnlinkObject(this);
00074     }
00075     for (ita = m_actuators.begin(); !(ita==m_actuators.end()); ++ita)
00076     {
00077         (*ita)->Delete();
00078     }
00079 
00080     SCA_ObjectList::iterator ito;
00081     for (ito = m_registeredObjects.begin(); !(ito==m_registeredObjects.end()); ++ito)
00082     {
00083         (*ito)->UnlinkObject(this);
00084     }
00085 
00086     //T_InterpolatorList::iterator i;
00087     //for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) {
00088     //  delete *i;
00089     //}
00090 }
00091 
00092 void SCA_IObject::AddSensor(SCA_ISensor* act)
00093 {
00094     act->AddRef();
00095     m_sensors.push_back(act);
00096 }
00097 
00098 
00099 
00100 void SCA_IObject::AddController(SCA_IController* act)
00101 {
00102     act->AddRef();
00103     m_controllers.push_back(act);
00104 }
00105 
00106 
00107 
00108 void SCA_IObject::AddActuator(SCA_IActuator* act)
00109 {
00110     act->AddRef();
00111     m_actuators.push_back(act);
00112 }
00113 
00114 void SCA_IObject::RegisterActuator(SCA_IActuator* act)
00115 {
00116     // don't increase ref count, it would create dead lock
00117     m_registeredActuators.push_back(act);
00118 }
00119 
00120 void SCA_IObject::UnregisterActuator(SCA_IActuator* act)
00121 {
00122     SCA_ActuatorList::iterator ita;
00123     for (ita = m_registeredActuators.begin(); ita != m_registeredActuators.end(); ++ita)
00124     {
00125         if ((*ita) == act) {
00126             (*ita) = m_registeredActuators.back();
00127             m_registeredActuators.pop_back();
00128             break;
00129         }
00130     }
00131 }
00132 
00133 void SCA_IObject::RegisterObject(SCA_IObject* obj)
00134 {
00135     // one object may be registered multiple times via constraint target
00136     // store multiple reference, this will serve as registration counter
00137     m_registeredObjects.push_back(obj);
00138 }
00139 
00140 void SCA_IObject::UnregisterObject(SCA_IObject* obj)
00141 {
00142     SCA_ObjectList::iterator ito;
00143     for (ito = m_registeredObjects.begin(); ito != m_registeredObjects.end(); ++ito)
00144     {
00145         if ((*ito) == obj) {
00146             (*ito) = m_registeredObjects.back();
00147             m_registeredObjects.pop_back();
00148             break;
00149         }
00150     }
00151 }
00152 
00153 void SCA_IObject::ReParentLogic()
00154 {
00155     SCA_ActuatorList& oldactuators  = GetActuators();
00156     int act = 0;
00157     SCA_ActuatorList::iterator ita;
00158     for (ita = oldactuators.begin(); !(ita==oldactuators.end()); ++ita)
00159     {
00160         SCA_IActuator* newactuator = (SCA_IActuator*) (*ita)->GetReplica();
00161         newactuator->ReParent(this);
00162         // actuators are initially not connected to any controller
00163         newactuator->SetActive(false);
00164         newactuator->ClrLink();
00165         oldactuators[act++] = newactuator;
00166     }
00167 
00168     SCA_ControllerList& oldcontrollers = GetControllers();
00169     int con = 0;
00170     SCA_ControllerList::iterator itc;
00171     for (itc = oldcontrollers.begin(); !(itc==oldcontrollers.end()); ++itc)
00172     {
00173         SCA_IController* newcontroller = (SCA_IController*)(*itc)->GetReplica();
00174         newcontroller->ReParent(this);
00175         newcontroller->SetActive(false);
00176         oldcontrollers[con++]=newcontroller;
00177 
00178     }
00179     // convert sensors last so that actuators are already available for Actuator sensor
00180     SCA_SensorList& oldsensors = GetSensors();
00181     int sen = 0;
00182     SCA_SensorList::iterator its;
00183     for (its = oldsensors.begin(); !(its==oldsensors.end()); ++its)
00184     {
00185         SCA_ISensor* newsensor = (SCA_ISensor*)(*its)->GetReplica();
00186         newsensor->ReParent(this);
00187         newsensor->SetActive(false);
00188         // sensors are initially not connected to any controller
00189         newsensor->ClrLink();
00190         oldsensors[sen++] = newsensor;
00191     }
00192 
00193     // a new object cannot be client of any actuator
00194     m_registeredActuators.clear();
00195     m_registeredObjects.clear();
00196 }
00197 
00198 
00199 
00200 SCA_ISensor* SCA_IObject::FindSensor(const STR_String& sensorname)
00201 {
00202     SCA_ISensor* foundsensor = NULL;
00203 
00204     for (SCA_SensorList::iterator its = m_sensors.begin();!(its==m_sensors.end());++its)
00205     {
00206         if ((*its)->GetName() == sensorname)
00207         {
00208             foundsensor = (*its);
00209             break;
00210         }
00211     }
00212     return foundsensor;
00213 }
00214 
00215 
00216 
00217 SCA_IController* SCA_IObject::FindController(const STR_String& controllername)
00218 {
00219     SCA_IController* foundcontroller = NULL;
00220 
00221     for (SCA_ControllerList::iterator itc = m_controllers.begin();!(itc==m_controllers.end());++itc)
00222     {
00223         if ((*itc)->GetName() == controllername)
00224         {
00225             foundcontroller = (*itc);
00226             break;
00227         }   
00228     }
00229     return foundcontroller;
00230 }
00231 
00232 
00233 
00234 SCA_IActuator* SCA_IObject::FindActuator(const STR_String& actuatorname)
00235 {
00236     SCA_IActuator* foundactuator = NULL;
00237 
00238     for (SCA_ActuatorList::iterator ita = m_actuators.begin();!(ita==m_actuators.end());++ita)
00239     {
00240         if ((*ita)->GetName() == actuatorname)
00241         {
00242             foundactuator = (*ita);
00243             break;
00244         }
00245     }
00246 
00247     return foundactuator;
00248 }
00249 
00250 
00251 void SCA_IObject::Suspend()
00252 {
00253     if ((!m_ignore_activity_culling) 
00254         && (!m_suspended))  {
00255         m_suspended = true;
00256         /* flag suspend for all sensors */
00257         SCA_SensorList::iterator i = m_sensors.begin();
00258         while (i != m_sensors.end()) {
00259             (*i)->Suspend();
00260             ++i;
00261         }
00262     }
00263 }
00264 
00265 
00266 
00267 void SCA_IObject::Resume(void)
00268 {
00269     if (m_suspended) {
00270         m_suspended = false;
00271         /* unflag suspend for all sensors */
00272         SCA_SensorList::iterator i = m_sensors.begin();
00273         while (i != m_sensors.end()) {
00274             (*i)->Resume();
00275             ++i;
00276         }
00277     }
00278 }
00279 
00280 void SCA_IObject::SetState(unsigned int state)
00281 {
00282     unsigned int tmpstate;
00283     SCA_ControllerList::iterator contit;
00284 
00285     // we will update the state in two steps:
00286     // 1) set the new state bits that are 1
00287     // 2) clr the new state bits that are 0
00288     // This to ensure continuity if a sensor is attached to two states
00289     // that are switching state: no need to deactive and reactive the sensor 
00290     
00291     tmpstate = m_state | state;
00292     if (tmpstate != m_state)
00293     {
00294         // update the status of the controllers
00295         for (contit = m_controllers.begin(); contit != m_controllers.end(); ++contit)
00296         {
00297             (*contit)->ApplyState(tmpstate);
00298         }
00299     }
00300     m_state = state;
00301     if (m_state != tmpstate)
00302     {
00303         for (contit = m_controllers.begin(); contit != m_controllers.end(); ++contit)
00304         {
00305             (*contit)->ApplyState(m_state);
00306         }
00307     }
00308 }
00309 
00310 #ifdef WITH_PYTHON
00311 
00312 /* ------------------------------------------------------------------------- */
00313 /* Python functions                                                          */
00314 /* ------------------------------------------------------------------------- */
00315 
00316 /* Integration hooks ------------------------------------------------------- */
00317 PyTypeObject SCA_IObject::Type = {
00318     PyVarObject_HEAD_INIT(NULL, 0)
00319     "SCA_IObject",
00320     sizeof(PyObjectPlus_Proxy),
00321     0,
00322     py_base_dealloc,
00323     0,
00324     0,
00325     0,
00326     0,
00327     py_base_repr,
00328     0,0,0,0,0,0,0,0,0,
00329     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00330     0,0,0,0,0,0,0,
00331     Methods,
00332     0,
00333     0,
00334     &CValue::Type,
00335     0,0,0,0,0,0,
00336     py_base_new
00337 };
00338 
00339 PyMethodDef SCA_IObject::Methods[] = {
00340     //{"setOrientation", (PyCFunction) SCA_IObject::sPySetOrientation, METH_VARARGS},
00341     //{"getOrientation", (PyCFunction) SCA_IObject::sPyGetOrientation, METH_VARARGS},
00342     {NULL,NULL} //Sentinel
00343 };
00344 
00345 PyAttributeDef SCA_IObject::Attributes[] = {
00346     { NULL }    //Sentinel
00347 };
00348 
00349 #endif // WITH_PYTHON