new round of warning fixes. we are now down to 24 with Xcode on blender
[blender.git] / source / gameengine / Ketsji / KX_TouchEventManager.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL 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. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 #include "KX_TouchEventManager.h"
33 #include "SCA_ISensor.h"
34 #include "KX_TouchSensor.h"
35 #include "KX_GameObject.h"
36 #include "PHY_IPhysicsEnvironment.h"
37 #include "PHY_IPhysicsController.h"
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43
44 KX_TouchEventManager::KX_TouchEventManager(class SCA_LogicManager* logicmgr,
45         PHY_IPhysicsEnvironment* physEnv)
46         : SCA_EventManager(TOUCH_EVENTMGR),
47           m_logicmgr(logicmgr),
48           m_physEnv(physEnv)
49 {
50         //notm_scene->addTouchCallback(STATIC_RESPONSE, KX_TouchEventManager::collisionResponse, this);
51
52         //m_scene->addTouchCallback(OBJECT_RESPONSE, KX_TouchEventManager::collisionResponse, this);
53         //m_scene->addTouchCallback(SENSOR_RESPONSE, KX_TouchEventManager::collisionResponse, this);
54
55         m_physEnv->addTouchCallback(PHY_OBJECT_RESPONSE, KX_TouchEventManager::newCollisionResponse, this);
56         m_physEnv->addTouchCallback(PHY_SENSOR_RESPONSE, KX_TouchEventManager::newCollisionResponse, this);
57
58 }
59
60 bool    KX_TouchEventManager::NewHandleCollision(void* object1, void* object2, const PHY_CollData *coll_data)
61 {
62
63         PHY_IPhysicsController* obj1 = static_cast<PHY_IPhysicsController*>(object1);
64         PHY_IPhysicsController* obj2 = static_cast<PHY_IPhysicsController*>(object2);
65         
66         m_newCollisions.insert(std::pair<PHY_IPhysicsController*, PHY_IPhysicsController*>(obj1, obj2));
67                 
68         return false;
69 }
70
71
72 bool     KX_TouchEventManager::newCollisionResponse(void *client_data, 
73                                                         void *object1,
74                                                         void *object2,
75                                                         const PHY_CollData *coll_data)
76 {
77         KX_TouchEventManager *touchmgr = (KX_TouchEventManager *) client_data;
78         touchmgr->NewHandleCollision(object1, object2, coll_data);
79         return false;
80 }
81
82 void KX_TouchEventManager::RegisterSensor(SCA_ISensor* sensor)
83 {
84         KX_TouchSensor* touchsensor = static_cast<KX_TouchSensor*>(sensor);
85         m_sensors.push_back(touchsensor);
86
87         touchsensor->RegisterSumo(this);
88 }
89
90
91
92 void KX_TouchEventManager::EndFrame()
93 {
94         vector<SCA_ISensor*>::iterator it;
95         for ( it = m_sensors.begin();
96         !(it==m_sensors.end());it++)
97         {
98                 ((KX_TouchSensor*)*it)->EndFrame();
99
100         }
101 }
102
103
104
105 void KX_TouchEventManager::NextFrame()
106 {
107         if (m_sensors.size() > 0)
108         {
109                 vector<SCA_ISensor*>::iterator it;
110                 
111                 for (it = m_sensors.begin();!(it==m_sensors.end());++it)
112                         static_cast<KX_TouchSensor*>(*it)->SynchronizeTransform();
113                 
114                 for (std::set<NewCollision>::iterator cit = m_newCollisions.begin(); cit != m_newCollisions.end(); ++cit)
115                 {
116                         PHY_IPhysicsController* ctrl1 = (*cit).first;
117 //                      PHY_IPhysicsController* ctrl2 = (*cit).second;
118 //                      KX_GameObject* gameOb1 = ctrl1->getClientInfo();
119 //                      KX_GameObject* gameOb1 = ctrl1->getClientInfo();
120
121                         KX_ClientObjectInfo *client_info = static_cast<KX_ClientObjectInfo *>(ctrl1->getNewClientInfo());
122         
123                         list<SCA_ISensor*>::iterator sit;
124                         for ( sit = client_info->m_sensors.begin(); sit != client_info->m_sensors.end(); ++sit)
125                                 static_cast<KX_TouchSensor*>(*sit)->NewHandleCollision((*cit).first, (*cit).second, NULL);
126                 
127                         client_info = static_cast<KX_ClientObjectInfo *>((*cit).second->getNewClientInfo());
128                         for ( sit = client_info->m_sensors.begin(); sit != client_info->m_sensors.end(); ++sit)
129                                 static_cast<KX_TouchSensor*>(*sit)->NewHandleCollision((*cit).second, (*cit).first, NULL);
130                 }
131                         
132                 m_newCollisions.clear();
133                         
134                 for (it = m_sensors.begin();!(it==m_sensors.end());++it)
135                         (*it)->Activate(m_logicmgr,NULL);
136         }
137 }
138
139
140
141 void KX_TouchEventManager::RemoveSensor(class SCA_ISensor* sensor)
142 {
143         std::vector<SCA_ISensor*>::iterator i =
144         std::find(m_sensors.begin(), m_sensors.end(), sensor);
145         if (!(i == m_sensors.end()))
146         {
147                 std::swap(*i, m_sensors.back());
148                 m_sensors.pop_back();
149         }
150         
151         // remove the sensor forever :)
152         SCA_EventManager::RemoveSensor(sensor);
153 }
154