From 885fa49aa4cd49a552bfe6210e3cb4701c4a5d04 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 7 Apr 2009 06:23:45 +0000 Subject: BGE Joystick Sensor - Raised limit of 2 axis to 4 axis pairs (4==8 joysticks axis pairs) - Added a new Joystick Sensor type "Single Axis", so you can detect horizontal or vertical movement, rather then just Up/Down/Left/Right - added Python attribute "axisSingle" so you can get the value from the selected axis (rather then getting it out of the axis list) - renamed Py attribute "axisPosition" to "axisValues" (was never in a release) If we need to increase the axis limit again just change JOYAXIS_MAX and the button limits. --- .../gameengine/GameLogic/Joystick/SCA_Joystick.cpp | 90 +++++++--------------- .../gameengine/GameLogic/Joystick/SCA_Joystick.h | 41 ++++------ .../GameLogic/Joystick/SCA_JoystickDefines.h | 6 ++ .../GameLogic/Joystick/SCA_JoystickEvents.cpp | 4 +- 4 files changed, 51 insertions(+), 90 deletions(-) (limited to 'source/gameengine/GameLogic/Joystick') diff --git a/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp b/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp index c21e5db1410..eaffd483d70 100644 --- a/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp +++ b/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp @@ -36,21 +36,19 @@ SCA_Joystick::SCA_Joystick(short int index) : m_joyindex(index), - m_axis10(0), - m_axis11(0), - m_axis20(0), - m_axis21(0), m_prec(3200), m_buttonnum(-2), m_axismax(-1), - m_hatdir(-2), m_buttonmax(-1), m_hatmax(-1), + m_hatdir(-2), m_isinit(0), m_istrig_axis(0), m_istrig_button(0), m_istrig_hat(0) { + for(int i=0; i m_prec? result = true: result = false; - return result; + return (pAxisTest(axis) > m_prec) ? true:false; } -bool SCA_Joystick::aRightAxisIsPositive(int axis) +bool SCA_Joystick::aAxisPairDirectionIsPositive(int axis, int dir) { - bool result; - int res = pGetAxis(axis,1); - res > m_prec? result = true: result = false; - return result; -} - -bool SCA_Joystick::aUpAxisIsPositive(int axis) -{ - bool result; - int res = pGetAxis(axis,0); - res < -m_prec? result = true : result = false; - return result; + int res; + + if (dir==JOYAXIS_UP || dir==JOYAXIS_DOWN) + res = pGetAxis(axis, 1); + else /* JOYAXIS_LEFT || JOYAXIS_RIGHT */ + res = pGetAxis(axis, 0); + + if (dir==JOYAXIS_DOWN || dir==JOYAXIS_RIGHT) + return (res > m_prec) ? true : false; + else /* JOYAXIS_UP || JOYAXIS_LEFT */ + return (res < -m_prec) ? true : false; } - -bool SCA_Joystick::aLeftAxisIsPositive(int axis) +bool SCA_Joystick::aAxisIsPositive(int axis_single) { - bool result; - int res = pGetAxis(axis,1); - res < -m_prec ? result = true : result = false; - return result; -} - - -bool SCA_Joystick::aDownAxisIsPositive(int axis) -{ - bool result; - int res = pGetAxis(axis,0); - res > m_prec ? result = true:result = false; - return result; + return abs(m_axis_array[axis_single]) > m_prec ? true:false; } bool SCA_Joystick::aAnyButtonPressIsPositive(void) @@ -255,8 +236,12 @@ bool SCA_Joystick::CreateJoystickDevice(void) /* must run after being initialized */ m_axismax = SDL_JoystickNumAxes(m_private->m_joystick); + if (m_axismax > JOYAXIS_MAX) m_axismax= JOYAXIS_MAX; /* very unlikely */ + m_buttonmax = SDL_JoystickNumButtons(m_private->m_joystick); m_hatmax = SDL_JoystickNumHats(m_private->m_joystick); + + } return true; #endif @@ -288,17 +273,8 @@ int SCA_Joystick::Connected(void) void SCA_Joystick::pFillAxes() { #ifndef DISABLE_SDL - if(m_axismax == 1){ - m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0); - m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1); - }else if(m_axismax > 1){ - m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0); - m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1); - m_axis20 = SDL_JoystickGetAxis(m_private->m_joystick, 2); - m_axis21 = SDL_JoystickGetAxis(m_private->m_joystick, 3); - }else{ - m_axis10 = m_axis11 = m_axis20 = m_axis21 = 0; - } + for(int i=0; im_joystick, i); #endif } @@ -306,10 +282,7 @@ void SCA_Joystick::pFillAxes() int SCA_Joystick::pGetAxis(int axisnum, int udlr) { #ifndef DISABLE_SDL - if(axisnum == 1 && udlr == 1)return m_axis10; //u/d - if(axisnum == 1 && udlr == 0)return m_axis11; //l/r - if(axisnum == 2 && udlr == 0)return m_axis20; //... - if(axisnum == 2 && udlr == 1)return m_axis21; + return m_axis_array[(axisnum*2)+udlr]; #endif return 0; } @@ -317,13 +290,9 @@ int SCA_Joystick::pGetAxis(int axisnum, int udlr) int SCA_Joystick::pAxisTest(int axisnum) { #ifndef DISABLE_SDL - short i1,i2; - if(axisnum == 1) { - i1 = m_axis10; i2 = m_axis11; - } - else if(axisnum == 2) { - i1 = m_axis20; i2 = m_axis21; - } + short i1= m_axis_array[(axisnum*2)]; + short i2= m_axis_array[(axisnum*2)+1]; + /* long winded way to do * return MAX2(abs(i1), abs(i2)) * avoid abs from math.h */ @@ -335,4 +304,3 @@ int SCA_Joystick::pAxisTest(int axisnum) return 0; #endif } - diff --git a/source/gameengine/GameLogic/Joystick/SCA_Joystick.h b/source/gameengine/GameLogic/Joystick/SCA_Joystick.h index 8335d5538ad..53cd65cd495 100644 --- a/source/gameengine/GameLogic/Joystick/SCA_Joystick.h +++ b/source/gameengine/GameLogic/Joystick/SCA_Joystick.h @@ -55,10 +55,8 @@ class SCA_Joystick /* *support for 2 axes */ - - int m_axis10,m_axis11; - int m_axis20,m_axis21; - + int m_axis_array[JOYAXIS_MAX]; + /* * Precision or range of the axes */ @@ -120,7 +118,10 @@ class SCA_Joystick void OnButtonUp(SDL_Event *sdl_event); void OnButtonDown(SDL_Event *sdl_event); void OnNothing(SDL_Event *sdl_event); +#if 0 /* not used yet */ void OnBallMotion(SDL_Event *sdl_event){} +#endif + #endif /* * Open the joystick @@ -139,12 +140,12 @@ class SCA_Joystick void pFillButtons(void); /* - * returns m_axis10,m_axis11... + * returns m_axis_array */ int pAxisTest(int axisnum); /* - * returns m_axis10,m_axis11... + * returns m_axis_array */ int pGetAxis(int axisnum, int udlr); @@ -166,11 +167,9 @@ public: /* */ - bool aAnyAxisIsPositive(int axis); - bool aUpAxisIsPositive(int axis); - bool aDownAxisIsPositive(int axis); - bool aLeftAxisIsPositive(int axis); - bool aRightAxisIsPositive(int axis); + bool aAxisPairIsPositive(int axis); + bool aAxisPairDirectionIsPositive(int axis, int dir); /* function assumes joysticks are in axis pairs */ + bool aAxisIsPositive(int axis_single); /* check a single axis only */ bool aAnyButtonPressIsPositive(void); bool aAnyButtonReleaseIsPositive(void); @@ -184,24 +183,10 @@ public: void cSetPrecision(int val); - int GetAxis10(void){ - - return m_axis10; - - } - - int GetAxis11(void){ - return m_axis11; - } - - int GetAxis20(void){ - return m_axis20; + int GetAxisPosition(int index){ + return m_axis_array[index]; } - - int GetAxis21(void){ - return m_axis21; - } - + int GetButton(void){ return m_buttonnum; } diff --git a/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h b/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h index 73ffe1406d9..42fed51b19f 100644 --- a/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h +++ b/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h @@ -39,5 +39,11 @@ #endif #define JOYINDEX_MAX 8 +#define JOYAXIS_MAX 8 + +#define JOYAXIS_RIGHT 0 +#define JOYAXIS_UP 1 +#define JOYAXIS_DOWN 3 +#define JOYAXIS_LEFT 2 #endif diff --git a/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp b/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp index 73ca288861d..8e190060e95 100644 --- a/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp +++ b/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp @@ -68,7 +68,7 @@ void SCA_Joystick::OnButtonUp(SDL_Event* sdl_event) void SCA_Joystick::OnButtonDown(SDL_Event* sdl_event) { - if(sdl_event->jbutton.button >= 0 || sdl_event->jbutton.button <= m_buttonmax) + if(sdl_event->jbutton.button <= m_buttonmax) /* unsigned int so always above 0 */ { m_istrig_button = 1; m_buttonnum = sdl_event->jbutton.button; @@ -111,9 +111,11 @@ void SCA_Joystick::HandleEvents(void) case SDL_JOYBUTTONDOWN: SCA_Joystick::m_instance[sdl_event.jbutton.which]->OnButtonDown(&sdl_event); break; +#if 0 /* Not used yet */ case SDL_JOYBALLMOTION: SCA_Joystick::m_instance[sdl_event.jball.which]->OnBallMotion(&sdl_event); break; +#endif default: printf("SCA_Joystick::HandleEvents, Unknown SDL event, this should not happen\n"); break; -- cgit v1.2.3