From ab17a995a49d4e819fe445ef57340e5c9dfa1093 Mon Sep 17 00:00:00 2001 From: Raf <> Date: Mon, 28 Aug 2023 04:50:26 +0800 Subject: [PATCH] enable using mouse on the radial menu --- port/include/input.h | 7 +++++++ port/src/input.c | 18 +++++++++++++++++- src/game/activemenu.c | 7 +++++++ src/game/activemenutick.c | 30 ++++++++++++++++++++++++++++-- 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/port/include/input.h b/port/include/input.h index a3859c5d3..3b38d9b91 100644 --- a/port/include/input.h +++ b/port/include/input.h @@ -14,6 +14,9 @@ #define CONT_NUM_BUTTONS 16 // not including the stick axes +#define CURDEV_MNK 0x01 +#define CURDEV_CONTR 0x02 + enum virtkey { /* same order as SDL scancodes */ VK_KEYBOARD_BEGIN = 0, @@ -128,4 +131,8 @@ void inputUpdate(void); // call this before configSave() void inputSaveConfig(void); +// returns the current used device (mouse+kb/controller) for the specified player +// TODO: in the future when the input system can differentiate inputs from different players, will need to update this +u8 inputGetCurrentUsedDevice(u8 playerNum); + #endif diff --git a/port/src/input.c b/port/src/input.c index 38bc1f005..65eab168c 100644 --- a/port/src/input.c +++ b/port/src/input.c @@ -8,6 +8,7 @@ #include "input.h" #include "video.h" #include "config.h" +#include "constants.h" #define MAX_BINDS 4 #define TRIG_THRESHOLD (30 * 256) @@ -130,6 +131,8 @@ static const char *vkJoyNames[] = { static char vkNames[VK_TOTAL_COUNT][64]; +static u8 currentUsedDevice[MAX_PLAYERS]; + void inputSetDefaultKeyBinds(void) { // TODO: make VK constants for all these @@ -479,7 +482,8 @@ static inline s32 inputAxisScale(s32 x, const s32 deadzone, const f32 scale) if (abs(x) < deadzone) { return 0; } else { - // rescale to fit the non-deadzone range + // rescale to fit the non-deadzone range + currentUsedDevice[0] = CURDEV_CONTR; if (x < 0) { x += deadzone; } else { @@ -503,6 +507,7 @@ s32 inputReadController(s32 idx, OSContPad *npad) for (u32 i = 0; i < CONT_NUM_BUTTONS; ++i) { if (inputBindPressed(idx, i)) { npad->button |= 1U << i; + currentUsedDevice[0] = CURDEV_CONTR; } } @@ -555,14 +560,18 @@ static inline void inputUpdateMouse(void) if (mouseWheel > 0) { mouseButtons |= WHEEL_UP_MASK; + currentUsedDevice[0] = CURDEV_MNK; } else if (mouseWheel < 0) { mouseButtons |= WHEEL_DN_MASK; + currentUsedDevice[0] = CURDEV_MNK; } mouseWheel = 0; if (mouseLocked) { SDL_GetRelativeMouseState(&mouseDX, &mouseDY); + if (mouseDX || mouseDY) + currentUsedDevice[0] = CURDEV_MNK; } else { mouseDX = mx - mouseX; mouseDY = my - mouseY; @@ -651,8 +660,10 @@ s32 inputKeyPressed(u32 vk) { if (vk >= VK_KEYBOARD_BEGIN && vk < VK_MOUSE_BEGIN) { const u8 *state = SDL_GetKeyboardState(NULL); + currentUsedDevice[0] = CURDEV_MNK; return state[vk - VK_KEYBOARD_BEGIN]; } else if (vk >= VK_MOUSE_BEGIN && vk < VK_JOY_BEGIN) { + currentUsedDevice[0] = CURDEV_MNK; return mouseButtons & SDL_BUTTON(vk - VK_MOUSE_BEGIN + 1); } else if (vk >= VK_JOY_BEGIN && vk < VK_TOTAL_COUNT) { vk -= VK_JOY_BEGIN; @@ -789,3 +800,8 @@ s32 inputGetKeyByName(const char *name) return -1; } + +u8 inputGetCurrentUsedDevice(u8 playerNum) +{ + return currentUsedDevice[playerNum]; +} diff --git a/src/game/activemenu.c b/src/game/activemenu.c index f3f496a2a..980b9a7b1 100644 --- a/src/game/activemenu.c +++ b/src/game/activemenu.c @@ -56,6 +56,9 @@ const u8 g_AmMapping[] = { 2, // unused }; +extern f32 menuMX; +extern f32 menuMY; + struct chrdata *currentPlayerGetCommandingAibot(void) { return g_Vars.currentplayer->commandingaibot; @@ -754,6 +757,10 @@ void amOpen(void) g_AmMenus[g_AmIndex].origscreennum = 0; g_AmMenus[g_AmIndex].prevallbots = 0; g_AmMenus[g_AmIndex].allbots = false; + +#ifndef PLATFORM_N64 + menuMX = menuMY = 0; +#endif } } diff --git a/src/game/activemenutick.c b/src/game/activemenutick.c index 39681565a..a9b79bb61 100644 --- a/src/game/activemenutick.c +++ b/src/game/activemenutick.c @@ -12,6 +12,10 @@ #include "lib/str.h" #include "data.h" #include "types.h" +#include "input.h" + +f32 menuMX; +f32 menuMY; void amTick(void) { @@ -43,8 +47,30 @@ void amTick(void) for (j = 0; j < numsamples; j++) { s8 gotonextscreen = false; - s8 cstickx = joyGetStickXOnSample(j, contpadnum); - s8 csticky = joyGetStickYOnSample(j, contpadnum); + s8 cstickx = joyGetStickXOnSample(j, contpadnum); + s8 csticky = joyGetStickYOnSample(j, contpadnum); + +#ifndef PLATFORM_N64 + if (inputGetCurrentUsedDevice(i) == CURDEV_MNK) { + f32 dx, dy; + inputMouseGetScaledDelta(&dx, &dy); + + menuMX += dx; + menuMY += dy; + + f32 sc = 8.f; + + s8 signx = (menuMX >= 0) ? 1 : -1; + s8 signy = (menuMY >= 0) ? 1 : -1; + + s32 lim = 100; + menuMX = fabsf(menuMX * sc) >= lim ? (menuMX - dx) : menuMX; + menuMY = fabsf(menuMY * sc) >= lim ? (menuMY - dy) : menuMY; + + cstickx = (s8) (menuMX * sc); + csticky = (s8) -(menuMY * sc); + } +#endif s8 absstickx; s8 abssticky; u16 buttonsstate = joyGetButtonsOnSample(j, contpadnum, 0xffff);