Added Joystick sensor (from snailrose)
[blender.git] / source / gameengine / GameLogic / Joystick / SCA_Joystick.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): snailrose.
27  *
28  * ***** END GPL/BL DUAL LICENSE BLOCK *****
29  */
30 #include <SDL.h>
31  
32 #include "SCA_Joystick.h"
33 #include "SCA_JoystickPrivate.h"
34
35 SCA_Joystick::SCA_Joystick()
36         :
37         m_axis10(0),
38         m_axis11(0),
39         m_axis20(0),
40         m_axis21(0),
41         m_prec(3200),
42         m_buttonnum(-2),
43         m_hatdir(-2),
44         m_isinit(0),
45         m_istrig(0)
46 {
47         m_private = new PrivateData();
48 }
49
50 SCA_Joystick::~SCA_Joystick()
51 {
52         delete m_private;
53 }
54
55 bool SCA_Joystick::CreateJoystickDevice()
56 {
57         bool init = false;
58         init = pCreateJoystickDevice();
59         return init;
60 }
61
62
63 void SCA_Joystick::DestroyJoystickDevice()
64 {
65         if(m_isinit)
66                 pDestroyJoystickDevice();
67 }
68
69 void SCA_Joystick::HandleEvents()
70 {
71         if(m_isinit)
72         {
73                 if(SDL_PollEvent(&m_private->m_event))
74                 {
75                         switch(m_private->m_event.type)
76                         {
77                         case SDL_JOYAXISMOTION: {HANDLE_AXISMOTION(OnAxisMotion);break;}
78                         case SDL_JOYHATMOTION:  {HANDLE_HATMOTION(OnHatMotion);  break;}
79                         case SDL_JOYBUTTONUP:   {HANDLE_BUTTONUP(OnButtonUp);    break;}
80                         case SDL_JOYBUTTONDOWN: {HANDLE_BUTTONDOWN(OnButtonDown);break;}
81                         case SDL_JOYBALLMOTION: {HANDLE_BALLMOTION(OnBallMotion);break;}
82                         default:                                {HANDLE_NOEVENT(OnNothing);      break;}
83                         }
84                 }
85         }
86 }
87
88
89 void SCA_Joystick::cSetPrecision(int val)
90 {
91         m_prec = val;
92 }
93
94 bool SCA_Joystick::aRightAxisIsPositive(int axis)
95 {
96         bool result;
97         int res = pGetAxis(axis,1);
98         res > m_prec? result = true: result = false;
99         m_istrig = result;
100         return result;
101 }
102
103
104 bool SCA_Joystick::aUpAxisIsPositive(int axis)
105 {
106         bool result;
107         int res = pGetAxis(axis,0);
108         res < -m_prec? result = true : result = false;
109         m_istrig = result;
110         return result;
111 }
112
113
114 bool SCA_Joystick::aLeftAxisIsPositive(int axis)
115 {
116         bool result;
117         int res = pGetAxis(axis,1);
118         res < -m_prec ? result = true : result = false;
119         m_istrig = result;
120         return result;
121 }
122
123
124 bool SCA_Joystick::aDownAxisIsPositive(int axis)
125 {
126         bool result;
127         int res = pGetAxis(axis,0);
128         res > m_prec ? result = true:result = false;
129         m_istrig = result;
130         return result;
131 }
132
133
134 bool SCA_Joystick::aButtonPressIsPositive(int button)
135 {
136         bool result;
137         SDL_JoystickGetButton(m_private->m_joystick, button)? result = true:result = false;
138         m_istrig = result;
139         return result;
140 }
141
142
143 bool SCA_Joystick::aButtonReleaseIsPositive(int button)
144 {
145         bool result;
146         SDL_JoystickGetButton(m_private->m_joystick, button)? result = false : result = true;
147         m_istrig = result;
148         return result;
149 }
150
151
152 bool SCA_Joystick::aHatIsPositive(int dir)
153 {
154         bool result;
155         int res = pGetHat(dir);
156         res == dir? result = true : result = false;
157         m_istrig = result;
158         return result;
159 }
160
161
162 int SCA_Joystick::pGetButtonPress(int button)
163 {
164         if(button == m_buttonnum)
165                 return m_buttonnum;
166         return -2;
167
168 }
169
170
171 int SCA_Joystick::pGetButtonRelease(int button)
172 {
173         if(button == m_buttonnum)
174                 return m_buttonnum;
175         return -2;
176 }
177
178
179 int SCA_Joystick::pGetHat(int direction)
180 {
181         if(direction == m_hatdir){
182                 return m_hatdir;
183         }
184         return 0;
185 }
186
187
188 bool SCA_Joystick::GetJoyAxisMotion()
189 {
190         bool result = false;
191         if(m_isinit){
192                 if(SDL_PollEvent(&m_private->m_event)){
193                         switch(m_private->m_event.type)
194                         {
195                         case SDL_JOYAXISMOTION:
196                                 result = true;
197                                 break;
198                         }
199                 }
200         }
201         return result;
202 }
203
204
205 bool SCA_Joystick::GetJoyButtonPress()
206 {
207         bool result = false;
208         if(m_isinit){
209                 if(SDL_PollEvent(&m_private->m_event)){
210                         switch(m_private->m_event.type)
211                         {
212                         case SDL_JOYBUTTONDOWN:
213                                 result = true;
214                                 break;
215                         }
216                 }
217         }
218         return result;
219 }
220
221
222 bool SCA_Joystick::GetJoyButtonRelease()
223 {
224         bool result = false;
225         if(m_isinit)
226         {
227                 if(SDL_PollEvent(&m_private->m_event)){
228                         switch(m_private->m_event.type)
229                         {
230                         case SDL_JOYBUTTONUP:
231                                 result = true;
232                                 break;
233                         }
234                 }
235         }
236         return result;
237 }
238
239
240 bool SCA_Joystick::GetJoyHatMotion()
241 {
242         bool result = false;
243         if(m_isinit){
244                 if(SDL_PollEvent(&m_private->m_event)){
245                         switch(m_private->m_event.type)
246                         {
247                         case SDL_JOYHATMOTION:
248                                 result = true;
249                                 break;
250                         }
251                 }
252         }
253         return 0;
254 }
255
256
257 int SCA_Joystick::GetNumberOfAxes()
258 {
259         int number;
260         if(m_isinit){
261                 if(m_private->m_joystick){
262                         number = SDL_JoystickNumAxes(m_private->m_joystick);
263                         return number;
264                 }
265         }
266         return -1;
267 }
268
269
270 int SCA_Joystick::GetNumberOfButtons()
271 {
272         int number;
273         if(m_isinit){
274                 if(m_private->m_joystick){
275                         number = SDL_JoystickNumButtons(m_private->m_joystick);
276                         return number;
277                 }
278         }
279         return -1;
280 }
281
282 int SCA_Joystick::GetNumberOfHats()
283 {
284         int number;
285         if(m_isinit){
286                 if(m_private->m_joystick){
287                         number = SDL_JoystickNumHats(m_private->m_joystick);
288                         return number;
289                 }
290         }
291         return -1;
292 }
293
294
295 bool SCA_Joystick::pCreateJoystickDevice()
296 {
297         if(m_isinit == false){
298                 if(SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO ) == -1 ){
299                         echo("Error-Initializing-SDL: " << SDL_GetError());
300                         return false;
301                 }
302                 if(SDL_NumJoysticks() > 0){
303                         for(int i=0; i<SDL_NumJoysticks();i++){
304                                 m_private->m_joystick = SDL_JoystickOpen(i);
305                                 SDL_JoystickEventState(SDL_ENABLE);
306                                 m_numjoys = i;
307                         }
308                         echo("Joystick-initialized");
309                         m_isinit = true;
310                         return true;
311                 }else{
312                         echo("Joystick-Error: " << SDL_NumJoysticks() << " avaiable joystick(s)");
313                         return false;
314                 }
315         }
316         return false;
317 }
318
319
320 void SCA_Joystick::pDestroyJoystickDevice()
321 {
322         echo("Closing-");
323         for(int i=0; i<SDL_NumJoysticks(); i++){
324                 if(SDL_JoystickOpened(i)){
325                         SDL_JoystickClose(m_private->m_joystick);
326                 }
327         }
328         SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO );
329 }
330
331 void SCA_Joystick::pFillAxes()
332 {
333         if(GetNumberOfAxes() == 1){
334                 m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
335                 m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
336         }else if(GetNumberOfAxes() > 1){
337                 m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
338                 m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
339                 m_axis20 = SDL_JoystickGetAxis(m_private->m_joystick, 2);
340                 m_axis21 = SDL_JoystickGetAxis(m_private->m_joystick, 3);
341         }else{
342                 m_axis10 = 0;m_axis11 = 0;
343                 m_axis20 = 0;m_axis21 = 0;
344         }
345 }
346
347 int SCA_Joystick::pGetAxis(int axisnum, int udlr)
348 {
349         if(axisnum == 1 && udlr == 1)return m_axis10; //u/d
350         if(axisnum == 1 && udlr == 0)return m_axis11; //l/r
351         if(axisnum == 2 && udlr == 0)return m_axis20; //...
352         if(axisnum == 2 && udlr == 1)return m_axis21;
353         return 0;
354 }
355