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>
This commit is contained in:
dingo
2026-10-02 11:30:17 -04:00
co-authored by Claude Opus 5.5
parent bb8c2fc359
commit 5cf8709912
23 changed files with 1292 additions and 45 deletions
+94 -1
View File
@@ -14,6 +14,7 @@
#include <cmath>
#include <cstdio>
#include <exception>
#include <filesystem>
#include <memory>
#include <optional>
#include <string_view>
@@ -214,6 +215,8 @@ int main(int argc, char** argv) try {
bool start_fixture = false;
std::optional<evo::client::ScreenPoint> scripted_pick;
std::string screenshot;
std::filesystem::path controls_path; // Empty: per-user settings directory.
bool show_controls = false;
evo::render::EnvironmentDisplay display;
evo::client::ConnectForm form;
bool connect_now = false;
@@ -251,6 +254,12 @@ int main(int argc, char** argv) try {
return 2;
} else if (arg == "--screenshot" && i + 1 < argc) { // Last frame, as BMP.
screenshot = argv[++i];
} else if (arg == "--controls" && i + 1 < argc) {
controls_path = argv[++i];
if (controls_path.empty())
return 2;
} else if (arg == "--show-controls") { // Engineering review: open the panel.
show_controls = true;
} else {
std::fputs("usage: evo-client [--renderer NAME] [--hidden] [--frames 1..100000]\n"
" [--fixture | --connect HOST:PORT --server-name NAME "
@@ -258,6 +267,7 @@ int main(int argc, char** argv) try {
" [--layer water|nitrogen|...|heat|moisture]\n"
" [--refresh-every 1..1000] [--pick X,Y] "
"[--screenshot FILE.bmp --frames N]\n"
" [--controls FILE] [--show-controls]\n"
"Without --fixture or --connect the launcher opens.\n",
stderr);
return 2;
@@ -327,6 +337,29 @@ int main(int argc, char** argv) try {
}
// A minimized native window may start with no drawable area. Adopt the
// native viewport before projecting; until then use the requested size.
// Controls (ADR-0085): an invalid file is reported and left untouched; defaults
// apply until the user saves new bindings.
if (controls_path.empty())
if (const auto dir = evo::client::Application::settings_directory())
controls_path = *dir / "controls-v1.txt";
if (std::error_code missing;
!controls_path.empty() && std::filesystem::exists(controls_path, missing)) {
// Notices expire on the same monotonic clock the loop uses.
static_cast<void>(ui.advance(milliseconds(std::chrono::steady_clock::now())).has_value());
if (const auto loaded = evo::client::load_bindings(controls_path))
(*app)->set_bindings(*loaded);
else
static_cast<void>(
ui.notify("Saved controls rejected: " + evo::client::describe(loaded.error()) +
" Default controls are in use; the file was not changed.",
evo::client::NoticeLevel::warning, 15000)
.has_value());
}
std::optional<evo::client::BindingEditor> editor;
std::optional<evo::client_app::ControlSlot> capturing;
std::optional<evo::client::BindingProblem> controls_problem;
if (show_controls)
editor.emplace((*app)->bindings());
auto camera = evo::client::Camera::create(
{config.width, config.height, config.width, config.height}, {}, 0.05);
if (!camera)
@@ -371,6 +404,10 @@ int main(int argc, char** argv) try {
view.outcomes = &remote->outcomes();
}
view.visible_organisms = drawn_organisms;
view.bindings = &(*app)->bindings();
view.controls = editor ? &*editor : nullptr;
view.capturing = capturing;
view.controls_problem = controls_problem;
actions = evo::client_app::draw_panels(view, buffers, display);
});
std::vector<evo::render::Rectangle> boxes;
@@ -414,9 +451,65 @@ int main(int argc, char** argv) try {
disconnect_dialog.reset();
if (actions.dismiss_notice)
static_cast<void>(ui.dismiss(*actions.dismiss_notice));
if (actions.open_controls && !editor) {
editor.emplace((*app)->bindings());
controls_problem.reset();
}
if (editor) {
if (actions.capture_key && !capturing) {
capturing = actions.capture_key;
controls_problem.reset();
(*app)->begin_key_capture();
}
if (actions.cancel_capture && capturing) {
(*app)->cancel_key_capture();
capturing.reset();
}
if (actions.clear_secondary)
editor->clear_secondary(*actions.clear_secondary);
if (actions.reset_controls) {
editor->reset_defaults();
controls_problem.reset();
}
if (actions.apply_controls) {
if (const auto applied = editor->apply(); !applied) {
controls_problem = applied.error();
} else {
(*app)->set_bindings(*applied); // Releases held actions.
const bool saved = !controls_path.empty() &&
evo::client::save_bindings(*applied, controls_path);
static_cast<void>(
ui.notify(saved ? "Controls saved."
: "Controls apply to this session but could not be saved.",
saved ? evo::client::NoticeLevel::info
: evo::client::NoticeLevel::warning)
.has_value());
editor.reset();
}
}
if (actions.close_controls) {
editor.reset();
controls_problem.reset();
}
if (!editor && capturing) {
(*app)->cancel_key_capture();
capturing.reset();
}
}
// A finished capture (key, Escape or focus loss) updates the draft only.
if (capturing && editor && !(*app)->key_capture_active()) {
if (const auto key = (*app)->take_captured_key();
key && *key != evo::client::escape_key) {
if (auto assigned = editor->assign(capturing->action, capturing->secondary, *key);
!assigned)
controls_problem = assigned.error();
}
capturing.reset();
}
actions = {};
// The launcher and modal dialogs own input: no world movement or selection.
(*app)->set_ui_capture(mode() == Mode::launcher || ui.dialog().has_value());
(*app)->set_ui_capture(mode() == Mode::launcher || ui.dialog().has_value() ||
editor.has_value());
const auto navigation = (*app)->take_navigation();
// Local input counter; in remote mode the session sets the server tick.
const auto intention = (*app)->sample_intention(count);
+91 -1
View File
@@ -61,6 +61,17 @@ void disabled_wrapped(const char* text) {
ImGui::TextWrapped("%s", text);
ImGui::PopStyleColor();
}
std::string key_label(client::KeyCode key) {
return key == client::no_key ? std::string{"-"} : std::string{client::key_name(key)};
}
std::string controls_hint(const client::Bindings& b) {
using client::Action;
const auto k = [&](Action action) { return key_label(b.slot(action).primary); };
return "Click selects, wheel zooms. Move " + k(Action::move_up) + " " + k(Action::move_left) +
" " + k(Action::move_down) + " " + k(Action::move_right) + "; pan " + k(Action::pan_up) +
" " + k(Action::pan_left) + " " + k(Action::pan_down) + " " + k(Action::pan_right) +
"; UI capture " + k(Action::toggle_capture) + ".";
}
// Fixed wording only (never echoes paths or file bytes); wraps inside the launcher.
void error_text(client::LaunchError error) {
ImGui::PushStyleColor(ImGuiCol_Text, red);
@@ -83,6 +94,8 @@ void launcher(const PanelView& v, FormBuffers& form, PanelActions& a) {
ImGui::Begin("Aeternalis Evolution - engineering client", nullptr,
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse);
ImGui::TextWrapped("Choose a data source. Nothing here is a game or a player account.");
if (ImGui::SmallButton("Controls..."))
a.open_controls = true;
ImGui::Separator();
if (ImGui::Button("Local fixture", {160, 0}))
a.fixture = true;
@@ -132,12 +145,18 @@ void banner(const PanelView& v, PanelActions& a) {
ImGui::SameLine();
if (ImGui::SmallButton("Launcher"))
a.back = true;
ImGui::SameLine();
if (ImGui::SmallButton("Controls"))
a.open_controls = true;
} else {
ImGui::TextColored(v.remote && v.remote->phase == "connected" ? green : amber,
"DEV REGION - %s", v.remote ? v.remote->phase.c_str() : "?");
ImGui::SameLine();
if (ImGui::SmallButton("Disconnect"))
a.ask_disconnect = true;
ImGui::SameLine();
if (ImGui::SmallButton("Controls"))
a.open_controls = true;
}
ImGui::End();
}
@@ -183,7 +202,8 @@ void legend(const PanelView& v, render::EnvironmentDisplay& display) {
ImGui::TextWrapped("Fixture organisms are static presentation data built from genomes.");
if (v.visible_organisms >= 0)
ImGui::Text("Organisms drawn: %d", v.visible_organisms);
disabled_wrapped("Click to select. Arrows pan, wheel zooms, WASD moves.");
if (v.bindings != nullptr)
disabled_wrapped(controls_hint(*v.bindings).c_str());
ImGui::End();
}
@@ -242,6 +262,75 @@ void inspector(const PanelView& v) {
ImGui::End();
}
// Rebinding: each slot button arms key capture for that slot; the panel holds UI
// capture, so no key or click reaches the world while it is open (ADR-0085).
void controls(const PanelView& v, PanelActions& a) {
if (v.controls == nullptr)
return;
const auto& io = ImGui::GetIO();
ImGui::SetNextWindowPos({io.DisplaySize.x * 0.5F, io.DisplaySize.y * 0.5F}, ImGuiCond_Always,
{0.5F, 0.5F});
ImGui::SetNextWindowSize({std::min(460.0F, io.DisplaySize.x - 16), 0}, ImGuiCond_Always);
ImGui::Begin("Controls", nullptr,
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoSavedSettings);
disabled_wrapped("Keys are named by their US-layout position. Mouse click (select) and "
"wheel (zoom) are fixed. Escape cancels a key capture.");
if (ImGui::BeginTable("bindings", 3, ImGuiTableFlags_RowBg)) {
ImGui::TableSetupColumn("Action", ImGuiTableColumnFlags_WidthStretch);
ImGui::TableSetupColumn("Primary", ImGuiTableColumnFlags_WidthFixed, 120);
ImGui::TableSetupColumn("Secondary", ImGuiTableColumnFlags_WidthFixed, 150);
ImGui::TableHeadersRow();
for (std::size_t k = 0; k < client::action_count; ++k) {
const auto action = static_cast<client::Action>(k);
const auto& slot = v.controls->draft()[k];
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::TextUnformatted(std::string{client::action_name(action)}.c_str());
for (const bool secondary : {false, true}) {
ImGui::TableNextColumn();
const ControlSlot here{action, secondary};
const auto key = secondary ? slot.secondary : slot.primary;
const std::string label =
(v.capturing == here ? std::string{"Press a key"} : key_label(key)) + "##" +
std::to_string(k) + (secondary ? "s" : "p");
ImGui::BeginDisabled(v.capturing.has_value());
if (ImGui::Button(label.c_str(), {100, 0}))
a.capture_key = here;
if (secondary && key != client::no_key) {
ImGui::SameLine();
if (ImGui::SmallButton(("x##clear" + std::to_string(k)).c_str()))
a.clear_secondary = action;
}
ImGui::EndDisabled();
}
}
ImGui::EndTable();
}
if (v.capturing) {
ImGui::TextColored(amber, "Press a key for %s (Escape cancels).",
std::string{client::action_name(v.capturing->action)}.c_str());
if (ImGui::Button("Cancel capture", {140, 0}))
a.cancel_capture = true;
}
if (v.controls_problem) {
ImGui::PushStyleColor(ImGuiCol_Text, red);
ImGui::TextWrapped("%s", client::describe(*v.controls_problem).c_str());
ImGui::PopStyleColor();
}
ImGui::BeginDisabled(v.capturing.has_value());
if (ImGui::Button("Apply", {100, 0}))
a.apply_controls = true;
ImGui::SameLine();
if (ImGui::Button("Reset defaults", {130, 0}))
a.reset_controls = true;
ImGui::SameLine();
if (ImGui::Button("Cancel", {100, 0}))
a.close_controls = true;
ImGui::EndDisabled();
ImGui::End();
}
void session(const PanelView& v) {
if (!v.remote)
return;
@@ -376,6 +465,7 @@ PanelActions draw_panels(const PanelView& view, FormBuffers& form,
inspector(view);
session(view);
}
controls(view, actions);
notices(view, actions);
dialog(view, actions);
return actions;
+16 -1
View File
@@ -2,6 +2,7 @@
// M9.8c engineering panels (ADR-0084). Drawn inside Application's overlay callback;
// they read presentation copies and report what the user asked for. Only this file
// pair and the platform adapter use the toolkit.
#include "evo/client/bindings.hpp"
#include "evo/client/environment_view.hpp"
#include "evo/client/launcher.hpp"
#include "evo/client/organism_view.hpp"
@@ -25,6 +26,12 @@ struct FormBuffers {
void load(const client::ConnectForm& form);
[[nodiscard]] client::ConnectForm form() const;
};
// One key slot of one action in the controls panel.
struct ControlSlot {
client::Action action{};
bool secondary{};
friend bool operator==(const ControlSlot&, const ControlSlot&) = default;
};
struct PanelView {
Mode mode{Mode::launcher};
const client::Launcher* launcher{};
@@ -36,11 +43,19 @@ struct PanelView {
const client::UiState* ui{};
std::optional<RemoteStatus> remote;
const client::OutcomeLog* outcomes{};
int visible_organisms{-1}; // -1 when unknown.
int visible_organisms{-1}; // -1 when unknown.
const client::Bindings* bindings{}; // In force (hints).
// Controls panel, drawn while `controls` is set. Edits stay in the draft until Apply.
const client::BindingEditor* controls{};
std::optional<ControlSlot> capturing; // Waiting for the next key press.
std::optional<client::BindingProblem> controls_problem;
};
struct PanelActions {
bool fixture{}, connect{}, cancel{}, retry{}, back{}, ask_disconnect{};
std::optional<std::uint64_t> dismiss_notice, confirm_dialog, cancel_dialog;
bool open_controls{}, apply_controls{}, reset_controls{}, close_controls{}, cancel_capture{};
std::optional<ControlSlot> capture_key;
std::optional<client::Action> clear_secondary;
};
// Draws every panel for this frame. `display` may be changed by the layer selector.
[[nodiscard]] PanelActions draw_panels(const PanelView& view, FormBuffers& form,