patch 8235 8218 8211 added: various gameengine improvements, fixed windows project...
[blender.git] / source / gameengine / Converter / KX_ConvertSensors.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  * Conversion of Blender data blocks to KX sensor system
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #ifdef WIN32
39 #pragma warning (disable : 4786)
40 #endif //WIN32
41
42 #include "KX_BlenderSceneConverter.h"
43 #include "KX_ConvertSensors.h"
44
45 /* This little block needed for linking to Blender... */
46 #ifdef WIN32
47 #include "BLI_winstuff.h"
48 #endif
49
50 #include "DNA_object_types.h"
51 #include "DNA_material_types.h"
52 #include "DNA_sensor_types.h"
53 #include "DNA_actuator_types.h" /* for SENS_ALL_KEYS ? this define is
54 probably misplaced */
55 /* end of blender include block */
56
57 #include "RAS_IPolygonMaterial.h"
58 // Sensors
59 #include "KX_GameObject.h"
60 #include "RAS_MeshObject.h"
61 #include "SCA_KeyboardSensor.h"
62 #include "SCA_MouseSensor.h"
63 #include "SCA_AlwaysSensor.h"
64 #include "KX_TouchSensor.h"
65 #include "KX_NearSensor.h"
66 #include "KX_RadarSensor.h"
67 #include "KX_MouseFocusSensor.h"
68 #include "SCA_JoystickSensor.h"
69 #include "KX_NetworkMessageSensor.h"
70
71
72 #include "SCA_PropertySensor.h"
73 #include "SCA_RandomSensor.h"
74 #include "KX_RaySensor.h"
75 #include "SCA_EventManager.h"
76 #include "SCA_LogicManager.h"
77 #include "KX_BlenderInputDevice.h"
78 #include "KX_Scene.h"
79 #include "IntValue.h"
80 #include "KX_BlenderKeyboardDevice.h"
81 #include "KX_BlenderGL.h"
82 #include "RAS_ICanvas.h"
83 #include "PHY_IPhysicsEnvironment.h"
84
85 #include "KX_KetsjiEngine.h"
86 #include "KX_BlenderSceneConverter.h"
87
88 // this map is Blender specific: a conversion between blender and ketsji enums
89 std::map<int,SCA_IInputDevice::KX_EnumInputs> gReverseKeyTranslateTable;
90
91
92 void BL_ConvertSensors(struct Object* blenderobject,
93                                            class KX_GameObject* gameobj,
94                                            SCA_LogicManager* logicmgr,
95                                            KX_Scene* kxscene,
96                                            SCA_IInputDevice* keydev,
97                                            int & executePriority,
98                                            int activeLayerBitInfo,
99                                            bool isInActiveLayer,
100                                            RAS_ICanvas* canvas,
101                                            KX_BlenderSceneConverter* converter
102                                            )
103 {
104         
105         
106         
107         /* The reverse table. In order to not confuse ourselves, we      */
108         /* immediately convert all events that come in to KX codes.      */
109         gReverseKeyTranslateTable[LEFTMOUSE                     ] =     SCA_IInputDevice::KX_LEFTMOUSE;
110         gReverseKeyTranslateTable[MIDDLEMOUSE           ] =     SCA_IInputDevice::KX_MIDDLEMOUSE;
111         gReverseKeyTranslateTable[RIGHTMOUSE            ] =     SCA_IInputDevice::KX_RIGHTMOUSE;
112         gReverseKeyTranslateTable[WHEELUPMOUSE          ] =     SCA_IInputDevice::KX_WHEELUPMOUSE;
113         gReverseKeyTranslateTable[WHEELDOWNMOUSE        ] =     SCA_IInputDevice::KX_WHEELDOWNMOUSE;
114         gReverseKeyTranslateTable[MOUSEX                        ] = SCA_IInputDevice::KX_MOUSEX;
115         gReverseKeyTranslateTable[MOUSEY                        ] =     SCA_IInputDevice::KX_MOUSEY;
116         
117         // TIMERS                                                                                                  
118         
119         gReverseKeyTranslateTable[TIMER0                        ] = SCA_IInputDevice::KX_TIMER0;                  
120         gReverseKeyTranslateTable[TIMER1                        ] = SCA_IInputDevice::KX_TIMER1;                  
121         gReverseKeyTranslateTable[TIMER2                        ] = SCA_IInputDevice::KX_TIMER2;                  
122         gReverseKeyTranslateTable[TIMER3                        ] = SCA_IInputDevice::KX_TIMER3;                  
123         
124         // SYSTEM                                                                                                  
125         
126         gReverseKeyTranslateTable[KEYBD                         ] = SCA_IInputDevice::KX_KEYBD;                  
127         gReverseKeyTranslateTable[RAWKEYBD                      ] = SCA_IInputDevice::KX_RAWKEYBD;                  
128         gReverseKeyTranslateTable[REDRAW                        ] = SCA_IInputDevice::KX_REDRAW;                  
129         gReverseKeyTranslateTable[INPUTCHANGE           ] = SCA_IInputDevice::KX_INPUTCHANGE;                  
130         gReverseKeyTranslateTable[QFULL                         ] = SCA_IInputDevice::KX_QFULL;                  
131         gReverseKeyTranslateTable[WINFREEZE                     ] = SCA_IInputDevice::KX_WINFREEZE;                  
132         gReverseKeyTranslateTable[WINTHAW                       ] = SCA_IInputDevice::KX_WINTHAW;                  
133         gReverseKeyTranslateTable[WINCLOSE                      ] = SCA_IInputDevice::KX_WINCLOSE;                  
134         gReverseKeyTranslateTable[WINQUIT                       ] = SCA_IInputDevice::KX_WINQUIT;                  
135         gReverseKeyTranslateTable[Q_FIRSTTIME           ] = SCA_IInputDevice::KX_Q_FIRSTTIME;                  
136         
137         // standard keyboard                                                                                       
138         
139         gReverseKeyTranslateTable[AKEY                          ] = SCA_IInputDevice::KX_AKEY;                  
140         gReverseKeyTranslateTable[BKEY                          ] = SCA_IInputDevice::KX_BKEY;                  
141         gReverseKeyTranslateTable[CKEY                          ] = SCA_IInputDevice::KX_CKEY;                  
142         gReverseKeyTranslateTable[DKEY                          ] = SCA_IInputDevice::KX_DKEY;                  
143         gReverseKeyTranslateTable[EKEY                          ] = SCA_IInputDevice::KX_EKEY;                  
144         gReverseKeyTranslateTable[FKEY                          ] = SCA_IInputDevice::KX_FKEY;                  
145         gReverseKeyTranslateTable[GKEY                          ] = SCA_IInputDevice::KX_GKEY;                  
146         gReverseKeyTranslateTable[HKEY                          ] = SCA_IInputDevice::KX_HKEY;                  
147         gReverseKeyTranslateTable[IKEY                          ] = SCA_IInputDevice::KX_IKEY;                  
148         gReverseKeyTranslateTable[JKEY                          ] = SCA_IInputDevice::KX_JKEY;                  
149         gReverseKeyTranslateTable[KKEY                          ] = SCA_IInputDevice::KX_KKEY;                  
150         gReverseKeyTranslateTable[LKEY                          ] = SCA_IInputDevice::KX_LKEY;                  
151         gReverseKeyTranslateTable[MKEY                          ] = SCA_IInputDevice::KX_MKEY;                  
152         gReverseKeyTranslateTable[NKEY                          ] = SCA_IInputDevice::KX_NKEY;                  
153         gReverseKeyTranslateTable[OKEY                          ] = SCA_IInputDevice::KX_OKEY;                  
154         gReverseKeyTranslateTable[PKEY                          ] = SCA_IInputDevice::KX_PKEY;                  
155         gReverseKeyTranslateTable[QKEY                          ] = SCA_IInputDevice::KX_QKEY;                  
156         gReverseKeyTranslateTable[RKEY                          ] = SCA_IInputDevice::KX_RKEY;                  
157         gReverseKeyTranslateTable[SKEY                          ] = SCA_IInputDevice::KX_SKEY;                  
158         gReverseKeyTranslateTable[TKEY                          ] = SCA_IInputDevice::KX_TKEY;                  
159         gReverseKeyTranslateTable[UKEY                          ] = SCA_IInputDevice::KX_UKEY;                  
160         gReverseKeyTranslateTable[VKEY                          ] = SCA_IInputDevice::KX_VKEY;                  
161         gReverseKeyTranslateTable[WKEY                          ] = SCA_IInputDevice::KX_WKEY;                  
162         gReverseKeyTranslateTable[XKEY                          ] = SCA_IInputDevice::KX_XKEY;                  
163         gReverseKeyTranslateTable[YKEY                          ] = SCA_IInputDevice::KX_YKEY;                  
164         gReverseKeyTranslateTable[ZKEY                          ] = SCA_IInputDevice::KX_ZKEY;                  
165         
166         gReverseKeyTranslateTable[ZEROKEY                       ] = SCA_IInputDevice::KX_ZEROKEY;                  
167         gReverseKeyTranslateTable[ONEKEY                        ] = SCA_IInputDevice::KX_ONEKEY;                  
168         gReverseKeyTranslateTable[TWOKEY                        ] = SCA_IInputDevice::KX_TWOKEY;                  
169         gReverseKeyTranslateTable[THREEKEY                      ] = SCA_IInputDevice::KX_THREEKEY;                  
170         gReverseKeyTranslateTable[FOURKEY                       ] = SCA_IInputDevice::KX_FOURKEY;                  
171         gReverseKeyTranslateTable[FIVEKEY                       ] = SCA_IInputDevice::KX_FIVEKEY;                  
172         gReverseKeyTranslateTable[SIXKEY                        ] = SCA_IInputDevice::KX_SIXKEY;                  
173         gReverseKeyTranslateTable[SEVENKEY                      ] = SCA_IInputDevice::KX_SEVENKEY;                  
174         gReverseKeyTranslateTable[EIGHTKEY                      ] = SCA_IInputDevice::KX_EIGHTKEY;                  
175         gReverseKeyTranslateTable[NINEKEY                       ] = SCA_IInputDevice::KX_NINEKEY;                  
176         
177         gReverseKeyTranslateTable[CAPSLOCKKEY           ] = SCA_IInputDevice::KX_CAPSLOCKKEY;                  
178         
179         gReverseKeyTranslateTable[LEFTCTRLKEY           ] = SCA_IInputDevice::KX_LEFTCTRLKEY;                  
180         gReverseKeyTranslateTable[LEFTALTKEY            ] = SCA_IInputDevice::KX_LEFTALTKEY;                  
181         gReverseKeyTranslateTable[RIGHTALTKEY           ] = SCA_IInputDevice::KX_RIGHTALTKEY;                  
182         gReverseKeyTranslateTable[RIGHTCTRLKEY          ] = SCA_IInputDevice::KX_RIGHTCTRLKEY;                  
183         gReverseKeyTranslateTable[RIGHTSHIFTKEY         ] = SCA_IInputDevice::KX_RIGHTSHIFTKEY;                  
184         gReverseKeyTranslateTable[LEFTSHIFTKEY          ] = SCA_IInputDevice::KX_LEFTSHIFTKEY;                  
185         
186         gReverseKeyTranslateTable[ESCKEY                        ] = SCA_IInputDevice::KX_ESCKEY;                  
187         gReverseKeyTranslateTable[TABKEY                        ] = SCA_IInputDevice::KX_TABKEY;                  
188         gReverseKeyTranslateTable[RETKEY                        ] = SCA_IInputDevice::KX_RETKEY;                  
189         gReverseKeyTranslateTable[SPACEKEY                      ] = SCA_IInputDevice::KX_SPACEKEY;                  
190         gReverseKeyTranslateTable[LINEFEEDKEY           ] = SCA_IInputDevice::KX_LINEFEEDKEY;                  
191         gReverseKeyTranslateTable[BACKSPACEKEY          ] = SCA_IInputDevice::KX_BACKSPACEKEY;                  
192         gReverseKeyTranslateTable[DELKEY                        ] = SCA_IInputDevice::KX_DELKEY;                  
193         gReverseKeyTranslateTable[SEMICOLONKEY          ] = SCA_IInputDevice::KX_SEMICOLONKEY;                  
194         gReverseKeyTranslateTable[PERIODKEY                     ] = SCA_IInputDevice::KX_PERIODKEY;                  
195         gReverseKeyTranslateTable[COMMAKEY                      ] = SCA_IInputDevice::KX_COMMAKEY;                  
196         gReverseKeyTranslateTable[QUOTEKEY                      ] = SCA_IInputDevice::KX_QUOTEKEY;                  
197         gReverseKeyTranslateTable[ACCENTGRAVEKEY        ] = SCA_IInputDevice::KX_ACCENTGRAVEKEY;                  
198         gReverseKeyTranslateTable[MINUSKEY                      ] = SCA_IInputDevice::KX_MINUSKEY;                  
199         gReverseKeyTranslateTable[SLASHKEY                      ] = SCA_IInputDevice::KX_SLASHKEY;                  
200         gReverseKeyTranslateTable[BACKSLASHKEY          ] = SCA_IInputDevice::KX_BACKSLASHKEY;                  
201         gReverseKeyTranslateTable[EQUALKEY                      ] = SCA_IInputDevice::KX_EQUALKEY;                  
202         gReverseKeyTranslateTable[LEFTBRACKETKEY        ] = SCA_IInputDevice::KX_LEFTBRACKETKEY;                  
203         gReverseKeyTranslateTable[RIGHTBRACKETKEY       ] = SCA_IInputDevice::KX_RIGHTBRACKETKEY;                  
204         
205         gReverseKeyTranslateTable[LEFTARROWKEY          ] = SCA_IInputDevice::KX_LEFTARROWKEY;                  
206         gReverseKeyTranslateTable[DOWNARROWKEY          ] = SCA_IInputDevice::KX_DOWNARROWKEY;                  
207         gReverseKeyTranslateTable[RIGHTARROWKEY         ] = SCA_IInputDevice::KX_RIGHTARROWKEY;                  
208         gReverseKeyTranslateTable[UPARROWKEY            ] = SCA_IInputDevice::KX_UPARROWKEY;                  
209         
210         gReverseKeyTranslateTable[PAD2                          ] = SCA_IInputDevice::KX_PAD2;                  
211         gReverseKeyTranslateTable[PAD4                          ] = SCA_IInputDevice::KX_PAD4;                  
212         gReverseKeyTranslateTable[PAD6                          ] = SCA_IInputDevice::KX_PAD6;                  
213         gReverseKeyTranslateTable[PAD8                          ] = SCA_IInputDevice::KX_PAD8;                  
214         
215         gReverseKeyTranslateTable[PAD1                          ] = SCA_IInputDevice::KX_PAD1;                  
216         gReverseKeyTranslateTable[PAD3                          ] = SCA_IInputDevice::KX_PAD3;                  
217         gReverseKeyTranslateTable[PAD5                          ] = SCA_IInputDevice::KX_PAD5;                  
218         gReverseKeyTranslateTable[PAD7                          ] = SCA_IInputDevice::KX_PAD7;                  
219         gReverseKeyTranslateTable[PAD9                          ] = SCA_IInputDevice::KX_PAD9;                  
220         
221         gReverseKeyTranslateTable[PADPERIOD                     ] = SCA_IInputDevice::KX_PADPERIOD;                  
222         gReverseKeyTranslateTable[PADSLASHKEY           ] = SCA_IInputDevice::KX_PADSLASHKEY;                  
223         gReverseKeyTranslateTable[PADASTERKEY           ] = SCA_IInputDevice::KX_PADASTERKEY;                  
224         
225         gReverseKeyTranslateTable[PAD0                          ] = SCA_IInputDevice::KX_PAD0;                  
226         gReverseKeyTranslateTable[PADMINUS                      ] = SCA_IInputDevice::KX_PADMINUS;                  
227         gReverseKeyTranslateTable[PADENTER                      ] = SCA_IInputDevice::KX_PADENTER;                  
228         gReverseKeyTranslateTable[PADPLUSKEY            ] = SCA_IInputDevice::KX_PADPLUSKEY;                  
229         
230         
231         gReverseKeyTranslateTable[F1KEY                         ] = SCA_IInputDevice::KX_F1KEY;                  
232         gReverseKeyTranslateTable[F2KEY                         ] = SCA_IInputDevice::KX_F2KEY;                  
233         gReverseKeyTranslateTable[F3KEY                         ] = SCA_IInputDevice::KX_F3KEY;                  
234         gReverseKeyTranslateTable[F4KEY                         ] = SCA_IInputDevice::KX_F4KEY;                  
235         gReverseKeyTranslateTable[F5KEY                         ] = SCA_IInputDevice::KX_F5KEY;                  
236         gReverseKeyTranslateTable[F6KEY                         ] = SCA_IInputDevice::KX_F6KEY;                  
237         gReverseKeyTranslateTable[F7KEY                         ] = SCA_IInputDevice::KX_F7KEY;                  
238         gReverseKeyTranslateTable[F8KEY                         ] = SCA_IInputDevice::KX_F8KEY;                  
239         gReverseKeyTranslateTable[F9KEY                         ] = SCA_IInputDevice::KX_F9KEY;                  
240         gReverseKeyTranslateTable[F10KEY                        ] = SCA_IInputDevice::KX_F10KEY;                  
241         gReverseKeyTranslateTable[F11KEY                        ] = SCA_IInputDevice::KX_F11KEY;                  
242         gReverseKeyTranslateTable[F12KEY                        ] = SCA_IInputDevice::KX_F12KEY;                  
243         
244         gReverseKeyTranslateTable[PAUSEKEY                      ] = SCA_IInputDevice::KX_PAUSEKEY;                  
245         gReverseKeyTranslateTable[INSERTKEY                     ] = SCA_IInputDevice::KX_INSERTKEY;                  
246         gReverseKeyTranslateTable[HOMEKEY                       ] = SCA_IInputDevice::KX_HOMEKEY;                  
247         gReverseKeyTranslateTable[PAGEUPKEY                     ] = SCA_IInputDevice::KX_PAGEUPKEY;                  
248         gReverseKeyTranslateTable[PAGEDOWNKEY           ] = SCA_IInputDevice::KX_PAGEDOWNKEY;                  
249         gReverseKeyTranslateTable[ENDKEY                        ] = SCA_IInputDevice::KX_ENDKEY;
250         
251         int uniqueint = 0;
252         bSensor* sens = (bSensor*)blenderobject->sensors.first;
253         bool pos_pulsemode = false;
254         bool neg_pulsemode = false;
255         int frequency = 0;
256         bool invert = false;
257         
258         while(sens)
259         {
260                 SCA_ISensor* gamesensor=NULL;
261                 /* All sensors have a pulse toggle, frequency, and invert field.     */
262                 /* These are extracted here, and set when the sensor is added to the */
263                 /* list.                                                             */
264                 pos_pulsemode = (sens->pulse & SENS_PULSE_REPEAT)!=0;
265                 neg_pulsemode = (sens->pulse & SENS_NEG_PULSE_MODE)!=0;
266                 
267                 frequency = sens->freq;
268                 invert    = !(sens->invert == 0);
269                 
270                 switch (sens->type)
271                 {
272                 case  SENS_ALWAYS:
273                         {
274                                 
275                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::ALWAYS_EVENTMGR);
276                                 if (eventmgr)
277                                 {
278                                         gamesensor = new SCA_AlwaysSensor(eventmgr, gameobj);
279                                 }
280                                 
281                                 break;
282                         }
283                         
284                 case SENS_COLLISION:
285                         {
286                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::TOUCH_EVENTMGR);
287                                 if (eventmgr)
288                                 {
289                                         // collision sensor can sense both materials and properties. 
290                                         
291                                         bool bFindMaterial = false;
292                                         
293                                         bCollisionSensor* blendertouchsensor = (bCollisionSensor*)sens->data;
294                                         
295                                         bFindMaterial = (blendertouchsensor->mode 
296                                                 & SENS_COLLISION_MATERIAL);
297                                         
298                                         
299                                         STR_String touchPropOrMatName = ( bFindMaterial ? 
300                                                 blendertouchsensor->materialName:
301                                         (blendertouchsensor->name ? blendertouchsensor->name: ""));
302                                         
303                                         
304                                         if (gameobj->GetPhysicsController())
305                                         {       
306                                                 gamesensor = new KX_TouchSensor(eventmgr,
307                                                         gameobj,
308                                                         bFindMaterial,
309                                                         touchPropOrMatName);
310                                         }
311                                         
312                                 }
313                                 
314                                 break;
315                         }
316                 case SENS_TOUCH:
317                         {
318                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::TOUCH_EVENTMGR);
319                                 if (eventmgr)
320                                 {
321                                         STR_String touchpropertyname;
322                                         bTouchSensor* blendertouchsensor = (bTouchSensor*)sens->data;
323                                         
324                                         if (blendertouchsensor->ma)
325                                         {
326                                                 touchpropertyname = (char*) (blendertouchsensor->ma->id.name+2);
327                                         }
328                                         bool bFindMaterial = true;
329                                         if (gameobj->GetPhysicsController())
330                                         {       
331                                                 gamesensor = new KX_TouchSensor(eventmgr,
332                                                         gameobj,
333                                                         bFindMaterial,
334                                                         touchpropertyname);
335                                         }
336                                 }
337                                 break;
338                         }
339                 case SENS_MESSAGE:
340                         {
341                                 KX_NetworkEventManager* eventmgr = (KX_NetworkEventManager*)
342                                         logicmgr->FindEventManager(SCA_EventManager::NETWORK_EVENTMGR);
343                                 if (eventmgr) {
344                                         bMessageSensor* msgSens = (bMessageSensor*) sens->data; 
345                                         
346                                         /* Get our NetworkScene */
347                                         NG_NetworkScene *NetworkScene = kxscene->GetNetworkScene();
348                                         /* filter on the incoming subjects, might be empty */
349                                         STR_String subject = (msgSens->subject
350                                                 ? (char*)msgSens->subject
351                                                 : "");
352                                         
353                                         gamesensor = new KX_NetworkMessageSensor(
354                                                 eventmgr,               // our eventmanager
355                                                 NetworkScene,   // our NetworkScene
356                                                 gameobj,                // the sensor controlling object
357                                                 subject);               // subject to filter on
358                                 }
359                                 break;
360                         }
361                 case SENS_NEAR:
362                         {
363                                 
364                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::TOUCH_EVENTMGR);
365                                 if (eventmgr)
366                                 {
367                                         STR_String nearpropertyname;    
368                                         bNearSensor* blendernearsensor = (bNearSensor*)sens->data;
369                                         if (blendernearsensor->name)
370                                         {
371                                                 // only objects that own this property will be taken into account
372                                                 nearpropertyname = (char*) blendernearsensor->name;
373                                         }
374                                         
375                                         //DT_ShapeHandle shape  =       DT_Sphere(0.0);
376                                         
377                                         // this sumoObject is not deleted by a gameobj, so delete it ourself
378                                         // later (memleaks)!
379                                         float radius = blendernearsensor->dist;
380                                         PHY__Vector3 pos;
381                                         const MT_Vector3& wpos = gameobj->NodeGetWorldPosition();
382                                         pos[0] = wpos[0];
383                                         pos[1] = wpos[1];
384                                         pos[2] = wpos[2];
385                                         pos[3] = 0.f;
386                                         bool bFindMaterial = false;
387                                         PHY_IPhysicsController* physCtrl = kxscene->GetPhysicsEnvironment()->CreateSphereController(radius,pos);
388
389                                         //will be done in KX_TouchEventManager::RegisterSensor()  
390                                         //if (isInActiveLayer)
391                                         //      kxscene->GetPhysicsEnvironment()->addSensor(physCtrl);
392
393                                                 
394
395                                         gamesensor = new KX_NearSensor(eventmgr,gameobj,
396                                                 blendernearsensor->dist,
397                                                 blendernearsensor->resetdist,
398                                                 bFindMaterial,
399                                                 nearpropertyname,kxscene,
400                                                 physCtrl
401                                                 );
402                                         
403                                 }
404                                 break;
405                         }
406                         
407                         
408                 case SENS_KEYBOARD:
409                         {
410                                 /* temporary input device, for converting the code for the keyboard sensor */
411                                 
412                                 bKeyboardSensor* blenderkeybdsensor = (bKeyboardSensor*)sens->data;
413                                 SCA_KeyboardManager* eventmgr = (SCA_KeyboardManager*) logicmgr->FindEventManager(SCA_EventManager::KEYBOARD_EVENTMGR);
414                                 if (eventmgr)
415                                 {
416                                         gamesensor = new SCA_KeyboardSensor(eventmgr,
417                                                 gReverseKeyTranslateTable[blenderkeybdsensor->key],
418                                                 blenderkeybdsensor->qual,
419                                                 blenderkeybdsensor->qual2,
420                                                 (blenderkeybdsensor->type == SENS_ALL_KEYS),
421                                                 blenderkeybdsensor->targetName,
422                                                 blenderkeybdsensor->toggleName,
423                                                 gameobj); //                    blenderkeybdsensor->pad);
424                                         
425                                 } 
426                                 
427                                 break;
428                         }
429                 case SENS_MOUSE:
430                         {
431                                 int keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_NODEF;                        
432                                 int trackfocus = 0;
433                                 bMouseSensor *bmouse = (bMouseSensor *)sens->data;
434                                 
435                                 /* There are two main types of mouse sensors. If there is
436                                 * no focus-related behaviour requested, we can make do
437                                 * with a basic sensor. This cuts down memory usage and
438                                 * gives a slight performance gain. */
439                                 
440                                 SCA_MouseManager *eventmgr 
441                                         = (SCA_MouseManager*) logicmgr->FindEventManager(SCA_EventManager::MOUSE_EVENTMGR);
442                                 if (eventmgr) {
443                                         
444                                         /* Determine key mode. There is at most one active mode. */
445                                         switch (bmouse->type) {
446                                         case BL_SENS_MOUSE_LEFT_BUTTON:
447                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_LEFTBUTTON;
448                                                 break;
449                                         case BL_SENS_MOUSE_MIDDLE_BUTTON:
450                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_MIDDLEBUTTON;
451                                                 break;
452                                         case BL_SENS_MOUSE_RIGHT_BUTTON:
453                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_RIGHTBUTTON;
454                                                 break;
455                                         case BL_SENS_MOUSE_WHEEL_UP:
456                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_WHEELUP;
457                                                 break;
458                                         case BL_SENS_MOUSE_WHEEL_DOWN:
459                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_WHEELDOWN;
460                                                 break;
461                                         case BL_SENS_MOUSE_MOVEMENT:
462                                                 keytype = SCA_MouseSensor::KX_MOUSESENSORMODE_MOVEMENT;
463                                                 break;
464                                         case BL_SENS_MOUSE_MOUSEOVER:
465                                                 trackfocus = 1;
466                                                 break;
467                                         case BL_SENS_MOUSE_MOUSEOVER_ANY:
468                                                 trackfocus = 2;
469                                                 break;
470
471                                         default:
472                                                 ; /* error */
473                                         }
474                                         
475                                         /* initial mouse position */                             
476                                         int startx  = canvas->GetWidth()/2;
477                                         int starty = canvas->GetHeight()/2;
478                                         
479                                         if (!trackfocus) {
480                                                 /* plain, simple mouse sensor */
481                                                 gamesensor = new SCA_MouseSensor(eventmgr,
482                                                         startx,starty,
483                                                         keytype,
484                                                         gameobj);
485                                         } else {
486                                                 /* give us a focus-aware sensor */
487                                                 gamesensor = new KX_MouseFocusSensor(eventmgr,
488                                                         startx,
489                                                         starty,
490                                                         keytype,
491                                                         trackfocus,
492                                                         canvas,
493                                                         kxscene,
494                                                         gameobj); 
495                                         }
496                                 } else {
497                                         //                              cout << "\n Could't find mouse event manager..."; - should throw an error here... 
498                                 }
499                                 break;
500                         }
501                 case SENS_PROPERTY:
502                         {
503                                 bPropertySensor* blenderpropsensor = (bPropertySensor*) sens->data;
504                                 SCA_EventManager* eventmgr 
505                                         = logicmgr->FindEventManager(SCA_EventManager::PROPERTY_EVENTMGR);
506                                 if (eventmgr)
507                                 {
508                                         STR_String propname=blenderpropsensor->name;
509                                         STR_String propval=blenderpropsensor->value;
510                                         STR_String propmaxval=blenderpropsensor->maxvalue;
511                                         
512                                         SCA_PropertySensor::KX_PROPSENSOR_TYPE 
513                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_NODEF;
514                                         
515                                         /* Better do an explicit conversion here! (was implicit      */
516                                         /* before...)                                                */
517                                         switch(blenderpropsensor->type) {
518                                         case SENS_PROP_EQUAL:
519                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_EQUAL;
520                                                 break;
521                                         case SENS_PROP_NEQUAL:
522                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_NOTEQUAL;
523                                                 break;
524                                         case SENS_PROP_INTERVAL:
525                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_INTERVAL;
526                                                 break;
527                                         case SENS_PROP_CHANGED:
528                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_CHANGED;
529                                                 break;
530                                         case SENS_PROP_EXPRESSION:
531                                                 propchecktype = SCA_PropertySensor::KX_PROPSENSOR_EXPRESSION;
532                                                 /* error */
533                                                 break;
534                                         default:
535                                                 ; /* error */
536                                         }
537                                         gamesensor = new SCA_PropertySensor(eventmgr,gameobj,propname,propval,propmaxval,propchecktype);
538                                 }
539                                 
540                                 break;
541                         }
542                         
543                 case SENS_RADAR:
544                         {
545                                 
546                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::TOUCH_EVENTMGR);
547                                 if (eventmgr)
548                                 {
549                                         STR_String radarpropertyname;
550                                         STR_String touchpropertyname;
551                                         bRadarSensor* blenderradarsensor = (bRadarSensor*) sens->data;
552                                         
553                                         int radaraxis = blenderradarsensor->axis;
554                                         
555                                         if (blenderradarsensor->name)
556                                         {
557                                                 // only objects that own this property will be taken into account
558                                                 radarpropertyname = (char*) blenderradarsensor->name;
559                                         }
560                                         
561                                         MT_Scalar coneheight = blenderradarsensor->range;
562                                         
563                                         // janco: the angle was doubled, so should I divide the factor in 2
564                                         // or the blenderradarsensor->angle?
565                                         // nzc: the angle is the opening angle. We need to init with 
566                                         // the axis-hull angle,so /2.0.
567                                         MT_Scalar factor = tan(MT_radians((blenderradarsensor->angle)/2.0));
568                                         //MT_Scalar coneradius = coneheight * (factor / 2);
569                                         MT_Scalar coneradius = coneheight * factor;
570                                         
571                                         
572                                         // this sumoObject is not deleted by a gameobj, so delete it ourself
573                                         // later (memleaks)!
574                                         MT_Scalar smallmargin = 0.0;
575                                         MT_Scalar largemargin = 0.0;
576                                         
577                                         bool bFindMaterial = false;
578                                         PHY_IPhysicsController* ctrl = kxscene->GetPhysicsEnvironment()->CreateConeController(coneradius,coneheight);
579
580                                         gamesensor = new KX_RadarSensor(
581                                                 eventmgr,
582                                                 gameobj,
583                                                 ctrl,
584                                                 coneradius,
585                                                 coneheight,
586                                                 radaraxis,
587                                                 smallmargin,
588                                                 largemargin,
589                                                 bFindMaterial,
590                                                 radarpropertyname,
591                                                 kxscene);
592                                                 
593                                 }
594                         
595                                 break;
596                         }
597                 case SENS_RAY:
598                         {
599                                 bRaySensor* blenderraysensor = (bRaySensor*) sens->data;
600                                 
601                                 //blenderradarsensor->angle;
602                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::RAY_EVENTMGR);
603                                 if (eventmgr)
604                                 {
605                                         bool bFindMaterial = (blenderraysensor->mode & SENS_COLLISION_MATERIAL);
606                                         
607                                         STR_String checkname = (bFindMaterial? blenderraysensor->matname : blenderraysensor->propname);
608
609                                         // don't want to get rays of length 0.0 or so
610                                         double distance = (blenderraysensor->range < 0.01 ? 0.01 : blenderraysensor->range );
611                                         int axis = blenderraysensor->axisflag;
612
613                                         
614                                         gamesensor = new KX_RaySensor(eventmgr,
615                                                                                                   gameobj,
616                                                                                                   checkname,
617                                                                                                   bFindMaterial,
618                                                                                                   distance,
619                                                                                                   axis,
620                                                                                                   kxscene);
621
622                                 }
623                                 break;
624                         }
625                         
626                 case SENS_RANDOM:
627                         {
628                                 bRandomSensor* blenderrndsensor = (bRandomSensor*) sens->data;
629                                 // some files didn't write randomsensor, avoid crash now for NULL ptr's
630                                 if (blenderrndsensor)
631                                 {
632                                         SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::RANDOM_EVENTMGR);
633                                         if (eventmgr)
634                                         {
635                                                 int randomSeed = blenderrndsensor->seed;
636                                                 gamesensor = new SCA_RandomSensor(eventmgr, gameobj, randomSeed);
637                                         }
638                                 }
639                                 break;
640                         }
641                 case SENS_JOYSTICK:
642                         {
643                                 int joysticktype = SCA_JoystickSensor::KX_JOYSENSORMODE_NODEF;
644                                 
645                                 bJoystickSensor* bjoy = (bJoystickSensor*) sens->data;
646                                 
647                                 SCA_JoystickManager *eventmgr 
648                                         = (SCA_JoystickManager*) logicmgr->FindEventManager(SCA_EventManager::JOY_EVENTMGR);
649                                 if (eventmgr) 
650                                 {
651                                         int axis        =0;
652                                         int axisf       =0;
653                                         int button      =0;
654                                         int buttonf =0; 
655                                         int hat         =0; 
656                                         int hatf        =0;
657                                         int prec        =0;
658                                         
659                                         switch(bjoy->type)
660                                         {
661                                         case SENS_JOY_AXIS:
662                                                 axis    = bjoy->axis;
663                                                 axisf   = bjoy->axisf;
664                                                 prec    = bjoy->precision;
665                                                 joysticktype  = SCA_JoystickSensor::KX_JOYSENSORMODE_AXIS;
666                                                 break;
667                                         case SENS_JOY_BUTTON:
668                                                 button  = bjoy->button;
669                                                 buttonf = bjoy->buttonf;
670                                                 joysticktype  = SCA_JoystickSensor::KX_JOYSENSORMODE_BUTTON;
671                                                 break;
672                                         case SENS_JOY_HAT:
673                                                 hat             = bjoy->hat;
674                                                 hatf    = bjoy->hatf;
675                                                 joysticktype  = SCA_JoystickSensor::KX_JOYSENSORMODE_HAT;
676                                                 break;
677                                         default:
678                                                 printf("Error: bad case statement\n");
679                                                 break;
680                                         }
681                                         gamesensor = new SCA_JoystickSensor(
682                                                 eventmgr,
683                                                 gameobj,
684                                                 joysticktype,
685                                                 axis,axisf,
686                                                 prec,
687                                                 button,buttonf,
688                                                 hat,hatf);
689                                 } 
690                                 else
691                                 {
692                                         printf("Error there was a problem finding the event manager\n");
693                                 }
694
695                                 break;
696                         }
697                 default:
698                         {
699                         }
700                 }
701
702                 if (gamesensor)
703                 {
704                         gamesensor->SetExecutePriority(executePriority++);
705                         STR_String uniquename = sens->name;
706                         uniquename += "#SENS#";
707                         uniqueint++;
708                         CIntValue* uniqueval = new CIntValue(uniqueint);
709                         uniquename += uniqueval->GetText();
710                         uniqueval->Release();
711                         
712                         /* Conversion succeeded, so we can set the generic props here.   */
713                         gamesensor->SetPulseMode(pos_pulsemode, 
714                                                                          neg_pulsemode, 
715                                                                          frequency);
716                         gamesensor->SetInvert(invert);
717                         gamesensor->SetName(STR_String(sens->name));                    
718                         
719                         gameobj->AddSensor(gamesensor);
720                         
721                         // only register to manager if it's in an active layer
722                         
723                         if (isInActiveLayer)
724                                 gamesensor->RegisterToManager();
725                         
726                         
727                         for (int i=0;i<sens->totlinks;i++)
728                         {
729                                 bController* linkedcont = (bController*) sens->links[i];
730                                 SCA_IController* gamecont = converter->FindGameController(linkedcont);
731
732                                 if (gamecont) {
733                                         logicmgr->RegisterToSensor(gamecont,gamesensor);
734                                 }
735                         }
736                         
737                 }
738                 sens=sens->next;
739         }
740 }
741