2
0
mirror of https://github.com/FWGS/xash3d-fwgs synced 2024-12-23 09:16:04 +01:00

SDL2 GameController: Map axes to engine explicitly

This commit is contained in:
Gleb Mazovetskiy 2021-01-03 02:23:19 +00:00 committed by Alibek Omarov
parent 3e8e751dc7
commit 4daa7c23a0
3 changed files with 29 additions and 24 deletions

View File

@ -24,17 +24,6 @@ GNU General Public License for more details.
#define SHRT_MAX 0x7FFF
#endif
typedef enum engineAxis_e
{
JOY_AXIS_SIDE = 0,
JOY_AXIS_FWD,
JOY_AXIS_PITCH,
JOY_AXIS_YAW,
JOY_AXIS_RT,
JOY_AXIS_LT,
JOY_AXIS_NULL
} engineAxis_t;
#define MAX_AXES JOY_AXIS_NULL
// index - axis num come from event
@ -246,8 +235,6 @@ Axis events
*/
void Joy_AxisMotionEvent( byte axis, short value )
{
byte engineAxis;
if( !joy_found->value )
return;
@ -257,8 +244,11 @@ void Joy_AxisMotionEvent( byte axis, short value )
return;
}
engineAxis = joyaxesmap[axis]; // convert to engine inner axis control
return Joy_KnownAxisMotionEvent( joyaxesmap[axis], value );
}
void Joy_KnownAxisMotionEvent( engineAxis_t engineAxis, short value )
{
if( engineAxis == JOY_AXIS_NULL )
return;

View File

@ -93,9 +93,21 @@ enum
JOY_HAT_LEFTDOWN = JOY_HAT_LEFT | JOY_HAT_DOWN
};
typedef enum engineAxis_e
{
JOY_AXIS_SIDE = 0,
JOY_AXIS_FWD,
JOY_AXIS_PITCH,
JOY_AXIS_YAW,
JOY_AXIS_RT,
JOY_AXIS_LT,
JOY_AXIS_NULL
} engineAxis_t;
qboolean Joy_IsActive( void );
void Joy_HatMotionEvent( byte hat, byte value );
void Joy_AxisMotionEvent( byte axis, short value );
void Joy_KnownAxisMotionEvent( engineAxis_t engineAxis, short value );
void Joy_BallMotionEvent( byte ball, short xrel, short yrel );
void Joy_ButtonEvent( byte button, byte down );
void Joy_AddEvent( void );

View File

@ -532,18 +532,21 @@ static void SDLash_EventFilter( SDL_Event *event )
/* GameController API */
case SDL_CONTROLLERAXISMOTION:
if( event->caxis.axis == (Uint8)SDL_CONTROLLER_AXIS_INVALID )
break;
{
// Swap axis to follow default axis binding:
// LeftX, LeftY, RightX, RightY, TriggerRight, TriggerLeft
if( event->caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT )
event->caxis.axis = SDL_CONTROLLER_AXIS_TRIGGERRIGHT;
else if( event->caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT )
event->caxis.axis = SDL_CONTROLLER_AXIS_TRIGGERLEFT;
Joy_AxisMotionEvent( event->caxis.axis, event->caxis.value );
static int sdlControllerAxisToEngine[] = {
JOY_AXIS_SIDE, // SDL_CONTROLLER_AXIS_LEFTX,
JOY_AXIS_FWD, // SDL_CONTROLLER_AXIS_LEFTY,
JOY_AXIS_PITCH, // SDL_CONTROLLER_AXIS_RIGHTX,
JOY_AXIS_YAW, // SDL_CONTROLLER_AXIS_RIGHTY,
JOY_AXIS_LT, // SDL_CONTROLLER_AXIS_TRIGGERLEFT,
JOY_AXIS_RT, // SDL_CONTROLLER_AXIS_TRIGGERRIGHT,
};
if( Joy_IsActive() && event->caxis.axis != (Uint8)SDL_CONTROLLER_AXIS_INVALID )
Joy_KnownAxisMotionEvent( sdlControllerAxisToEngine[event->caxis.axis], event->caxis.value );
break;
}
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP:
@ -558,7 +561,7 @@ static void SDLash_EventFilter( SDL_Event *event )
};
// TODO: Use joyinput funcs, for future multiple gamepads support
if( Joy_IsActive() && event->cbutton.button != SDL_CONTROLLER_BUTTON_INVALID )
if( Joy_IsActive() && event->cbutton.button != (Uint8)SDL_CONTROLLER_BUTTON_INVALID )
Key_Event( sdlControllerButtonToEngine[event->cbutton.button], event->cbutton.state );
break;
}