2911b3b1a1405185e6e9b8639904261063377e57
[blender.git] / source / gameengine / GameLogic / Joystick / SCA_JoystickEvents.cpp
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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): snailrose.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp
29  *  \ingroup gamelogic
30  */
31
32 #ifdef WITH_SDL
33 #  include <SDL.h>
34 #endif
35
36 #include "SCA_Joystick.h"
37 #include "SCA_JoystickPrivate.h"
38
39
40 #ifdef WITH_SDL
41 void SCA_Joystick::OnAxisMotion(SDL_Event* sdl_event)
42 {
43         if (sdl_event->jaxis.axis >= JOYAXIS_MAX)
44                 return;
45         
46         m_axis_array[sdl_event->jaxis.axis] = sdl_event->jaxis.value;
47         m_istrig_axis = 1;
48 }
49
50 /* See notes below in the event loop */
51 void SCA_Joystick::OnHatMotion(SDL_Event* sdl_event)
52 {
53         if (sdl_event->jhat.hat >= JOYHAT_MAX)
54                 return;
55
56         m_hat_array[sdl_event->jhat.hat] = sdl_event->jhat.value;
57         m_istrig_hat = 1;
58 }
59
60 /* See notes below in the event loop */
61 void SCA_Joystick::OnButtonUp(SDL_Event* sdl_event)
62 {
63         m_istrig_button = 1;
64 }
65
66
67 void SCA_Joystick::OnButtonDown(SDL_Event* sdl_event)
68 {
69         //if (sdl_event->jbutton.button > m_buttonmax) /* unsigned int so always above 0 */
70         //      return;
71         // sdl_event->jbutton.button;
72         
73         m_istrig_button = 1;
74 }
75
76
77 void SCA_Joystick::OnNothing(SDL_Event* sdl_event)
78 {
79         m_istrig_axis = m_istrig_button = m_istrig_hat = 0;
80 }
81
82 void SCA_Joystick::HandleEvents(void)
83 {
84         SDL_Event               sdl_event;
85         
86         int i;
87         for (i=0; i<m_joynum; i++) { /* could use JOYINDEX_MAX but no reason to */
88                 if (SCA_Joystick::m_instance[i])
89                         SCA_Joystick::m_instance[i]->OnNothing(&sdl_event);
90         }
91         
92         while (SDL_PollEvent(&sdl_event)) {
93                 /* Note! m_instance[sdl_event.jaxis.which]
94                  * will segfault if over JOYINDEX_MAX, not too nice but what are the chances? */
95                 
96                 /* Note!, with buttons, this wont care which button is pressed,
97                  * only to set 'm_istrig_button', actual pressed buttons are detected by SDL_JoystickGetButton */
98                 
99                 /* Note!, if you manage to press and release a button within 1 logic tick
100                  * it wont work as it should */
101                 
102                 switch (sdl_event.type) {
103                         case SDL_JOYAXISMOTION:
104                                 SCA_Joystick::m_instance[sdl_event.jaxis.which]->OnAxisMotion(&sdl_event);
105                                 break;
106                         case SDL_JOYHATMOTION:
107                                 SCA_Joystick::m_instance[sdl_event.jhat.which]->OnHatMotion(&sdl_event);
108                                 break;
109                         case SDL_JOYBUTTONUP:
110                                 SCA_Joystick::m_instance[sdl_event.jbutton.which]->OnButtonUp(&sdl_event);
111                                 break;
112                         case SDL_JOYBUTTONDOWN:
113                                 SCA_Joystick::m_instance[sdl_event.jbutton.which]->OnButtonDown(&sdl_event);
114                                 break;
115 #if 0   /* Not used yet */
116                         case SDL_JOYBALLMOTION:
117                                 SCA_Joystick::m_instance[sdl_event.jball.which]->OnBallMotion(&sdl_event);
118                                 break;
119 #endif
120                         default:
121                                 printf("SCA_Joystick::HandleEvents, Unknown SDL event (%d), this should not happen\n", sdl_event.type);
122                                 break;
123                 }
124         }
125 }
126 #endif /* WITH_SDL */