Fix for bug #5345, sculpt tool doesn't seems to use correctly the undo setting on...
[blender-staging.git] / source / gameengine / Ketsji / KX_TouchSensor.cpp
1 /**
2  * Senses touch and collision events
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version. The Blender
12  * Foundation also sells licenses for use in proprietary software under
13  * the Blender License.  See http://www.blender.org/BL/ for information
14  * about this.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL/BL DUAL LICENSE BLOCK *****
33  */
34
35 #include "KX_TouchSensor.h"
36 #include "SCA_EventManager.h"
37 #include "SCA_LogicManager.h"
38 #include "KX_GameObject.h"
39 #include "KX_TouchEventManager.h"
40 #include "KX_SumoPhysicsController.h"
41 #include <iostream>
42 #include "PHY_IPhysicsEnvironment.h"
43
44 #ifdef HAVE_CONFIG_H
45 #include <config.h>
46 #endif
47
48 /* ------------------------------------------------------------------------- */
49 /* Native functions                                                          */
50 /* ------------------------------------------------------------------------- */
51
52 void KX_TouchSensor::SynchronizeTransform()
53 {
54
55         if (m_physCtrl)
56         {
57                 MT_Vector3 pos = ((KX_GameObject*)GetParent())->NodeGetWorldPosition();
58                 MT_Quaternion orn = ((KX_GameObject*)GetParent())->NodeGetWorldOrientation().getRotation();
59                 m_physCtrl->setPosition(pos.x(),pos.y(),pos.z());
60                 m_physCtrl->setOrientation(orn.x(),orn.y(),orn.z(),orn.w());
61                 m_physCtrl->calcXform();
62         }
63         
64 }
65
66
67 void KX_TouchSensor::EndFrame() {
68         m_colliders->ReleaseAndRemoveAll();
69         m_bTriggered = false;
70 }
71
72 bool KX_TouchSensor::Evaluate(CValue* event)
73 {
74         bool result = false;
75
76         if (m_bTriggered != m_bLastTriggered)
77         {
78                 m_bLastTriggered = m_bTriggered;
79                 if (!m_bTriggered)
80                         m_hitObject = NULL;
81                 result = true;
82         }
83         
84         return result;
85 }
86
87 KX_TouchSensor::KX_TouchSensor(SCA_EventManager* eventmgr,KX_GameObject* gameobj,bool bFindMaterial,const STR_String& touchedpropname,PyTypeObject* T)
88 :SCA_ISensor(gameobj,eventmgr,T),
89 m_touchedpropname(touchedpropname),
90 m_bFindMaterial(bFindMaterial),
91 m_eventmgr(eventmgr),
92 /*m_sumoObj(sumoObj),*/
93 m_bCollision(false),
94 m_bTriggered(false),
95 m_bLastTriggered(false)
96 {
97 //      KX_TouchEventManager* touchmgr = (KX_TouchEventManager*) eventmgr;
98 //      m_resptable = touchmgr->GetResponseTable();
99         
100 //      m_solidHandle = m_sumoObj->getObjectHandle();
101
102         m_hitObject =  NULL;
103         m_colliders = new CListValue();
104         
105         KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
106         client_info->m_gameobject = gameobj;
107         client_info->m_auxilary_info = NULL;
108         client_info->m_sensors.push_back(this);
109         
110         m_physCtrl = dynamic_cast<PHY_IPhysicsController*>(gameobj->GetPhysicsController());
111         MT_assert( !gameobj->GetPhysicsController() || m_physCtrl );
112 }
113
114
115 KX_TouchSensor::~KX_TouchSensor()
116 {
117         //DT_ClearObjectResponse(m_resptable,m_solidHandle);
118         m_colliders->Release();
119 }
120
121 CValue* KX_TouchSensor::GetReplica() 
122 {
123         KX_TouchSensor* replica = new KX_TouchSensor(*this);
124         replica->m_colliders = new CListValue();
125         replica->m_bCollision = false;
126         replica->m_bTriggered= false;
127         replica->m_hitObject = NULL;
128         replica->m_bLastTriggered = false;
129         // this will copy properties and so on...
130         CValue::AddDataToReplica(replica);
131         return replica;
132 }
133
134 void    KX_TouchSensor::ReParent(SCA_IObject* parent)
135 {
136         KX_GameObject *gameobj = static_cast<KX_GameObject *>(parent);
137         PHY_IPhysicsController *sphy = dynamic_cast<PHY_IPhysicsController*>(((KX_GameObject*)parent)->GetPhysicsController());
138         if (sphy)
139                 m_physCtrl = sphy;
140         
141 //      m_solidHandle = m_sumoObj->getObjectHandle();
142         KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
143         client_info->m_gameobject = gameobj;
144         client_info->m_auxilary_info = NULL;
145         
146         client_info->m_sensors.push_back(this);
147         SCA_ISensor::ReParent(parent);
148 }
149
150 void KX_TouchSensor::RegisterSumo(KX_TouchEventManager *touchman)
151 {
152         if (m_physCtrl)
153         {
154                 touchman->GetPhysicsEnvironment()->requestCollisionCallback(m_physCtrl);
155                 // collision
156                 // Deprecated   
157
158         }
159 }
160
161 bool    KX_TouchSensor::NewHandleCollision(void*object1,void*object2,const PHY_CollData* colldata)
162 {
163 //      KX_TouchEventManager* toucheventmgr = (KX_TouchEventManager*)m_eventmgr;
164         KX_GameObject* parent = (KX_GameObject*)GetParent();
165
166         // need the mapping from PHY_IPhysicsController to gameobjects now
167         
168         KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*> (object1 == m_physCtrl? 
169                                         ((PHY_IPhysicsController*)object2)->getNewClientInfo(): 
170                                         ((PHY_IPhysicsController*)object1)->getNewClientInfo());
171
172         KX_GameObject* gameobj = ( client_info ? 
173                         client_info->m_gameobject : 
174                         NULL);
175         
176         if (gameobj && (gameobj != parent) && client_info->isActor())
177         {
178                 if (!m_colliders->SearchValue(gameobj))
179                         m_colliders->Add(gameobj->AddRef());
180                 
181                 bool found = m_touchedpropname.IsEmpty();
182                 if (!found)
183                 {
184                         if (m_bFindMaterial)
185                         {
186                                 if (client_info->m_auxilary_info)
187                                 {
188                                         found = (m_touchedpropname == STR_String((char*)client_info->m_auxilary_info));
189                                 }
190                         } else
191                         {
192                                 found = (gameobj->GetProperty(m_touchedpropname) != NULL);
193                         }
194                 }
195                 if (found)
196                 {
197                         m_bTriggered = true;
198                         m_hitObject = gameobj;
199                         //printf("KX_TouchSensor::HandleCollision\n");
200                 }
201                 
202         } 
203         return DT_CONTINUE;
204 }
205
206
207 /* ------------------------------------------------------------------------- */
208 /* Python functions                                                          */
209 /* ------------------------------------------------------------------------- */
210 /* Integration hooks ------------------------------------------------------- */
211 PyTypeObject KX_TouchSensor::Type = {
212         PyObject_HEAD_INIT(&PyType_Type)
213         0,
214         "KX_TouchSensor",
215         sizeof(KX_TouchSensor),
216         0,
217         PyDestructor,
218         0,
219         __getattr,
220         __setattr,
221         0, //&MyPyCompare,
222         __repr,
223         0, //&cvalue_as_number,
224         0,
225         0,
226         0,
227         0
228 };
229
230 PyParentObject KX_TouchSensor::Parents[] = {
231         &KX_TouchSensor::Type,
232         &SCA_ISensor::Type,
233         &SCA_ILogicBrick::Type,
234         &CValue::Type,
235         NULL
236 };
237
238 PyMethodDef KX_TouchSensor::Methods[] = {
239         {"setProperty", 
240          (PyCFunction) KX_TouchSensor::sPySetProperty,      METH_VARARGS, SetProperty_doc},
241         {"getProperty", 
242          (PyCFunction) KX_TouchSensor::sPyGetProperty,      METH_VARARGS, GetProperty_doc},
243         {"getHitObject", 
244          (PyCFunction) KX_TouchSensor::sPyGetHitObject,     METH_VARARGS, GetHitObject_doc},
245         {"getHitObjectList", 
246          (PyCFunction) KX_TouchSensor::sPyGetHitObjectList, METH_VARARGS, GetHitObjectList_doc},
247         {NULL,NULL} //Sentinel
248 };
249
250 PyObject* KX_TouchSensor::_getattr(const STR_String& attr) {
251         _getattr_up(SCA_ISensor);
252 }
253
254 /* Python API */
255
256 /* 1. setProperty */
257 char KX_TouchSensor::SetProperty_doc[] = 
258 "setProperty(name)\n"
259 "\t- name: string\n"
260 "\tSet the property or material to collide with. Use\n"
261 "\tsetTouchMaterial() to switch between properties and\n"
262 "\tmaterials.";
263 PyObject* KX_TouchSensor::PySetProperty(PyObject* self, 
264                                                                                 PyObject* args, 
265                                                                                 PyObject* kwds) {
266         char *nameArg;
267         if (!PyArg_ParseTuple(args, "s", &nameArg)) {
268                 return NULL;
269         }
270
271         CValue* prop = GetParent()->FindIdentifier(nameArg);
272
273         if (!prop->IsError()) {
274                 m_touchedpropname = nameArg;
275                 prop->Release();
276         } else {
277                 ; /* not found ... */
278         }
279         
280         Py_Return;
281 }
282 /* 2. getProperty */
283 char KX_TouchSensor::GetProperty_doc[] = 
284 "getProperty(name)\n"
285 "\tReturns the property or material to collide with. Use\n"
286 "\tgetTouchMaterial() to find out whether this sensor\n"
287 "\tlooks for properties or materials.";
288 PyObject*  KX_TouchSensor::PyGetProperty(PyObject* self, 
289                                                                                  PyObject* args, 
290                                                                                  PyObject* kwds) {
291         return PyString_FromString(m_touchedpropname);
292 }
293
294 char KX_TouchSensor::GetHitObject_doc[] = 
295 "getHitObject()\n"
296 ;
297 PyObject* KX_TouchSensor::PyGetHitObject(PyObject* self, 
298                                                                                  PyObject* args, 
299                                                                                  PyObject* kwds)
300 {
301         /* to do: do Py_IncRef if the object is already known in Python */
302         /* otherwise, this leaks memory */
303         if (m_hitObject)
304         {
305                 return m_hitObject->AddRef();
306         }
307         Py_Return;
308 }
309
310 char KX_TouchSensor::GetHitObjectList_doc[] = 
311 "getHitObjectList()\n"
312 "\tReturn a list of the objects this object collided with,\n"
313 "\tbut only those matching the property/material condition.\n";
314 PyObject* KX_TouchSensor::PyGetHitObjectList(PyObject* self, 
315                                                                                  PyObject* args, 
316                                                                                  PyObject* kwds)
317 {
318
319         /* to do: do Py_IncRef if the object is already known in Python */
320         /* otherwise, this leaks memory */
321
322         if ( m_touchedpropname.IsEmpty() ) {
323                 return m_colliders->AddRef();
324         } else {
325                 CListValue* newList = new CListValue();
326                 int i = 0;
327                 while (i < m_colliders->GetCount()) {
328                         if (m_bFindMaterial) {
329                                 /* need to associate the CValues from the list to material
330                                  * names. The collider list _should_ contains only
331                                  * KX_GameObjects. I am loathe to cast them, though... The
332                                  * material name must be retrieved from Sumo. To a Sumo
333                                  * object, a client-info block is attached. This block
334                                  * contains the material name. 
335                                  * - this also doesn't work (obviously) for multi-materials... 
336                                  */
337                                 KX_GameObject* gameob = (KX_GameObject*) m_colliders->GetValue(i);
338                                 PHY_IPhysicsController* spc = dynamic_cast<PHY_IPhysicsController*>(gameob->GetPhysicsController());
339                                 
340                                 if (spc) {
341                                         KX_ClientObjectInfo* cl_inf = static_cast<KX_ClientObjectInfo*>(spc->getNewClientInfo());
342                                         
343                                         if (m_touchedpropname == ((char*)cl_inf->m_auxilary_info)) {
344                                                 newList->Add(m_colliders->GetValue(i)->AddRef());
345                                         } 
346                                 }
347                                 
348                         } else {
349                                 CValue* val = m_colliders->GetValue(i)->FindIdentifier(m_touchedpropname);
350                                 if (!val->IsError()) {
351                                         newList->Add(m_colliders->GetValue(i)->AddRef());
352                                         val->Release();
353                                 }
354                         }
355                         
356                         i++;
357                 }
358                 return newList->AddRef();
359         }
360
361 }
362
363 /* 5. getTouchMaterial */
364 char KX_TouchSensor::GetTouchMaterial_doc[] = 
365 "getTouchMaterial()\n"
366 "\tReturns KX_TRUE if this sensor looks for a specific material,\n"
367 "\tKX_FALSE if it looks for a specific property.\n" ;
368 PyObject* KX_TouchSensor::PyGetTouchMaterial(PyObject* self, 
369                                                                                          PyObject* args, 
370                                                                                          PyObject* kwds)
371 {
372         return PyInt_FromLong(m_bFindMaterial);
373 }
374
375 /* 6. setTouchMaterial */
376 char KX_TouchSensor::SetTouchMaterial_doc[] = 
377 "setTouchMaterial(flag)\n"
378 "\t- flag: KX_TRUE or KX_FALSE.\n"
379 "\tSet flag to KX_TRUE to switch on positive pulse mode,\n"
380 "\tKX_FALSE to switch off positive pulse mode.\n" ;
381 PyObject* KX_TouchSensor::PySetTouchMaterial(PyObject* self, PyObject* args, PyObject* kwds)
382 {
383         int pulseArg = 0;
384
385         if(!PyArg_ParseTuple(args, "i", &pulseArg)) {
386                 return NULL;
387         }
388         
389         m_bFindMaterial = pulseArg != 0;
390
391         Py_Return;
392 }
393
394
395 /* eof */