Implement basic cursor availability

This commit is contained in:
Héctor Ramón Jiménez 2023-06-08 20:11:59 +02:00
parent c15f1b5f65
commit 34451bff18
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
55 changed files with 731 additions and 886 deletions

51
core/src/mouse/cursor.rs Normal file
View file

@ -0,0 +1,51 @@
use crate::{Point, Rectangle, Vector};
/// The mouse cursor state.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Cursor {
/// The cursor has a defined position.
Available(Point),
/// The cursor is currently unavailable (i.e. out of bounds or busy).
Unavailable,
}
impl Cursor {
/// Returns the absolute position of the [`Cursor`], if available.
pub fn position(self) -> Option<Point> {
match self {
Cursor::Available(position) => Some(position),
Cursor::Unavailable => None,
}
}
/// Returns the absolute position of the [`Cursor`], if available and inside
/// the given bounds.
///
/// If the [`Cursor`] is not over the provided bounds, this method will
/// return `None`.
pub fn position_over(self, bounds: &Rectangle) -> Option<Point> {
self.position().filter(|p| bounds.contains(*p))
}
/// Returns the relative position of the [`Cursor`] inside the given bounds,
/// if available.
///
/// If the [`Cursor`] is not over the provided bounds, this method will
/// return `None`.
pub fn position_in(self, bounds: &Rectangle) -> Option<Point> {
self.position_over(bounds)
.map(|p| p - Vector::new(bounds.x, bounds.y))
}
/// Returns the relative position of the [`Cursor`] from the given origin,
/// if available.
pub fn position_from(self, origin: Point) -> Option<Point> {
self.position().map(|p| p - Vector::new(origin.x, origin.y))
}
/// Returns true if the [`Cursor`] is over the given `bounds`.
pub fn is_over(self, bounds: &Rectangle) -> bool {
self.position_over(bounds).is_some()
}
}