rust_minifb/src/mouse_handler.rs

30 lines
774 B
Rust
Raw Normal View History

2016-01-30 06:16:00 +11:00
use MouseMode;
fn clamp(v: f32, lb: f32, ub: f32) -> f32 {
f32::min(f32::max(v, lb), ub)
}
pub fn get_pos(mode: MouseMode, mx: f32, my: f32, scale: f32, width: f32, height: f32) -> Option<(f32, f32)> {
let s = 1.0 / scale as f32;
let x = mx * s;
let y = my * s;
let window_width = width * s;
let window_height = height * s;
match mode {
MouseMode::Pass => Some((x, y)),
MouseMode::Clamp => {
Some((clamp(x, 0.0, window_width - 1.0),
clamp(y, 0.0, window_height - 1.0)))
2016-01-30 06:16:00 +11:00
},
MouseMode::Discard => {
if x < 0.0 || y < 0.0 || x >= window_width || y >= window_height {
None
} else {
Some((x, y))
}
},
}
}