fix for bug #18898: GE perspective 3D View not working properly (missing LENS)
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include "KX_TouchEventManager.h"
30 #include "SCA_ISensor.h"
31 #include "KX_TouchSensor.h"
32 #include "KX_GameObject.h"
33 #include "PHY_IPhysicsEnvironment.h"
34 #include "PHY_IPhysicsController.h"
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40
41 KX_TouchEventManager::KX_TouchEventManager(class SCA_LogicManager* logicmgr,
42         PHY_IPhysicsEnvironment* physEnv)
43         : SCA_EventManager(TOUCH_EVENTMGR),
44           m_logicmgr(logicmgr),
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         return true;
119 }
120
121 void KX_TouchEventManager::RegisterSensor(SCA_ISensor* sensor)
122 {
123         KX_TouchSensor* touchsensor = static_cast<KX_TouchSensor*>(sensor);
124         if (m_sensors.AddBack(touchsensor))
125                 // the sensor was effectively inserted, register it
126                 touchsensor->RegisterSumo(this);
127 }
128
129 void KX_TouchEventManager::RemoveSensor(SCA_ISensor* sensor)
130 {
131         KX_TouchSensor* touchsensor = static_cast<KX_TouchSensor*>(sensor);
132         if (touchsensor->Delink())
133                 // the sensor was effectively removed, unregister it
134                 touchsensor->UnregisterSumo(this);
135 }
136
137
138
139 void KX_TouchEventManager::EndFrame()
140 {
141         SG_DList::iterator<KX_TouchSensor> it(m_sensors);
142         for (it.begin();!it.end();++it)
143         {
144                 (*it)->EndFrame();
145         }
146 }
147
148
149
150 void KX_TouchEventManager::NextFrame()
151 {
152         if (!m_sensors.Empty())
153         {
154                 SG_DList::iterator<KX_TouchSensor> it(m_sensors);
155                 for (it.begin();!it.end();++it)
156                         (*it)->SynchronizeTransform();
157                 
158                 for (std::set<NewCollision>::iterator cit = m_newCollisions.begin(); cit != m_newCollisions.end(); ++cit)
159                 {
160                         PHY_IPhysicsController* ctrl1 = (*cit).first;
161 //                      PHY_IPhysicsController* ctrl2 = (*cit).second;
162 //                      KX_GameObject* gameOb1 = ctrl1->getClientInfo();
163 //                      KX_GameObject* gameOb1 = ctrl1->getClientInfo();
164
165                         KX_ClientObjectInfo *client_info = static_cast<KX_ClientObjectInfo *>(ctrl1->getNewClientInfo());
166                         list<SCA_ISensor*>::iterator sit;
167                         if (client_info) {
168                                 for ( sit = client_info->m_sensors.begin(); sit != client_info->m_sensors.end(); ++sit) {
169                                         static_cast<KX_TouchSensor*>(*sit)->NewHandleCollision((*cit).first, (*cit).second, NULL);
170                                 }
171                         }
172                         client_info = static_cast<KX_ClientObjectInfo *>((*cit).second->getNewClientInfo());
173                         if (client_info) {
174                                 for ( sit = client_info->m_sensors.begin(); sit != client_info->m_sensors.end(); ++sit) {
175                                         static_cast<KX_TouchSensor*>(*sit)->NewHandleCollision((*cit).second, (*cit).first, NULL);
176                                 }
177                         }
178                 }
179                         
180                 m_newCollisions.clear();
181                         
182                 for (it.begin();!it.end();++it)
183                         (*it)->Activate(m_logicmgr);
184         }
185 }