Files
aevolution/tests/client_controls.cpp

134 lines
6.0 KiB
C++

#include "evo/client/camera.hpp"
#include "evo/client/input.hpp"
#include <array>
#include <cmath>
#include <limits>
#include <source_location>
#include <stdexcept>
#include <string>
namespace {
using namespace evo::client;
void check(bool value, std::source_location at = std::source_location::current()) {
if (!value)
throw std::runtime_error("client controls line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
bool near(double a, double b) {
return std::abs(a - b) < 1e-6;
}
void camera_checks() {
constexpr std::array<Viewport, 4> views{{{800, 600, 800, 600},
{800, 600, 1600, 1200},
{1200, 500, 1800, 1000},
{320, 240, 640, 360}}};
for (const auto view : views)
for (const double scale : {1e-4, 0.25, 1.0, 32.0, 1e4}) {
auto camera = Camera::create(view, {1000000, -1000000}, scale);
CHECK(camera);
for (int i = -5; i <= 5; ++i) {
const WorldPoint world{1000000 + i * 17.125, -1000000 + i * 8.375};
const auto screen = camera->world_to_screen(world);
CHECK(screen);
const auto pixels = camera->to_pixels(*screen);
CHECK(pixels);
const auto logical = camera->from_pixels(*pixels);
CHECK(logical);
const auto restored = camera->screen_to_world(*logical);
CHECK(restored);
CHECK(near(restored->x_mm, world.x_mm) && near(restored->y_mm, world.y_mm));
}
const auto centre = camera->world_to_screen(camera->centre());
CHECK(centre && near(centre->x, view.width / 2.0) &&
near(centre->y, view.height / 2.0));
}
auto camera = Camera::create(views[0]);
CHECK(camera);
const ScreenPoint anchor{123, 456};
const auto before = camera->screen_to_world(anchor);
CHECK(before);
CHECK(camera->zoom(2, anchor));
const auto after = camera->screen_to_world(anchor);
CHECK(after && near(before->x_mm, after->x_mm) && near(before->y_mm, after->y_mm));
CHECK(camera->zoom(0.5, anchor));
CHECK(near(camera->centre().x_mm, 0) && near(camera->centre().y_mm, 0));
CHECK(camera->pan({20, -10}));
CHECK(near(camera->centre().x_mm, 20) && near(camera->centre().y_mm, 10));
CHECK(camera->resize(views[2]));
CHECK(camera->units_per_mm() == 1 && near(camera->centre().x_mm, 20));
const double nan = std::numeric_limits<double>::quiet_NaN();
CHECK(!camera->resize({0, 1, 1, 1}));
CHECK(camera->viewport().width == views[2].width);
CHECK(!camera->zoom(nan, anchor) && !camera->zoom(1e10, anchor));
CHECK(!camera->zoom(1, {-1, 5}) && !camera->pan({nan, 0}));
CHECK(!camera->pan({40000, 0}));
CHECK(!camera->screen_to_world({nan, 0}));
CHECK(!camera->world_to_screen({std::numeric_limits<double>::infinity(), 0}));
CHECK(!camera->from_pixels({nan, 0}) && !camera->to_pixels({nan, 0}));
CHECK(camera->units_per_mm() == 1 && near(camera->centre().x_mm, 20));
CHECK(!Camera::create(views[0], {}, 0));
CHECK(!Camera::create(views[0], {1048576000, 0}));
auto edge = Camera::create(views[0], {1048575999, 0});
CHECK(edge && !edge->pan({2, 0}));
CHECK(edge->centre().x_mm == 1048575999);
CHECK(!edge->screen_to_world({800, 300}));
}
Intention required(std::expected<Intention, InputError> result) {
CHECK(result);
return result.value();
}
void input_checks() {
InputState input;
CHECK(input.key(Key::up, true)); // No focus: ignored.
auto first = input.sample(0);
CHECK(first && first->sequence == 1 && first->move_y == 0 && first->tick == 0);
input.admit(true);
CHECK(input.key(Key::up, true, true)); // Repeats cannot re-arm an ignored press.
CHECK(required(input.sample(1)).move_y == 0);
CHECK(input.key(Key::up, true));
CHECK(input.key(Key::right, true));
const auto diagonal = input.sample(2);
CHECK(diagonal && diagonal->move_x == 707 && diagonal->move_y == 707);
CHECK(diagonal->move_x * diagonal->move_x + diagonal->move_y * diagonal->move_y <= 1000000);
CHECK(input.key(Key::right, false));
const auto straight = input.sample(3);
CHECK(straight && straight->move_x == 0 && straight->move_y == 1000);
CHECK(input.key(Key::down, true));
CHECK(required(input.sample(4)).move_y == 0);
CHECK(input.key(Key::pan_right, true));
CHECK(input.pointer({20, 30}) && input.wheel(128));
const auto nav = input.take_navigation();
CHECK(nav.x == 1 && nav.y == 0 && nav.wheel == 8 && nav.pointer.x == 20);
CHECK(input.take_navigation().wheel == 0);
CHECK(input.wheel(3));
input.capture(true);
CHECK(required(input.sample(5)).move_y == 0 && input.take_navigation().x == 0);
CHECK(input.key(Key::up, true) && input.wheel(4));
input.capture(false);
CHECK(required(input.sample(6)).move_y == 0 && input.take_navigation().wheel == 0);
CHECK(input.key(Key::up, true, true));
CHECK(required(input.sample(7)).move_y == 0);
CHECK(input.key(Key::up, false) && input.key(Key::up, true));
CHECK(required(input.sample(8)).move_y == 1000);
input.admit(false);
input.admit(true);
CHECK(required(input.sample(9)).move_y == 0);
CHECK(!input.sample(9) && !input.sample(8));
const auto next = input.sample(10);
CHECK(next && next->sequence == 11);
CHECK(!input.key(static_cast<Key>(255), true));
CHECK(!input.wheel(std::numeric_limits<double>::quiet_NaN()));
CHECK(!input.pointer({1e9, 0}));
CHECK(input.take_navigation().wheel == 0);
InputState exhausted{std::numeric_limits<std::uint64_t>::max()};
const auto failure = exhausted.sample(1);
CHECK(!failure && failure.error() == InputError::exhausted);
CHECK(input.sample(std::numeric_limits<std::uint64_t>::max()));
CHECK(!input.sample(0));
}
} // namespace
void client_controls_tests() {
camera_checks();
input_checks();
}