BGE: bookmark option on controller to make them run before all other controllers.
[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
34 class SCA_JoystickSensor :public SCA_ISensor
35 {
36         Py_Header;
37         class SCA_JoystickManager*      m_pJoystickMgr;
38         
39         /**
40          * Axis 1-or-2, MUST be followed by m_axisf
41          */
42         int     m_axis;
43         /**
44          * Axis flag to find direction, MUST be an int
45          */
46         int     m_axisf;
47         /**
48          * The actual button
49          */
50         int     m_button;
51         /**
52          * Flag for a pressed or released button
53          */
54         int     m_buttonf;
55         /**
56          * The actual hat. MUST be followed by m_hatf
57          */
58         int     m_hat;
59         /**
60          * Flag to find direction 0-11, MUST be an int
61          */
62         int     m_hatf;
63         /**
64          * The threshold value the axis acts opon
65          */
66         int     m_precision;
67         /**
68          * Is an event triggered ?
69          */
70         bool    m_istrig;
71         /**
72          * Last trigger state for this sensors joystick,
73          * Otherwise it will trigger all the time
74          * this is used to see if the trigger state changes.
75          */
76         bool    m_istrig_prev;
77         /**
78          * The mode to determine axis,button or hat
79          */
80         short int m_joymode;
81         /**
82          * Select which joystick to use
83          */
84         short int m_joyindex;
85
86         /**
87          * Detect all events for the currently selected type
88          */
89         bool m_bAllEvents;
90
91         enum KX_JOYSENSORMODE {
92                 KX_JOYSENSORMODE_NODEF = 0,
93                 KX_JOYSENSORMODE_AXIS,
94                 KX_JOYSENSORMODE_BUTTON,
95                 KX_JOYSENSORMODE_HAT,
96                 KX_JOYSENSORMODE_AXIS_SINGLE,
97                 KX_JOYSENSORMODE_MAX
98         };
99         bool isValid(KX_JOYSENSORMODE);
100
101 public:
102         SCA_JoystickSensor(class SCA_JoystickManager* eventmgr,
103                                            SCA_IObject* gameobj,
104                                            short int joyindex,
105                                            short int joymode,
106                                            int axis, int axisf,int prec,
107                                            int button,
108                                            int hat, int hatf, bool allevents,
109                                            PyTypeObject* T=&Type );
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         virtual PyObject* py_getattro(PyObject *attr);
126         virtual PyObject* py_getattro_dict();
127         virtual int py_setattro(PyObject *attr, PyObject *value);
128
129         /* Joystick Index */
130         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetIndex);
131         KX_PYMETHOD_DOC_O(SCA_JoystickSensor,SetIndex);
132         /* Axes*/
133         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetAxis);
134         KX_PYMETHOD_DOC_VARARGS(SCA_JoystickSensor,SetAxis);
135         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetAxisValue);
136         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetThreshold);
137         KX_PYMETHOD_DOC_VARARGS(SCA_JoystickSensor,SetThreshold);
138         /* Buttons */
139         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetButton);
140         KX_PYMETHOD_DOC_O(SCA_JoystickSensor,SetButton);
141         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetButtonValue);
142         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetButtonActiveList);
143         KX_PYMETHOD_DOC_VARARGS(SCA_JoystickSensor,GetButtonStatus);
144         /* Hats */
145         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetHat);
146         KX_PYMETHOD_DOC_VARARGS(SCA_JoystickSensor,SetHat);
147         /* number of */
148         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,NumberOfAxes);
149         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,NumberOfButtons);
150         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,NumberOfHats);
151         KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,Connected);
152
153         static PyObject*        pyattr_get_axis_values(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
154         static PyObject*        pyattr_get_axis_single(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
155         static PyObject*        pyattr_get_num_axis(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
156         static PyObject*        pyattr_get_num_buttons(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
157         static PyObject*        pyattr_get_num_hats(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
158         static PyObject*        pyattr_get_connected(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
159         
160
161         /* attribute check */
162         static int CheckAxis(void *self, const PyAttributeDef*)
163         {
164                 SCA_JoystickSensor* sensor = reinterpret_cast<SCA_JoystickSensor*>(self);
165                 if (sensor->m_axis < 1)
166                         sensor->m_axis = 1;
167                 else if (sensor->m_axis > 2)
168                         sensor->m_axis = 2;
169                 return 0;
170         }
171         static int CheckHat(void *self, const PyAttributeDef*)
172         {
173                 SCA_JoystickSensor* sensor = reinterpret_cast<SCA_JoystickSensor*>(self);
174                 if (sensor->m_hat < 1)
175                         sensor->m_hat = 1;
176                 else if (sensor->m_hat > 2)
177                         sensor->m_hat = 2;
178                 return 0;
179         }
180         
181 };
182
183 #endif