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