add missing files after merging
[blender.git] / source / gameengine / Ketsji / KX_TouchEventManager.cpp
1 /*
2  * $Id$
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file gameengine/Ketsji/KX_TouchEventManager.cpp
30  *  \ingroup ketsji
31  */
32
33
34 #include "KX_TouchEventManager.h"
35 #include "SCA_ISensor.h"
36 #include "KX_TouchSensor.h"
37 #include "KX_GameObject.h"
38 #include "PHY_IPhysicsEnvironment.h"
39 #include "PHY_IPhysicsController.h"
40
41
42 KX_TouchEventManager::KX_TouchEventManager(class SCA_LogicManager* logicmgr,
43         PHY_IPhysicsEnvironment* physEnv)
44         : SCA_EventManager(logicmgr, TOUCH_EVENTMGR),
45           m_physEnv(physEnv)
46 {
47         //notm_scene->addTouchCallback(STATIC_RESPONSE, KX_TouchEventManager::collisionResponse, this);
48
49         //m_scene->addTouchCallback(OBJECT_RESPONSE, KX_TouchEventManager::collisionResponse, this);
50         //m_scene->addTouchCallback(SENSOR_RESPONSE, KX_TouchEventManager::collisionResponse, this);
51
52         m_physEnv->addTouchCallback(PHY_OBJECT_RESPONSE, KX_TouchEventManager::newCollisionResponse, this);
53         m_physEnv->addTouchCallback(PHY_SENSOR_RESPONSE, KX_TouchEventManager::newCollisionResponse, this);
54         m_physEnv->addTouchCallback(PHY_BROADPH_RESPONSE, KX_TouchEventManager::newBroadphaseResponse, this);
55
56 }
57
58 bool    KX_TouchEventManager::NewHandleCollision(void* object1, void* object2, const PHY_CollData *coll_data)
59 {
60
61         PHY_IPhysicsController* obj1 = static_cast<PHY_IPhysicsController*>(object1);
62         PHY_IPhysicsController* obj2 = static_cast<PHY_IPhysicsController*>(object2);
63         
64         m_newCollisions.insert(std::pair<PHY_IPhysicsController*, PHY_IPhysicsController*>(obj1, obj2));
65                 
66         return false;
67 }
68
69
70 bool     KX_TouchEventManager::newCollisionResponse(void *client_data, 
71                                                         void *object1,
72                                                         void *object2,
73                                                         const PHY_CollData *coll_data)
74 {
75         KX_TouchEventManager *touchmgr = (KX_TouchEventManager *) client_data;
76         touchmgr->NewHandleCollision(object1, object2, coll_data);
77         return false;
78 }
79
80 bool     KX_TouchEventManager::newBroadphaseResponse(void *client_data, 
81                                                         void *object1,
82                                                         void *object2,
83                                                         const PHY_CollData *coll_data)
84 {
85         PHY_IPhysicsController* ctrl = static_cast<PHY_IPhysicsController*>(object1);
86         KX_ClientObjectInfo* info = (ctrl) ? static_cast<KX_ClientObjectInfo*>(ctrl->getNewClientInfo()) : NULL;
87         // This call back should only be called for controllers of Near and Radar sensor
88         if (!info)
89                 return true;
90
91         switch (info->m_type)
92         {
93         case KX_ClientObjectInfo::SENSOR:
94                 if (info->m_sensors.size() == 1)
95                 {
96                         // only one sensor for this type of object
97                         KX_TouchSensor* touchsensor = static_cast<KX_TouchSensor*>(*info->m_sensors.begin());
98                         return touchsensor->BroadPhaseFilterCollision(object1,object2);
99                 }
100                 break;
101         case KX_ClientObjectInfo::OBSENSOR:
102         case KX_ClientObjectInfo::OBACTORSENSOR:
103                 // this object may have multiple collision sensors, 
104                 // check is any of them is interested in this object
105                 for(std::list<SCA_ISensor*>::iterator it = info->m_sensors.begin();
106                         it != info->m_sensors.end();
107                         ++it)
108                 {
109                         if ((*it)->GetSensorType() == SCA_ISensor::ST_TOUCH) 
110                         {
111                                 KX_TouchSensor* touchsensor = static_cast<KX_TouchSensor*>(*it);
112                                 if (touchsensor->BroadPhaseSensorFilterCollision(object1, object2))
113                                         return true;
114                         }
115                 }
116                 return false;
117
118         // quiet the compiler
119         case KX_ClientObjectInfo::STATIC:
120         case KX_ClientObjectInfo::ACTOR:
121         case KX_ClientObjectInfo::RESERVED1:
122                 /* do nothing*/
123                 break;
124         }
125         return true;
126 }
127
128 void KX_TouchEventManager::RegisterSensor(SCA_ISensor* sensor)
129 {
130         KX_TouchSensor* touchsensor = static_cast<KX_TouchSensor*>(sensor);
131         if (m_sensors.AddBack(touchsensor))
132                 // the sensor was effectively inserted, register it
133                 touchsensor->RegisterSumo(this);
134 }
135
136 void KX_TouchEventManager::RemoveSensor(SCA_ISensor* sensor)
137 {
138         KX_TouchSensor* touchsensor = static_cast<KX_TouchSensor*>(sensor);
139         if (touchsensor->Delink())
140                 // the sensor was effectively removed, unregister it
141                 touchsensor->UnregisterSumo(this);
142 }
143
144
145
146 void KX_TouchEventManager::EndFrame()
147 {
148         SG_DList::iterator<KX_TouchSensor> it(m_sensors);
149         for (it.begin();!it.end();++it)
150         {
151                 (*it)->EndFrame();
152         }
153 }
154
155
156
157 void KX_TouchEventManager::NextFrame()
158 {
159         if (!m_sensors.Empty())
160         {
161                 SG_DList::iterator<KX_TouchSensor> it(m_sensors);
162                 for (it.begin();!it.end();++it)
163                         (*it)->SynchronizeTransform();
164                 
165                 for (std::set<NewCollision>::iterator cit = m_newCollisions.begin(); cit != m_newCollisions.end(); ++cit)
166                 {
167                         PHY_IPhysicsController* ctrl1 = (*cit).first;
168 //                      PHY_IPhysicsController* ctrl2 = (*cit).second;
169 //                      KX_GameObject* gameOb1 = ctrl1->getClientInfo();
170 //                      KX_GameObject* gameOb1 = ctrl1->getClientInfo();
171
172                         KX_ClientObjectInfo *client_info = static_cast<KX_ClientObjectInfo *>(ctrl1->getNewClientInfo());
173                         list<SCA_ISensor*>::iterator sit;
174                         if (client_info) {
175                                 for ( sit = client_info->m_sensors.begin(); sit != client_info->m_sensors.end(); ++sit) {
176                                         static_cast<KX_TouchSensor*>(*sit)->NewHandleCollision((*cit).first, (*cit).second, NULL);
177                                 }
178                         }
179                         client_info = static_cast<KX_ClientObjectInfo *>((*cit).second->getNewClientInfo());
180                         if (client_info) {
181                                 for ( sit = client_info->m_sensors.begin(); sit != client_info->m_sensors.end(); ++sit) {
182                                         static_cast<KX_TouchSensor*>(*sit)->NewHandleCollision((*cit).second, (*cit).first, NULL);
183                                 }
184                         }
185                 }
186                         
187                 m_newCollisions.clear();
188                         
189                 for (it.begin();!it.end();++it)
190                         (*it)->Activate(m_logicmgr);
191         }
192 }