joystick update
[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                                                                            PyTypeObject* T )
51                                                                            :SCA_ISensor(gameobj,eventmgr,T),
52                                                                            m_pJoystickMgr(eventmgr),
53                                                                            m_axis(axis),
54                                                                            m_axisf(axisf),
55                                                                            m_button(button),
56                                                                            m_hat(hat),
57                                                                            m_hatf(hatf),
58                                                                            m_precision(prec),
59                                                                            m_joymode(joymode),
60                                                                            m_joyindex(joyindex),
61                                                                            m_bAllEvents(allevents)
62 {       
63 /*
64 std::cout << " axis "           << m_axis               << std::endl;
65 std::cout << " axis flag "      << m_axisf              << std::endl;
66 std::cout << " precision "      << m_precision  << std::endl;
67 std::cout << " button "         << m_button     << std::endl;
68 std::cout << " hat "            << m_hat                << std::endl;
69 std::cout << " hat flag "       << m_hatf               << std::endl;
70 */
71         Init();
72 }
73
74 void SCA_JoystickSensor::Init()
75 {
76         m_istrig=(m_invert)?1: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         CValue::AddDataToReplica(replica);
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(CValue* event)
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         if (!js->IsTrig()) /* No events from SDL? - dont bother */
114                 return reset ? true : false;
115         
116         m_reset = false;
117         switch(m_joymode)
118         {
119         case KX_JOYSENSORMODE_AXIS:
120                 {
121                 /* what is what!
122                         m_axisf == 0 == right
123                         m_axisf == 1 == up
124                         m_axisf == 2 == left
125                         m_axisf == 3 == down
126                         numberof== m_axis  -- max 2
127                         */
128                         js->cSetPrecision(m_precision);
129                         if (m_bAllEvents) {
130                                 if(js->aAnyAxisIsPositive(m_axis)){
131                                         m_istrig = 1;
132                                         result = true;
133                                 }else{
134                                         if(m_istrig){
135                                                 m_istrig = 0;
136                                                 result = true;
137                                         }
138                                 }
139                         }
140                         else if(m_axisf == 1){
141                                 if(js->aUpAxisIsPositive(m_axis)){
142                                         m_istrig = 1;
143                                         result = true;
144                                 }else{
145                                         if(m_istrig){
146                                                 m_istrig = 0;
147                                                 result = true;
148                                         }
149                                 }
150                         }
151                         else if(m_axisf == 3){
152                                 if(js->aDownAxisIsPositive(m_axis)){
153                                         m_istrig = 1;
154                                         result = true;
155                                 }else{
156                                         if(m_istrig){
157                                                 m_istrig = 0;
158                                                 result = true;
159                                         }
160                                 }
161                         }
162                         else if(m_axisf == 2){
163                                 if(js->aLeftAxisIsPositive(m_axis)){
164                                         m_istrig = 1;
165                                         result = true;
166                                 }else{
167                                         if(m_istrig){
168                                                 m_istrig = 0;
169                                                 result = true;
170                                         }
171                                 }
172                         }
173                         else if(m_axisf == 0){
174                                 if(js->aRightAxisIsPositive(m_axis)){
175                                         m_istrig = 1;
176                                         result = true;
177                                 }else{
178                                         if(m_istrig){
179                                                 m_istrig = 0;
180                                                 result = true;
181                                         }
182                                 }
183                         }
184                         break;
185                 }
186         case KX_JOYSENSORMODE_BUTTON:
187                 {
188                 /* what is what!
189                         m_button = the actual button in question
190                         */
191                         if(( m_bAllEvents && js->aAnyButtonPressIsPositive()) || (!m_bAllEvents && js->aButtonPressIsPositive(m_button))) {
192                                 m_istrig = 1;
193                                 result = true;
194                         }else {
195                                 if(m_istrig){
196                                         m_istrig = 0;
197                                         result = true;
198                                 }
199                         }
200                         break;
201                 }
202         case KX_JOYSENSORMODE_HAT:
203                 {
204                 /* what is what!
205                         numberof = m_hat  -- max 2
206                         direction= m_hatf -- max 12
207                         */
208                         if(m_hat == 1){
209                                 if(js->aHatIsPositive(m_hatf)){
210                                         m_istrig = 1;
211                                         result = true;
212                                 }else{
213                                         if(m_istrig){
214                                                 m_istrig = 0;
215                                                 result = true;
216                                         }
217                                 }
218                         }
219                         if(m_hat == 2){
220                                 if(js->aHatIsPositive(m_hatf)){
221                                         m_istrig = 1;
222                                         result = true;
223                                 }else{
224                                         if(m_istrig){
225                                                 m_istrig = 0;
226                                                 result = true;
227                                         }
228                                 }
229                         }
230                         /*
231                         if(m_hat == 3){
232                                 if(js->aHatIsPositive(m_hatf)){
233                                         m_istrig = 1;
234                                         result = true;
235                                 }else{
236                                         if(m_istrig){
237                                                 m_istrig = 0;
238                                                 result = true;
239                                         }
240                                 }
241                         }
242                         */
243                         break;
244                 }
245                 /* test for ball anyone ?*/
246         default:
247                 printf("Error invalid switch statement\n");
248                 break;
249         }
250         
251         /* if not all events are enabled, only send a positive pulse when 
252          * the button state changes */
253         if (!m_bAllEvents) {
254                 if (m_istrig_prev == m_istrig) {
255                         result = false;
256                 } else {
257                         m_istrig_prev = m_istrig;
258                 }
259         }
260         
261         if (reset)
262                 result = true;
263         
264         return result;
265 }
266
267
268 bool SCA_JoystickSensor::isValid(SCA_JoystickSensor::KX_JOYSENSORMODE m)
269 {
270         bool res = false;
271         res = ((m > KX_JOYSENSORMODE_NODEF) && (m < KX_JOYSENSORMODE_MAX));
272         return res;
273 }
274
275
276 /* ------------------------------------------------------------------------- */
277 /* Python functions                                                                                                              */
278 /* ------------------------------------------------------------------------- */
279
280 /* Integration hooks ------------------------------------------------------- */
281 PyTypeObject SCA_JoystickSensor::Type = {
282         PyObject_HEAD_INIT(&PyType_Type)
283                 0,
284                 "SCA_JoystickSensor",
285                 sizeof(SCA_JoystickSensor),
286                 0,
287                 PyDestructor,
288                 0,
289                 __getattr,
290                 __setattr,
291                 0, //&MyPyCompare,
292                 __repr,
293                 0, //&cvalue_as_number,
294                 0,
295                 0,
296                 0,
297                 0
298 };
299
300
301 PyParentObject SCA_JoystickSensor::Parents[] = {
302                 &SCA_JoystickSensor::Type,
303                 &SCA_ISensor::Type,
304                 &SCA_ILogicBrick::Type,
305                 &CValue::Type,
306                 NULL
307 };
308
309
310 PyMethodDef SCA_JoystickSensor::Methods[] = {
311         {"getIndex",     (PyCFunction) SCA_JoystickSensor::sPyGetIndex,         METH_NOARGS,    (PY_METHODCHAR)GetIndex_doc},
312         {"setIndex",     (PyCFunction) SCA_JoystickSensor::sPySetIndex,         METH_O,                 (PY_METHODCHAR)SetIndex_doc},
313         {"getAxis",      (PyCFunction) SCA_JoystickSensor::sPyGetAxis,          METH_NOARGS,    (PY_METHODCHAR)GetAxis_doc},
314         {"setAxis",      (PyCFunction) SCA_JoystickSensor::sPySetAxis,          METH_VARARGS,   (PY_METHODCHAR)SetAxis_doc},
315         {"getAxisValue", (PyCFunction) SCA_JoystickSensor::sPyGetAxisValue,     METH_NOARGS,    (PY_METHODCHAR)GetAxisValue_doc},
316         {"getThreshold", (PyCFunction) SCA_JoystickSensor::sPyGetThreshold, METH_NOARGS,        (PY_METHODCHAR)GetThreshold_doc},
317         {"setThreshold", (PyCFunction) SCA_JoystickSensor::sPySetThreshold, METH_VARARGS,       (PY_METHODCHAR)SetThreshold_doc},
318         {"getButton",    (PyCFunction) SCA_JoystickSensor::sPyGetButton,        METH_NOARGS,    (PY_METHODCHAR)GetButton_doc},
319         {"setButton",    (PyCFunction) SCA_JoystickSensor::sPySetButton,        METH_O,                 (PY_METHODCHAR)SetButton_doc},
320         {"getButtonValue",(PyCFunction) SCA_JoystickSensor::sPyGetButtonValue,  METH_NOARGS,(PY_METHODCHAR)GetButtonValue_doc},
321         {"getHat",               (PyCFunction) SCA_JoystickSensor::sPyGetHat,           METH_NOARGS,    (PY_METHODCHAR)GetHat_doc},
322         {"setHat",               (PyCFunction) SCA_JoystickSensor::sPySetHat,           METH_VARARGS,   (PY_METHODCHAR)SetHat_doc},
323         {"getNumAxes",   (PyCFunction) SCA_JoystickSensor::sPyNumberOfAxes,     METH_NOARGS,    (PY_METHODCHAR)NumberOfAxes_doc},
324         {"getNumButtons",(PyCFunction) SCA_JoystickSensor::sPyNumberOfButtons,METH_NOARGS,      (PY_METHODCHAR)NumberOfButtons_doc},
325         {"getNumHats",   (PyCFunction) SCA_JoystickSensor::sPyNumberOfHats,     METH_NOARGS,    (PY_METHODCHAR)NumberOfHats_doc},
326         {"isConnected", (PyCFunction) SCA_JoystickSensor::sPyConnected,         METH_NOARGS,    (PY_METHODCHAR)Connected_doc},
327         {NULL,NULL} //Sentinel
328 };
329
330
331 PyObject* SCA_JoystickSensor::_getattr(const STR_String& attr) {
332         _getattr_up(SCA_ISensor);
333 }
334
335
336 /* get index ---------------------------------------------------------- */
337 const char SCA_JoystickSensor::GetIndex_doc[] = 
338 "getIndex\n"
339 "\tReturns the joystick index to use.\n";
340 PyObject* SCA_JoystickSensor::PyGetIndex( PyObject* self ) {
341         return PyInt_FromLong(m_joyindex);
342 }
343
344
345 /* set index ---------------------------------------------------------- */
346 const char SCA_JoystickSensor::SetIndex_doc[] = 
347 "setIndex\n"
348 "\tSets the joystick index to use.\n";
349 PyObject* SCA_JoystickSensor::PySetIndex( PyObject* self, PyObject* value ) {
350         int index = PyInt_AsLong( value ); /* -1 on error, will raise an error in this case */
351         if (index < 0 || index >= JOYINDEX_MAX) {
352                 PyErr_SetString(PyExc_ValueError, "joystick index out of range or not an int");
353                 return NULL;
354         }
355         
356         m_joyindex = index;
357         Py_RETURN_NONE;
358 }
359
360 /* get axis  ---------------------------------------------------------- */
361 const char SCA_JoystickSensor::GetAxis_doc[] = 
362 "getAxis\n"
363 "\tReturns the current state of the axis.\n";
364 PyObject* SCA_JoystickSensor::PyGetAxis( PyObject* self) {
365         return Py_BuildValue("[ii]",m_axis, m_axisf);
366 }
367
368
369 /* set axis  ---------------------------------------------------------- */
370 const char SCA_JoystickSensor::SetAxis_doc[] = 
371 "setAxis\n"
372 "\tSets the current state of the axis.\n";
373 PyObject* SCA_JoystickSensor::PySetAxis( PyObject* self, PyObject* args ) {
374         
375         int axis,axisflag;
376         if(!PyArg_ParseTuple(args, "ii", &axis, &axisflag)){
377                 return NULL;
378         }
379         m_axis = axis;
380         m_axisf = axisflag;
381         Py_RETURN_NONE;
382 }
383
384
385 /* get axis value ----------------------------------------------------- */
386 const char SCA_JoystickSensor::GetAxisValue_doc[] = 
387 "getAxisValue\n"
388 "\tReturns a list of the values for each axis .\n";
389 PyObject* SCA_JoystickSensor::PyGetAxisValue( PyObject* self) {
390         SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
391         if(joy)
392                 return Py_BuildValue("[iiii]", joy->GetAxis10(), joy->GetAxis11(), joy->GetAxis20(), joy->GetAxis21());
393         else
394                 return Py_BuildValue("[iiii]", 0, 0, 0, 0);
395 }
396
397
398 /* get threshold  ----------------------------------------------------- */
399 const char SCA_JoystickSensor::GetThreshold_doc[] = 
400 "getThreshold\n"
401 "\tReturns the threshold of the axis.\n";
402 PyObject* SCA_JoystickSensor::PyGetThreshold( PyObject* self) {
403         return PyInt_FromLong(m_precision);
404 }
405
406
407 /* set threshold  ----------------------------------------------------- */
408 const char SCA_JoystickSensor::SetThreshold_doc[] = 
409 "setThreshold\n"
410 "\tSets the threshold of the axis.\n";
411 PyObject* SCA_JoystickSensor::PySetThreshold( PyObject* self, PyObject* args ) {
412         int thresh;
413         if(!PyArg_ParseTuple(args, "i", &thresh)){
414                 return NULL;
415         }
416         m_precision = thresh;
417         Py_RETURN_NONE;
418 }
419
420 /* get button  -------------------------------------------------------- */
421 const char SCA_JoystickSensor::GetButton_doc[] = 
422 "getButton\n"
423 "\tReturns the current button this sensor is checking.\n";
424 PyObject* SCA_JoystickSensor::PyGetButton( PyObject* self) {
425         return PyInt_FromLong(m_button);
426 }
427
428 /* set button  -------------------------------------------------------- */
429 const char SCA_JoystickSensor::SetButton_doc[] = 
430 "setButton\n"
431 "\tSets the button the sensor reacts to.\n";
432 PyObject* SCA_JoystickSensor::PySetButton( PyObject* self, PyObject* value ) {
433         int button = PyInt_AsLong(value);
434         if(button==-1 && PyErr_Occurred()) {
435                 PyErr_SetString(PyExc_ValueError, "expected an int");
436                 return NULL;
437         }
438         m_button = button;
439         Py_RETURN_NONE; 
440 }
441
442 /* get button value  -------------------------------------------------- */
443 const char SCA_JoystickSensor::GetButtonValue_doc[] = 
444 "getButtonValue\n"
445 "\tReturns a list containing the indicies of the currently pressed buttons.\n";
446 PyObject* SCA_JoystickSensor::PyGetButtonValue( PyObject* self) {
447         SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
448         PyObject *ls = PyList_New(0);
449         PyObject *value;
450         int i;
451         
452         if(joy) {
453                 for (i=0; i < joy->GetNumberOfButtons(); i++) {
454                         if (joy->aButtonPressIsPositive(i)) {
455                                 value = PyInt_FromLong(i);
456                                 PyList_Append(ls, value);
457                                 Py_DECREF(value);
458                         }
459                 }
460         }
461         return ls;
462 }
463
464 /* get hat      ----------------------------------------------------------- */
465 const char SCA_JoystickSensor::GetHat_doc[] = 
466 "getHat\n"
467 "\tReturns the current direction of the hat.\n";
468 PyObject* SCA_JoystickSensor::PyGetHat( PyObject* self ) {
469         return Py_BuildValue("[ii]",m_hat, m_hatf);
470 }
471
472
473 /* set hat      ----------------------------------------------------------- */
474 const char SCA_JoystickSensor::SetHat_doc[] = 
475 "setHat\n"
476 "\tSets the hat the sensor reacts to.\n";
477 PyObject* SCA_JoystickSensor::PySetHat( PyObject* self, PyObject* args ) {
478         int hat,hatflag;
479         if(!PyArg_ParseTuple(args, "ii", &hat, &hatflag)){
480                 return NULL;
481         }
482         m_hat = hat;
483         m_hatf = hatflag;
484         Py_RETURN_NONE;
485 }
486
487
488 /* get # of ----------------------------------------------------- */
489 const char SCA_JoystickSensor::NumberOfAxes_doc[] = 
490 "getNumAxes\n"
491 "\tReturns the number of axes .\n";
492 PyObject* SCA_JoystickSensor::PyNumberOfAxes( PyObject* self ) {
493         SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
494         // when the joystick is null their is 0 exis still. dumb but scripters should use isConnected()
495         return PyInt_FromLong( joy ? joy->GetNumberOfAxes() : 0 );
496 }
497
498
499 const char SCA_JoystickSensor::NumberOfButtons_doc[] = 
500 "getNumButtons\n"
501 "\tReturns the number of buttons .\n";
502 PyObject* SCA_JoystickSensor::PyNumberOfButtons( PyObject* self ) {
503         SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
504         return PyInt_FromLong( joy ? joy->GetNumberOfButtons() : 0 );
505 }
506
507
508 const char SCA_JoystickSensor::NumberOfHats_doc[] = 
509 "getNumHats\n"
510 "\tReturns the number of hats .\n";
511 PyObject* SCA_JoystickSensor::PyNumberOfHats( PyObject* self ) {
512         SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
513         return PyInt_FromLong( joy ? joy->GetNumberOfHats() : 0 );
514 }
515
516 const char SCA_JoystickSensor::Connected_doc[] = 
517 "getConnected\n"
518 "\tReturns True if a joystick is connected at this joysticks index.\n";
519 PyObject* SCA_JoystickSensor::PyConnected( PyObject* self ) {
520         SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
521         return PyBool_FromLong( joy ? joy->Connected() : 0 );
522 }