Implement M9.3 camera navigation and bounded input intentions
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
void client_controls_tests();
|
||||
namespace {
|
||||
using namespace evo::client;
|
||||
void check(bool ok, std::source_location at = std::source_location::current()) {
|
||||
@@ -147,10 +148,107 @@ void lifecycle() {
|
||||
send(SDL_EVENT_QUIT, 0);
|
||||
CHECK((*hidden)->step(frame) == Step::closed);
|
||||
}
|
||||
Intention required(std::expected<Intention, InputError> result) {
|
||||
CHECK(result);
|
||||
return result.value();
|
||||
}
|
||||
void native_input() {
|
||||
const auto frame = fixture();
|
||||
auto result = Application::open({320, 240, false, "software"});
|
||||
CHECK(result);
|
||||
auto app = std::move(result.value());
|
||||
CHECK(app->poll_events());
|
||||
const auto id = SDL_GetWindowID(window());
|
||||
send(SDL_EVENT_WINDOW_FOCUS_GAINED, id);
|
||||
auto key = [id](SDL_Scancode scancode, bool down, bool repeat = false) {
|
||||
SDL_Event event{};
|
||||
event.type = down ? SDL_EVENT_KEY_DOWN : SDL_EVENT_KEY_UP;
|
||||
event.key.windowID = id;
|
||||
event.key.scancode = scancode;
|
||||
event.key.repeat = repeat;
|
||||
CHECK(SDL_PushEvent(&event));
|
||||
};
|
||||
key(SDL_SCANCODE_W, true);
|
||||
key(SDL_SCANCODE_RIGHT, true);
|
||||
CHECK(app->poll_events());
|
||||
const auto first = app->sample_intention(40);
|
||||
CHECK(first && first->move_y == 1000 && first->sequence == 1 && first->tick == 40);
|
||||
CHECK(app->take_navigation().x == 1);
|
||||
key(SDL_SCANCODE_W, false);
|
||||
CHECK(app->poll_events());
|
||||
CHECK(required(app->sample_intention(41)).move_y == 0);
|
||||
key(SDL_SCANCODE_W, true);
|
||||
send(SDL_EVENT_WINDOW_FOCUS_LOST, id);
|
||||
send(SDL_EVENT_WINDOW_FOCUS_GAINED, id);
|
||||
key(SDL_SCANCODE_W, true, true);
|
||||
CHECK(app->poll_events());
|
||||
CHECK(required(app->sample_intention(42)).move_y == 0 && app->take_navigation().x == 0);
|
||||
key(SDL_SCANCODE_W, true);
|
||||
CHECK(app->poll_events());
|
||||
app->set_ui_capture(true);
|
||||
CHECK(required(app->sample_intention(43)).move_y == 0);
|
||||
key(SDL_SCANCODE_W, true);
|
||||
CHECK(app->poll_events());
|
||||
app->set_ui_capture(false);
|
||||
CHECK(required(app->sample_intention(44)).move_y == 0);
|
||||
CHECK(app->poll_events());
|
||||
key(SDL_SCANCODE_TAB, true);
|
||||
key(SDL_SCANCODE_W, true);
|
||||
CHECK(app->poll_events());
|
||||
CHECK(app->ui_captured() && required(app->sample_intention(45)).move_y == 0);
|
||||
key(SDL_SCANCODE_TAB, true, true);
|
||||
CHECK(app->poll_events());
|
||||
CHECK(app->ui_captured());
|
||||
key(SDL_SCANCODE_TAB, false);
|
||||
key(SDL_SCANCODE_TAB, true);
|
||||
key(SDL_SCANCODE_W, true);
|
||||
CHECK(app->poll_events());
|
||||
CHECK(!app->ui_captured() && required(app->sample_intention(46)).move_y == 1000);
|
||||
// A saturated event batch cancels motion until the backlog drains.
|
||||
for (int i = 0; i < 300; ++i)
|
||||
key(SDL_SCANCODE_RIGHT, true);
|
||||
CHECK(app->poll_events());
|
||||
CHECK(required(app->sample_intention(47)).move_y == 0 && app->take_navigation().x == 0);
|
||||
CHECK(app->poll_events());
|
||||
CHECK(required(app->sample_intention(48)).move_y == 0 && app->take_navigation().x == 0);
|
||||
SDL_Event wheel{};
|
||||
wheel.type = SDL_EVENT_MOUSE_WHEEL;
|
||||
wheel.wheel.windowID = id;
|
||||
wheel.wheel.y = 2;
|
||||
wheel.wheel.mouse_x = 123;
|
||||
wheel.wheel.mouse_y = 45;
|
||||
wheel.wheel.direction = SDL_MOUSEWHEEL_FLIPPED;
|
||||
CHECK(SDL_PushEvent(&wheel));
|
||||
CHECK(app->poll_events());
|
||||
const auto navigation = app->take_navigation();
|
||||
CHECK(navigation.wheel == -2 && navigation.pointer.x == 123 && navigation.pointer.y == 45);
|
||||
app->set_ui_capture(true);
|
||||
CHECK(SDL_PushEvent(&wheel));
|
||||
CHECK(app->poll_events());
|
||||
CHECK(app->take_navigation().wheel == 0);
|
||||
app->set_ui_capture(false);
|
||||
key(SDL_SCANCODE_W, true);
|
||||
CHECK(app->poll_events());
|
||||
key(SDL_SCANCODE_W, true);
|
||||
send(SDL_EVENT_WINDOW_MINIMIZED, id);
|
||||
CHECK(app->poll_events());
|
||||
CHECK(required(app->sample_intention(49)).move_y == 0);
|
||||
send(SDL_EVENT_WINDOW_RESTORED, id);
|
||||
CHECK(app->poll_events());
|
||||
CHECK(required(app->sample_intention(50)).move_y == 0);
|
||||
app->set_ui_capture(true);
|
||||
key(SDL_SCANCODE_W, true); // Queued while captured; releasing capture must not replay it.
|
||||
app->set_ui_capture(false);
|
||||
CHECK(app->poll_events());
|
||||
CHECK(required(app->sample_intention(51)).move_y == 0);
|
||||
CHECK(app->present(frame) == Step::rendered);
|
||||
}
|
||||
} // namespace
|
||||
int main() try {
|
||||
client_controls_tests();
|
||||
frame_contract();
|
||||
lifecycle();
|
||||
native_input();
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
std::fprintf(stderr, "%s: %s\n", e.what(), SDL_GetError());
|
||||
|
||||
Reference in New Issue
Block a user