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