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