diff --git a/runtime/gfx_window_probe.c b/runtime/gfx_window_probe.c index a7e6494d..8c4ad97f 100644 --- a/runtime/gfx_window_probe.c +++ b/runtime/gfx_window_probe.c @@ -7,6 +7,7 @@ #include "gfx.h" #include "input.h" +#include "ui/menu.h" #include "vk.h" #define SRC_W 640 @@ -162,6 +163,7 @@ int probe_window(void) { SendMessageW(hw, WM_ACTIVATEAPP, FALSE, 0); SendMessageW(hw, WM_ACTIVATEAPP, TRUE, 0); int back = input_cursor_released(); + if (menu_is_open()) menu_toggle(); // Esc opened the menu: the presents below compare the bare game image if (!esc || click || client || !caption || back) { printf("probe-window: released esc=%d click=%d client=%d caption=%d reactivated=%d\n", esc, click, client, caption, back); return fail("cursor release"); diff --git a/runtime/input.h b/runtime/input.h index dbefc096..ed438df2 100644 --- a/runtime/input.h +++ b/runtime/input.h @@ -17,6 +17,9 @@ void input_block(int on); // 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. +// Minus+Plus combo and the neutral pad while the menu is open (input_map.c, window-system free): *held is the caller's memory +// of the combo, sticks are lx, ly, rx, ry. Returns 1 in the poll where the combo goes down (toggle the menu once). +int input_menu_gate(int *held, int menu_open, uint64_t *buttons, int32_t sticks[4]); 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 diff --git a/runtime/input_map.c b/runtime/input_map.c index d2f13b77..e54feb16 100644 --- a/runtime/input_map.c +++ b/runtime/input_map.c @@ -159,3 +159,20 @@ void input_mouse_stick(int32_t dx, int32_t dy, double dt_s, float sens, int32_t out[0] = (int32_t)lrint(x); out[1] = (int32_t)lrint(y); } + +// Minus and Plus held together open or close the menu. *held: the combo was down at the previous poll (the caller keeps it). +// The poll where both are down reports neither to the game, and neither does any later poll of the same hold; Minus alone +// reaches the game unchanged. With the menu open the game sees a neutral pad. Returns 1 in the poll where the combo goes +// down: the caller toggles the menu once. +int input_menu_gate(int *held, int menu_open, uint64_t *buttons, int32_t sticks[4]) { + const uint64_t combo = 1ull << B_MINUS | 1ull << B_PLUS; + int down = (*buttons & combo) == combo; + int edge = down && !*held; + *held = down; + if (down) *buttons &= ~combo; + if (menu_open) { + *buttons = 0; + sticks[0] = sticks[1] = sticks[2] = sticks[3] = 0; + } + return edge; +} diff --git a/runtime/input_win.c b/runtime/input_win.c index 74fbc151..740b0eb1 100644 --- a/runtime/input_win.c +++ b/runtime/input_win.c @@ -192,6 +192,7 @@ static volatile LONG mouse_dx, mouse_dy; // raw counts since the last frame sa static volatile LONG cur_captured; // cursor hidden and confined to the client area static volatile LONG cur_active; // WM_ACTIVATEAPP: the window's application is active static volatile LONG cur_released; // Esc, or activation by a click on the title bar or border: until a client click +static volatile LONG cur_esc; // cur_released was set by Esc (not before): closing the menu gives the capture back 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 @@ -257,11 +258,11 @@ int input_window_msg(HWND h, UINT m, WPARAM w, LPARAM l, LRESULT *res) { case WM_SETCURSOR: if (cur_captured && LOWORD(l) == HTCLIENT) { SetCursor(NULL); *res = TRUE; return 1; } break; - case WM_KEYDOWN: // Esc is unbound in the game layout, so it only releases the cursor - if (w == VK_ESCAPE) { InterlockedExchange(&cur_released, 1); cursor_update(h); } + case WM_KEYDOWN: // Esc is unbound in the game layout: it releases the cursor and opens the menu (menu_window_msg) + if (w == VK_ESCAPE && !cur_released) { InterlockedExchange(&cur_released, 1); InterlockedExchange(&cur_esc, 1); cursor_update(h); } break; case WM_LBUTTONDOWN: case WM_RBUTTONDOWN: case WM_MBUTTONDOWN: // client area only; poll_merged drops the click - if (cur_released) { InterlockedExchange(&cur_released, 0); cursor_update(h); } + if (cur_released) { InterlockedExchange(&cur_released, 0); InterlockedExchange(&cur_esc, 0); cursor_update(h); } break; case WM_MOUSEACTIVATE: // sent before WM_ACTIVATEAPP: a title bar or border click must not capture if (LOWORD(l) != HTCLIENT) InterlockedExchange(&cur_released, 1); @@ -388,8 +389,6 @@ static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx AcquireSRWLockExclusive(&lock); if (!state) start(); if (state > 0) { - 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}; @@ -419,7 +418,13 @@ static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx } script_apply(&b, sl, sr); rec_apply(b, sl, sr); + // Minus and Plus held together (gamepad or keys) open or close the menu and never reach the game + static int combo_held; + int32_t st[4] = {sl[0], sl[1], sr[0], sr[1]}; + int toggle = input_menu_gate(&combo_held, menu_is_open(), &b, st); + sl[0] = st[0], sl[1] = st[1], sr[0] = st[2], sr[1] = st[3]; ReleaseSRWLockExclusive(&lock); + if (toggle) menu_toggle(); stick_bits(&b, sl[0], sl[1], B_SL_LEFT); stick_bits(&b, sr[0], sr[1], B_SR_LEFT); *buttons = b; *lx = sl[0]; *ly = sl[1]; *rx = sr[0]; *ry = sr[1]; @@ -437,6 +442,7 @@ void input_block(int on) { // 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); + if (!open && InterlockedExchange(&cur_esc, 0)) InterlockedExchange(&cur_released, 0); // the capture before Esc comes back cursor_update(gfx_window_handle()); } diff --git a/runtime/ov_probe.c b/runtime/ov_probe.c index b3c4e3c0..338fd75e 100644 --- a/runtime/ov_probe.c +++ b/runtime/ov_probe.c @@ -733,6 +733,32 @@ static void probe_swkbd(void) { "minimum length, the Win32 window with OK and Cancel)\n", n - bad, n, def); } +// ---- the menu's input gate: Minus+Plus is consumed, a menu that is open leaves the game a neutral pad +static void probe_menu_gate(void) { + const uint64_t minus = 1ull << 11, plus = 1ull << 10, a = 1ull; + int held = 0; + int32_t st[4] = {100, -200, 300, -400}; + uint64_t b = minus | a; // Minus alone, a frame before Plus: reaches the guest unchanged + int t1 = input_menu_gate(&held, 0, &b, st); + CHECK(!t1 && b == (minus | a) && st[0] == 100 && st[3] == -400 && !held, "Minus alone changed: toggle %d buttons %#llx", t1, + (unsigned long long)b); + b = minus | plus | a; // both down: the guest sees neither, the menu toggles once + int t2 = input_menu_gate(&held, 0, &b, st); + CHECK(t2 == 1 && b == a && held, "Minus+Plus: toggle %d buttons %#llx", t2, (unsigned long long)b); + b = minus | plus; // still held with the menu open: no second toggle, a neutral pad + int t3 = input_menu_gate(&held, 1, &b, st); + CHECK(!t3 && b == 0 && st[0] == 0 && st[1] == 0 && st[2] == 0 && st[3] == 0, "menu open: toggle %d buttons %#llx", t3, + (unsigned long long)b); + b = a | plus; st[0] = 5; // the menu is open and the pad is neutral whatever is pressed + input_menu_gate(&held, 1, &b, st); + CHECK(b == 0 && st[0] == 0, "an open menu left buttons %#llx", (unsigned long long)b); + b = 0; // released, then pressed again toggles again + input_menu_gate(&held, 0, &b, st); + b = minus | plus; + CHECK(input_menu_gate(&held, 0, &b, st) == 1, "the second hold did not toggle"); + printf("menu gate: Minus alone passes, Minus+Plus is consumed and toggles once per hold, an open menu gives a neutral pad\n"); +} + // ---- shared fonts: the extracted files when present (the missing case is a fatal child) static void probe_fonts(void) { char dir[MAX_PATH], path[MAX_PATH + 32]; @@ -802,6 +828,7 @@ static void probe_main(Cpu *c) { probe_media_gfx(); probe_swkbd(); probe_menu_text(); + probe_menu_gate(); probe_fonts(); } diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 5459ac78..0cee6b4f 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -19,6 +19,7 @@ extern "C" { #include "input_map.h" #include "platform/platform.h" #include "ui/menu.h" +#include "ui_text.h" // host.h and vk.h pull in cpu.h, which is C only: the three functions the menu needs from them, as declared there. void rt_log(const char *fmt, ...) __attribute__((format(printf, 1, 2))); void vk_queue_lock(void); @@ -36,7 +37,6 @@ static const unsigned char roboto_ttf[] = { // ---- open state, any thread static std::atomic g_open; // the menu is open static std::atomic g_reset; // opened or closed since the last laid-out frame: drop stale input -static std::atomic g_combo; // Minus and Plus were held at the last menu_pad_combo static void set_open(int open) { if (g_open.exchange(open) == open) return; @@ -45,7 +45,7 @@ static void set_open(int open) { rt_log("menu: %s", open ? "open" : "closed"); } -// TOTK_MENU_TEST=: the presented frame the test window opens at; 0 when unset (the menu cannot open, no pages yet) +// TOTK_MENU_TEST=[:]: the presented frame the menu opens at for the shot hook; 0 when unset static uint64_t test_frame(void) { static std::atomic f{-1}; long long v = f.load(std::memory_order_relaxed); @@ -58,14 +58,11 @@ static uint64_t test_frame(void) { return (uint64_t)v; } -extern "C" int menu_available(void) { return test_frame() != 0; } +static int g_failed_flag(); +extern "C" int menu_available(void) { return 1; } extern "C" int menu_is_open(void) { return g_open.load(std::memory_order_relaxed); } extern "C" void menu_toggle(void) { - if (menu_available()) set_open(!g_open.load()); -} - -extern "C" void menu_pad_combo(int held) { - if (g_combo.exchange(held) != held && held) menu_toggle(); + if (!g_failed_flag()) set_open(!g_open.load()); } // ---- events from the window adapter, queued while the menu is open; the present path hands them to ImGui @@ -147,7 +144,9 @@ static void pad_events(ImGuiIO &io, int reset) { } // ---- ImGui and its Vulkan back end (present path) -static int g_ctx_ok, g_failed; // context made; set-up failed for good (the menu stays closed) +static int g_ctx_ok; // context made +static std::atomic g_failed; // set-up failed for good (the menu stays closed) +static int g_failed_flag() { return g_failed.load(std::memory_order_relaxed); } static int g_vk_ok; // back end initialised for g_vk_fmt static VkFormat g_vk_fmt; static uint64_t g_last_ns; @@ -218,20 +217,20 @@ extern "C" void menu_vk_release(void) { if (g_ctx_ok) vk_down(); } -// ---- the test window (TOTK_MENU_TEST): no words, so no catalog entry; the pages and the translation catalog replace it. -// A dimmed frame, a centred window with a progress bar (font glyphs: the percentage) and a colour swatch. -static void layout_test(uint32_t w, uint32_t h, float progress) { +// ---- the page: a dimmed frame and a centred window with "Continue" and "Quit game" (Task 4 moves them into the bottom row +// of the tabbed menu). Esc on the keyboard and B on the gamepad close it. +static void layout_page(uint32_t w, uint32_t h) { ImGui::GetBackgroundDrawList()->AddRectFilled(ImVec2(0, 0), ImVec2((float)w, (float)h), IM_COL32(0, 0, 0, 120)); ImGui::SetNextWindowPos(ImVec2(w * 0.5f, h * 0.5f), ImGuiCond_Always, ImVec2(0.5f, 0.5f)); - ImGui::SetNextWindowSize(ImVec2(w * 0.32f, 0), ImGuiCond_Always); - ImGui::Begin("##menu_test", nullptr, + ImGui::SetNextWindowSize(ImVec2(w * 0.28f, 0), ImGuiCond_Always); + ImGui::Begin("##menu", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings); - ImGui::ProgressBar(progress, ImVec2(-1, 0)); + if (ImGui::IsWindowAppearing()) ImGui::SetKeyboardFocusHere(); // "Continue" is selected on opening + ImVec2 full(-1, 0); + if (ImGui::Button(menu_text(UI_MENU_CONTINUE), full)) set_open(0); ImGui::Spacing(); - ImGui::ColorButton("##swatch", ImVec4(0.2f, 0.6f, 0.9f, 1), ImGuiColorEditFlags_NoTooltip, - ImVec2(ImGui::GetContentRegionAvail().x, 40)); - // Esc on the keyboard, B on the gamepad: close + if (ImGui::Button(menu_text(UI_MENU_QUIT), full)) gfx_window_request_close(); if (ImGui::IsKeyPressed(ImGuiKey_Escape, false) || ImGui::IsKeyPressed(ImGuiKey_GamepadFaceRight, false)) set_open(0); ImGui::End(); } @@ -239,7 +238,7 @@ static void layout_test(uint32_t w, uint32_t h, float progress) { enum { TEST_SHOT_AFTER = 30 }; // frames between the test window opening and its shot extern "C" int menu_overlay_frame(uint64_t frame, const MenuTarget *t, const char **shot) { - static uint64_t opened_at; // frame of the test hook's open; ~0 once its shot is taken + static uint64_t opened_at; // frame of the shot hook's open; ~0 once its shot is taken *shot = NULL; if (!g_open.load(std::memory_order_relaxed)) { uint64_t at = test_frame(); @@ -247,7 +246,7 @@ extern "C" int menu_overlay_frame(uint64_t frame, const MenuTarget *t, const cha opened_at = frame; set_open(1); } - if (g_failed) { + if (g_failed_flag()) { set_open(0); return 0; } @@ -255,7 +254,7 @@ extern "C" int menu_overlay_frame(uint64_t frame, const MenuTarget *t, const cha if (!g_ctx_ok) make_context(t->height); if (!vk_up(t)) { rt_log("menu: Dear ImGui Vulkan back end set-up failed; the menu stays closed"); - g_failed = 1; + g_failed.store(1); set_open(0); return 0; } @@ -275,7 +274,7 @@ extern "C" int menu_overlay_frame(uint64_t frame, const MenuTarget *t, const cha ImGui_ImplVulkan_NewFrame(); ImGui::NewFrame(); uint64_t since = opened_at != ~0ull && frame > opened_at ? frame - opened_at : 0; - layout_test(t->width, t->height, since >= TEST_SHOT_AFTER ? 1.0f : (float)since / TEST_SHOT_AFTER); + layout_page(t->width, t->height); ImGui::Render(); g_cost_ns += plat_now_ns() - t0; if (opened_at != ~0ull && since >= TEST_SHOT_AFTER) { // this frame's image goes to a file, then the window closes diff --git a/runtime/ui/menu.h b/runtime/ui/menu.h index b699ab87..b8cdae53 100644 --- a/runtime/ui/menu.h +++ b/runtime/ui/menu.h @@ -3,13 +3,13 @@ // (gfx_window.c). The menu core calls no window system: events come in through menu_event_*, the drawing happens in the // present path's command buffer. // -// No pages yet (the tabs and options are still being agreed): the menu opens only for the test hook -// TOTK_MENU_TEST=, which opens a text-free test window at that presented frame, saves one swapchain shot 30 -// frames later into TOTK_MENU_TEST_DIR (default: the working directory) and closes it. While the test hook is set, F1 or -// Esc on the keyboard and Minus and Plus held together on the gamepad also open and close it. While it is open the game -// gets no input (input_menu, input.h) and keeps running; the cursor is released from the mouse camera. +// F1 or Esc on the keyboard and Minus and Plus held together (consumed in poll_merged, input_menu_gate in input.h) open it; +// F1, Esc, B or "Continue" close it. While it is open the game gets no input (input_menu, input.h) and keeps running; the +// cursor is released from the mouse camera. Only "Continue" and "Quit game" so far (the tabs are still being agreed). +// Test hook TOTK_MENU_TEST= opens the menu at that presented frame, saves one swapchain shot 30 frames later into +// TOTK_MENU_TEST_DIR (default: the working directory) and closes it. // -// Threads: the event functions are called by the window thread, menu_pad_combo by whichever thread polls the gamepad, the +// Threads: the event functions are called by the window thread, menu_toggle by whichever thread polls the pad, the // overlay functions by the present path under its lock. ImGui itself runs on the present path only. #pragma once #include @@ -20,10 +20,9 @@ extern "C" { #endif // ---- state, any thread -int menu_available(void); // the menu can open (for now only with the test hook TOTK_MENU_TEST) +int menu_available(void); // always 1: the menu opens on every platform int menu_is_open(void); -void menu_toggle(void); // F1 / Esc on the keyboard -void menu_pad_combo(int held); // Minus and Plus held together on the gamepad (toggles once per hold) +void menu_toggle(void); // F1 / Esc on the keyboard, Minus+Plus on the pad // ---- events from the window adapter, used only while the menu is open. key: an ImGuiKey; coordinates in pixels of the // window's client area, which is the swapchain image. diff --git a/runtime/ui/menu_win.cpp b/runtime/ui/menu_win.cpp index 2b6deb78..b7085fed 100644 --- a/runtime/ui/menu_win.cpp +++ b/runtime/ui/menu_win.cpp @@ -1,10 +1,10 @@ // Win32 adapter of the in-game menu: window messages to menu events (menu.h). The only Win32 code of the menu; an SDL3 // window gets its own adapter later. Window thread. // -// F1 opens and closes the menu, Esc opens it (with the menu open, Esc goes to the menu, which closes); both only while -// menu_available. Esc also releases the cursor (input_window_msg runs first). While the menu is open, its keys, the -// mouse and focus changes go to it; the game polls the keyboard on its own (input_win.c) and gets nothing while the menu -// is open (input_menu). +// F1 opens and closes the menu, Esc opens it (with the menu open, Esc goes to the menu, which closes). Esc also releases +// the cursor (input_window_msg runs first; closing the menu gives the capture back). While the menu is open, its keys, +// the mouse and focus changes go to it; the game polls the keyboard on its own (input_win.c) and gets nothing while the +// menu is open (input_menu). #include #include @@ -44,7 +44,7 @@ extern "C" int menu_window_msg(HWND h, UINT m, WPARAM w, LPARAM l, LRESULT *res) case WM_KEYDOWN: case WM_SYSKEYDOWN: // bit 30: auto-repeat - if (!(l & (1 << 30)) && menu_available() && (w == VK_F1 || (w == VK_ESCAPE && !menu_is_open()))) { + if (!(l & (1 << 30)) && (w == VK_F1 || (w == VK_ESCAPE && !menu_is_open()))) { menu_toggle(); *res = 0; return 1;