// M9.8c: the production engineering panels through the real adapter (dummy driver, // software renderer, pinned toolkit). Notices fit their fixed-width window with a // visible close button; clicking it (move + press + release in one batch, no settling // frame) dismisses exactly that notice and never reaches the world. #include "controls_flow.hpp" #include "evo/client/application.hpp" #include "panels.hpp" #include #include #include #include #include #include #include #include #include #include namespace { using namespace evo; using namespace evo::client; using namespace evo::client_app; void check(bool ok, std::source_location at = std::source_location::current()) { if (!ok) throw std::runtime_error("client panels line " + std::to_string(at.line())); } #define CHECK(...) check(static_cast(__VA_ARGS__)) SDL_Window* window() { int count{}; SDL_Window** windows = SDL_GetWindows(&count); SDL_Window* result = count == 1 ? windows[0] : nullptr; SDL_free(static_cast(windows)); CHECK(result); return result; } void mouse(SDL_WindowID id, SDL_EventType type, float x, float y) { SDL_Event event{}; event.type = type; if (type == SDL_EVENT_MOUSE_MOTION) { event.motion.windowID = id; event.motion.x = x; event.motion.y = y; } else { event.button.windowID = id; event.button.button = SDL_BUTTON_LEFT; event.button.down = type == SDL_EVENT_MOUSE_BUTTON_DOWN; event.button.clicks = 1; event.button.x = x; event.button.y = y; } CHECK(SDL_PushEvent(&event)); } ImGuiWindow* notice_window(std::uint64_t id) { return ImGui::FindWindowByName((notice_window_prefix + std::to_string(id)).c_str()); } void notices() { const std::array boxes{{{0, 0, 32, 32, {40, 40, 40, 255}}}}; auto frame = render::Frame::create(boxes); CHECK(frame); auto opened = Application::open({960, 640, false, "software"}); CHECK(opened); auto app = std::move(*opened); const auto id = SDL_GetWindowID(window()); SDL_Event focus{}; focus.type = SDL_EVENT_WINDOW_FOCUS_GAINED; focus.window.windowID = id; CHECK(SDL_PushEvent(&focus) && app->poll_events()); UiState ui; EnvironmentLayer environment; OrganismLayer organisms; Selection selection; Launcher launcher; FormBuffers form; render::EnvironmentDisplay display; PanelView view; view.mode = Mode::fixture; view.launcher = &launcher; view.environment = &environment; view.organisms = &organisms; view.selection = &selection; view.ui = &ui; std::optional dismissed; app->set_overlay([&] { if (const auto a = draw_panels(view, form, display); a.dismiss_notice) dismissed = a.dismiss_notice; }); // The application's real notices plus a maximal 240-byte message of wide glyphs. const std::string longest(UiState::max_text, 'W'); std::vector ids; for (const auto* text : {"Connected to the dev region (fresh baseline).", "Connection lost; views are stale.", "Session event backlog; reconnecting after draining.", longest.c_str()}) { const auto made = ui.notify(text, NoticeLevel::warning, 600000); CHECK(made); ids.push_back(*made); } for (int i = 0; i < 2; ++i) CHECK(app->poll_events() && app->present(*frame) == Step::rendered); const auto& style = ImGui::GetStyle(); for (const auto n : ids) { const auto* w = notice_window(n); CHECK(w != nullptr && w->Active); // Nothing wider than the window: no clipped text, no horizontal scrolling. CHECK(w->ContentSize.x <= w->Size.x - (2 * style.WindowPadding.x) + 0.5F); CHECK(w->ScrollMax.x == 0 && w->Pos.x >= 0 && w->Pos.x + w->Size.x <= 960); CHECK(w->Pos.y >= 0 && w->Pos.y + w->Size.y <= 640); } // Close the longest notice: pointer last over the world, then one batch. const auto* target = notice_window(ids.back()); const float x = target->Pos.x + target->Size.x - style.WindowPadding.x - style.CellPadding.x - (notice_close_width / 2); const float y = target->Pos.y + style.WindowPadding.y + style.CellPadding.y + style.FramePadding.y + (ImGui::GetFontSize() / 2); mouse(id, SDL_EVENT_MOUSE_MOTION, 480, 320); CHECK(app->poll_events() && app->present(*frame) == Step::rendered); static_cast(app->take_navigation()); mouse(id, SDL_EVENT_MOUSE_MOTION, x, y); mouse(id, SDL_EVENT_MOUSE_BUTTON_DOWN, x, y); mouse(id, SDL_EVENT_MOUSE_BUTTON_UP, x, y); CHECK(app->poll_events() && !app->take_navigation().click); for (int i = 0; i < 5 && !dismissed; ++i) CHECK(app->present(*frame) == Step::rendered && app->poll_events()); CHECK(dismissed == ids.back() && ui.dismiss(*dismissed) && ui.notices().size() == 3); CHECK(!app->take_navigation().click); CHECK(app->present(*frame) == Step::rendered && app->poll_events()); const auto* gone = notice_window(ids.back()); CHECK(gone == nullptr || !gone->Active); for (std::size_t k = 0; k + 1 < ids.size(); ++k) CHECK(notice_window(ids[k])->Active); // A full stack stays inside the lower part of the viewport and counts the rest. for (int k = 0; k < 8; ++k) CHECK(ui.notify(longest, NoticeLevel::info, 600000)); for (int i = 0; i < 2; ++i) CHECK(app->present(*frame) == Step::rendered && app->poll_events()); for (const auto& n : ui.notices()) if (const auto* w = notice_window(n.id); w != nullptr && w->Active) CHECK(w->Pos.y >= 0 && w->Pos.y + w->Size.y <= 640); const auto* more = ImGui::FindWindowByName("##evicted"); CHECK(more != nullptr && more->Active && more->Pos.y >= 0); } // M9.8d controls panel: fits the viewport, a slot button requests key capture for that // slot without a world click, and the chosen key reaches neither toolkit nor world. void controls_panel() { const std::array boxes{{{0, 0, 32, 32, {40, 40, 40, 255}}}}; auto frame = render::Frame::create(boxes); CHECK(frame); auto opened = Application::open({960, 640, false, "software"}); CHECK(opened); auto app = std::move(*opened); const auto id = SDL_GetWindowID(window()); SDL_Event focus{}; focus.type = SDL_EVENT_WINDOW_FOCUS_GAINED; focus.window.windowID = id; CHECK(SDL_PushEvent(&focus) && app->poll_events()); UiState ui; EnvironmentLayer environment; OrganismLayer organisms; Selection selection; Launcher launcher; FormBuffers form; render::EnvironmentDisplay display; BindingEditor editor{app->bindings()}; PanelView view; view.mode = Mode::fixture; view.launcher = &launcher; view.environment = &environment; view.organisms = &organisms; view.selection = &selection; view.ui = &ui; view.bindings = &app->bindings(); view.controls = &editor; PanelActions last; app->set_overlay([&] { const auto a = draw_panels(view, form, display); if (a.capture_key) last.capture_key = a.capture_key; last.apply_controls = last.apply_controls || a.apply_controls; }); app->set_ui_capture(true); // As the host does while the panel is open. for (int i = 0; i < 2; ++i) CHECK(app->poll_events() && app->present(*frame) == Step::rendered); const auto* w = ImGui::FindWindowByName("Controls"); CHECK(w != nullptr && w->Active); const auto& style = ImGui::GetStyle(); CHECK(w->ContentSize.x <= w->Size.x - (2 * style.WindowPadding.x) + 0.5F); CHECK(w->Pos.x >= 0 && w->Pos.y >= 0 && w->Pos.x + w->Size.x <= 960 && w->Pos.y + w->Size.y <= 640); // Probe down the primary column until a slot button answers: the first row is // Move left. Every probe is one batch (move, press, release). const float x = w->Pos.x + w->Size.x - style.WindowPadding.x - 150 - (2 * style.CellPadding.x) - 50; for (float y = w->Pos.y + 20; y < w->Pos.y + w->Size.y && !last.capture_key; y += 4) { mouse(id, SDL_EVENT_MOUSE_MOTION, x, y); mouse(id, SDL_EVENT_MOUSE_BUTTON_DOWN, x, y); mouse(id, SDL_EVENT_MOUSE_BUTTON_UP, x, y); CHECK(app->poll_events() && !app->take_navigation().click); for (int i = 0; i < 3 && !last.capture_key; ++i) CHECK(app->present(*frame) == Step::rendered && app->poll_events()); } CHECK(last.capture_key == ControlSlot{Action::move_left, false} && !last.apply_controls); // The host arms capture; the chosen key is consumed before the toolkit. view.capturing = last.capture_key; app->begin_key_capture(); SDL_Event key{}; key.type = SDL_EVENT_KEY_DOWN; key.key.windowID = id; key.key.scancode = SDL_SCANCODE_J; key.key.key = SDL_GetKeyFromScancode(SDL_SCANCODE_J, SDL_KMOD_NONE, false); key.key.down = true; CHECK(SDL_PushEvent(&key) && app->poll_events()); CHECK(app->take_captured_key() == KeyCode{SDL_SCANCODE_J}); CHECK(editor.assign(Action::move_left, false, SDL_SCANCODE_J)); view.capturing.reset(); CHECK(app->present(*frame) == Step::rendered && !ImGui::IsKeyDown(ImGuiKey_J)); CHECK(editor.apply() && editor.apply()->action(SDL_SCANCODE_J) == Action::move_left); CHECK(!app->take_navigation().click); } // M9.8d review: host rehearsal through the real adapter, production panels and the // host's ControlsFlow: click a slot, a conflicting key is refused, choose J, Apply // writes the file; a restarted application loads it and J moves left. SDL_Event key_event(SDL_WindowID id, SDL_Scancode scancode, bool down) { SDL_Event event{}; event.type = down ? SDL_EVENT_KEY_DOWN : SDL_EVENT_KEY_UP; event.key.windowID = id; event.key.scancode = scancode; event.key.key = SDL_GetKeyFromScancode(scancode, SDL_KMOD_NONE, false); event.key.down = down; return event; } void apply_save_restart() { const auto dir = std::filesystem::temp_directory_path() / "evo-controls-rehearsal"; std::filesystem::remove_all(dir); std::filesystem::create_directories(dir); const auto file = dir / "controls-v1.txt"; const std::array boxes{{{0, 0, 32, 32, {40, 40, 40, 255}}}}; auto frame = render::Frame::create(boxes); CHECK(frame); { auto opened = Application::open({960, 640, false, "software"}); CHECK(opened); auto app = std::move(*opened); const auto id = SDL_GetWindowID(window()); SDL_Event focus{}; focus.type = SDL_EVENT_WINDOW_FOCUS_GAINED; focus.window.windowID = id; CHECK(SDL_PushEvent(&focus) && app->poll_events()); UiState ui; EnvironmentLayer environment; OrganismLayer organisms; Selection selection; Launcher launcher; FormBuffers form; render::EnvironmentDisplay display; ControlsFlow flow{file}; flow.load(*app, ui); // No file yet: defaults. CHECK(app->bindings() == Bindings::defaults()); flow.show(*app); PanelActions pending; app->set_overlay([&] { PanelView view; view.mode = Mode::fixture; view.launcher = &launcher; view.environment = &environment; view.organisms = &organisms; view.selection = &selection; view.ui = &ui; view.bindings = &app->bindings(); flow.fill(view); pending = draw_panels(view, form, display); }); // The host's frame order: poll, panel actions of the last frame, capture, draw. const auto host_frame = [&] { CHECK(app->poll_events()); flow.step(pending, *app, ui); pending = {}; app->set_ui_capture(flow.open()); CHECK(!app->take_navigation().click && app->present(*frame) == Step::rendered); }; for (int i = 0; i < 2; ++i) host_frame(); const auto* w = ImGui::FindWindowByName("Controls"); CHECK(w != nullptr && w->Active); const auto& style = ImGui::GetStyle(); // Probe one-batch clicks until `done` reports the intended widget answered. const auto probe = [&](float x, float y0, float y1, float dy, const auto& done) { for (float y = y0; (dy > 0 ? y < y1 : y > y1); y += dy) { mouse(id, SDL_EVENT_MOUSE_MOTION, x, y); mouse(id, SDL_EVENT_MOUSE_BUTTON_DOWN, x, y); mouse(id, SDL_EVENT_MOUSE_BUTTON_UP, x, y); for (int i = 0; i < 3; ++i) { host_frame(); if (done()) return true; } } return false; }; const float primary_x = w->Pos.x + w->Size.x - style.WindowPadding.x - 150 - (2 * style.CellPadding.x) - 50; CHECK(probe(primary_x, w->Pos.y + 20, w->Pos.y + w->Size.y, 4, [&] { return app->key_capture_active(); })); // Up is Pan up's key: refused, the draft keeps A. Then arm again and choose J. auto up = key_event(id, SDL_SCANCODE_UP, true); CHECK(SDL_PushEvent(&up)); host_frame(); CHECK(!app->key_capture_active()); PanelView shown; flow.fill(shown); CHECK(shown.controls != nullptr && shown.controls->draft()[0].primary == SDL_SCANCODE_A); CHECK(shown.controls_problem && shown.controls_problem->error == BindingError::conflict && shown.controls_problem->other == Action::pan_up); auto up_release = key_event(id, SDL_SCANCODE_UP, false); CHECK(SDL_PushEvent(&up_release)); host_frame(); CHECK(probe(primary_x, w->Pos.y + 20, w->Pos.y + w->Size.y, 4, [&] { return app->key_capture_active(); })); auto j = key_event(id, SDL_SCANCODE_J, true); auto j_release = key_event(id, SDL_SCANCODE_J, false); CHECK(SDL_PushEvent(&j) && SDL_PushEvent(&j_release)); host_frame(); CHECK(!app->key_capture_active() && flow.open() && !std::filesystem::exists(file)); // Apply sits at the bottom left of the panel. CHECK(probe(w->Pos.x + style.WindowPadding.x + 40, w->Pos.y + w->Size.y - 6, w->Pos.y, -4, [&] { return !flow.open(); })); CHECK(app->bindings().action(SDL_SCANCODE_J) == Action::move_left); CHECK(!app->bindings().action(SDL_SCANCODE_A)); CHECK(std::filesystem::exists(file) && !ui.notices().empty() && ui.notices().back().text == "Controls saved."); } // Restart: a new application loads the saved file; J moves left after a press. auto opened = Application::open({320, 240, false, "software"}); CHECK(opened); auto app = std::move(*opened); const auto id = SDL_GetWindowID(window()); UiState ui; ControlsFlow{file}.load(*app, ui); CHECK(ui.notices().empty() && app->bindings().action(SDL_SCANCODE_J) == Action::move_left); SDL_Event focus{}; focus.type = SDL_EVENT_WINDOW_FOCUS_GAINED; focus.window.windowID = id; CHECK(SDL_PushEvent(&focus) && app->poll_events() && app->poll_events()); auto j = key_event(id, SDL_SCANCODE_J, true); CHECK(SDL_PushEvent(&j) && app->poll_events()); const auto intention = app->sample_intention(1); CHECK(intention && intention->move_x == -1000); std::filesystem::remove_all(dir); } } // namespace int main() try { notices(); controls_panel(); apply_save_restart(); std::puts("client panel tests passed"); return 0; } catch (const std::exception& e) { std::fprintf(stderr, "%s: %s\n", e.what(), SDL_GetError()); return 1; }