0.1.0-0-pre.11-fix.3

This commit is contained in:
2026-09-16 12:46:36 +02:00
parent fea73e898b
commit 69a268712c
5 changed files with 198 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
// file: crates/engines/engine-v1-sdl/src/runtime.rs
// version: 8
// version: 9
/// Minimal SDL3 runtime used by Desktop and Android POC runners.
pub struct SdlRuntime {
@@ -39,6 +39,7 @@ impl SdlRuntime {
std::result::Result::Err(error) => return std::result::Result::Err(error.to_string()),
};
let mut runner = engine_v1_common::FixedStepRunner::new(self.frame_duration);
let mut gesture_start: Option<engine_v1_common::PointerState> = None;
let mut pointer = engine_v1_common::PointerState::inactive();
tracing::info!(title = %self.title, width = self.width, height = self.height, "SDL3 runtime started");
'running: loop {
@@ -61,11 +62,14 @@ impl SdlRuntime {
},
sdl3::event::Event::MouseButtonDown { mouse_btn: sdl3::mouse::MouseButton::Left, x, y, .. } => {
pointer = normalize_mouse_pointer(&canvas, true, x, y);
gesture_start = Some(pointer);
input = input.with_pointer(pointer).with_action(engine_v1_common::GameAction::Primary, true);
input = apply_pointer_direction(input, pointer);
},
sdl3::event::Event::MouseButtonUp { mouse_btn: sdl3::mouse::MouseButton::Left, x, y, .. } => {
pointer = normalize_mouse_pointer(&canvas, false, x, y);
let released = normalize_mouse_pointer(&canvas, false, x, y);
input = apply_swipe_direction(input, gesture_start, released);
gesture_start = None;
pointer = released;
input = input.with_pointer(pointer);
},
sdl3::event::Event::MouseMotion { x, y, .. } if pointer.active() => {
@@ -74,14 +78,22 @@ impl SdlRuntime {
},
sdl3::event::Event::FingerDown { x, y, .. } => {
pointer = engine_v1_common::PointerState::normalized(true, x, y);
gesture_start = Some(pointer);
input = input.with_pointer(pointer).with_action(engine_v1_common::GameAction::Primary, true);
input = apply_pointer_direction(input, pointer);
},
sdl3::event::Event::FingerMotion { x, y, .. } => {
pointer = engine_v1_common::PointerState::normalized(true, x, y);
input = input.with_pointer(pointer);
},
sdl3::event::Event::FingerUp { x, y, .. } | sdl3::event::Event::FingerCanceled { x, y, .. } => {
sdl3::event::Event::FingerUp { x, y, .. } => {
let released = engine_v1_common::PointerState::normalized(false, x, y);
input = apply_swipe_direction(input, gesture_start, released);
gesture_start = None;
pointer = released;
input = input.with_pointer(pointer);
},
sdl3::event::Event::FingerCanceled { x, y, .. } => {
gesture_start = None;
pointer = engine_v1_common::PointerState::normalized(false, x, y);
input = input.with_pointer(pointer);
},
@@ -134,9 +146,23 @@ fn render_scene(canvas: &mut sdl3::render::WindowCanvas, scene: engine_v1_common
return std::result::Result::Ok(());
}
fn apply_pointer_direction(mut input: engine_v1_common::InputState, pointer: engine_v1_common::PointerState) -> engine_v1_common::InputState {
let horizontal = pointer.x() - 0.5;
let vertical = pointer.y() - 0.5;
const SWIPE_DIRECTION_THRESHOLD: f32 = 0.04;
fn apply_swipe_direction(
mut input: engine_v1_common::InputState,
start: Option<engine_v1_common::PointerState>,
end: engine_v1_common::PointerState,
) -> engine_v1_common::InputState {
let start = match start {
Some(value) => value,
None => return input,
};
let horizontal = end.x() - start.x();
let vertical = end.y() - start.y();
let dominant = horizontal.abs().max(vertical.abs());
if dominant < SWIPE_DIRECTION_THRESHOLD {
return input;
}
if horizontal.abs() >= vertical.abs() {
if horizontal < 0.0 {
input = input.with_action(engine_v1_common::GameAction::Left, true);
@@ -150,3 +176,7 @@ fn apply_pointer_direction(mut input: engine_v1_common::InputState, pointer: eng
}
return input;
}
#[cfg(test)]
#[path = "../unit_tests/swipe.rs"]
mod swipe_tests;

View File

@@ -0,0 +1,27 @@
// file: crates/engines/engine-v1-sdl/unit_tests/swipe.rs
// version: 1
fn pointer(x: f32, y: f32) -> engine_v1_common::PointerState {
return engine_v1_common::PointerState::normalized(true, x, y);
}
#[test]
fn short_gesture_does_not_emit_direction() {
let input = super::apply_swipe_direction(engine_v1_common::InputState::none(), Some(pointer(0.5, 0.5)), pointer(0.52, 0.51));
assert!(!input.is_active(engine_v1_common::GameAction::Left));
assert!(!input.is_active(engine_v1_common::GameAction::Right));
assert!(!input.is_active(engine_v1_common::GameAction::Up));
assert!(!input.is_active(engine_v1_common::GameAction::Down));
}
#[test]
fn horizontal_swipe_uses_dominant_axis() {
let input = super::apply_swipe_direction(engine_v1_common::InputState::none(), Some(pointer(0.7, 0.5)), pointer(0.2, 0.55));
assert!(input.is_active(engine_v1_common::GameAction::Left));
}
#[test]
fn vertical_swipe_uses_dominant_axis() {
let input = super::apply_swipe_direction(engine_v1_common::InputState::none(), Some(pointer(0.5, 0.8)), pointer(0.55, 0.2));
assert!(input.is_active(engine_v1_common::GameAction::Up));
}