From 8b386a2175dfa0f625d46056157ec94430198ec5 Mon Sep 17 00:00:00 2001 From: fgsfds Date: Mon, 1 Jun 2020 00:03:34 +0300 Subject: [PATCH] allow binding stick directions to controller buttons --- src/pc/controller/controller_api.h | 10 +++++++++- src/pc/controller/controller_keyboard.c | 22 ++++++++++------------ src/pc/controller/controller_sdl.c | 22 +++++++++++++++++++--- 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/pc/controller/controller_api.h b/src/pc/controller/controller_api.h index 74db6552..6e55c846 100644 --- a/src/pc/controller/controller_api.h +++ b/src/pc/controller/controller_api.h @@ -1,10 +1,18 @@ #ifndef CONTROLLER_API #define CONTROLLER_API -#define DEADZONE_STEP 310 // original deadzone is 4960 +#define DEADZONE_STEP 310 // original deadzone is 4960 #define VK_INVALID 0xFFFF #define VK_SIZE 0x1000 +// fake buttons for binding the stick directions +#define STICK_UP 0x80000 +#define STICK_DOWN 0x40000 +#define STICK_LEFT 0x10000 +#define STICK_RIGHT 0x20000 +#define STICK_XMASK 0x30000 +#define STICK_YMASK 0xc0000 + #include struct ControllerAPI { diff --git a/src/pc/controller/controller_keyboard.c b/src/pc/controller/controller_keyboard.c index 9fb4b635..b38352dd 100644 --- a/src/pc/controller/controller_keyboard.c +++ b/src/pc/controller/controller_keyboard.c @@ -61,10 +61,10 @@ static void keyboard_bindkeys(void) { bzero(keyboard_mapping, sizeof(keyboard_mapping)); num_keybinds = 0; - keyboard_add_binds(0x80000, configKeyStickUp); - keyboard_add_binds(0x10000, configKeyStickLeft); - keyboard_add_binds(0x40000, configKeyStickDown); - keyboard_add_binds(0x20000, configKeyStickRight); + keyboard_add_binds(STICK_UP, configKeyStickUp); + keyboard_add_binds(STICK_LEFT, configKeyStickLeft); + keyboard_add_binds(STICK_DOWN, configKeyStickDown); + keyboard_add_binds(STICK_RIGHT, configKeyStickRight); keyboard_add_binds(A_BUTTON, configKeyA); keyboard_add_binds(B_BUTTON, configKeyB); keyboard_add_binds(Z_TRIG, configKeyZ); @@ -87,18 +87,16 @@ static void keyboard_init(void) { static void keyboard_read(OSContPad *pad) { pad->button |= keyboard_buttons_down; - if ((keyboard_buttons_down & 0x30000) == 0x10000) { + const u32 xstick = keyboard_buttons_down & STICK_XMASK; + const u32 ystick = keyboard_buttons_down & STICK_YMASK; + if (xstick == STICK_LEFT) pad->stick_x = -128; - } - if ((keyboard_buttons_down & 0x30000) == 0x20000) { + else if (xstick == STICK_RIGHT) pad->stick_x = 127; - } - if ((keyboard_buttons_down & 0xc0000) == 0x40000) { + if (ystick == STICK_DOWN) pad->stick_y = -128; - } - if ((keyboard_buttons_down & 0xc0000) == 0x80000) { + else if (ystick == STICK_UP) pad->stick_y = 127; - } } static u32 keyboard_rawkey(void) { diff --git a/src/pc/controller/controller_sdl.c b/src/pc/controller/controller_sdl.c index b28e3f7b..1f56236c 100644 --- a/src/pc/controller/controller_sdl.c +++ b/src/pc/controller/controller_sdl.c @@ -35,7 +35,6 @@ extern u8 newcam_mouse; static bool init_ok; static SDL_GameController *sdl_cntrl; - static u32 num_joy_binds = 0; static u32 num_mouse_binds = 0; static u32 joy_binds[MAX_JOYBINDS][2]; @@ -71,6 +70,10 @@ static void controller_sdl_bind(void) { controller_add_binds(A_BUTTON, configKeyA); controller_add_binds(B_BUTTON, configKeyB); controller_add_binds(Z_TRIG, configKeyZ); + controller_add_binds(STICK_UP, configKeyStickUp); + controller_add_binds(STICK_LEFT, configKeyStickLeft); + controller_add_binds(STICK_DOWN, configKeyStickDown); + controller_add_binds(STICK_RIGHT, configKeyStickRight); controller_add_binds(U_CBUTTONS, configKeyCUp); controller_add_binds(L_CBUTTONS, configKeyCLeft); controller_add_binds(D_CBUTTONS, configKeyCDown); @@ -117,7 +120,6 @@ static void controller_sdl_read(OSContPad *pad) { // remember buttons that changed from 0 to 1 last_mouse = (mouse_buttons ^ mouse) & mouse; mouse_buttons = mouse; - #endif SDL_GameControllerUpdate(); @@ -147,9 +149,23 @@ static void controller_sdl_read(OSContPad *pad) { if (pressed) last_joybutton = i; } + u32 buttons_down = 0; for (u32 i = 0; i < num_joy_binds; ++i) if (joy_buttons[joy_binds[i][0]]) - pad->button |= joy_binds[i][1]; + buttons_down |= joy_binds[i][1]; + + pad->button |= buttons_down; + + const u32 xstick = buttons_down & STICK_XMASK; + const u32 ystick = buttons_down & STICK_YMASK; + if (xstick == STICK_LEFT) + pad->stick_x = -128; + else if (xstick == STICK_RIGHT) + pad->stick_x = 127; + if (ystick == STICK_DOWN) + pad->stick_y = -128; + else if (ystick == STICK_UP) + pad->stick_y = 127; int16_t leftx = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_LEFTX); int16_t lefty = SDL_GameControllerGetAxis(sdl_cntrl, SDL_CONTROLLER_AXIS_LEFTY);