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