Implement M9.3 camera navigation and bounded input intentions
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
#include "evo/client/camera.hpp"
|
||||
#include "evo/world/coordinates.hpp"
|
||||
#include <cmath>
|
||||
|
||||
namespace evo::client {
|
||||
namespace {
|
||||
constexpr double world_limit = static_cast<double>(world::voxel_limit * world::cell_pitch_mm);
|
||||
bool valid(Viewport v) {
|
||||
return v.width > 0 && v.height > 0 && v.pixel_width > 0 && v.pixel_height > 0 &&
|
||||
v.width <= 32768 && v.height <= 32768 && v.pixel_width <= 32768 &&
|
||||
v.pixel_height <= 32768;
|
||||
}
|
||||
bool valid(WorldPoint p) {
|
||||
return std::isfinite(p.x_mm) && std::isfinite(p.y_mm) && p.x_mm >= -world_limit &&
|
||||
p.y_mm >= -world_limit && p.x_mm < world_limit && p.y_mm < world_limit;
|
||||
}
|
||||
bool valid(ScreenPoint p) {
|
||||
return std::isfinite(p.x) && std::isfinite(p.y) && std::abs(p.x) <= 1e15 &&
|
||||
std::abs(p.y) <= 1e15;
|
||||
}
|
||||
bool valid_scale(double scale) {
|
||||
return std::isfinite(scale) && scale >= 1e-4 && scale <= 1e4;
|
||||
}
|
||||
} // namespace
|
||||
std::expected<Camera, CameraError> Camera::create(Viewport viewport, WorldPoint centre,
|
||||
double scale) {
|
||||
if (!valid(viewport))
|
||||
return std::unexpected(CameraError::invalid_viewport);
|
||||
if (!valid(centre) || !valid_scale(scale))
|
||||
return std::unexpected(CameraError::invalid_value);
|
||||
return Camera{viewport, centre, scale};
|
||||
}
|
||||
std::expected<void, CameraError> Camera::resize(Viewport viewport) {
|
||||
if (!valid(viewport))
|
||||
return std::unexpected(CameraError::invalid_viewport);
|
||||
viewport_ = viewport;
|
||||
return {};
|
||||
}
|
||||
std::expected<ScreenPoint, CameraError> Camera::world_to_screen(WorldPoint point) const {
|
||||
if (!valid(point))
|
||||
return std::unexpected(CameraError::invalid_value);
|
||||
return ScreenPoint{(point.x_mm - centre_.x_mm) * scale_ + viewport_.width / 2.0,
|
||||
(centre_.y_mm - point.y_mm) * scale_ + viewport_.height / 2.0};
|
||||
}
|
||||
std::expected<WorldPoint, CameraError> Camera::screen_to_world(ScreenPoint point) const {
|
||||
if (!valid(point))
|
||||
return std::unexpected(CameraError::invalid_value);
|
||||
const WorldPoint result{centre_.x_mm + (point.x - viewport_.width / 2.0) / scale_,
|
||||
centre_.y_mm - (point.y - viewport_.height / 2.0) / scale_};
|
||||
if (!valid(result))
|
||||
return std::unexpected(CameraError::out_of_range);
|
||||
return result;
|
||||
}
|
||||
std::expected<PixelPoint, CameraError> Camera::to_pixels(ScreenPoint point) const {
|
||||
if (!valid(point))
|
||||
return std::unexpected(CameraError::invalid_value);
|
||||
const PixelPoint result{point.x * viewport_.pixel_width / viewport_.width,
|
||||
point.y * viewport_.pixel_height / viewport_.height};
|
||||
if (!valid(ScreenPoint{result.x, result.y}))
|
||||
return std::unexpected(CameraError::out_of_range);
|
||||
return result;
|
||||
}
|
||||
std::expected<ScreenPoint, CameraError> Camera::from_pixels(PixelPoint point) const {
|
||||
if (!valid(ScreenPoint{point.x, point.y}))
|
||||
return std::unexpected(CameraError::invalid_value);
|
||||
const ScreenPoint result{point.x * viewport_.width / viewport_.pixel_width,
|
||||
point.y * viewport_.height / viewport_.pixel_height};
|
||||
if (!valid(result))
|
||||
return std::unexpected(CameraError::out_of_range);
|
||||
return result;
|
||||
}
|
||||
std::expected<void, CameraError> Camera::pan(ScreenPoint delta) {
|
||||
if (!valid(delta) || std::abs(delta.x) > 32768 || std::abs(delta.y) > 32768)
|
||||
return std::unexpected(CameraError::invalid_value);
|
||||
const WorldPoint next{centre_.x_mm + delta.x / scale_, centre_.y_mm - delta.y / scale_};
|
||||
if (!valid(next))
|
||||
return std::unexpected(CameraError::out_of_range);
|
||||
centre_ = next;
|
||||
return {};
|
||||
}
|
||||
std::expected<void, CameraError> Camera::zoom(double factor, ScreenPoint anchor) {
|
||||
if (!std::isfinite(factor) || factor <= 0 || !valid(anchor) || anchor.x < 0 || anchor.y < 0 ||
|
||||
anchor.x > viewport_.width || anchor.y > viewport_.height)
|
||||
return std::unexpected(CameraError::invalid_value);
|
||||
const double next_scale = scale_ * factor;
|
||||
if (!valid_scale(next_scale))
|
||||
return std::unexpected(CameraError::out_of_range);
|
||||
if (factor == 1)
|
||||
return {};
|
||||
const auto world = screen_to_world(anchor);
|
||||
if (!world)
|
||||
return std::unexpected(world.error());
|
||||
const WorldPoint next{world->x_mm - (anchor.x - viewport_.width / 2.0) / next_scale,
|
||||
world->y_mm + (anchor.y - viewport_.height / 2.0) / next_scale};
|
||||
if (!valid(next))
|
||||
return std::unexpected(CameraError::out_of_range);
|
||||
centre_ = next;
|
||||
scale_ = next_scale;
|
||||
return {};
|
||||
}
|
||||
} // namespace evo::client
|
||||
@@ -0,0 +1,64 @@
|
||||
#include "evo/client/input.hpp"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
namespace evo::client {
|
||||
void InputState::clear() noexcept {
|
||||
keys_.fill(false);
|
||||
wheel_ = 0;
|
||||
}
|
||||
void InputState::admit(bool enabled) noexcept {
|
||||
if (!enabled)
|
||||
clear();
|
||||
admitted_ = enabled;
|
||||
}
|
||||
void InputState::capture(bool captured) noexcept {
|
||||
if (captured)
|
||||
clear();
|
||||
captured_ = captured;
|
||||
}
|
||||
std::expected<void, InputError> InputState::key(Key key, bool down, bool repeat) {
|
||||
const auto index = static_cast<std::size_t>(key);
|
||||
if (index >= keys_.size())
|
||||
return std::unexpected(InputError::invalid_key);
|
||||
if (!down)
|
||||
keys_[index] = false;
|
||||
else if (enabled() && !repeat)
|
||||
keys_[index] = true;
|
||||
return {};
|
||||
}
|
||||
std::expected<void, InputError> InputState::pointer(ScreenPoint point) {
|
||||
if (!std::isfinite(point.x) || !std::isfinite(point.y) || std::abs(point.x) > 32768 ||
|
||||
std::abs(point.y) > 32768)
|
||||
return std::unexpected(InputError::invalid_pointer);
|
||||
pointer_ = point;
|
||||
return {};
|
||||
}
|
||||
std::expected<void, InputError> InputState::wheel(double steps) {
|
||||
if (!std::isfinite(steps) || std::abs(steps) > 128)
|
||||
return std::unexpected(InputError::invalid_wheel);
|
||||
if (enabled())
|
||||
wheel_ = std::clamp(wheel_ + steps, -8.0, 8.0);
|
||||
return {};
|
||||
}
|
||||
Navigation InputState::take_navigation() noexcept {
|
||||
const Navigation result{static_cast<int>(keys_[5]) - static_cast<int>(keys_[4]),
|
||||
static_cast<int>(keys_[7]) - static_cast<int>(keys_[6]), wheel_,
|
||||
pointer_};
|
||||
wheel_ = 0;
|
||||
return result;
|
||||
}
|
||||
std::expected<Intention, InputError> InputState::sample(std::uint64_t tick) {
|
||||
if (tick_ && tick <= *tick_)
|
||||
return std::unexpected(InputError::stale_tick);
|
||||
if (sequence_ == std::numeric_limits<std::uint64_t>::max())
|
||||
return std::unexpected(InputError::exhausted);
|
||||
const int x = static_cast<int>(keys_[1]) - static_cast<int>(keys_[0]);
|
||||
const int y = static_cast<int>(keys_[2]) - static_cast<int>(keys_[3]);
|
||||
const int magnitude = x != 0 && y != 0 ? 707 : 1000;
|
||||
tick_ = tick;
|
||||
return Intention{++sequence_, tick, static_cast<std::int16_t>(x * magnitude),
|
||||
static_cast<std::int16_t>(y * magnitude)};
|
||||
}
|
||||
} // namespace evo::client
|
||||
@@ -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