diff --git a/runtime/input.h b/runtime/input.h index b55012da..8a02281e 100644 --- a/runtime/input.h +++ b/runtime/input.h @@ -13,12 +13,20 @@ void input_poll(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int32_ // reports nothing; input_block(0) when it closes: nothing until all buttons and stick directions are released. void input_poll_ui(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry); void input_block(int on); +// The in-game menu (ui/menu.h): input_menu(1) when it opens (input_poll reports nothing, the cursor is released), (0) when +// it closes (nothing until every button and stick direction is released). Independent of input_block. +void input_menu(int open); +// The gamepad alone, for the menu: Switch button bits and the left stick (up +y). 0 without a connected gamepad. +int input_pad_read(uint64_t *buttons, int32_t *lx, int32_t *ly); // Mouse camera and cursor (input_win.c, Win32 window thread). The window procedure calls input_window_msg first for raw // mouse input, cursor hiding and confinement; it returns 1 with *res set when the message is handled. input_cursor_suspend(1) // releases the cursor around an F11 window change, (0) captures it again once the client area has its new size. #ifdef _WIN32 int input_window_msg(HWND h, UINT m, WPARAM w, LPARAM l, LRESULT *res); +// The menu's Win32 adapter (ui/menu_win.cpp), called next: F1 / Esc open or close the menu; while it is open, keys, +// mouse and focus go to it. Returns 1 with *res set when the message is fully handled. +int menu_window_msg(HWND h, UINT m, WPARAM w, LPARAM l, LRESULT *res); #endif void input_cursor_suspend(int on); diff --git a/runtime/input_win.c b/runtime/input_win.c index 407ed996..7317cd31 100644 --- a/runtime/input_win.c +++ b/runtime/input_win.c @@ -16,6 +16,7 @@ #include "input.h" #include "input_map.h" #include "nvn_api.h" +#include "ui/menu.h" #include "vk.h" enum { STICK_MAX = 32767, STICK_DEAD = 2500, STICK_DIR = 16384, TRIGGER_ON = 16384, RESCAN_MS = 1000 }; @@ -192,6 +193,7 @@ static volatile LONG cur_active; // WM_ACTIVATEAPP: the window's appli static volatile LONG cur_suspend; // F11 or a modal size or move loop under way static SRWLOCK cur_lock = SRWLOCK_INIT; // serialises cursor_update (window thread, input_block callers) static volatile LONG g_block; // text entry state, see input_block +static volatile LONG g_menu; // in-game menu state, see input_menu static float mouse_sens = -1; // TOTK_MOUSE_SENS, 0 = mouse camera off; live (cfg_version) static unsigned mouse_sens_seen; @@ -208,12 +210,12 @@ static float mouse_sens_get(void) { } // Hide the cursor and confine it to the client area while the window is focused, not suspended, not behind a text entry -// and the mouse camera is on; release it otherwise. Safe from any thread. +// or the menu, and the mouse camera is on; release it otherwise. Safe from any thread. static void cursor_update(HWND h) { const char *in = cfg_get("TOTK_INPUT"); AcquireSRWLockExclusive(&cur_lock); - int want = h && cur_active && !cur_suspend && g_block != 1 && mouse_sens_get() > 0 && !(in && !strcmp(in, "none")) && - GetForegroundWindow() == h && !IsIconic(h); + int want = h && cur_active && !cur_suspend && g_block != 1 && g_menu != 1 && mouse_sens_get() > 0 && + !(in && !strcmp(in, "none")) && GetForegroundWindow() == h && !IsIconic(h); RECT r; POINT tl = {0, 0}; if (want && GetClientRect(h, &r) && ClientToScreen(h, &tl)) { @@ -350,26 +352,30 @@ static void pick(int32_t *x, int32_t *y, int32_t kx, int32_t ky) { if (b > a) { *x = kx; *y = ky; } } +// Under lock, state 1: the open gamepad's buttons (Switch bits) and sticks; 0 without a connected gamepad. +static int pad_read(uint64_t *b, int32_t sl[2], int32_t sr[2]) { + if ((!pad || !SDL_GamepadConnected(pad)) && GetTickCount64() >= next_scan) { next_scan = GetTickCount64() + RESCAN_MS; scan_pads(); } + SDL_UpdateGamepads(); + if (!pad || !SDL_GamepadConnected(pad)) return 0; + for (size_t i = 0; i < sizeof pad_buttons / sizeof *pad_buttons; i++) + if (SDL_GetGamepadButton(pad, pad_buttons[i].sdl)) *b |= 1ull << pad_buttons[i].bit; + if (SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) > TRIGGER_ON) *b |= 1ull << B_ZL; + if (SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) > TRIGGER_ON) *b |= 1ull << B_ZR; + sl[0] = axis(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTX)); + sl[1] = -axis(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTY)); + sr[0] = axis(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHTX)); + sr[1] = -axis(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHTY)); + return 1; +} + static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry) { uint64_t b = 0; int32_t sl[2] = {0, 0}, sr[2] = {0, 0}; AcquireSRWLockExclusive(&lock); if (!state) start(); if (state > 0) { - if (state == 1) { - if ((!pad || !SDL_GamepadConnected(pad)) && GetTickCount64() >= next_scan) { next_scan = GetTickCount64() + RESCAN_MS; scan_pads(); } - SDL_UpdateGamepads(); - if (pad && SDL_GamepadConnected(pad)) { - for (size_t i = 0; i < sizeof pad_buttons / sizeof *pad_buttons; i++) - if (SDL_GetGamepadButton(pad, pad_buttons[i].sdl)) b |= 1ull << pad_buttons[i].bit; - if (SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) > TRIGGER_ON) b |= 1ull << B_ZL; - if (SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) > TRIGGER_ON) b |= 1ull << B_ZR; - sl[0] = axis(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTX)); - sl[1] = -axis(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTY)); - sr[0] = axis(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHTX)); - sr[1] = -axis(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHTY)); - } - } + if (state == 1 && pad_read(&b, sl, sr)) // Minus and Plus held together on the gamepad open or close the menu + menu_pad_combo((b >> B_MINUS & 1) && (b >> B_PLUS & 1)); HWND w = gfx_window_handle(); if (w && w == GetForegroundWindow()) { uint8_t down[KEY_COUNT] = {0}; @@ -413,10 +419,23 @@ void input_block(int on) { cursor_update(gfx_window_handle()); } +// The in-game menu (ui/menu.cpp) blocks the game's input the same way: g_menu 0 closed, 1 open, 2 closed, waiting for +// the release. The cursor is released while it is open, for the menu's mouse. +void input_menu(int open) { + InterlockedExchange(&g_menu, open ? 1 : 2); + cursor_update(gfx_window_handle()); +} + +// 1 while flag (g_block, g_menu) blocks the game's input; state 2 ends at the first poll with nothing held. +static int blocked(volatile LONG *flag, uint64_t buttons) { + LONG k = *flag; + return k && !(k == 2 && !buttons && InterlockedCompareExchange(flag, 0, 2) == 2); +} + void input_poll(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry) { poll_merged(buttons, lx, ly, rx, ry); - LONG k = g_block; - if (!k || (k == 2 && !*buttons && InterlockedCompareExchange(&g_block, 0, 2) == 2)) return; + int text = blocked(&g_block, *buttons), menu = blocked(&g_menu, *buttons); + if (!text && !menu) return; *buttons = 0; *lx = *ly = *rx = *ry = 0; } @@ -424,6 +443,19 @@ void input_poll_ui(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int poll_merged(buttons, lx, ly, rx, ry); } +int input_pad_read(uint64_t *buttons, int32_t *lx, int32_t *ly) { + uint64_t b = 0; + int32_t sl[2] = {0, 0}, sr[2] = {0, 0}; + AcquireSRWLockExclusive(&lock); + if (!state) start(); + int ok = state == 1 && pad_read(&b, sl, sr); + ReleaseSRWLockExclusive(&lock); + *buttons = b; + *lx = sl[0]; + *ly = sl[1]; + return ok; +} + uint16_t rumble_motor(float a, float curve, float strength) { if (!(a > 0) || !(strength > 0)) return 0; float v = powf(a > 1 ? 1 : a, curve) * strength * 65535.0f + 0.5f; diff --git a/tools/build_game.py b/tools/build_game.py index 4ac220cc..9d2175df 100644 --- a/tools/build_game.py +++ b/tools/build_game.py @@ -139,9 +139,9 @@ RUNTIME = ROOT / 'runtime' # --runtime: a copy of runtime/ lets a task build w def runtime_sources(skip=()): - """Every runtime/*.c and runtime/platform/*.c except the names in skip, so a task adds a file without editing this - script.""" - pats = ('*.c', '*.cpp', 'platform/*.c') + """Every runtime/*.c, *.cpp, platform/*.c, the in-game menu (ui/*.cpp) and Dear ImGui except the names in skip, so + a task adds a file without editing this script.""" + pats = ('*.c', '*.cpp', 'platform/*.c', 'ui/*.cpp', 'third_party/imgui/*.cpp', 'third_party/imgui/backends/*.cpp') return sorted(p for pat in pats for p in RUNTIME.glob(pat) if p.name not in skip) @@ -186,7 +186,8 @@ def ninja_file(gen, out, mods, pool_depth, skip=(), prebuilt=False): """build.ninja text, and the object lists per module. With prebuilt the generated objects already exist in out/obj and are link inputs without a compile edge.""" inc = f'-I {flag_path(RUNTIME)} -I {flag_path(gen)}' - rt_inc = f'{inc} -I {flag_path(out)} -I {flag_path(VMA_INCLUDE)}' # romfs_config.h, vk_mem_alloc.h + # romfs_config.h, vk_mem_alloc.h, imgui.h + rt_inc = f'{inc} -I {flag_path(out)} -I {flag_path(VMA_INCLUDE)} -I {flag_path(RUNTIME / "third_party" / "imgui")}' lines = [ '# Written by tools/build_game.py; do not edit.', 'ninja_required_version = 1.10',