Patch to change license to GPL only, from GSR.
[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 <iostream>
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39
40 SCA_JoystickSensor::SCA_JoystickSensor(class SCA_JoystickManager* eventmgr,
41                                                                            SCA_IObject* gameobj,
42                                                                            short int joymode,
43                                                                            int axis, int axisf,int prec,
44                                                                            int button, int buttonf,
45                                                                            int hat, int hatf,
46                                                                            PyTypeObject* T )
47                                                                            :SCA_ISensor(gameobj,eventmgr,T),
48                                                                            m_pJoystickMgr(eventmgr),
49                                                                            m_axis(axis),
50                                                                            m_axisf(axisf),
51                                                                            m_button(button),
52                                                                            m_buttonf(buttonf),
53                                                                            m_hat(hat),
54                                                                            m_hatf(hatf),
55                                                                            m_precision(prec),
56                                                                            m_joymode(joymode)
57 {       
58 /*
59 std::cout << " axis "           << m_axis               << std::endl;
60 std::cout << " axis flag "      << m_axisf              << std::endl;
61 std::cout << " precision "      << m_precision  << std::endl;
62 std::cout << " button "         << m_button     << std::endl;
63 std::cout << " button flag "<< m_buttonf        << std::endl;
64 std::cout << " hat "            << m_hat                << std::endl;
65 std::cout << " hat flag "       << m_hatf               << std::endl;
66 */
67         m_istrig=0;
68 }
69
70
71 SCA_JoystickSensor::~SCA_JoystickSensor()
72 {
73 }
74
75
76 CValue* SCA_JoystickSensor::GetReplica()
77 {
78         CValue* replica = new SCA_JoystickSensor(*this);
79         // this will copy properties and so on...
80         CValue::AddDataToReplica(replica);
81         return replica;
82 }
83
84
85 bool SCA_JoystickSensor::IsPositiveTrigger()
86
87         bool result =   m_istrig;
88         if (m_invert)
89                 result = !result;
90         return result;
91 }
92
93
94 bool SCA_JoystickSensor::Evaluate(CValue* event)
95 {
96         SCA_Joystick *js = m_pJoystickMgr->GetJoystickDevice();
97         bool result = false;
98         
99         switch(m_joymode)
100         {
101         case KX_JOYSENSORMODE_AXIS:
102                 {
103                 /* what is what!
104                         m_axisf == 0 == right
105                         m_axisf == 1 == up
106                         m_axisf == 2 == left
107                         m_axisf == 3 == down
108                         numberof== m_axis  -- max 2
109                         */
110                         js->cSetPrecision(m_precision);
111                         if(m_axisf == 1){
112                                 if(js->aUpAxisIsPositive(m_axis)){
113                                         m_istrig =1;
114                                         result = true;
115                                 }else{
116                                         if(m_istrig){
117                                                 m_istrig = 0;
118                                                 result = true;
119                                         }
120                                 }
121                         }
122                         if(m_axisf == 3){
123                                 if(js->aDownAxisIsPositive(m_axis)){
124                                         m_istrig = 1;
125                                         result = true;
126                                 }else{
127                                         if(m_istrig){
128                                                 m_istrig = 0;
129                                                 result = true;
130                                         }
131                                 }
132                         }
133                         if(m_axisf == 2){
134                                 if(js->aLeftAxisIsPositive(m_axis)){
135                                         m_istrig = 1;
136                                         result = true;
137                                 }else{
138                                         if(m_istrig){
139                                                 m_istrig = 0;
140                                                 result = true;
141                                         }
142                                 }
143                         }
144                         if(m_axisf == 0){
145                                 if(js->aRightAxisIsPositive(m_axis)){
146                                         m_istrig = 1;
147                                         result = true;
148                                 }else{
149                                         if(m_istrig){
150                                                 m_istrig = 0;
151                                                 result = true;
152                                         }
153                                 }
154                         }
155                         break;
156                 }
157         case KX_JOYSENSORMODE_BUTTON:
158                 {
159                 /* what is what!
160                         pressed  = m_buttonf == 0
161                         released = m_buttonf == 1
162                         m_button = the actual button in question
163                         */
164                         if(m_buttonf == 0){
165                                 if(js->aButtonPressIsPositive(m_button)){
166                                         m_istrig = 1;
167                                         result = true;
168                                 }else {
169                                         if(m_istrig){
170                                                 m_istrig = 0;
171                                                 result = true;
172                                         }
173                                 }
174                         }
175                         if(m_buttonf == 1){
176                                 if(js->aButtonReleaseIsPositive(m_button)){
177                                         m_istrig = 1;
178                                         result = true;
179                                 }else {
180                                         if(m_istrig){
181                                                 m_istrig = 0;
182                                                 result = true;
183                                         }
184                                 }                       
185                         }
186                         break;
187                 }
188         case KX_JOYSENSORMODE_HAT:
189                 {
190                 /* what is what!
191                         numberof = m_hat  -- max 2
192                         direction= m_hatf -- max 12
193                         */
194                         if(m_hat == 1){
195                                 if(js->aHatIsPositive(m_hatf)){
196                                         m_istrig = 1;
197                                         result = true;
198                                 }else{
199                                         if(m_istrig){
200                                                 m_istrig = 0;
201                                                 result = true;
202                                         }
203                                 }
204                         }
205                         if(m_hat == 2){
206                                 if(js->aHatIsPositive(m_hatf)){
207                                         m_istrig = 1;
208                                         result = true;
209                                 }else{
210                                         if(m_istrig){
211                                                 m_istrig = 0;
212                                                 result = true;
213                                         }
214                                 }
215                         }
216                         /*
217                         if(m_hat == 3){
218                                 if(js->aHatIsPositive(m_hatf)){
219                                         m_istrig = 1;
220                                         result = true;
221                                 }else{
222                                         if(m_istrig){
223                                                 m_istrig = 0;
224                                                 result = true;
225                                         }
226                                 }
227                         }
228                         */
229                         break;
230                 }
231                 /* test for ball anyone ?*/
232         default:
233                 printf("Error invalid switch statement\n");
234                 break;
235         }
236         if(!js->IsTrig()){
237                 m_istrig = 0;
238         }
239         return result;
240 }
241
242
243 bool SCA_JoystickSensor::isValid(SCA_JoystickSensor::KX_JOYSENSORMODE m)
244 {
245         bool res = false;
246         res = ((m > KX_JOYSENSORMODE_NODEF) && (m < KX_JOYSENSORMODE_MAX));
247         return res;
248 }
249
250
251 /* ------------------------------------------------------------------------- */
252 /* Python functions                                                                                                              */
253 /* ------------------------------------------------------------------------- */
254
255 /* Integration hooks ------------------------------------------------------- */
256 PyTypeObject SCA_JoystickSensor::Type = {
257         PyObject_HEAD_INIT(&PyType_Type)
258                 0,
259                 "SCA_JoystickSensor",
260                 sizeof(SCA_JoystickSensor),
261                 0,
262                 PyDestructor,
263                 0,
264                 __getattr,
265                 __setattr,
266                 0, //&MyPyCompare,
267                 __repr,
268                 0, //&cvalue_as_number,
269                 0,
270                 0,
271                 0,
272                 0
273 };
274
275
276 PyParentObject SCA_JoystickSensor::Parents[] = {
277                 &SCA_JoystickSensor::Type,
278                 &SCA_ISensor::Type,
279                 &SCA_ILogicBrick::Type,
280                 &CValue::Type,
281                 NULL
282 };
283
284
285 PyMethodDef SCA_JoystickSensor::Methods[] = {
286         {"getAxis",      (PyCFunction) SCA_JoystickSensor::sPyGetAxis,          METH_NOARGS,    GetAxis_doc},
287         {"setAxis",      (PyCFunction) SCA_JoystickSensor::sPySetAxis,          METH_VARARGS,   SetAxis_doc},
288         {"getAxisValue", (PyCFunction) SCA_JoystickSensor::sPyGetRealAxis,      METH_NOARGS,    GetRealAxis_doc},
289         {"getThreshold", (PyCFunction) SCA_JoystickSensor::sPyGetThreshold, METH_NOARGS,        GetThreshold_doc},
290         {"setThreshold", (PyCFunction) SCA_JoystickSensor::sPySetThreshold, METH_VARARGS,       SetThreshold_doc},
291         {"getButton",    (PyCFunction) SCA_JoystickSensor::sPyGetButton,        METH_NOARGS,    GetButton_doc},
292         {"setButton",    (PyCFunction) SCA_JoystickSensor::sPySetButton,        METH_VARARGS,   SetButton_doc},
293         {"getHat",               (PyCFunction) SCA_JoystickSensor::sPyGetHat,           METH_NOARGS,    GetHat_doc},
294         {"setHat",               (PyCFunction) SCA_JoystickSensor::sPySetHat,           METH_VARARGS,   SetHat_doc},
295         {"getNumAxes",   (PyCFunction) SCA_JoystickSensor::sPyNumberOfAxes,     METH_NOARGS,    NumberOfAxes_doc},
296         {"getNumButtons",(PyCFunction) SCA_JoystickSensor::sPyNumberOfButtons,METH_NOARGS,      NumberOfButtons_doc},
297         {"getNumHats",   (PyCFunction) SCA_JoystickSensor::sPyNumberOfHats,     METH_NOARGS,    NumberOfHats_doc},
298         {NULL,NULL} //Sentinel
299 };
300
301
302 PyObject* SCA_JoystickSensor::_getattr(const STR_String& attr) {
303         _getattr_up(SCA_ISensor);
304 }
305
306
307 /* get axis  ---------------------------------------------------------- */
308 char SCA_JoystickSensor::GetAxis_doc[] = 
309 "getAxis\n"
310 "\tReturns the current state of the axis.\n";
311 PyObject* SCA_JoystickSensor::PyGetAxis( PyObject* self, 
312                                                                                 PyObject* args, 
313                                                                                 PyObject* kwds) {
314         return Py_BuildValue("[ii]",m_axis, m_axisf);
315 }
316
317
318 /* set axis  ---------------------------------------------------------- */
319 char SCA_JoystickSensor::SetAxis_doc[] = 
320 "setAxis\n"
321 "\tSets the current state of the axis.\n";
322 PyObject* SCA_JoystickSensor::PySetAxis( PyObject* self, 
323                                                                                 PyObject* args, 
324                                                                                 PyObject* kwds) {
325         
326         int axis,axisflag;
327         if(!PyArg_ParseTuple(args, "ii", &axis, &axisflag)){
328                 return NULL;
329         }
330         m_axis = axis;
331         m_axisf = axisflag;
332         Py_Return;
333 }
334
335
336 /* get axis value ----------------------------------------------------- */
337 char SCA_JoystickSensor::GetRealAxis_doc[] = 
338 "getAxisValue\n"
339 "\tReturns a list of the values for each axis .\n";
340 PyObject* SCA_JoystickSensor::PyGetRealAxis( PyObject* self, 
341                                                                                         PyObject* args, 
342                                                                                         PyObject* kwds) {
343         int a,b,c,d;
344         SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice();
345         a = joy->GetAxis10();
346         b = joy->GetAxis11();
347         c = joy->GetAxis20();
348         d = joy->GetAxis21();
349         return Py_BuildValue("[iiii]",a,b,c,d);
350 }
351
352
353 /* get threshold  ----------------------------------------------------- */
354 char SCA_JoystickSensor::GetThreshold_doc[] = 
355 "getThreshold\n"
356 "\tReturns the threshold of the axis.\n";
357 PyObject* SCA_JoystickSensor::PyGetThreshold( PyObject* self, 
358                                                                                          PyObject* args, 
359                                                                                          PyObject* kwds) {
360         return Py_BuildValue("i", m_precision);
361 }
362
363
364 /* set threshold  ----------------------------------------------------- */
365 char SCA_JoystickSensor::SetThreshold_doc[] = 
366 "setThreshold\n"
367 "\tSets the threshold of the axis.\n";
368 PyObject* SCA_JoystickSensor::PySetThreshold( PyObject* self, 
369                                                                                          PyObject* args, 
370                                                                                          PyObject* kwds) {
371         int thresh;
372         if(!PyArg_ParseTuple(args, "i", &thresh)){
373                 return NULL;
374         }
375         m_precision = thresh;
376         Py_Return;
377 }
378
379
380 /* get button  -------------------------------------------------------- */
381 char SCA_JoystickSensor::GetButton_doc[] = 
382 "getButton\n"
383 "\tReturns the currently pressed button.\n";
384 PyObject* SCA_JoystickSensor::PyGetButton( PyObject* self, 
385                                                                                   PyObject* args, 
386                                                                                   PyObject* kwds) {
387         return Py_BuildValue("[ii]",m_button, m_buttonf);
388 }
389
390
391 /* set button  -------------------------------------------------------- */
392 char SCA_JoystickSensor::SetButton_doc[] = 
393 "setButton\n"
394 "\tSets the button the sensor reacts to.\n";
395 PyObject* SCA_JoystickSensor::PySetButton( PyObject* self, 
396                                                                                   PyObject* args, 
397                                                                                   PyObject* kwds) {
398         int button,buttonflag;
399         if(!PyArg_ParseTuple(args, "ii", &button, &buttonflag)){
400                 return NULL;
401         }
402         m_button = button;
403         m_buttonf = buttonflag;
404         Py_Return;      
405 }
406
407
408 /* get hat      ----------------------------------------------------------- */
409 char SCA_JoystickSensor::GetHat_doc[] = 
410 "getHat\n"
411 "\tReturns the current direction of the hat.\n";
412 PyObject* SCA_JoystickSensor::PyGetHat( PyObject* self, 
413                                                                            PyObject* args, 
414                                                                            PyObject* kwds) {
415         return Py_BuildValue("[ii]",m_hat, m_hatf);
416 }
417
418
419 /* set hat      ----------------------------------------------------------- */
420 char SCA_JoystickSensor::SetHat_doc[] = 
421 "setHat\n"
422 "\tSets the hat the sensor reacts to.\n";
423 PyObject* SCA_JoystickSensor::PySetHat( PyObject* self, 
424                                                                            PyObject* args, 
425                                                                            PyObject* kwds) {
426         int hat,hatflag;
427         if(!PyArg_ParseTuple(args, "ii", &hat, &hatflag)){
428                 return NULL;
429         }
430         m_hat = hat;
431         m_hatf = hatflag;
432         Py_Return;
433 }
434
435
436 /* get # of ----------------------------------------------------- */
437 char SCA_JoystickSensor::NumberOfAxes_doc[] = 
438 "getNumAxes\n"
439 "\tReturns the number of axes .\n";
440 PyObject* SCA_JoystickSensor::PyNumberOfAxes( PyObject* self, 
441                                                                                         PyObject* args, 
442                                                                                         PyObject* kwds) {
443         int num;
444         SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice();
445         num = joy->GetNumberOfAxes();
446         return Py_BuildValue("i",num);
447 }
448
449
450 char SCA_JoystickSensor::NumberOfButtons_doc[] = 
451 "getNumButtons\n"
452 "\tReturns the number of buttons .\n";
453 PyObject* SCA_JoystickSensor::PyNumberOfButtons( PyObject* self, 
454                                                                                         PyObject* args, 
455                                                                                         PyObject* kwds) {
456         int num;
457         SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice();
458         num = joy->GetNumberOfButtons();
459         return Py_BuildValue("i",num);
460 }
461
462
463 char SCA_JoystickSensor::NumberOfHats_doc[] = 
464 "getNumHats\n"
465 "\tReturns the number of hats .\n";
466 PyObject* SCA_JoystickSensor::PyNumberOfHats( PyObject* self, 
467                                                                                         PyObject* args, 
468                                                                                         PyObject* kwds) {
469         int num;
470         SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice();
471         num = joy->GetNumberOfHats();
472         return Py_BuildValue("i",num);
473 }