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