Files
aevolution/tests/client_panels.cpp
T
dingoandClaude Opus 5.5 5cf8709912 Add M9.8d rebindable engineering controls
SDL-free action/HID-key bindings with conflict validation, a staging editor and
a versioned all-or-nothing controls file (ADR-0085). The adapter maps keys
through the bindings, releases and drains on rebinding, and captures the chosen
key ahead of the toolkit and world. The client adds a Controls panel, key hints,
startup loading with a bounded rejection notice, and --controls/--show-controls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 11:30:17 -04:00

228 lines
9.4 KiB
C++

// 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 "evo/client/application.hpp"
#include "panels.hpp"
#include <SDL3/SDL.h>
#include <array>
#include <cstdio>
#include <imgui.h>
#include <imgui_internal.h>
#include <source_location>
#include <stdexcept>
#include <string>
#include <vector>
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<bool>(__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<void*>(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<render::Rectangle, 1> 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<std::uint64_t> 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<std::uint64_t> 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<void>(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<render::Rectangle, 1> 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;
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);
}
} // namespace
int main() try {
notices();
controls_panel();
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;
}