f55921e648b1d6456942593e4054ae413240b8a8
[blender.git] / source / gameengine / GameLogic / SCA_JoystickSensor.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27 #include "SCA_JoystickManager.h"
28 #include "SCA_JoystickSensor.h"
29
30 #include "SCA_EventManager.h"
31 #include "SCA_LogicManager.h"
32
33 #include "PyObjectPlus.h"
34
35 #include <iostream>
36
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42
43 SCA_JoystickSensor::SCA_JoystickSensor(class SCA_JoystickManager* eventmgr,
44                                                                            SCA_IObject* gameobj,
45                                                                            short int joyindex,
46                                                                            short int joymode,
47                                                                            int axis, int axisf,int prec,
48                                                                            int button,
49                                                                            int hat, int hatf, bool allevents)
50                                                                            :SCA_ISensor(gameobj,eventmgr),
51                                                                            m_pJoystickMgr(eventmgr),
52                                                                            m_axis(axis),
53                                                                            m_axisf(axisf),
54                                                                            m_button(button),
55                                                                            m_hat(hat),
56                                                                            m_hatf(hatf),
57                                                                            m_precision(prec),
58                                                                            m_joymode(joymode),
59                                                                            m_joyindex(joyindex),
60                                                                            m_bAllEvents(allevents)
61 {       
62 /*
63 std::cout << " axis "           << m_axis               << std::endl;
64 std::cout << " axis flag "      << m_axisf              << std::endl;
65 std::cout << " precision "      << m_precision  << std::endl;
66 std::cout << " button "         << m_button     << std::endl;
67 std::cout << " hat "            << m_hat                << std::endl;
68 std::cout << " hat flag "       << m_hatf               << std::endl;
69 */
70         Init();
71 }
72
73 void SCA_JoystickSensor::Init()
74 {
75         m_istrig=(m_invert)?1:0;
76         m_istrig_prev=0;
77         m_reset = true;
78 }
79
80 SCA_JoystickSensor::~SCA_JoystickSensor()
81 {
82 }
83
84
85 CValue* SCA_JoystickSensor::GetReplica()
86 {
87         SCA_JoystickSensor* replica = new SCA_JoystickSensor(*this);
88         // this will copy properties and so on...
89         replica->ProcessReplica();
90         replica->Init();
91         return replica;
92 }
93
94
95 bool SCA_JoystickSensor::IsPositiveTrigger()
96
97         bool result =   m_istrig;
98         if (m_invert)
99                 result = !result;
100         return result;
101 }
102
103
104 bool SCA_JoystickSensor::Evaluate()
105 {
106         SCA_Joystick *js = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
107         bool result = false;
108         bool reset = m_reset && m_level;
109         
110         if(js==NULL) /* no joystick - dont do anything */
111                 return false;
112         
113         m_reset = false;
114         switch(m_joymode)
115         {
116         case KX_JOYSENSORMODE_AXIS:
117                 {
118                 /* what is what!
119                         m_axisf == JOYAXIS_RIGHT, JOYAXIS_UP, JOYAXIS_DOWN, JOYAXIS_LEFT
120                         m_axisf == 1 == up
121                         m_axisf == 2 == left
122                         m_axisf == 3 == down
123                         
124                         numberof== m_axis (1-4), range is half of JOYAXIS_MAX since 
125                                 it assumes the axis joysticks are axis parirs (0,1), (2,3), etc
126                                 also note that this starts at 1 where functions its used
127                                 with expect a zero index.
128                         */
129                         
130                         if (!js->IsTrigAxis() && !reset) /* No events from SDL? - dont bother */
131                                 return false;
132                         
133                         js->cSetPrecision(m_precision);
134                         if (m_bAllEvents) {
135                                 if(js->aAxisPairIsPositive(m_axis-1)){ /* use zero based axis index internally */
136                                         m_istrig = 1;
137                                         result = true;
138                                 }else{
139                                         if(m_istrig){
140                                                 m_istrig = 0;
141                                                 result = true;
142                                         }
143                                 }
144                         }
145                         else {
146                                 if(js->aAxisPairDirectionIsPositive(m_axis-1, m_axisf)){ /* use zero based axis index internally */
147                                         m_istrig = 1;
148                                         result = true;
149                                 }else{
150                                         if(m_istrig){
151                                                 m_istrig = 0;
152                                                 result = true;
153                                         }
154                                 }
155                         }
156                         break;
157                 }
158         case KX_JOYSENSORMODE_AXIS_SINGLE:
159                 {
160                         /* Like KX_JOYSENSORMODE_AXIS but dont pair up axis */
161                         if (!js->IsTrigAxis() && !reset) /* No events from SDL? - dont bother */
162                                 return false;
163                         
164                         /* No need for 'm_bAllEvents' check here since were only checking 1 axis */
165                         js->cSetPrecision(m_precision);
166                         if(js->aAxisIsPositive(m_axis-1)){ /* use zero based axis index internally */
167                                 m_istrig = 1;
168                                 result = true;
169                         }else{
170                                 if(m_istrig){
171                                         m_istrig = 0;
172                                         result = true;
173                                 }
174                         }
175                         break;
176                 }
177                 
178         case KX_JOYSENSORMODE_BUTTON:
179                 {
180                 /* what is what!
181                         m_button = the actual button in question
182                         */
183                         if (!js->IsTrigButton() && !reset) /* No events from SDL? - dont bother */
184                                 return false;
185                         
186                         if(( m_bAllEvents && js->aAnyButtonPressIsPositive()) || (!m_bAllEvents && js->aButtonPressIsPositive(m_button))) {
187                                 m_istrig = 1;
188                                 result = true;
189                         }else {
190                                 if(m_istrig){
191                                         m_istrig = 0;
192                                         result = true;
193                                 }
194                         }
195                         break;
196                 }
197         case KX_JOYSENSORMODE_HAT:
198                 {
199                 /* what is what!
200                         numberof = m_hat  -- max 4
201                         direction= m_hatf -- max 12
202                         */
203                         
204                         if (!js->IsTrigHat() && !reset) /* No events from SDL? - dont bother */
205                                 return false;
206                         
207                         if((m_bAllEvents && js->GetHat(m_hat-1)) || js->aHatIsPositive(m_hat-1, m_hatf)) {
208                                 m_istrig = 1;
209                                 result = true;
210                         }else{
211                                 if(m_istrig){
212                                         m_istrig = 0;
213                                         result = true;
214                                 }
215                         }
216                         break;
217                 }
218                 /* test for ball anyone ?*/
219         default:
220                 printf("Error invalid switch statement\n");
221                 break;
222         }
223         
224         /* if not all events are enabled, only send a positive pulse when 
225          * the button state changes */
226         if (!m_bAllEvents) {
227                 if (m_istrig_prev == m_istrig) {
228                         result = false;
229                 } else {
230                         m_istrig_prev = m_istrig;
231                 }
232         }
233         
234         if (reset)
235                 result = true;
236         
237         return result;
238 }
239
240
241 bool SCA_JoystickSensor::isValid(SCA_JoystickSensor::KX_JOYSENSORMODE m)
242 {
243         bool res = false;
244         res = ((m > KX_JOYSENSORMODE_NODEF) && (m < KX_JOYSENSORMODE_MAX));
245         return res;
246 }
247
248
249 /* ------------------------------------------------------------------------- */
250 /* Python functions                                                                                                              */
251 /* ------------------------------------------------------------------------- */
252
253 /* Integration hooks ------------------------------------------------------- */
254 PyTypeObject SCA_JoystickSensor::Type = {
255 #if (PY_VERSION_HEX >= 0x02060000)
256         PyVarObject_HEAD_INIT(NULL, 0)
257 #else
258         /* python 2.5 and below */
259         PyObject_HEAD_INIT( NULL )  /* required py macro */
260         0,                          /* ob_size */
261 #endif
262                 "SCA_JoystickSensor",
263                 sizeof(PyObjectPlus_Proxy),
264                 0,
265                 py_base_dealloc,
266                 0,
267                 0,
268                 0,
269                 0,
270                 py_base_repr,
271                 0,0,0,0,0,0,0,0,0,
272                 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
273                 0,0,0,0,0,0,0,
274                 Methods,
275                 0,
276                 0,
277                 &SCA_ISensor::Type,
278                 0,0,0,0,0,0,
279                 py_base_new
280 };
281
282 PyMethodDef SCA_JoystickSensor::Methods[] = {
283         //Deprecated functions ------>
284         {"getIndex",     (PyCFunction) SCA_JoystickSensor::sPyGetIndex,         METH_NOARGS,    (PY_METHODCHAR)GetIndex_doc},
285         {"setIndex",     (PyCFunction) SCA_JoystickSensor::sPySetIndex,         METH_O,                 (PY_METHODCHAR)SetIndex_doc},
286         {"getAxis",      (PyCFunction) SCA_JoystickSensor::sPyGetAxis,          METH_NOARGS,    (PY_METHODCHAR)GetAxis_doc},
287         {"setAxis",      (PyCFunction) SCA_JoystickSensor::sPySetAxis,          METH_VARARGS,   (PY_METHODCHAR)SetAxis_doc},
288         {"getAxisValue", (PyCFunction) SCA_JoystickSensor::sPyGetAxisValue,     METH_NOARGS,    (PY_METHODCHAR)GetAxisValue_doc},
289         {"getThreshold", (PyCFunction) SCA_JoystickSensor::sPyGetThreshold, METH_NOARGS,        (PY_METHODCHAR)GetThreshold_doc},
290         {"setThreshold", (PyCFunction) SCA_JoystickSensor::sPySetThreshold, METH_VARARGS,       (PY_METHODCHAR)SetThreshold_doc},
291         {"getButton",    (PyCFunction) SCA_JoystickSensor::sPyGetButton,        METH_NOARGS,    (PY_METHODCHAR)GetButton_doc},
292         {"setButton",    (PyCFunction) SCA_JoystickSensor::sPySetButton,        METH_O,                 (PY_METHODCHAR)SetButton_doc},
293         {"getHat",               (PyCFunction) SCA_JoystickSensor::sPyGetHat,           METH_NOARGS,    (PY_METHODCHAR)GetHat_doc},
294         {"setHat",               (PyCFunction) SCA_JoystickSensor::sPySetHat,           METH_VARARGS,   (PY_METHODCHAR)SetHat_doc},
295         {"getNumAxes",   (PyCFunction) SCA_JoystickSensor::sPyNumberOfAxes,     METH_NOARGS,    (PY_METHODCHAR)NumberOfAxes_doc},
296         {"getNumButtons",(PyCFunction) SCA_JoystickSensor::sPyNumberOfButtons,METH_NOARGS,      (PY_METHODCHAR)NumberOfButtons_doc},
297         {"getNumHats",   (PyCFunction) SCA_JoystickSensor::sPyNumberOfHats,     METH_NOARGS,    (PY_METHODCHAR)NumberOfHats_doc},
298         {"isConnected", (PyCFunction) SCA_JoystickSensor::sPyConnected,         METH_NOARGS,    (PY_METHODCHAR)Connected_doc},
299         {"getButtonValue",(PyCFunction) SCA_JoystickSensor::sPyGetButtonValue,  METH_NOARGS,(PY_METHODCHAR)GetButtonValue_doc},
300         //<----- Deprecated
301         {"getButtonActiveList",(PyCFunction) SCA_JoystickSensor::sPyGetButtonActiveList,        METH_NOARGS,(PY_METHODCHAR)GetButtonActiveList_doc},
302         {"getButtonStatus",(PyCFunction) SCA_JoystickSensor::sPyGetButtonStatus,        METH_VARARGS,(PY_METHODCHAR)GetButtonStatus_doc},
303         {NULL,NULL} //Sentinel
304 };
305
306 PyAttributeDef SCA_JoystickSensor::Attributes[] = {
307         KX_PYATTRIBUTE_SHORT_RW("index",0,JOYINDEX_MAX-1,true,SCA_JoystickSensor,m_joyindex),
308         KX_PYATTRIBUTE_INT_RW("threshold",0,32768,true,SCA_JoystickSensor,m_precision),
309         KX_PYATTRIBUTE_INT_RW("button",0,100,false,SCA_JoystickSensor,m_button),
310         KX_PYATTRIBUTE_INT_LIST_RW_CHECK("axis",0,3,true,SCA_JoystickSensor,m_axis,2,CheckAxis),
311         KX_PYATTRIBUTE_INT_LIST_RW_CHECK("hat",0,12,true,SCA_JoystickSensor,m_hat,2,CheckHat),
312         KX_PYATTRIBUTE_RO_FUNCTION("axisValues",        SCA_JoystickSensor, pyattr_get_axis_values),
313         KX_PYATTRIBUTE_RO_FUNCTION("axisSingle", SCA_JoystickSensor, pyattr_get_axis_single),
314         KX_PYATTRIBUTE_RO_FUNCTION("hatValues", SCA_JoystickSensor, pyattr_get_hat_values),
315         KX_PYATTRIBUTE_RO_FUNCTION("hatSingle", SCA_JoystickSensor, pyattr_get_hat_single),
316         KX_PYATTRIBUTE_RO_FUNCTION("numAxis",           SCA_JoystickSensor, pyattr_get_num_axis),
317         KX_PYATTRIBUTE_RO_FUNCTION("numButtons",        SCA_JoystickSensor, pyattr_get_num_buttons),
318         KX_PYATTRIBUTE_RO_FUNCTION("numHats",           SCA_JoystickSensor, pyattr_get_num_hats),
319         KX_PYATTRIBUTE_RO_FUNCTION("connected",         SCA_JoystickSensor, pyattr_get_connected),
320         //KX_PYATTRIBUTE_TODO("events"),
321         { NULL }        //Sentinel
322 };
323
324
325 /* get index ---------------------------------------------------------- */
326 const char SCA_JoystickSensor::GetIndex_doc[] = 
327 "getIndex\n"
328 "\tReturns the joystick index to use.\n";
329 PyObject* SCA_JoystickSensor::PyGetIndex( ) {
330         ShowDeprecationWarning("getIndex()", "the index property");
331         return PyLong_FromSsize_t(m_joyindex);
332 }
333
334
335 /* set index ---------------------------------------------------------- */
336 const char SCA_JoystickSensor::SetIndex_doc[] = 
337 "setIndex\n"
338 "\tSets the joystick index to use.\n";
339 PyObject* SCA_JoystickSensor::PySetIndex( PyObject* value ) {
340         ShowDeprecationWarning("setIndex()", "the index property");
341         int index = PyLong_AsSsize_t( value ); /* -1 on error, will raise an error in this case */
342         if (index < 0 || index >= JOYINDEX_MAX) {
343                 PyErr_SetString(PyExc_ValueError, "joystick index out of range or not an int");
344                 return NULL;
345         }
346         
347         m_joyindex = index;
348         Py_RETURN_NONE;
349 }
350
351 /* get axis  ---------------------------------------------------------- */
352 const char SCA_JoystickSensor::GetAxis_doc[] = 
353 "getAxis\n"
354 "\tReturns the current axis this sensor reacts to.\n";
355 PyObject* SCA_JoystickSensor::PyGetAxis( ) {
356         ShowDeprecationWarning("getAxis()", "the axis property");
357         return Py_BuildValue("[ii]",m_axis, m_axisf);
358 }
359
360
361 /* set axis  ---------------------------------------------------------- */
362 const char SCA_JoystickSensor::SetAxis_doc[] = 
363 "setAxis\n"
364 "\tSets the current axis this sensor reacts to.\n";
365 PyObject* SCA_JoystickSensor::PySetAxis( PyObject* args ) {
366         ShowDeprecationWarning("setAxis()", "the axis property");
367         
368         int axis,axisflag;
369         if(!PyArg_ParseTuple(args, "ii:setAxis", &axis, &axisflag)){
370                 return NULL;
371         }
372         m_axis = axis;
373         m_axisf = axisflag;
374         
375         CheckAxis((void *)this, NULL); /* clamp values */
376         Py_RETURN_NONE;
377 }
378
379
380 /* get axis value ----------------------------------------------------- */
381 const char SCA_JoystickSensor::GetAxisValue_doc[] = 
382 "getAxisValue\n"
383 "\tReturns a list of the values for the current state of each axis.\n";
384 PyObject* SCA_JoystickSensor::PyGetAxisValue( ) {
385         ShowDeprecationWarning("getAxisValue()", "the axisPosition property");
386         SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
387         
388         int axis_index= joy->GetNumberOfAxes();
389         PyObject *list= PyList_New(axis_index);
390         
391         while(axis_index--) {
392                 PyList_SET_ITEM(list, axis_index, PyLong_FromSsize_t(joy->GetAxisPosition(axis_index)));
393         }
394         
395         return list;
396 }
397
398
399 /* get threshold  ----------------------------------------------------- */
400 const char SCA_JoystickSensor::GetThreshold_doc[] = 
401 "getThreshold\n"
402 "\tReturns the threshold of the axis.\n";
403 PyObject* SCA_JoystickSensor::PyGetThreshold( ) {
404         ShowDeprecationWarning("getThreshold()", "the threshold property");
405         return PyLong_FromSsize_t(m_precision);
406 }
407
408
409 /* set threshold  ----------------------------------------------------- */
410 const char SCA_JoystickSensor::SetThreshold_doc[] = 
411 "setThreshold\n"
412 "\tSets the threshold of the axis.\n";
413 PyObject* SCA_JoystickSensor::PySetThreshold( PyObject* args ) {
414         ShowDeprecationWarning("setThreshold()", "the threshold property");
415         int thresh;
416         if(!PyArg_ParseTuple(args, "i:setThreshold", &thresh)){
417                 return NULL;
418         }
419         m_precision = thresh;
420         Py_RETURN_NONE;
421 }
422
423 /* get button  -------------------------------------------------------- */
424 const char SCA_JoystickSensor::GetButton_doc[] = 
425 "getButton\n"
426 "\tReturns the current button this sensor is checking.\n";
427 PyObject* SCA_JoystickSensor::PyGetButton( ) {
428         ShowDeprecationWarning("getButton()", "the button property");
429         return PyLong_FromSsize_t(m_button);
430 }
431
432 /* set button  -------------------------------------------------------- */
433 const char SCA_JoystickSensor::SetButton_doc[] = 
434 "setButton\n"
435 "\tSets the button the sensor reacts to.\n";
436 PyObject* SCA_JoystickSensor::PySetButton( PyObject* value ) {
437         ShowDeprecationWarning("setButton()", "the button property");
438         int button = PyLong_AsSsize_t(value);
439         if(button==-1 && PyErr_Occurred()) {
440                 PyErr_SetString(PyExc_ValueError, "expected an int");
441                 return NULL;
442         }
443         m_button = button;
444         Py_RETURN_NONE; 
445 }
446
447 /* get button value  -------------------------------------------------- */
448 const char SCA_JoystickSensor::GetButtonValue_doc[] = 
449 "getButtonValue\n"
450 "\tReturns a list containing the indicies of the current pressed state of each button.\n";
451 PyObject* SCA_JoystickSensor::PyGetButtonValue( ) {
452         ShowDeprecationWarning("getButtonValue()", "getButtonActiveList");
453         return PyGetButtonActiveList( );
454 }
455
456 /* get button active list  -------------------------------------------------- */
457 const char SCA_JoystickSensor::GetButtonActiveList_doc[] = 
458 "getButtonActiveList\n"
459 "\tReturns a list containing the indicies of the button currently pressed.\n";
460 PyObject* SCA_JoystickSensor::PyGetButtonActiveList( ) {
461         SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
462         PyObject *ls = PyList_New(0);
463         PyObject *value;
464         int i;
465         
466         if(joy) {
467                 for (i=0; i < joy->GetNumberOfButtons(); i++) {
468                         if (joy->aButtonPressIsPositive(i)) {
469                                 value = PyLong_FromSsize_t(i);
470                                 PyList_Append(ls, value);
471                                 Py_DECREF(value);
472                         }
473                 }
474         }
475         return ls;
476 }
477
478 /* get button status  -------------------------------------------------- */
479 const char SCA_JoystickSensor::GetButtonStatus_doc[] = 
480 "getButtonStatus(buttonIndex)\n"
481 "\tReturns a bool of the current pressed state of the specified button.\n";
482 PyObject* SCA_JoystickSensor::PyGetButtonStatus( PyObject* args ) {
483         SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
484         int index;
485         
486         if(!PyArg_ParseTuple(args, "i:getButtonStatus", &index)){
487                 return NULL;
488         }
489         if(joy && index >= 0 && index < joy->GetNumberOfButtons()) {
490                 return PyBool_FromLong(joy->aButtonPressIsPositive(index) ? 1 : 0);
491         }
492         return PyBool_FromLong(0);
493 }
494
495 /* get hat      ----------------------------------------------------------- */
496 const char SCA_JoystickSensor::GetHat_doc[] = 
497 "getHat\n"
498 "\tReturns the current direction of the hat.\n";
499 PyObject* SCA_JoystickSensor::PyGetHat( ) {
500         ShowDeprecationWarning("getHat()", "the hat property");
501         return Py_BuildValue("[ii]",m_hat, m_hatf);
502 }
503
504
505 /* set hat      ----------------------------------------------------------- */
506 const char SCA_JoystickSensor::SetHat_doc[] = 
507 "setHat\n"
508 "\tSets the hat the sensor reacts to.\n";
509 PyObject* SCA_JoystickSensor::PySetHat( PyObject* args ) {
510         ShowDeprecationWarning("setHat()", "the hat property");
511         int hat,hatflag;
512         if(!PyArg_ParseTuple(args, "ii:setHat", &hat, &hatflag)){
513                 return NULL;
514         }
515         m_hat = hat;
516         m_hatf = hatflag;
517         
518         CheckHat((void *)this, NULL); /* clamp values */
519         Py_RETURN_NONE;
520 }
521
522
523 /* get # of ----------------------------------------------------- */
524 const char SCA_JoystickSensor::NumberOfAxes_doc[] = 
525 "getNumAxes\n"
526 "\tReturns the number of axes .\n";
527 PyObject* SCA_JoystickSensor::PyNumberOfAxes( ) {
528         ShowDeprecationWarning("getNumAxes()", "the numAxis property");
529         SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
530         // when the joystick is null their is 0 exis still. dumb but scripters should use isConnected()
531         return PyLong_FromSsize_t( joy ? joy->GetNumberOfAxes() : 0 );
532 }
533
534
535 const char SCA_JoystickSensor::NumberOfButtons_doc[] = 
536 "getNumButtons\n"
537 "\tReturns the number of buttons .\n";
538 PyObject* SCA_JoystickSensor::PyNumberOfButtons( ) {
539         ShowDeprecationWarning("getNumButtons()", "the numButtons property");
540         SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
541         return PyLong_FromSsize_t( joy ? joy->GetNumberOfButtons() : 0 );
542 }
543
544
545 const char SCA_JoystickSensor::NumberOfHats_doc[] = 
546 "getNumHats\n"
547 "\tReturns the number of hats .\n";
548 PyObject* SCA_JoystickSensor::PyNumberOfHats( ) {
549         ShowDeprecationWarning("getNumHats()", "the numHats property");
550         SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
551         return PyLong_FromSsize_t( joy ? joy->GetNumberOfHats() : 0 );
552 }
553
554 const char SCA_JoystickSensor::Connected_doc[] = 
555 "getConnected\n"
556 "\tReturns True if a joystick is connected at this joysticks index.\n";
557 PyObject* SCA_JoystickSensor::PyConnected( ) {
558         ShowDeprecationWarning("getConnected()", "the connected property");
559         SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
560         return PyBool_FromLong( joy ? joy->Connected() : 0 );
561 }
562
563
564 PyObject* SCA_JoystickSensor::pyattr_get_axis_values(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
565 {
566         SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
567         SCA_Joystick *joy = self->m_pJoystickMgr->GetJoystickDevice(self->m_joyindex);
568         
569         int axis_index= joy->GetNumberOfAxes();
570         PyObject *list= PyList_New(axis_index);
571         
572         while(axis_index--) {
573                 PyList_SET_ITEM(list, axis_index, PyLong_FromSsize_t(joy->GetAxisPosition(axis_index)));
574         }
575         
576         return list;
577 }
578
579 PyObject* SCA_JoystickSensor::pyattr_get_axis_single(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
580 {
581         SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
582         SCA_Joystick *joy = self->m_pJoystickMgr->GetJoystickDevice(self->m_joyindex);
583         
584         if(self->m_joymode != KX_JOYSENSORMODE_AXIS_SINGLE) {
585                 PyErr_SetString(PyExc_TypeError, "val = sensor.axisSingle: Joystick Sensor, not 'Single Axis' type");
586                 return NULL;
587         }
588         
589         return PyLong_FromSsize_t(joy->GetAxisPosition(self->m_axis-1));
590 }
591
592 PyObject* SCA_JoystickSensor::pyattr_get_hat_values(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
593 {
594         SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
595         SCA_Joystick *joy = self->m_pJoystickMgr->GetJoystickDevice(self->m_joyindex);
596         
597         int hat_index= joy->GetNumberOfHats();
598         PyObject *list= PyList_New(hat_index);
599         
600         while(hat_index--) {
601                 PyList_SET_ITEM(list, hat_index, PyLong_FromSsize_t(joy->GetHat(hat_index)));
602         }
603         
604         return list;
605 }
606
607 PyObject* SCA_JoystickSensor::pyattr_get_hat_single(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
608 {
609         SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
610         SCA_Joystick *joy = self->m_pJoystickMgr->GetJoystickDevice(self->m_joyindex);
611         
612         return PyLong_FromSsize_t(joy->GetHat(self->m_hat-1));
613 }
614
615 PyObject* SCA_JoystickSensor::pyattr_get_num_axis(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
616 {
617         SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
618         SCA_Joystick *joy = self->m_pJoystickMgr->GetJoystickDevice(self->m_joyindex);
619         return PyLong_FromSsize_t( joy ? joy->GetNumberOfAxes() : 0 );
620 }
621
622 PyObject* SCA_JoystickSensor::pyattr_get_num_buttons(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
623 {
624         SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
625         SCA_Joystick *joy = self->m_pJoystickMgr->GetJoystickDevice(self->m_joyindex);
626         return PyLong_FromSsize_t( joy ? joy->GetNumberOfButtons() : 0 );
627 }
628
629 PyObject* SCA_JoystickSensor::pyattr_get_num_hats(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
630 {
631         SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
632         SCA_Joystick *joy = self->m_pJoystickMgr->GetJoystickDevice(self->m_joyindex);
633         return PyLong_FromSsize_t( joy ? joy->GetNumberOfHats() : 0 );
634 }
635
636 PyObject* SCA_JoystickSensor::pyattr_get_connected(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
637 {
638         SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
639         SCA_Joystick *joy = self->m_pJoystickMgr->GetJoystickDevice(self->m_joyindex);
640         return PyBool_FromLong( joy ? joy->Connected() : 0 );
641 }