mirror of
https://github.com/italicsjenga/rust_minifb.git
synced 2024-12-23 19:31:30 +11:00
51bdfcc5ca
* Moved to Rust edition 2018 * Changed redox code as well * Undo typo * Another correction
36 lines
799 B
Rust
36 lines
799 B
Rust
use crate::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),
|
|
)),
|
|
MouseMode::Discard => {
|
|
if x < 0.0 || y < 0.0 || x >= window_width || y >= window_height {
|
|
None
|
|
} else {
|
|
Some((x, y))
|
|
}
|
|
}
|
|
}
|
|
}
|