NLA SoC: Merge from 2.5
[blender.git] / source / gameengine / GameLogic / SCA_JoystickSensor.h
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
28
29 #ifndef __JOYSENSOR_H_
30 #define __JOYSENSOR_H
31
32 #include "SCA_ISensor.h"
33 #include "./Joystick/SCA_JoystickDefines.h"
34
35 class SCA_JoystickSensor :public SCA_ISensor
36 {
37         Py_Header;
38         class SCA_JoystickManager*      m_pJoystickMgr;
39         
40         /**
41          * Axis 1-JOYAXIS_MAX, MUST be followed by m_axisf
42          */
43         int     m_axis;
44         /**
45          * Axis flag to find direction, MUST be an int
46          */
47         int     m_axisf;
48         /**
49          * The actual button
50          */
51         int     m_button;
52         /**
53          * Flag for a pressed or released button
54          */
55         int     m_buttonf;
56         /**
57          * The actual hat 1-JOYHAT_MAX. MUST be followed by m_hatf
58          */
59         int     m_hat;
60         /**
61          * Flag to find direction 1-12, MUST be an int
62          */
63         int     m_hatf;
64         /**
65          * The threshold value the axis acts opon
66          */
67         int     m_precision;
68         /**
69          * Is an event triggered ?
70          */
71         bool    m_istrig;
72         /**
73          * Last trigger state for this sensors joystick,
74          * Otherwise it will trigger all the time
75          * this is used to see if the trigger state changes.
76          */
77         bool    m_istrig_prev;
78         /**
79          * The mode to determine axis,button or hat
80          */
81         short int m_joymode;
82         /**
83          * Select which joystick to use
84          */
85         short int m_joyindex;
86
87         /**
88          * Detect all events for the currently selected type
89          */
90         bool m_bAllEvents;
91
92         enum KX_JOYSENSORMODE {
93                 KX_JOYSENSORMODE_NODEF = 0,
94                 KX_JOYSENSORMODE_AXIS,
95                 KX_JOYSENSORMODE_BUTTON,
96                 KX_JOYSENSORMODE_HAT,
97                 KX_JOYSENSORMODE_AXIS_SINGLE,
98                 KX_JOYSENSORMODE_MAX
99         };
100         bool isValid(KX_JOYSENSORMODE);
101
102 public:
103         SCA_JoystickSensor(class SCA_JoystickManager* eventmgr,
104                                            SCA_IObject* gameobj,
105                                            short int joyindex,
106                                            short int joymode,
107                                            int axis, int axisf,int prec,
108                                            int button,
109                                            int hat, int hatf, bool allevents);
110         virtual ~SCA_JoystickSensor();
111         virtual CValue* GetReplica();
112         
113         virtual bool Evaluate();
114         virtual bool IsPositiveTrigger();
115         virtual void Init();
116         
117         short int GetJoyIndex(void){
118                 return m_joyindex;
119         }
120
121         /* --------------------------------------------------------------------- */
122         /* Python interface ---------------------------------------------------- */
123         /* --------------------------------------------------------------------- */
124
125         /* Joystick Index */
126         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetIndex);
127         KX_PYMETHOD_DOC_O(SCA_JoystickSensor,SetIndex);
128         /* Axes*/
129         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetAxis);
130         KX_PYMETHOD_DOC_VARARGS(SCA_JoystickSensor,SetAxis);
131         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetAxisValue);
132         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetThreshold);
133         KX_PYMETHOD_DOC_VARARGS(SCA_JoystickSensor,SetThreshold);
134         /* Buttons */
135         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetButton);
136         KX_PYMETHOD_DOC_O(SCA_JoystickSensor,SetButton);
137         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetButtonValue);
138         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetButtonActiveList);
139         KX_PYMETHOD_DOC_VARARGS(SCA_JoystickSensor,GetButtonStatus);
140         /* Hats */
141         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetHat);
142         KX_PYMETHOD_DOC_VARARGS(SCA_JoystickSensor,SetHat);
143         /* number of */
144         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,NumberOfAxes);
145         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,NumberOfButtons);
146         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,NumberOfHats);
147         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,Connected);
148
149         static PyObject*        pyattr_get_axis_values(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
150         static PyObject*        pyattr_get_axis_single(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
151         static PyObject*        pyattr_get_hat_values(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
152         static PyObject*        pyattr_get_hat_single(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
153         static PyObject*        pyattr_get_num_axis(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
154         static PyObject*        pyattr_get_num_buttons(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
155         static PyObject*        pyattr_get_num_hats(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
156         static PyObject*        pyattr_get_connected(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
157         
158
159         /* attribute check */
160         static int CheckAxis(void *self, const PyAttributeDef*)
161         {
162                 SCA_JoystickSensor* sensor = reinterpret_cast<SCA_JoystickSensor*>(self);
163                 if (sensor->m_axis < 1)
164                         sensor->m_axis = 1;
165                 else if (sensor->m_axis > JOYAXIS_MAX)
166                         sensor->m_axis = JOYAXIS_MAX;
167                 return 0;
168         }
169         static int CheckHat(void *self, const PyAttributeDef*)
170         {
171                 SCA_JoystickSensor* sensor = reinterpret_cast<SCA_JoystickSensor*>(self);
172                 if (sensor->m_hat < 1)
173                         sensor->m_hat = 1;
174                 else if (sensor->m_hat > JOYHAT_MAX)
175                         sensor->m_hat = JOYHAT_MAX;
176                 return 0;
177         }
178         
179 };
180
181 #endif