From e2dc98d1449c3e6682837f04eda60814f8d6a5fc Mon Sep 17 00:00:00 2001 From: Arisotura Date: Thu, 20 Jun 2019 16:31:28 +0200 Subject: [PATCH] fix bug when mapping a joystick axis control but no button --- src/libui_sdl/DlgInputConfig.cpp | 35 +++++++++++++++++++----------- src/libui_sdl/main.cpp | 37 ++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/src/libui_sdl/DlgInputConfig.cpp b/src/libui_sdl/DlgInputConfig.cpp index 88822753..3818c048 100644 --- a/src/libui_sdl/DlgInputConfig.cpp +++ b/src/libui_sdl/DlgInputConfig.cpp @@ -125,21 +125,30 @@ void JoyMappingName(int id, char* str) return; } - if (id & 0x100) - { - int hatnum = ((id >> 4) & 0xF) + 1; + bool hasbtn = ((id & 0xFFFF) != 0xFFFF); - switch (id & 0xF) + if (hasbtn) + { + if (id & 0x100) { - case 0x1: sprintf(str, "Hat %d up", hatnum); break; - case 0x2: sprintf(str, "Hat %d right", hatnum); break; - case 0x4: sprintf(str, "Hat %d down", hatnum); break; - case 0x8: sprintf(str, "Hat %d left", hatnum); break; + int hatnum = ((id >> 4) & 0xF) + 1; + + switch (id & 0xF) + { + case 0x1: sprintf(str, "Hat %d up", hatnum); break; + case 0x2: sprintf(str, "Hat %d right", hatnum); break; + case 0x4: sprintf(str, "Hat %d down", hatnum); break; + case 0x8: sprintf(str, "Hat %d left", hatnum); break; + } + } + else + { + sprintf(str, "Button %d", (id & 0xFFFF) + 1); } } else { - sprintf(str, "Button %d", (id & 0xFFFF) + 1); + strcpy(str, ""); } if (id & 0x10000) @@ -151,9 +160,9 @@ void JoyMappingName(int id, char* str) switch ((id >> 20) & 0xF) { - case 0: sprintf(str, "%s / Axis %d +", tmp, axisnum); break; - case 1: sprintf(str, "%s / Axis %d -", tmp, axisnum); break; - case 2: sprintf(str, "%s / Trigger %d", tmp, axisnum); break; + case 0: sprintf(str, "%s%sAxis %d +", tmp, hasbtn?" / ":"", axisnum); break; + case 1: sprintf(str, "%s%sAxis %d -", tmp, hasbtn?" / ":"", axisnum); break; + case 2: sprintf(str, "%s%sTrigger %d", tmp, hasbtn?" / ":"", axisnum); break; } } } @@ -287,7 +296,7 @@ Uint32 JoyPoll(Uint32 interval, void* param) } int oldmap; - if (dlg->joymap[id] == -1) oldmap = 0; + if (dlg->joymap[id] == -1) oldmap = 0xFFFF; else oldmap = dlg->joymap[id]; int nbuttons = SDL_JoystickNumButtons(joy); diff --git a/src/libui_sdl/main.cpp b/src/libui_sdl/main.cpp index 693e9e36..01f67d77 100644 --- a/src/libui_sdl/main.cpp +++ b/src/libui_sdl/main.cpp @@ -710,26 +710,31 @@ bool JoystickButtonDown(int val) { if (val == -1) return false; - if (val & 0x100) + bool hasbtn = ((val & 0xFFFF) != 0xFFFF); + + if (hasbtn) { - int hatnum = (val >> 4) & 0xF; - int hatdir = val & 0xF; - Uint8 hatval = SDL_JoystickGetHat(Joystick, hatnum); + if (val & 0x100) + { + int hatnum = (val >> 4) & 0xF; + int hatdir = val & 0xF; + Uint8 hatval = SDL_JoystickGetHat(Joystick, hatnum); - bool pressed = false; - if (hatdir == 0x1) pressed = (hatval & SDL_HAT_UP); - else if (hatdir == 0x4) pressed = (hatval & SDL_HAT_DOWN); - else if (hatdir == 0x2) pressed = (hatval & SDL_HAT_RIGHT); - else if (hatdir == 0x8) pressed = (hatval & SDL_HAT_LEFT); + bool pressed = false; + if (hatdir == 0x1) pressed = (hatval & SDL_HAT_UP); + else if (hatdir == 0x4) pressed = (hatval & SDL_HAT_DOWN); + else if (hatdir == 0x2) pressed = (hatval & SDL_HAT_RIGHT); + else if (hatdir == 0x8) pressed = (hatval & SDL_HAT_LEFT); - if (pressed) return true; - } - else - { - int btnnum = val & 0xFFFF; - Uint8 btnval = SDL_JoystickGetButton(Joystick, btnnum); + if (pressed) return true; + } + else + { + int btnnum = val & 0xFFFF; + Uint8 btnval = SDL_JoystickGetButton(Joystick, btnnum); - if (btnval) return true; + if (btnval) return true; + } } if (val & 0x10000)