rename and negate DISABLE_PYTHON --> WITH_PYTHON
[blender.git] / source / gameengine / Converter / BL_ArmatureActuator.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include "DNA_action_types.h"
31 #include "DNA_constraint_types.h"
32 #include "DNA_actuator_types.h"
33 #include "BKE_constraint.h"
34 #include "BL_ArmatureActuator.h"
35 #include "BL_ArmatureObject.h"
36 #include "BLI_math.h"
37
38 /**
39  * This class is the conversion of the Pose channel constraint.
40  * It makes a link between the pose constraint and the KX scene.
41  * The main purpose is to give access to the constraint target 
42  * to link it to a game object. 
43  * It also allows to activate/deactivate constraints during the game.
44  * Later it will also be possible to create constraint on the fly
45  */
46
47 BL_ArmatureActuator::BL_ArmatureActuator(SCA_IObject* obj,
48                                                 int type,
49                                                 const char *posechannel,
50                                                 const char *constraintname,
51                                                 KX_GameObject* targetobj,
52                                                 KX_GameObject* subtargetobj,
53                                                 float weight) :
54         SCA_IActuator(obj, KX_ACT_ARMATURE),
55         m_constraint(NULL),
56         m_gametarget(targetobj),
57         m_gamesubtarget(subtargetobj),
58         m_posechannel(posechannel),
59         m_constraintname(constraintname),
60         m_weight(weight),
61         m_type(type)
62 {
63         if (m_gametarget)
64                 m_gametarget->RegisterActuator(this);
65         if (m_gamesubtarget)
66                 m_gamesubtarget->RegisterActuator(this);
67         FindConstraint();
68 }
69
70 BL_ArmatureActuator::~BL_ArmatureActuator()
71 {
72         if (m_gametarget)
73                 m_gametarget->UnregisterActuator(this);
74         if (m_gamesubtarget)
75                 m_gamesubtarget->UnregisterActuator(this);
76 }
77
78 void BL_ArmatureActuator::ProcessReplica()
79 {
80         // the replica is tracking the same object => register it (this may be changed in Relnk())
81         if (m_gametarget)
82                 m_gametarget->RegisterActuator(this);
83         if (m_gamesubtarget)
84                 m_gamesubtarget->UnregisterActuator(this);
85         SCA_IActuator::ProcessReplica();
86 }
87
88 void BL_ArmatureActuator::ReParent(SCA_IObject* parent)
89 {
90         SCA_IActuator::ReParent(parent);
91         // must remap the constraint
92         FindConstraint();
93 }
94
95 bool BL_ArmatureActuator::UnlinkObject(SCA_IObject* clientobj)
96 {
97         bool res=false;
98         if (clientobj == m_gametarget)
99         {
100                 // this object is being deleted, we cannot continue to track it.
101                 m_gametarget = NULL;
102                 res = true;
103         }
104         if (clientobj == m_gamesubtarget)
105         {
106                 // this object is being deleted, we cannot continue to track it.
107                 m_gamesubtarget = NULL;
108                 res = true;
109         }
110         return res;
111 }
112
113 void BL_ArmatureActuator::Relink(GEN_Map<GEN_HashedPtr, void*> *obj_map)
114 {
115         void **h_obj = (*obj_map)[m_gametarget];
116         if (h_obj) {
117                 if (m_gametarget)
118                         m_gametarget->UnregisterActuator(this);
119                 m_gametarget = (KX_GameObject*)(*h_obj);
120                 m_gametarget->RegisterActuator(this);
121         }
122         h_obj = (*obj_map)[m_gamesubtarget];
123         if (h_obj) {
124                 if (m_gamesubtarget)
125                         m_gamesubtarget->UnregisterActuator(this);
126                 m_gamesubtarget = (KX_GameObject*)(*h_obj);
127                 m_gamesubtarget->RegisterActuator(this);
128         }
129 }
130
131 void BL_ArmatureActuator::FindConstraint()
132 {
133         m_constraint = NULL;
134
135         if (m_gameobj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE) {
136                 BL_ArmatureObject* armobj = (BL_ArmatureObject*)m_gameobj;
137                 m_constraint = armobj->GetConstraint(m_posechannel, m_constraintname);
138         }
139 }
140
141 bool BL_ArmatureActuator::Update(double curtime, bool frame)
142 {
143         // the only role of this actuator is to ensure that the armature pose will be evaluated
144         bool result = false;    
145         bool bNegativeEvent = IsNegativeEvent();
146         RemoveAllEvents();
147
148         if (!bNegativeEvent) {
149                 BL_ArmatureObject *obj = (BL_ArmatureObject*)GetParent();
150                 switch (m_type) {
151                 case ACT_ARM_RUN:
152                         result = true;
153                         obj->SetActiveAction(NULL, 0, curtime);
154                         break;
155                 case ACT_ARM_ENABLE:
156                         if (m_constraint)
157                                 m_constraint->ClrConstraintFlag(CONSTRAINT_OFF);
158                         break;
159                 case ACT_ARM_DISABLE:
160                         if (m_constraint)
161                                 m_constraint->SetConstraintFlag(CONSTRAINT_OFF);
162                         break;
163                 case ACT_ARM_SETTARGET:
164                         if (m_constraint) {
165                                 m_constraint->SetTarget(m_gametarget);
166                                 m_constraint->SetSubtarget(m_gamesubtarget);
167                         }
168                         break;
169                 case ACT_ARM_SETWEIGHT:
170                         if (m_constraint)
171                                 m_constraint->SetWeight(m_weight);
172                         break;
173                 }
174         }
175         return result;
176 }
177
178 #ifdef WITH_PYTHON
179
180 /* ------------------------------------------------------------------------- */
181 /* Python Integration Hooks                                                                      */
182 /* ------------------------------------------------------------------------- */
183
184 PyTypeObject BL_ArmatureActuator::Type = {
185         PyVarObject_HEAD_INIT(NULL, 0)
186                 "BL_ArmatureActuator",
187                 sizeof(PyObjectPlus_Proxy),
188                 0,
189                 py_base_dealloc,
190                 0,
191                 0,
192                 0,
193                 0,
194                 py_base_repr,
195                 0,0,0,0,0,0,0,0,0,
196                 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
197                 0,0,0,0,0,0,0,
198                 Methods,
199                 0,
200                 0,
201                 &SCA_IActuator::Type,
202                 0,0,0,0,0,0,
203                 py_base_new
204 };
205
206
207 PyMethodDef BL_ArmatureActuator::Methods[] = {
208         {NULL,NULL} //Sentinel
209 };
210
211 PyAttributeDef BL_ArmatureActuator::Attributes[] = {
212         KX_PYATTRIBUTE_RO_FUNCTION("constraint", BL_ArmatureActuator, pyattr_get_constraint),
213         KX_PYATTRIBUTE_RW_FUNCTION("target", BL_ArmatureActuator, pyattr_get_object, pyattr_set_object),
214         KX_PYATTRIBUTE_RW_FUNCTION("subtarget", BL_ArmatureActuator, pyattr_get_object, pyattr_set_object),
215         KX_PYATTRIBUTE_FLOAT_RW("weight",0.0f,1.0f,BL_ArmatureActuator,m_weight),
216         KX_PYATTRIBUTE_INT_RW("type",0,ACT_ARM_MAXTYPE,false,BL_ArmatureActuator,m_type),
217         { NULL }        //Sentinel
218 };
219
220 PyObject* BL_ArmatureActuator::pyattr_get_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
221 {
222         BL_ArmatureActuator* actuator = static_cast<BL_ArmatureActuator*>(self);
223         KX_GameObject *target = (!strcmp(attrdef->m_name, "target")) ? actuator->m_gametarget : actuator->m_gamesubtarget;
224         if (!target)    
225                 Py_RETURN_NONE;
226         else
227                 return target->GetProxy();
228 }
229
230 int BL_ArmatureActuator::pyattr_set_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
231 {
232         BL_ArmatureActuator* actuator = static_cast<BL_ArmatureActuator*>(self);
233         KX_GameObject* &target = (!strcmp(attrdef->m_name, "target")) ? actuator->m_gametarget : actuator->m_gamesubtarget;
234         KX_GameObject *gameobj;
235                 
236         if (!ConvertPythonToGameObject(value, &gameobj, true, "actuator.object = value: BL_ArmatureActuator"))
237                 return PY_SET_ATTR_FAIL; // ConvertPythonToGameObject sets the error
238                 
239         if (target != NULL)
240                 target->UnregisterActuator(actuator);   
241
242         target = gameobj;
243                 
244         if (target)
245                 target->RegisterActuator(actuator);
246                 
247         return PY_SET_ATTR_SUCCESS;
248 }
249
250 PyObject* BL_ArmatureActuator::pyattr_get_constraint(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
251 {
252         BL_ArmatureActuator* actuator = static_cast<BL_ArmatureActuator*>(self);
253         BL_ArmatureConstraint* constraint = actuator->m_constraint;
254         if (!constraint)        
255                 Py_RETURN_NONE;
256         else
257                 return constraint->GetProxy();
258 }
259
260 #endif // WITH_PYTHON
261