Implement M9.3 camera navigation and bounded input intentions
This commit is contained in:
@@ -9,6 +9,28 @@
|
||||
namespace evo::client {
|
||||
namespace {
|
||||
bool runtime_owned = false; // Process main-thread-only contract; no hidden threads.
|
||||
std::optional<Key> input_key(SDL_Scancode code) {
|
||||
switch (code) {
|
||||
case SDL_SCANCODE_A:
|
||||
return Key::left;
|
||||
case SDL_SCANCODE_D:
|
||||
return Key::right;
|
||||
case SDL_SCANCODE_W:
|
||||
return Key::up;
|
||||
case SDL_SCANCODE_S:
|
||||
return Key::down;
|
||||
case SDL_SCANCODE_LEFT:
|
||||
return Key::pan_left;
|
||||
case SDL_SCANCODE_RIGHT:
|
||||
return Key::pan_right;
|
||||
case SDL_SCANCODE_UP:
|
||||
return Key::pan_up;
|
||||
case SDL_SCANCODE_DOWN:
|
||||
return Key::pan_down;
|
||||
default:
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
struct Runtime {
|
||||
bool acquired{false};
|
||||
~Runtime() {
|
||||
@@ -26,6 +48,8 @@ struct Application::Impl {
|
||||
std::string renderer_name;
|
||||
WindowState state;
|
||||
std::optional<Error> failure;
|
||||
InputState input;
|
||||
bool backlogged{false};
|
||||
|
||||
std::expected<void, Error> refresh() {
|
||||
if (!SDL_GetWindowSize(window.get(), &state.logical_width, &state.logical_height) ||
|
||||
@@ -47,6 +71,24 @@ WindowState Application::state() const noexcept {
|
||||
result.failed = impl_->failure.has_value();
|
||||
return result;
|
||||
}
|
||||
void Application::set_ui_capture(bool captured) noexcept {
|
||||
if (impl_->input.captured() && !captured) {
|
||||
// An external UI can release capture with old input still queued in SDL.
|
||||
// Drain that batch before admitting fresh world presses.
|
||||
impl_->backlogged = true;
|
||||
impl_->input.admit(false);
|
||||
}
|
||||
impl_->input.capture(captured);
|
||||
}
|
||||
bool Application::ui_captured() const noexcept {
|
||||
return impl_->input.captured();
|
||||
}
|
||||
Navigation Application::take_navigation() noexcept {
|
||||
return impl_->input.take_navigation();
|
||||
}
|
||||
std::expected<Intention, InputError> Application::sample_intention(std::uint64_t tick) {
|
||||
return impl_->input.sample(tick);
|
||||
}
|
||||
std::expected<std::unique_ptr<Application>, Error> Application::open(const Config& config) {
|
||||
if (config.width <= 0 || config.height <= 0 || config.width > 32768 || config.height > 32768 ||
|
||||
config.renderer.size() > 64 || config.renderer.find('\0') != std::string::npos)
|
||||
@@ -79,15 +121,22 @@ std::expected<std::unique_ptr<Application>, Error> Application::open(const Confi
|
||||
return std::unique_ptr<Application>{new Application{std::move(impl)}};
|
||||
}
|
||||
std::expected<Step, Error> Application::step(const render::Frame& frame) {
|
||||
if (auto result = poll_events(); !result)
|
||||
return std::unexpected(result.error());
|
||||
return present(frame);
|
||||
}
|
||||
std::expected<void, Error> Application::poll_events() {
|
||||
auto& p = *impl_;
|
||||
if (p.failure)
|
||||
return std::unexpected(*p.failure);
|
||||
if (p.state.closed)
|
||||
return Step::closed;
|
||||
return {};
|
||||
p.input.admit(p.state.accepts_input() && !p.backlogged);
|
||||
const auto id = SDL_GetWindowID(p.window.get());
|
||||
bool reset = false;
|
||||
SDL_Event event{};
|
||||
for (int count = 0; count < 256 && SDL_PollEvent(&event); ++count) {
|
||||
int count = 0;
|
||||
for (; count < 256 && SDL_PollEvent(&event); ++count) {
|
||||
if (event.type == SDL_EVENT_QUIT)
|
||||
p.state.closed = true;
|
||||
if (event.type >= SDL_EVENT_WINDOW_FIRST && event.type <= SDL_EVENT_WINDOW_LAST &&
|
||||
@@ -119,6 +168,29 @@ std::expected<Step, Error> Application::step(const render::Frame& frame) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Admission changes clear held inputs immediately, even if focus returns in this batch.
|
||||
p.input.admit(p.state.accepts_input() && !p.backlogged);
|
||||
if ((event.type == SDL_EVENT_KEY_DOWN || event.type == SDL_EVENT_KEY_UP) &&
|
||||
event.key.windowID == id) {
|
||||
const bool down = event.type == SDL_EVENT_KEY_DOWN;
|
||||
if (event.key.scancode == SDL_SCANCODE_TAB && down && !event.key.repeat &&
|
||||
p.state.accepts_input() && !p.backlogged)
|
||||
p.input.capture(!p.input.captured());
|
||||
if (const auto key = input_key(event.key.scancode)) {
|
||||
if (auto result = p.input.key(*key, down, event.key.repeat); !result)
|
||||
p.input.admit(false);
|
||||
}
|
||||
}
|
||||
if (event.type == SDL_EVENT_MOUSE_MOTION && event.motion.windowID == id) {
|
||||
if (auto result = p.input.pointer({event.motion.x, event.motion.y}); !result)
|
||||
p.input.admit(false);
|
||||
}
|
||||
if (event.type == SDL_EVENT_MOUSE_WHEEL && event.wheel.windowID == id) {
|
||||
const double direction = event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED ? -1 : 1;
|
||||
if (!p.input.pointer({event.wheel.mouse_x, event.wheel.mouse_y}) ||
|
||||
!p.input.wheel(event.wheel.y * direction))
|
||||
p.input.admit(false);
|
||||
}
|
||||
if (event.type >= SDL_EVENT_RENDER_TARGETS_RESET &&
|
||||
event.type <= SDL_EVENT_RENDER_DEVICE_LOST && event.render.windowID == id) {
|
||||
if (event.type == SDL_EVENT_RENDER_DEVICE_LOST)
|
||||
@@ -127,32 +199,49 @@ std::expected<Step, Error> Application::step(const render::Frame& frame) {
|
||||
reset = true;
|
||||
}
|
||||
}
|
||||
if (p.failure)
|
||||
if (p.failure) {
|
||||
p.input.admit(false);
|
||||
return std::unexpected(*p.failure);
|
||||
}
|
||||
p.backlogged = count == 256;
|
||||
p.input.admit(p.state.accepts_input() && !p.backlogged);
|
||||
if (p.state.closed)
|
||||
return Step::closed;
|
||||
return {};
|
||||
if (reset) {
|
||||
p.renderer.reset();
|
||||
auto renderer = detail::make_renderer(p.window.get(), p.renderer_name);
|
||||
if (!renderer) {
|
||||
p.failure = renderer.error();
|
||||
p.input.admit(false);
|
||||
return std::unexpected(*p.failure);
|
||||
}
|
||||
p.renderer = std::move(*renderer);
|
||||
}
|
||||
if (auto result = p.refresh(); !result) {
|
||||
p.failure = result.error();
|
||||
p.input.admit(false);
|
||||
return std::unexpected(*p.failure);
|
||||
}
|
||||
p.input.admit(p.state.accepts_input() && !p.backlogged);
|
||||
return {};
|
||||
}
|
||||
std::expected<Step, Error> Application::present(const render::Frame& frame) {
|
||||
auto& p = *impl_;
|
||||
if (p.failure)
|
||||
return std::unexpected(*p.failure);
|
||||
if (p.state.closed)
|
||||
return Step::closed;
|
||||
if (p.state.hidden || p.state.minimized || p.state.logical_width == 0 ||
|
||||
p.state.logical_height == 0 || p.state.pixel_width == 0 || p.state.pixel_height == 0)
|
||||
return Step::paused;
|
||||
if (auto result = detail::draw(p.renderer.get(), frame, p.state); !result) {
|
||||
p.failure = result.error();
|
||||
p.input.admit(false);
|
||||
return std::unexpected(*p.failure);
|
||||
}
|
||||
if (!SDL_RenderPresent(p.renderer.get())) {
|
||||
p.failure = Error::draw;
|
||||
p.input.admit(false);
|
||||
return std::unexpected(*p.failure);
|
||||
}
|
||||
return Step::rendered;
|
||||
|
||||
Reference in New Issue
Block a user