use crate::{MouseButton, platform};
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ScrollDelta {
pub horizontal: i32,
pub vertical: i32,
}
impl ScrollDelta {
pub const fn is_up(self) -> bool {
self.vertical > 0
}
pub const fn is_down(self) -> bool {
self.vertical < 0
}
pub const fn is_left(self) -> bool {
self.horizontal < 0
}
pub const fn is_right(self) -> bool {
self.horizontal > 0
}
}
pub fn pressed(button: MouseButton) -> bool {
platform::mouse_pressed(button)
}
pub fn down(button: MouseButton) -> bool {
platform::mouse_down(button)
}
pub fn released(button: MouseButton) -> bool {
platform::mouse_released(button)
}
pub fn position() -> (i32, i32) {
try_position().unwrap_or((0, 0))
}
pub fn try_position() -> Option<(i32, i32)> {
platform::cursor_position()
}
pub fn x() -> i32 {
position().0
}
pub fn y() -> i32 {
position().1
}
pub fn move_to(x: i32, y: i32) -> bool {
platform::set_cursor_position(x, y)
}
pub fn move_by(x: i32, y: i32) -> bool {
let Some((current_x, current_y)) = try_position() else {
return false;
};
move_to(current_x.saturating_add(x), current_y.saturating_add(y))
}
pub fn click(button: MouseButton) -> bool {
platform::click_mouse(button, 1)
}
pub fn double_click(button: MouseButton) -> bool {
platform::click_mouse(button, 2)
}
pub fn clicks(button: MouseButton, count: usize) -> bool {
platform::click_mouse(button, count)
}
pub fn press(button: MouseButton) -> bool {
platform::send_mouse_button(button, false)
}
pub fn release(button: MouseButton) -> bool {
platform::send_mouse_button(button, true)
}
pub fn scroll(steps: i32) -> bool {
platform::scroll_mouse_vertical(steps)
}
pub fn scroll_horizontal(steps: i32) -> bool {
platform::scroll_mouse_horizontal(steps)
}
pub fn scroll_delta() -> Option<ScrollDelta> {
platform::take_scroll_delta().map(|(horizontal, vertical)| ScrollDelta {
horizontal,
vertical,
})
}
#[cfg(test)]
mod tests {
use super::ScrollDelta;
#[test]
fn scroll_direction_helpers_follow_each_axis_sign() {
let delta = ScrollDelta {
horizontal: -40,
vertical: 120,
};
assert!(delta.is_up());
assert!(!delta.is_down());
assert!(delta.is_left());
assert!(!delta.is_right());
}
}