Files
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

87 lines
3.7 KiB
C++

#pragma once
#include "evo/client/bindings.hpp"
#include "evo/client/input.hpp"
#include "evo/render/frame.hpp"
#include <cstdint>
#include <expected>
#include <filesystem>
#include <functional>
#include <memory>
#include <optional>
#include <string>
namespace evo::client {
enum class Error : std::uint8_t {
invalid_config,
already_open,
initialization,
window,
renderer,
surface,
draw,
device_lost
};
enum class Step : std::uint8_t { rendered, paused, closed };
struct Config {
int width{960}, height{640};
bool hidden{false};
std::string renderer; // Empty selects SDL's supported default; explicit names must succeed.
};
struct WindowState {
int logical_width{}, logical_height{}, pixel_width{}, pixel_height{};
float content_scale{1};
bool focused{}, minimized{}, hidden{}, closed{}, failed{};
[[nodiscard]] bool accepts_input() const noexcept {
return focused && !minimized && !hidden && !closed && !failed && logical_width > 0 &&
logical_height > 0 && pixel_width > 0 && pixel_height > 0;
}
};
// Engineering UI callback, run once per rendered frame on the main thread after the
// immutable frame is drawn. It may call the private toolkit (ADR-0084); no toolkit
// type appears in this header.
using Overlay = std::function<void()>;
// One owner, process main thread only (including destruction). No SDL owner may coexist.
class Application {
public:
[[nodiscard]] static std::expected<std::unique_ptr<Application>, Error>
open(const Config& config);
~Application();
Application(const Application&) = delete;
Application& operator=(const Application&) = delete;
[[nodiscard]] WindowState state() const noexcept;
[[nodiscard]] std::expected<void, Error> poll_events();
[[nodiscard]] std::expected<Step, Error> present(const render::Frame& frame);
// External modal/widget capture cannot be disabled by the toggle-capture action.
void set_ui_capture(bool captured) noexcept;
// Replaces the key-to-action map (ADR-0085). Every held world action is released;
// input queued before the change is drained, then fresh presses are required.
void set_bindings(const Bindings& bindings) noexcept;
[[nodiscard]] const Bindings& bindings() const noexcept;
// Binding capture: the next non-repeat key press (any key, including Escape) is
// consumed for take_captured_key() and reaches neither the toolkit nor the world.
// World input stays captured while armed; focus loss cancels it without a key.
void begin_key_capture() noexcept;
void cancel_key_capture() noexcept;
[[nodiscard]] bool key_capture_active() const noexcept;
[[nodiscard]] std::optional<KeyCode> take_captured_key() noexcept;
// Per-user settings directory, created when needed; nullopt when unavailable.
// Callable without an open Application.
[[nodiscard]] static std::optional<std::filesystem::path> settings_directory();
[[nodiscard]] bool ui_captured() const noexcept;
void set_overlay(Overlay overlay);
// The toolkit held the pointer during the last poll (no world click, pan or zoom).
[[nodiscard]] bool ui_wants_pointer() const noexcept;
// Engineering visual review: the next rendered frame is saved as a BMP file.
[[nodiscard]] bool request_screenshot(std::string path);
[[nodiscard]] Navigation take_navigation() noexcept;
[[nodiscard]] std::expected<Intention, InputError> sample_intention(std::uint64_t tick);
// Borrows the immutable frame for this call only. Errors latch until destruction.
[[nodiscard]] std::expected<Step, Error> step(const render::Frame& frame);
private:
struct Impl;
explicit Application(std::unique_ptr<Impl> impl);
std::unique_ptr<Impl> impl_;
};
} // namespace evo::client