From 05f22d03aa25c55f5b7251d474eee16054f37e9e Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Thu, 1 May 2014 00:19:51 -0400 Subject: setup: Configure joystick buttons with virtual mapping. Refactor how joystick buttons are reassigned. Define a fixed mapping from joyb* variables to virtual buttons, and change the button assignments at the virtual->physical mapping level. --- src/m_controls.h | 2 - src/setup/joystick.c | 32 +---------- src/setup/txt_joybinput.c | 135 +++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 130 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/m_controls.h b/src/m_controls.h index 6ae15880..5b2f34d6 100644 --- a/src/m_controls.h +++ b/src/m_controls.h @@ -157,8 +157,6 @@ extern int joybstraferight; extern int joybprevweapon; extern int joybnextweapon; -extern int joy_physical_buttons[10]; - extern int joybmenu; extern int dclick_use; diff --git a/src/setup/joystick.c b/src/setup/joystick.c index cc5c4a8e..3eedda1e 100644 --- a/src/setup/joystick.c +++ b/src/setup/joystick.c @@ -90,18 +90,12 @@ static int joystick_strafe_axis = -1; static int joystick_strafe_invert = 0; // Virtual to physical mapping. -static int joystick_physical_buttons[NUM_VIRTUAL_BUTTONS] = { +int joystick_physical_buttons[NUM_VIRTUAL_BUTTONS] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; static txt_button_t *joystick_button; -static int *all_joystick_buttons[] = { - &joybstraferight, &joybstrafeleft, &joybfire, &joybspeed, - &joybuse, &joybstrafe, &joybprevweapon, &joybnextweapon, &joybjump, - &joybmenu, -}; - // // Calibration // @@ -580,27 +574,7 @@ static void CalibrateJoystick(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(unused)) SetCalibrationLabel(); } -void JoyButtonSetCallback(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(variable)) -{ - TXT_CAST_ARG(int, variable); - unsigned int i; - - // Only allow a button to be bound to one action at a time. If - // we assign a key that another action is using, set that other action - // to -1. - - for (i=0; i= 20) + || vbutton < 0) + { + new_mapping[i] = i; + } + else + { + new_mapping[i] = PhysicalForVirtualButton(vbutton); + *all_joystick_buttons[i] = i; + } + } + + for (i = 0; i < NUM_VIRTUAL_BUTTONS; ++i) + { + joystick_physical_buttons[i] = new_mapping[i]; + } +} + +// Check all existing buttons and clear any using the specified physical +// button. +static void ClearVariablesUsingButton(int physbutton) +{ + int vbutton; + int i; + + for (i = 0; i < arrlen(all_joystick_buttons); ++i) + { + vbutton = *all_joystick_buttons[i]; + + if (vbutton >= 0 && physbutton == PhysicalForVirtualButton(vbutton)) + { + *all_joystick_buttons[i] = -1; + } + } +} + // Called in response to SDL events when the prompt window is open: static int EventCallback(SDL_Event *event, TXT_UNCAST_ARG(joystick_input)) @@ -48,13 +156,24 @@ static int EventCallback(SDL_Event *event, TXT_UNCAST_ARG(joystick_input)) if (event->type == SDL_JOYBUTTONDOWN) { - *joystick_input->variable = event->jbutton.button; + int vbutton, physbutton; + + // Before changing anything, remap button configuration into + // canonical form, to avoid conflicts. + CanonicalizeButtons(); + + vbutton = VirtualButtonForVariable(joystick_input->variable); + physbutton = event->jbutton.button; if (joystick_input->check_conflicts) { - TXT_EmitSignal(joystick_input, "set"); + ClearVariablesUsingButton(physbutton); } + // Set mapping. + *joystick_input->variable = vbutton; + joystick_physical_buttons[vbutton] = physbutton; + TXT_CloseWindow(joystick_input->prompt_window); return 1; } @@ -125,9 +244,11 @@ static void TXT_JoystickInputSizeCalc(TXT_UNCAST_ARG(joystick_input)) joystick_input->widget.h = 1; } -static void GetJoystickButtonDescription(int button, char *buf, size_t buf_len) +static void GetJoystickButtonDescription(int vbutton, char *buf, + size_t buf_len) { - M_snprintf(buf, buf_len, "BUTTON #%i", button + 1); + M_snprintf(buf, buf_len, "BUTTON #%i", + PhysicalForVirtualButton(vbutton) + 1); } static void TXT_JoystickInputDrawer(TXT_UNCAST_ARG(joystick_input)) @@ -148,9 +269,9 @@ static void TXT_JoystickInputDrawer(TXT_UNCAST_ARG(joystick_input)) TXT_SetWidgetBG(joystick_input); TXT_FGColor(TXT_COLOR_BRIGHT_WHITE); - + TXT_DrawString(buf); - + for (i=strlen(buf); i