Blender V2.61 - r43446
|
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 00033 #include <stddef.h> 00034 00035 #include "SCA_IController.h" 00036 #include "SCA_LogicManager.h" 00037 #include "SCA_IActuator.h" 00038 #include "SCA_ISensor.h" 00039 #include "PyObjectPlus.h" 00040 #include "../Ketsji/KX_PythonSeq.h" /* not nice, only need for KX_PythonSeq_CreatePyObject */ 00041 00042 #include <stdio.h> 00043 00044 SCA_IController::SCA_IController(SCA_IObject* gameobj) 00045 : 00046 SCA_ILogicBrick(gameobj), 00047 m_statemask(0), 00048 m_justActivated(false) 00049 { 00050 } 00051 00052 00053 00054 SCA_IController::~SCA_IController() 00055 { 00056 //UnlinkAllActuators(); 00057 } 00058 00059 00060 00061 std::vector<class SCA_ISensor*>& SCA_IController::GetLinkedSensors() 00062 { 00063 return m_linkedsensors; 00064 } 00065 00066 00067 00068 std::vector<class SCA_IActuator*>& SCA_IController::GetLinkedActuators() 00069 { 00070 return m_linkedactuators; 00071 } 00072 00073 00074 00075 void SCA_IController::UnlinkAllSensors() 00076 { 00077 std::vector<class SCA_ISensor*>::iterator sensit; 00078 for (sensit = m_linkedsensors.begin();!(sensit==m_linkedsensors.end());++sensit) 00079 { 00080 if (IsActive()) 00081 { 00082 (*sensit)->DecLink(); 00083 } 00084 (*sensit)->UnlinkController(this); 00085 } 00086 m_linkedsensors.clear(); 00087 } 00088 00089 00090 00091 void SCA_IController::UnlinkAllActuators() 00092 { 00093 std::vector<class SCA_IActuator*>::iterator actit; 00094 for (actit = m_linkedactuators.begin();!(actit==m_linkedactuators.end());++actit) 00095 { 00096 if (IsActive()) 00097 { 00098 (*actit)->DecLink(); 00099 } 00100 (*actit)->UnlinkController(this); 00101 } 00102 m_linkedactuators.clear(); 00103 } 00104 00105 void SCA_IController::LinkToActuator(SCA_IActuator* actua) 00106 { 00107 m_linkedactuators.push_back(actua); 00108 if (IsActive()) 00109 { 00110 actua->IncLink(); 00111 } 00112 } 00113 00114 void SCA_IController::UnlinkActuator(class SCA_IActuator* actua) 00115 { 00116 std::vector<class SCA_IActuator*>::iterator actit; 00117 for (actit = m_linkedactuators.begin();!(actit==m_linkedactuators.end());++actit) 00118 { 00119 if ((*actit) == actua) 00120 { 00121 if (IsActive()) 00122 { 00123 (*actit)->DecLink(); 00124 } 00125 *actit = m_linkedactuators.back(); 00126 m_linkedactuators.pop_back(); 00127 return; 00128 } 00129 } 00130 printf("Missing link from controller %s:%s to actuator %s:%s\n", 00131 m_gameobj->GetName().ReadPtr(), GetName().ReadPtr(), 00132 actua->GetParent()->GetName().ReadPtr(), actua->GetName().ReadPtr()); 00133 } 00134 00135 void SCA_IController::LinkToSensor(SCA_ISensor* sensor) 00136 { 00137 m_linkedsensors.push_back(sensor); 00138 if (IsActive()) 00139 { 00140 sensor->IncLink(); 00141 } 00142 } 00143 00144 void SCA_IController::UnlinkSensor(class SCA_ISensor* sensor) 00145 { 00146 std::vector<class SCA_ISensor*>::iterator sensit; 00147 for (sensit = m_linkedsensors.begin();!(sensit==m_linkedsensors.end());++sensit) 00148 { 00149 if ((*sensit) == sensor) 00150 { 00151 if (IsActive()) 00152 { 00153 sensor->DecLink(); 00154 } 00155 *sensit = m_linkedsensors.back(); 00156 m_linkedsensors.pop_back(); 00157 return; 00158 } 00159 } 00160 printf("Missing link from controller %s:%s to sensor %s:%s\n", 00161 m_gameobj->GetName().ReadPtr(), GetName().ReadPtr(), 00162 sensor->GetParent()->GetName().ReadPtr(), sensor->GetName().ReadPtr()); 00163 } 00164 00165 00166 void SCA_IController::ApplyState(unsigned int state) 00167 { 00168 std::vector<class SCA_IActuator*>::iterator actit; 00169 std::vector<class SCA_ISensor*>::iterator sensit; 00170 00171 if (m_statemask & state) 00172 { 00173 if (!IsActive()) 00174 { 00175 // reactive the controller, all the links to actuator are valid again 00176 for (actit = m_linkedactuators.begin();!(actit==m_linkedactuators.end());++actit) 00177 { 00178 (*actit)->IncLink(); 00179 } 00180 00181 for (sensit = m_linkedsensors.begin();!(sensit==m_linkedsensors.end());++sensit) 00182 { 00183 (*sensit)->IncLink(); 00184 } 00185 SetActive(true); 00186 m_justActivated = true; 00187 } 00188 } else if (IsActive()) 00189 { 00190 for (actit = m_linkedactuators.begin();!(actit==m_linkedactuators.end());++actit) 00191 { 00192 (*actit)->DecLink(); 00193 } 00194 for (sensit = m_linkedsensors.begin();!(sensit==m_linkedsensors.end());++sensit) 00195 { 00196 (*sensit)->DecLink(); 00197 } 00198 SetActive(false); 00199 m_justActivated = false; 00200 } 00201 } 00202 00203 #ifdef WITH_PYTHON 00204 00205 /* Python api */ 00206 00207 PyTypeObject SCA_IController::Type = { 00208 PyVarObject_HEAD_INIT(NULL, 0) 00209 "SCA_IController", 00210 sizeof(PyObjectPlus_Proxy), 00211 0, 00212 py_base_dealloc, 00213 0, 00214 0, 00215 0, 00216 0, 00217 py_base_repr, 00218 0,0,0,0,0,0,0,0,0, 00219 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, 00220 0,0,0,0,0,0,0, 00221 Methods, 00222 0, 00223 0, 00224 &SCA_ILogicBrick::Type, 00225 0,0,0,0,0,0, 00226 py_base_new 00227 }; 00228 00229 PyMethodDef SCA_IController::Methods[] = { 00230 {NULL,NULL} //Sentinel 00231 }; 00232 00233 PyAttributeDef SCA_IController::Attributes[] = { 00234 KX_PYATTRIBUTE_RO_FUNCTION("state", SCA_IController, pyattr_get_state), 00235 KX_PYATTRIBUTE_RO_FUNCTION("sensors", SCA_IController, pyattr_get_sensors), 00236 KX_PYATTRIBUTE_RO_FUNCTION("actuators", SCA_IController, pyattr_get_actuators), 00237 KX_PYATTRIBUTE_BOOL_RW("useHighPriority",SCA_IController,m_bookmark), 00238 { NULL } //Sentinel 00239 }; 00240 00241 PyObject* SCA_IController::pyattr_get_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) 00242 { 00243 SCA_IController* self= static_cast<SCA_IController*>(self_v); 00244 return PyLong_FromSsize_t(self->m_statemask); 00245 } 00246 00247 PyObject* SCA_IController::pyattr_get_sensors(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) 00248 { 00249 return KX_PythonSeq_CreatePyObject((static_cast<SCA_IController*>(self_v))->m_proxy, KX_PYGENSEQ_CONT_TYPE_SENSORS); 00250 } 00251 00252 PyObject* SCA_IController::pyattr_get_actuators(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) 00253 { 00254 return KX_PythonSeq_CreatePyObject((static_cast<SCA_IController*>(self_v))->m_proxy, KX_PYGENSEQ_CONT_TYPE_ACTUATORS); 00255 } 00256 #endif // WITH_PYTHON