From c0ca89fdda75d3959799e54b1e09c5ca3f2dfe10 Mon Sep 17 00:00:00 2001 From: itaskaev2 <165683571+itaskaev2@users.noreply.github.com> Date: Wed, 11 Nov 2026 09:44:01 +0100 Subject: [PATCH] Keep the wheel's focus until the pointer leaves a dead zone, and draw a pointer of our own After the scroll wheel, the keys or the pad move the focus in a menu, the pointer takes it back only once it has moved 6 layout units from where it was then (menu_cursor_poll), so a hand resting on the mouse no longer undoes a scroll. While the cursor is not captured, the game window shows a gold arrowhead drawn at the window's DPI (cursor_art.c) instead of the Windows arrow. Co-Authored-By: Claude Opus 5.5 --- runtime/cursor_art.c | 89 +++++++++++++++++++++++++++++++++++++++++++ runtime/input_sdl.c | 18 +++++++++ runtime/input_win.c | 38 +++++++++++++++++- runtime/input_wsys.h | 5 +++ runtime/menu_cursor.c | 22 +++++++++-- 5 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 runtime/cursor_art.c diff --git a/runtime/cursor_art.c b/runtime/cursor_art.c new file mode 100644 index 00000000..3a9278f6 --- /dev/null +++ b/runtime/cursor_art.c @@ -0,0 +1,89 @@ +// The mouse pointer's image in the game window while the cursor is not captured (the game's menus, the settings menu): +// a gold arrowhead with a notched back, a dark outline, a turquoise gem and a soft shadow, drawn here at the size the +// window's DPI asks for instead of the Windows arrow. input_win.c and input_sdl.c make the system cursor from it. +// Nothing comes from the game's files. +#include +#include + +#include "input_wsys.h" + +typedef struct { float x, y; } V; + +// In units of a 32-pixel cursor: the tip at the top left (the hotspot), the wings to the right and below, the notch +// between them +static const V SHAPE[] = {{1.5f, 1.5f}, {25.0f, 10.5f}, {14.5f, 14.5f}, {10.5f, 25.0f}}; +static const V GEM[] = {{10.4f, 7.6f}, {13.0f, 10.2f}, {10.4f, 12.8f}, {7.8f, 10.2f}}; +#define OUTLINE 1.7f // the dark edge's width +#define SHADOW 1.6f // the shadow's offset to the bottom right + +static float seg_dist(V p, V a, V b) { + float dx = b.x - a.x, dy = b.y - a.y, t = ((p.x - a.x) * dx + (p.y - a.y) * dy) / (dx * dx + dy * dy); + t = t < 0 ? 0 : t > 1 ? 1 : t; + float ex = a.x + t * dx - p.x, ey = a.y + t * dy - p.y; + return sqrtf(ex * ex + ey * ey); +} + +// Distance to the polygon's edge, negative inside (even-odd: the shapes are simple) +static float poly_sdf(V p, const V *v, int n) { + float d = 1e9f; + int in = 0; + for (int i = 0, j = n - 1; i < n; j = i++) { + float e = seg_dist(p, v[j], v[i]); + if (e < d) d = e; + if ((v[i].y > p.y) != (v[j].y > p.y) && p.x < (v[j].x - v[i].x) * (p.y - v[i].y) / (v[j].y - v[i].y) + v[i].x) + in = !in; + } + return in ? -d : d; +} + +// One sample's colour (straight r, g, b in 0..1) and coverage +static float sample(V p, float *r, float *g, float *b) { + float d = poly_sdf(p, SHAPE, 4); + if (d < 0) { + if (-d < OUTLINE) { *r = 0.17f, *g = 0.11f, *b = 0.04f; return 1; } + if (poly_sdf(p, GEM, 4) < 0) { // the gem, lighter towards its top left + float t = (p.x + p.y - 20.4f) / 5.2f + 0.5f; + t = t < 0 ? 0 : t > 1 ? 1 : t; + *r = 0.62f - 0.42f * t, *g = 0.98f - 0.22f * t, *b = 0.92f - 0.12f * t; + return 1; + } + // gold, pale at the tip and deeper at the wings; a thin light line inside the outline + float t = (p.x + p.y) / 34.0f; + t = t < 0 ? 0 : t > 1 ? 1 : t; + float rim = -d < OUTLINE + 1.0f ? 0.12f : 0; + *r = fminf(1, 1.00f - 0.12f * t + rim), *g = fminf(1, 0.93f - 0.32f * t + rim), + *b = fminf(1, 0.70f - 0.52f * t + rim); + return 1; + } + V s = {p.x - SHADOW, p.y - SHADOW}; + float ds = poly_sdf(s, SHAPE, 4); + if (ds < 0.8f) { // the shadow, soft at its edge + *r = *g = *b = 0; + float a = ds < -0.8f ? 1 : (0.8f - ds) / 1.6f; + return 0.38f * a; + } + return 0; +} + +void cursor_art(uint32_t *argb, int size, int *hot) { + float k = 32.0f / (float)size; + enum { SS = 4 }; // 4 x 4 samples a pixel + for (int y = 0; y < size; y++) + for (int x = 0; x < size; x++) { + float r = 0, g = 0, b = 0, a = 0; + for (int sy = 0; sy < SS; sy++) + for (int sx = 0; sx < SS; sx++) { + V p = {(x + (sx + 0.5f) / SS) * k, (y + (sy + 0.5f) / SS) * k}; + float sr, sg, sb, c = sample(p, &sr, &sg, &sb); + r += sr * c, g += sg * c, b += sb * c, a += c; + } + uint32_t px = 0; + if (a > 0) { // straight alpha, as Windows and SDL take it + r /= a, g /= a, b /= a, a /= SS * SS; + px = (uint32_t)lrintf(a * 255) << 24 | (uint32_t)lrintf(r * 255) << 16 | + (uint32_t)lrintf(g * 255) << 8 | (uint32_t)lrintf(b * 255); + } + argb[y * size + x] = px; + } + *hot = (int)(1.5f / k); +} diff --git a/runtime/input_sdl.c b/runtime/input_sdl.c index 1f7383bd..05aadacc 100644 --- a/runtime/input_sdl.c +++ b/runtime/input_sdl.c @@ -200,8 +200,26 @@ void ikb_menu_closed(void) { if (o & CUR_ESC) plat_cas32(&cur_rel, 0, o); } +// The pointer over the window while the cursor is not captured (cursor_art.c), sized by the window's display scale. +// The default arrow stays when SDL refuses it. +static void art_cursor(SDL_Window *w) { + float scale = SDL_GetWindowDisplayScale(w); + int size = (int)lrintf(32 * (scale > 0 ? scale : 1)), hot; + size = size < 32 ? 32 : size > 128 ? 128 : size; + SDL_Surface *s = SDL_CreateSurface(size, size, SDL_PIXELFORMAT_ARGB8888); + if (!s) return; + if (s->pitch == size * 4) { + cursor_art(s->pixels, size, &hot); + SDL_Cursor *c = SDL_CreateColorCursor(s, hot, hot); + if (c) SDL_SetCursor(c); + else rt_log("input: the pointer's image failed (%s); the arrow stays", SDL_GetError()); + } + SDL_DestroySurface(s); +} + void input_sdl_window(SDL_Window *w) { win = w; + art_cursor(w); wake_event = SDL_RegisterEvents(1); plat_xchg32(&focused, (SDL_GetWindowFlags(w) & SDL_WINDOW_INPUT_FOCUS) != 0); __atomic_store_n(&ptr_have, 0, __ATOMIC_RELEASE); // the pointer's place is unknown until it enters or moves diff --git a/runtime/input_win.c b/runtime/input_win.c index 56ed7437..11c660b4 100644 --- a/runtime/input_win.c +++ b/runtime/input_win.c @@ -122,6 +122,39 @@ static void cursor_update(HWND h) { ReleaseSRWLockExclusive(&cur_lock); } +// The pointer shown over the client area while the cursor is not captured (cursor_art.c), 32 pixels at 96 DPI and +// larger with the window's DPI; made again when the DPI changes. Window thread. NULL when Windows refuses it: the +// window class's arrow stays. +static HCURSOR art_cursor(HWND h) { + static HCURSOR cur; + static UINT made_dpi; + UINT dpi = GetDpiForWindow(h); + if (!dpi) dpi = USER_DEFAULT_SCREEN_DPI; + if (cur && dpi == made_dpi) return cur; + int size = MulDiv(32, (int)dpi, USER_DEFAULT_SCREEN_DPI), hot; + size = size < 32 ? 32 : size > 128 ? 128 : size; + BITMAPV5HEADER bh = {.bV5Size = sizeof bh, .bV5Width = size, .bV5Height = -size, .bV5Planes = 1, .bV5BitCount = 32, + .bV5Compression = BI_BITFIELDS, .bV5RedMask = 0xff0000, .bV5GreenMask = 0xff00, + .bV5BlueMask = 0xff, .bV5AlphaMask = 0xff000000u}; + void *bits = NULL; + HDC dc = GetDC(NULL); + HBITMAP color = CreateDIBSection(dc, (BITMAPINFO *)&bh, DIB_RGB_COLORS, &bits, NULL, 0); + ReleaseDC(NULL, dc); + HBITMAP mask = CreateBitmap(size, size, 1, 1, NULL); + HCURSOR made = NULL; + if (color && mask && bits) { + cursor_art(bits, size, &hot); + ICONINFO ii = {.fIcon = FALSE, .xHotspot = (DWORD)hot, .yHotspot = (DWORD)hot, .hbmMask = mask, + .hbmColor = color}; + made = (HCURSOR)CreateIconIndirect(&ii); + } + if (color) DeleteObject(color); + if (mask) DeleteObject(mask); + if (!made) return cur; // the old size, or NULL + cur = made, made_dpi = dpi; // the old one may still be on screen: kept (a DPI change is rare) + return cur; +} + int input_cursor_released(void) { return (cur_rel & CUR_REL) != 0; } void input_cursor_suspend(int on) { @@ -159,7 +192,10 @@ int input_window_msg(HWND h, UINT m, WPARAM w, LPARAM l, LRESULT *res) { break; // DefWindowProc must see WM_INPUT for cleanup } case WM_SETCURSOR: - if (cur_captured && LOWORD(l) == HTCLIENT) { SetCursor(NULL); *res = TRUE; return 1; } + if (LOWORD(l) == HTCLIENT) { + HCURSOR c = cur_captured ? NULL : art_cursor(h); + if (cur_captured || c) { SetCursor(c); *res = TRUE; return 1; } + } break; case WM_SYSCOMMAND: // Alt pressed alone would enter the window menu loop, which stalls the game: Alt is a game key if ((w & 0xFFF0) == SC_KEYMENU && l == 0) { *res = 0; return 1; } diff --git a/runtime/input_wsys.h b/runtime/input_wsys.h index 0460595a..24fcdc04 100644 --- a/runtime/input_wsys.h +++ b/runtime/input_wsys.h @@ -19,5 +19,10 @@ int32_t ikb_wheel_take(void); // whole wheel notches turned sinc void ikb_cursor_update(void); // what input_cursor_allowed says may have changed void ikb_menu_closed(void); // the menu closed: a capture that Esc gave up comes back +// ---- shared by the back ends +// cursor_art.c: the pointer's image at size x size pixels (0xAARRGGBB, straight alpha, rows from the top) and its +// hotspot (*hot, the same in x and y). Pure. +void cursor_art(uint32_t *argb, int size, int *hot); + // ---- what input.c provides the back end int input_cursor_allowed(void); // no text entry or menu open, the mouse camera on, input not disabled diff --git a/runtime/menu_cursor.c b/runtime/menu_cursor.c index 2b5765d9..d6bcf266 100644 --- a/runtime/menu_cursor.c +++ b/runtime/menu_cursor.c @@ -50,6 +50,10 @@ enum { DI_PROJ = 0x10, DI_VIEW = 0x50, LYT_ROOT = 0x18, LYT_SIZE = 0x28, LYT_NAM // nn::ui2d::Pane enum { PANE_PARENT = 0x18, PANE_SIZE = 0x50, PANE_FLAG = 0x58, PANE_ALPHA = 0x59, PANE_BASE = 0x5b, PANE_MTX = 0x70 }; +// How far the pointer moves, in layout units (1280 x 720 across the picture: 1.5 window pixels at 1080p, 3 at 4K), +// before it takes back a focus that the wheel, the keys or the pad moved away from it (menu_cursor_poll) +#define DEAD_ZONE 6.0f + static uint64_t main_lo, main_hi; static int ok_at(uint64_t p, uint64_t n) { @@ -324,9 +328,10 @@ static void log_screen(const Focus *f, const char *nm, int n, float lw, float lh // Once per pad read on the game thread: the pointer's hover and clicks, as pad buttons (A, B) for this read void menu_cursor_poll(uint64_t *buttons) { - static int inited, pending_a, held_l, held_r; + static int inited, pending_a, held_l, held_r, anchored; static uint64_t pending_node, pending_until, hold_until, hold_bit, menu_seen; - static float last_px = -1, last_py = -1; + static uint64_t ptr_node, last_focus; // the item the pointer last pointed at, the focus at the last read + static float last_px = -1, last_py = -1, anchor_px, anchor_py; if (!inited) { inited = 1; main_lo = MOD_MAIN_BASE, main_hi = MOD_MAIN_BASE + MAIN_SIZE; @@ -367,7 +372,7 @@ void menu_cursor_poll(uint64_t *buttons) { return; } if (gap) return; // a click's focus request waits for the next screen - if (!menu) { pending_a = 0; last_px = last_py = -1; return; } + if (!menu) { pending_a = 0, anchored = 0, ptr_node = last_focus = 0; last_px = last_py = -1; return; } if (pending_a) { // the focus asked for by a click: A once it is there if (rd64(f.control + CTL_ACTIVE) == pending_node) { pending_a = 0, hold_until = now + 60, hold_bit = 1ull << B_A; @@ -381,11 +386,21 @@ void menu_cursor_poll(uint64_t *buttons) { if (click_r) { hold_until = now + 60, hold_bit = 1ull << B_B; *buttons |= hold_bit; return; } float px, py; if (!picture_pos(&px, &py)) return; + // The focus moved to another item than the pointer's (the scroll wheel, the keys, the pad): the pointer takes it + // back only after it has left a dead zone of DEAD_ZONE layout units around where it was then, so a hand resting on + // the mouse does not undo a scroll. A click still acts at once. + if (f.node != last_focus) { + if (f.node != ptr_node) anchored = 1, anchor_px = px, anchor_py = py; + last_focus = f.node; + } int moved = fabsf(px - last_px) > 0.001f || fabsf(py - last_py) > 0.001f; if (!moved && !click_l) return; last_px = px, last_py = py; float lw, lh; layout_size(f.screen, &lw, &lh); + if (anchored && !click_l && fabsf(px - anchor_px) * lw < DEAD_ZONE && fabsf(py - anchor_py) * lh < DEAD_ZONE) + return; + anchored = 0; Xf xf = screen_xf(f.screen); Pt p = {(px - 0.5f) * lw, (0.5f - py) * lh, px * 2 - 1, 1 - py * 2, NULL}; if (xf.ok && !xf_plain(&xf, lw, lh)) p.t = &xf; @@ -394,6 +409,7 @@ void menu_cursor_poll(uint64_t *buttons) { log_screen(&f, nm, n, lw, lh, p.t, n && stale_hit_panes(n, btn, lw, lh, p.t)); uint64_t at = n ? node_at(n, node, btn, &p, lw, lh) : 0; if (!at) return; + ptr_node = at; if (at != f.node) move_focus(&f, at); if (click_l) { if (at == f.node) { hold_until = now + 60, hold_bit = 1ull << B_A; *buttons |= hold_bit; }