2
* ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
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
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.
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.
21
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
22
* All rights reserved.
24
* The Original Code is: all of this file.
26
* Contributor(s): snailrose.
28
* ***** END GPL/BL DUAL LICENSE BLOCK *****
32
#include "SCA_Joystick.h"
33
#include "SCA_JoystickPrivate.h"
36
SCA_Joystick::SCA_Joystick()
48
m_private = new PrivateData();
52
SCA_Joystick::~SCA_Joystick()
59
bool SCA_Joystick::CreateJoystickDevice()
62
init = pCreateJoystickDevice();
67
void SCA_Joystick::DestroyJoystickDevice()
70
pDestroyJoystickDevice();
74
void SCA_Joystick::HandleEvents()
78
if(SDL_PollEvent(&m_private->m_event))
80
switch(m_private->m_event.type)
82
case SDL_JOYAXISMOTION: {HANDLE_AXISMOTION(OnAxisMotion);break;}
83
case SDL_JOYHATMOTION: {HANDLE_HATMOTION(OnHatMotion); break;}
84
case SDL_JOYBUTTONUP: {HANDLE_BUTTONUP(OnButtonUp); break;}
85
case SDL_JOYBUTTONDOWN: {HANDLE_BUTTONDOWN(OnButtonDown);break;}
86
case SDL_JOYBALLMOTION: {HANDLE_BALLMOTION(OnBallMotion);break;}
87
default: {HANDLE_NOEVENT(OnNothing); break;}
94
void SCA_Joystick::cSetPrecision(int val)
100
bool SCA_Joystick::aRightAxisIsPositive(int axis)
103
int res = pGetAxis(axis,1);
104
res > m_prec? result = true: result = false;
110
bool SCA_Joystick::aUpAxisIsPositive(int axis)
113
int res = pGetAxis(axis,0);
114
res < -m_prec? result = true : result = false;
120
bool SCA_Joystick::aLeftAxisIsPositive(int axis)
123
int res = pGetAxis(axis,1);
124
res < -m_prec ? result = true : result = false;
130
bool SCA_Joystick::aDownAxisIsPositive(int axis)
133
int res = pGetAxis(axis,0);
134
res > m_prec ? result = true:result = false;
140
bool SCA_Joystick::aButtonPressIsPositive(int button)
143
SDL_JoystickGetButton(m_private->m_joystick, button)? result = true:result = false;
149
bool SCA_Joystick::aButtonReleaseIsPositive(int button)
152
SDL_JoystickGetButton(m_private->m_joystick, button)? result = false : result = true;
158
bool SCA_Joystick::aHatIsPositive(int dir)
161
int res = pGetHat(dir);
162
res == dir? result = true : result = false;
168
int SCA_Joystick::pGetButtonPress(int button)
170
if(button == m_buttonnum)
176
int SCA_Joystick::pGetButtonRelease(int button)
178
if(button == m_buttonnum)
184
int SCA_Joystick::pGetHat(int direction)
186
if(direction == m_hatdir){
193
bool SCA_Joystick::GetJoyAxisMotion()
197
if(SDL_PollEvent(&m_private->m_event)){
198
switch(m_private->m_event.type)
200
case SDL_JOYAXISMOTION:
210
bool SCA_Joystick::GetJoyButtonPress()
214
if(SDL_PollEvent(&m_private->m_event)){
215
switch(m_private->m_event.type)
217
case SDL_JOYBUTTONDOWN:
227
bool SCA_Joystick::GetJoyButtonRelease()
232
if(SDL_PollEvent(&m_private->m_event)){
233
switch(m_private->m_event.type)
235
case SDL_JOYBUTTONUP:
245
bool SCA_Joystick::GetJoyHatMotion()
249
if(SDL_PollEvent(&m_private->m_event)){
250
switch(m_private->m_event.type)
252
case SDL_JOYHATMOTION:
262
int SCA_Joystick::GetNumberOfAxes()
266
if(m_private->m_joystick){
267
number = SDL_JoystickNumAxes(m_private->m_joystick);
275
int SCA_Joystick::GetNumberOfButtons()
279
if(m_private->m_joystick){
280
number = SDL_JoystickNumButtons(m_private->m_joystick);
288
int SCA_Joystick::GetNumberOfHats()
292
if(m_private->m_joystick){
293
number = SDL_JoystickNumHats(m_private->m_joystick);
300
bool SCA_Joystick::pCreateJoystickDevice()
302
if(m_isinit == false){
303
if(SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO ) == -1 ){
304
echo("Error-Initializing-SDL: " << SDL_GetError());
307
if(SDL_NumJoysticks() > 0){
308
for(int i=0; i<SDL_NumJoysticks();i++){
309
m_private->m_joystick = SDL_JoystickOpen(i);
310
SDL_JoystickEventState(SDL_ENABLE);
313
echo("Joystick-initialized");
317
echo("Joystick-Error: " << SDL_NumJoysticks() << " avaiable joystick(s)");
325
void SCA_Joystick::pDestroyJoystickDevice()
328
for(int i=0; i<SDL_NumJoysticks(); i++){
329
if(SDL_JoystickOpened(i)){
330
SDL_JoystickClose(m_private->m_joystick);
333
SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO );
337
void SCA_Joystick::pFillAxes()
339
if(GetNumberOfAxes() == 1){
340
m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
341
m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
342
}else if(GetNumberOfAxes() > 1){
343
m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
344
m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
345
m_axis20 = SDL_JoystickGetAxis(m_private->m_joystick, 2);
346
m_axis21 = SDL_JoystickGetAxis(m_private->m_joystick, 3);
348
m_axis10 = 0;m_axis11 = 0;
349
m_axis20 = 0;m_axis21 = 0;
354
int SCA_Joystick::pGetAxis(int axisnum, int udlr)
356
if(axisnum == 1 && udlr == 1)return m_axis10; //u/d
357
if(axisnum == 1 && udlr == 0)return m_axis11; //l/r
358
if(axisnum == 2 && udlr == 0)return m_axis20; //...
359
if(axisnum == 2 && udlr == 1)return m_axis21;