use glam::Vec2;
pub enum PickEvent {
Press {
world: Vec2,
screen: [f32; 2],
button: repose_core::input::PointerButton,
},
Click {
world: Vec2,
screen: [f32; 2],
button: repose_core::input::PointerButton,
},
Release {
world: Vec2,
screen: [f32; 2],
button: repose_core::input::PointerButton,
},
Hover {
world: Vec2,
screen: [f32; 2],
},
TouchDown { id: u64, screen: [f32; 2] },
TouchMove { id: u64, screen: [f32; 2] },
TouchUp { id: u64 },
}
pub const CLICK_SLOP_DP: f32 = 12.0;
pub fn click_within_slop_dp(a: [f32; 2], b: [f32; 2], density: f32) -> bool {
let d = if density.is_finite() && density > 1e-6 {
density
} else {
1.0
};
let dx = (a[0] - b[0]) / d;
let dy = (a[1] - b[1]) / d;
dx * dx + dy * dy <= CLICK_SLOP_DP * CLICK_SLOP_DP
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn click_slop_cancels_drags() {
assert!(click_within_slop_dp([100.0, 100.0], [105.0, 105.0], 1.0));
assert!(click_within_slop_dp([100.0, 100.0], [105.0, 105.0], 1.0));
assert!(!click_within_slop_dp(
[100.0, 100.0],
[100.0 + CLICK_SLOP_DP + 1.0, 100.0],
1.0
));
}
#[test]
fn click_slop_is_density_independent() {
assert!(click_within_slop_dp([0.0, 0.0], [36.0, 0.0], 3.0));
assert!(!click_within_slop_dp([0.0, 0.0], [37.0, 0.0], 3.0));
assert!(click_within_slop_dp([0.0, 0.0], [12.0, 0.0], 1.0));
assert!(!click_within_slop_dp([0.0, 0.0], [13.0, 0.0], 1.0));
}
}