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