From 4890591ba50bfc8b11cda684b223cb7c551e6dd3 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Wed, 30 Apr 2014 22:56:04 -0400 Subject: joystick: Add virtual-physical button mapping. The solution to solving #386 is to add a layer of indirection: the game code can only support up to ~20 joystick buttons, but this doesn't matter as long as we never want to bind more than 20 buttons to actions anyway. Redefine the game's notion of buttons to be based on "virtual" joystick buttons, and map these buttons to physical (SDL) buttons based on configuration file variables. --- src/i_joystick.c | 79 +++++++++++++++++++++++++++++++++++++++++----------- src/i_joystick.h | 5 ++++ src/m_config.c | 60 +++++++++++++++++++++++++++++++++++++++ src/m_controls.h | 2 ++ src/setup/joystick.c | 15 ++++++++++ 5 files changed, 144 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/i_joystick.c b/src/i_joystick.c index de10fa08..da93ce41 100644 --- a/src/i_joystick.c +++ b/src/i_joystick.c @@ -37,6 +37,7 @@ #include "i_system.h" #include "m_config.h" +#include "m_misc.h" // When an axis is within the dead zone, it is set to zero. // This is 5% of the full range: @@ -72,6 +73,12 @@ static int joystick_y_invert = 0; static int joystick_strafe_axis = -1; static int joystick_strafe_invert = 0; +// Virtual to physical button joystick button mapping. By default this +// is a straight mapping. +static int joystick_physical_buttons[NUM_VIRTUAL_BUTTONS] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 +}; + void I_ShutdownJoystick(void) { if (joystick != NULL) @@ -159,49 +166,78 @@ void I_InitJoystick(void) I_AtExit(I_ShutdownJoystick, true); } -static int ButtonAxisMask(void) +static boolean IsAxisButton(int physbutton) { - int result = 0; - if (IS_BUTTON_AXIS(joystick_x_axis)) { - result |= 1 << BUTTON_AXIS_NEG(joystick_x_axis); - result |= 1 << BUTTON_AXIS_POS(joystick_x_axis); + if (physbutton == BUTTON_AXIS_NEG(joystick_x_axis) + || physbutton == BUTTON_AXIS_POS(joystick_x_axis)) + { + return true; + } } if (IS_BUTTON_AXIS(joystick_y_axis)) { - result |= 1 << BUTTON_AXIS_NEG(joystick_y_axis); - result |= 1 << BUTTON_AXIS_POS(joystick_y_axis); + if (physbutton == BUTTON_AXIS_NEG(joystick_y_axis) + || physbutton == BUTTON_AXIS_POS(joystick_y_axis)) + { + return true; + } } if (IS_BUTTON_AXIS(joystick_strafe_axis)) { - result |= 1 << BUTTON_AXIS_NEG(joystick_strafe_axis); - result |= 1 << BUTTON_AXIS_POS(joystick_strafe_axis); + if (physbutton == BUTTON_AXIS_NEG(joystick_strafe_axis) + || physbutton == BUTTON_AXIS_POS(joystick_strafe_axis)) + { + return true; + } } - return result; + return false; +} + +// Get the state of the given virtual button. + +static int ReadButtonState(int vbutton) +{ + int physbutton; + + // Map from virtual button to physical (SDL) button. + if (vbutton < NUM_VIRTUAL_BUTTONS) + { + physbutton = joystick_physical_buttons[vbutton]; + } + else + { + physbutton = vbutton; + } + + // Never read axis buttons as buttons. + if (IsAxisButton(physbutton)) + { + return 0; + } + + return SDL_JoystickGetButton(joystick, physbutton); } // Get a bitmask of all currently-pressed buttons -static int GetButtonState(void) +static int GetButtonsState(void) { int i; int result; result = 0; - for (i=0; i