Implement M9.3 camera navigation and bounded input intentions
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include "evo/client/input.hpp"
|
||||
#include "evo/render/frame.hpp"
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
@@ -27,7 +28,8 @@ struct WindowState {
|
||||
float content_scale{1};
|
||||
bool focused{}, minimized{}, hidden{}, closed{}, failed{};
|
||||
[[nodiscard]] bool accepts_input() const noexcept {
|
||||
return focused && !minimized && !hidden && !closed && !failed;
|
||||
return focused && !minimized && !hidden && !closed && !failed && logical_width > 0 &&
|
||||
logical_height > 0 && pixel_width > 0 && pixel_height > 0;
|
||||
}
|
||||
};
|
||||
// One owner, process main thread only (including destruction). No SDL owner may coexist.
|
||||
@@ -39,6 +41,12 @@ class 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);
|
||||
void set_ui_capture(bool captured) noexcept;
|
||||
[[nodiscard]] bool ui_captured() const noexcept;
|
||||
[[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);
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
|
||||
namespace evo::client {
|
||||
struct WorldPoint {
|
||||
double x_mm{}, y_mm{};
|
||||
};
|
||||
struct ScreenPoint {
|
||||
double x{}, y{};
|
||||
}; // Logical window units, not pixels.
|
||||
struct PixelPoint {
|
||||
double x{}, y{};
|
||||
};
|
||||
struct Viewport {
|
||||
int width{}, height{}, pixel_width{}, pixel_height{};
|
||||
};
|
||||
enum class CameraError : std::uint8_t { invalid_viewport, invalid_value, out_of_range };
|
||||
class Camera {
|
||||
public:
|
||||
[[nodiscard]] static std::expected<Camera, CameraError>
|
||||
create(Viewport viewport, WorldPoint centre = {}, double units_per_mm = 1);
|
||||
[[nodiscard]] std::expected<void, CameraError> resize(Viewport viewport);
|
||||
// Move the camera in screen-axis directions; the scene moves oppositely.
|
||||
[[nodiscard]] std::expected<void, CameraError> pan(ScreenPoint delta);
|
||||
[[nodiscard]] std::expected<void, CameraError> zoom(double factor, ScreenPoint anchor);
|
||||
[[nodiscard]] std::expected<ScreenPoint, CameraError> world_to_screen(WorldPoint point) const;
|
||||
[[nodiscard]] std::expected<WorldPoint, CameraError> screen_to_world(ScreenPoint point) const;
|
||||
[[nodiscard]] std::expected<PixelPoint, CameraError> to_pixels(ScreenPoint point) const;
|
||||
[[nodiscard]] std::expected<ScreenPoint, CameraError> from_pixels(PixelPoint point) const;
|
||||
[[nodiscard]] Viewport viewport() const noexcept {
|
||||
return viewport_;
|
||||
}
|
||||
[[nodiscard]] WorldPoint centre() const noexcept {
|
||||
return centre_;
|
||||
}
|
||||
[[nodiscard]] double units_per_mm() const noexcept {
|
||||
return scale_;
|
||||
}
|
||||
|
||||
private:
|
||||
Camera(Viewport viewport, WorldPoint centre, double scale)
|
||||
: viewport_{viewport}, centre_{centre}, scale_{scale} {}
|
||||
Viewport viewport_;
|
||||
WorldPoint centre_;
|
||||
double scale_;
|
||||
};
|
||||
} // namespace evo::client
|
||||
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
#include "evo/client/camera.hpp"
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <optional>
|
||||
|
||||
namespace evo::client {
|
||||
enum class Key : std::uint8_t { left, right, up, down, pan_left, pan_right, pan_up, pan_down };
|
||||
enum class InputError : std::uint8_t {
|
||||
invalid_key,
|
||||
invalid_pointer,
|
||||
invalid_wheel,
|
||||
stale_tick,
|
||||
exhausted
|
||||
};
|
||||
struct Intention {
|
||||
std::uint64_t sequence{}, tick{};
|
||||
std::int16_t move_x{}, move_y{}; // Signed unit vector *1000, local XY (+Y up).
|
||||
};
|
||||
struct Navigation {
|
||||
int x{}, y{};
|
||||
double wheel{};
|
||||
ScreenPoint pointer{};
|
||||
};
|
||||
// Main-thread value state; no event queue or authoritative position.
|
||||
class InputState {
|
||||
public:
|
||||
// Host-owned sequence seed, for local streams/tests; not a session resume grant.
|
||||
explicit InputState(std::uint64_t last_sequence = 0) : sequence_{last_sequence} {}
|
||||
void admit(bool enabled) noexcept;
|
||||
void capture(bool captured) noexcept;
|
||||
[[nodiscard]] bool captured() const noexcept {
|
||||
return captured_;
|
||||
}
|
||||
[[nodiscard]] bool enabled() const noexcept {
|
||||
return admitted_ && !captured_;
|
||||
}
|
||||
[[nodiscard]] std::expected<void, InputError> key(Key key, bool down, bool repeat = false);
|
||||
[[nodiscard]] std::expected<void, InputError> pointer(ScreenPoint point);
|
||||
[[nodiscard]] std::expected<void, InputError> wheel(double steps);
|
||||
[[nodiscard]] Navigation take_navigation() noexcept;
|
||||
[[nodiscard]] std::expected<Intention, InputError> sample(std::uint64_t tick);
|
||||
|
||||
private:
|
||||
void clear() noexcept;
|
||||
std::array<bool, 8> keys_{};
|
||||
bool admitted_{false}, captured_{false};
|
||||
ScreenPoint pointer_{};
|
||||
double wheel_{};
|
||||
std::uint64_t sequence_{};
|
||||
std::optional<std::uint64_t> tick_;
|
||||
};
|
||||
} // namespace evo::client
|
||||
Reference in New Issue
Block a user