From a004920785e110da4f934b5c68a88be8de809225 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 29 Sep 2024 07:21:18 +1000 Subject: [PATCH] Fix pointer inputs not being properly intercepted by onscreen keyboard Xbox: Fix being too sensitive to joystick input --- Makefile | 2 +- src/Input.h | 8 ++++---- src/InputHandler.c | 6 +++--- src/Launcher.c | 2 +- src/Menus.c | 7 ++++--- src/VirtualKeyboard.h | 38 ++++++++++++++++++++++++++++---------- src/Window_Xbox.c | 4 ++-- 7 files changed, 43 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 15fe860b9..a10eb5214 100644 --- a/Makefile +++ b/Makefile @@ -133,9 +133,9 @@ endif ifeq ($(PLAT),dos) CC = i586-pc-msdosdjgpp-gcc LIBS = - BUILD_DIR = build-dos LDFLAGS = -g OEXT = .exe + BUILD_DIR = build-dos endif diff --git a/src/Input.h b/src/Input.h index aa891bd02..e32b228c1 100644 --- a/src/Input.h +++ b/src/Input.h @@ -79,8 +79,8 @@ extern struct _InputState { cc_bool RawMode; /* Sources available for input (Mouse/Keyboard, Gamepad) */ cc_uint8 Sources; - /* Function that overrides all normal input handling (e.g. for virtual keyboard) */ - void (*DownHook)(int btn, struct InputDevice* device); + /* Function that can override all normal input handling (e.g. for virtual keyboard) */ + cc_bool (*DownHook)(int btn, struct InputDevice* device); } Input; /* Sets Input_Pressed[key] to true and raises InputEvents.Down */ @@ -185,9 +185,9 @@ extern struct TouchPointer touches[INPUT_MAX_POINTERS]; struct Pointer { int x, y; /* Function that overrides all normal pointer input press handling */ - void (*DownHook)(int index); + cc_bool (*DownHook)(int index); /* Function that overrides all normal pointer input release handling */ - void (*UpHook) (int index); + cc_bool (*UpHook) (int index); }; CC_VAR extern struct Pointer Pointers[INPUT_MAX_POINTERS]; diff --git a/src/InputHandler.c b/src/InputHandler.c index e8f7c933d..b979b2df4 100644 --- a/src/InputHandler.c +++ b/src/InputHandler.c @@ -784,7 +784,7 @@ cc_bool KeyBind_IsPressed(InputBind binding) { return Bind_IsTriggered[binding]; static void OnPointerDown(void* obj, int idx) { struct Screen* s; int i, x, y, mask; - if (Pointers[idx].DownHook) { Pointers[idx].DownHook(idx); return; } + if (Pointers[0].DownHook && Pointers[0].DownHook(idx)) return; /* Always set last click time, otherwise quickly tapping */ /* sometimes triggers a 'delete' in InputHandler_Tick, */ @@ -822,7 +822,7 @@ static void OnPointerDown(void* obj, int idx) { static void OnPointerUp(void* obj, int idx) { struct Screen* s; int i, x, y; - if (Pointers[idx].UpHook) { Pointers[idx].UpHook(idx); return; } + if (Pointers[0].UpHook && Pointers[0].UpHook(idx)) return; #ifdef CC_BUILD_TOUCH CheckBlockTap(idx); @@ -841,7 +841,7 @@ static void OnInputDown(void* obj, int key, cc_bool was, struct InputDevice* dev struct Screen* s; cc_bool triggered; int i; - if (Input.DownHook) { Input.DownHook(key, device); return; } + if (Input.DownHook && Input.DownHook(key, device)) return; #ifndef CC_BUILD_WEB if (key == device->escapeButton && (s = Gui_GetClosable())) { diff --git a/src/Launcher.c b/src/Launcher.c index 1bf43d022..0d35f177d 100644 --- a/src/Launcher.c +++ b/src/Launcher.c @@ -190,7 +190,7 @@ static cc_bool IsShutdown(int key) { } static void OnInputDown(void* obj, int key, cc_bool was, struct InputDevice* device) { - if (Input.DownHook) { Input.DownHook(key, device); return; } + if (Input.DownHook && Input.DownHook(key, device)) return; if (IsShutdown(key)) Launcher_ShouldExit = true; Launcher_Active->KeyDown(Launcher_Active, key, was, device); diff --git a/src/Menus.c b/src/Menus.c index dd2aa636e..7454169be 100644 --- a/src/Menus.c +++ b/src/Menus.c @@ -1973,14 +1973,14 @@ static void KeyBindsScreen_Update(struct KeyBindsScreen* s, int i) { s->dirty = true; } -static void KeyBindsScreen_TriggerBinding(int key, struct InputDevice* device) { +static cc_bool KeyBindsScreen_TriggerBinding(int key, struct InputDevice* device) { struct KeyBindsScreen* s = &KeyBindsScreen; InputBind bind; int idx; - if (device->type != bind_device->type) return; + if (device->type != bind_device->type) return false; Input.DownHook = NULL; - if (s->curI == -1) return; + if (s->curI == -1) return false; bind = s->binds[s->curI]; if (key == device->escapeButton) { @@ -1993,6 +1993,7 @@ static void KeyBindsScreen_TriggerBinding(int key, struct InputDevice* device) { s->curI = -1; s->closable = true; KeyBindsScreen_Update(s, idx); + return true; } static void KeyBindsScreen_OnBindingClick(void* screen, void* widget) { diff --git a/src/VirtualKeyboard.h b/src/VirtualKeyboard.h index 954e8396a..a23052762 100644 --- a/src/VirtualKeyboard.h +++ b/src/VirtualKeyboard.h @@ -271,7 +271,7 @@ static void VirtualKeyboard_ClickSelected(void) { } } -static void VirtualKeyboard_ProcessDown(int key, struct InputDevice* device) { +static cc_bool VirtualKeyboard_OnInputDown(int key, struct InputDevice* device) { int deltaX, deltaY; Input_CalcDelta(key, device, &deltaX, &deltaY); @@ -290,6 +290,7 @@ static void VirtualKeyboard_ProcessDown(int key, struct InputDevice* device) { } else if (key == CCPAD_R) { VirtualKeyboard_AppendChar('/'); } + return true; } static void VirtualKeyboard_PadAxis(void* obj, int port, int axis, float x, float y) { @@ -302,21 +303,36 @@ static void VirtualKeyboard_PadAxis(void* obj, int port, int axis, float x, floa if (ySteps) VirtualKeyboard_Scroll(0, ySteps > 0 ? 1 : -1); } -static void VirtualKeyboard_PointerDown(void* obj, int idx) { +static cc_bool VirtualKeyboard_GetPointerPosition(int idx, int* kbX, int* kbY) { int width = VirtualKeyboard_Width(); int height = VirtualKeyboard_Height(); - int kbX, kbY; - VirtualKeyboard_CalcPosition(&kbX, &kbY, VirtualKeyboard_WindowWidth(), VirtualKeyboard_WindowHeight()); + int originX, originY; + VirtualKeyboard_CalcPosition(&originX, &originY, VirtualKeyboard_WindowWidth(), VirtualKeyboard_WindowHeight()); int x = Pointers[idx].x, y = Pointers[idx].y; - if (x < kbX || y < kbY || x >= kbX + width || y >= kbY + height) return; + if (x < originX || y < originY || x >= originX + width || y >= originY + height) return false; - kb_curX = (x - kbX) / kb_tileWidth; - kb_curY = (y - kbY) / kb_tileHeight; + *kbX = x - originX; + *kbY = y - originY; + return true; +} + +static cc_bool VirtualKeyboard_PointerDown(int idx) { + int kbX, kbY; + if (!VirtualKeyboard_GetPointerPosition(idx, &kbX, &kbY)) return false; + + kb_curX = kbX / kb_tileWidth; + kb_curY = kbY / kb_tileHeight; if (kb_curX >= kb->cellsPerRow) kb_curX = kb->cellsPerRow - 1; VirtualKeyboard_Clamp(); VirtualKeyboard_ClickSelected(); + return true; +} + +static cc_bool VirtualKeyboard_PointerUp(int idx) { + int kbX, kbY; + return VirtualKeyboard_GetPointerPosition(idx, &kbX, &kbY); } @@ -415,7 +431,8 @@ static void VirtualKeyboard_Hook(void) { /* the virtual keyboard in the first place gets mistakenly processed */ kb_needsHook = false; Event_Register_(&ControllerEvents.AxisUpdate, NULL, VirtualKeyboard_PadAxis); - Event_Register_(&PointerEvents.Down, NULL, VirtualKeyboard_PointerDown); + Pointers[0].DownHook = VirtualKeyboard_PointerDown; + Pointers[0].UpHook = VirtualKeyboard_PointerUp; } static void VirtualKeyboard_Open(struct OpenKeyboardArgs* args, cc_bool launcher) { @@ -445,7 +462,7 @@ static void VirtualKeyboard_Open(struct OpenKeyboardArgs* args, cc_bool launcher } Window_Main.SoftKeyboardFocus = true; - Input.DownHook = VirtualKeyboard_ProcessDown; + Input.DownHook = VirtualKeyboard_OnInputDown; LBackend_Hooks[0] = VirtualKeyboard_Display2D; Game.Draw2DHooks[0] = VirtualKeyboard_Display3D; } @@ -463,7 +480,8 @@ static void VirtualKeyboard_Close(void) { VirtualKeyboard_Close3D(); Event_Unregister_(&ControllerEvents.AxisUpdate, NULL, VirtualKeyboard_PadAxis); - Event_Unregister_(&PointerEvents.Down, NULL, VirtualKeyboard_PointerDown); + Pointers[0].DownHook = NULL; + Pointers[0].UpHook = NULL; Window_Main.SoftKeyboardFocus = false; KB_MarkDirty = NULL; diff --git a/src/Window_Xbox.c b/src/Window_Xbox.c index 406e482d6..9edf590b8 100644 --- a/src/Window_Xbox.c +++ b/src/Window_Xbox.c @@ -171,8 +171,8 @@ static void HandleButtons(int port, xid_gamepad_in* gp) { #define AXIS_SCALE 8192.0f static void HandleJoystick(int port, int axis, int x, int y, float delta) { - if (Math_AbsI(x) <= 512) x = 0; - if (Math_AbsI(y) <= 512) y = 0; + if (Math_AbsI(x) <= 4096) x = 0; + if (Math_AbsI(y) <= 4096) y = 0; Gamepad_SetAxis(port, axis, x / AXIS_SCALE, -y / AXIS_SCALE, delta); }