Blender  V2.59
KX_ArmatureSensor.cpp
Go to the documentation of this file.
00001 /*
00002  * Armature sensor
00003  *
00004  * $Id: KX_ArmatureSensor.cpp 35171 2011-02-25 13:35:59Z jesterking $
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.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software Foundation,
00020  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021  *
00022  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00023  * All rights reserved.
00024  *
00025  * The Original Code is: all of this file.
00026  *
00027  * Contributor(s): none yet.
00028  *
00029  * ***** END GPL LICENSE BLOCK *****
00030  */
00031 
00037 #include "DNA_action_types.h"
00038 #include "DNA_constraint_types.h"
00039 #include "BKE_constraint.h"
00040 #include "DNA_sensor_types.h"
00041 
00042 #include "BL_ArmatureObject.h"
00043 #include "KX_ArmatureSensor.h"
00044 #include "SCA_EventManager.h"
00045 #include "SCA_LogicManager.h"
00046 
00047 KX_ArmatureSensor::KX_ArmatureSensor(class SCA_EventManager* eventmgr,
00048                                         SCA_IObject* gameobj,
00049                                         const char *posechannel,
00050                                         const char *constraintname,
00051                                         int type,
00052                                         float value)
00053         : SCA_ISensor(gameobj,eventmgr),
00054         m_constraint(NULL),
00055         m_posechannel(posechannel),
00056         m_constraintname(constraintname),
00057         m_type(type),
00058         m_value(value)
00059 {
00060         FindConstraint();
00061 }
00062 
00063 void KX_ArmatureSensor::Init()
00064 {
00065         m_lastresult = m_invert?true:false;
00066         m_result = false;
00067         m_reset = true;
00068 }
00069 
00070 void KX_ArmatureSensor::FindConstraint()
00071 {
00072         m_constraint = NULL;
00073 
00074         if (m_gameobj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE) {
00075                 BL_ArmatureObject* armobj = (BL_ArmatureObject*)m_gameobj;
00076                 // get the persistent pose structure
00077                 bPose* pose = armobj->GetOrigPose();
00078                 bPoseChannel* pchan;
00079                 bConstraint* pcon;
00080                 // and locate the constraint
00081                 for (pchan = (bPoseChannel*)pose->chanbase.first; pchan; pchan=(bPoseChannel*)pchan->next) {
00082                         if (!strcmp(pchan->name, m_posechannel)) {
00083                                 // now locate the constraint
00084                                 for (pcon = (bConstraint*)pchan->constraints.first; pcon; pcon=(bConstraint*)pcon->next) {
00085                                         if (!strcmp(pcon->name, m_constraintname)) {
00086                                                 if (pcon->flag & CONSTRAINT_DISABLE)
00087                                                         /* this constraint is not valid, can't use it */
00088                                                         break;
00089                                                 m_constraint = pcon;
00090                                                 break;  
00091                                         }
00092                                 }
00093                                 break;
00094                         }
00095                 }
00096         }
00097 }
00098 
00099 
00100 CValue* KX_ArmatureSensor::GetReplica()
00101 {
00102         KX_ArmatureSensor* replica = new KX_ArmatureSensor(*this);
00103         // m_range_expr must be recalculated on replica!
00104         replica->ProcessReplica();
00105         return replica;
00106 }
00107 
00108 void KX_ArmatureSensor::ReParent(SCA_IObject* parent)
00109 {
00110         SCA_ISensor::ReParent(parent);
00111         // must remap the constraint
00112         FindConstraint();
00113 }
00114 
00115 bool KX_ArmatureSensor::IsPositiveTrigger()
00116 {
00117         return (m_invert) ? !m_result : m_result;
00118 }
00119 
00120 
00121 KX_ArmatureSensor::~KX_ArmatureSensor()
00122 {
00123 }
00124 
00125 bool KX_ArmatureSensor::Evaluate()
00126 {
00127         bool reset = m_reset && m_level;
00128 
00129         m_reset = false;
00130         if (!m_constraint)
00131                 return false;
00132         switch (m_type) {
00133         case SENS_ARM_STATE_CHANGED:
00134                 m_result = !(m_constraint->flag & CONSTRAINT_OFF);
00135                 break;
00136         case SENS_ARM_LIN_ERROR_BELOW:
00137                 m_result = (m_constraint->lin_error < m_value);
00138                 break;
00139         case SENS_ARM_LIN_ERROR_ABOVE:
00140                 m_result = (m_constraint->lin_error > m_value);
00141                 break;
00142         case SENS_ARM_ROT_ERROR_BELOW:
00143                 m_result = (m_constraint->rot_error < m_value);
00144                 break;
00145         case SENS_ARM_ROT_ERROR_ABOVE:
00146                 m_result = (m_constraint->rot_error > m_value);
00147                 break;
00148         }
00149         if (m_lastresult!=m_result)
00150         {
00151                 m_lastresult = m_result;
00152                 return true;
00153         }
00154         return (reset) ? true : false;
00155 }
00156 
00157 #ifdef WITH_PYTHON
00158 
00159 /* ------------------------------------------------------------------------- */
00160 /* Python functions                                                          */
00161 /* ------------------------------------------------------------------------- */
00162 
00163 /* Integration hooks ------------------------------------------------------- */
00164 PyTypeObject KX_ArmatureSensor::Type = {
00165         PyVarObject_HEAD_INIT(NULL, 0)
00166         "KX_ArmatureSensor",
00167         sizeof(PyObjectPlus_Proxy),
00168         0,
00169         py_base_dealloc,
00170         0,
00171         0,
00172         0,
00173         0,
00174         py_base_repr,
00175         0,0,0,0,0,0,0,0,0,
00176         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00177         0,0,0,0,0,0,0,
00178         Methods,
00179         0,
00180         0,
00181         &SCA_ISensor::Type,
00182         0,0,0,0,0,0,
00183         py_base_new
00184 };
00185 
00186 PyMethodDef KX_ArmatureSensor::Methods[] = {
00187         {NULL,NULL} //Sentinel
00188 };
00189 
00190 PyAttributeDef KX_ArmatureSensor::Attributes[] = {
00191         KX_PYATTRIBUTE_RO_FUNCTION("constraint", KX_ArmatureSensor, pyattr_get_constraint),
00192         KX_PYATTRIBUTE_FLOAT_RW("value",-FLT_MAX,FLT_MAX,KX_ArmatureSensor,m_value),
00193         KX_PYATTRIBUTE_INT_RW("type",0,SENS_ARM_MAXTYPE,false,KX_ArmatureSensor,m_type),
00194         { NULL }        //Sentinel
00195 };
00196 
00197 PyObject* KX_ArmatureSensor::pyattr_get_constraint(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
00198 {
00199         KX_ArmatureSensor* sensor = static_cast<KX_ArmatureSensor*>(self);
00200         if (sensor->m_gameobj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE) {
00201                 BL_ArmatureObject* armobj = (BL_ArmatureObject*)sensor->m_gameobj;
00202                 BL_ArmatureConstraint* constraint = armobj->GetConstraint(sensor->m_posechannel, sensor->m_constraintname);
00203                 if (constraint) 
00204                         return constraint->GetProxy();
00205         }
00206         Py_RETURN_NONE;
00207 }
00208 
00209 #endif // WITH_PYTHON