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