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