use crate::{Point, Rectangle, Transformation, Vector};
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum Cursor {
Available(Point),
Levitating(Point),
#[default]
Unavailable,
}
impl Cursor {
pub fn position(self) -> Option<Point> {
match self {
Cursor::Available(position) => Some(position),
Cursor::Levitating(_) | Cursor::Unavailable => None,
}
}
pub fn position_over(self, bounds: Rectangle) -> Option<Point> {
self.position().filter(|p| bounds.contains(*p))
}
pub fn position_in(self, bounds: Rectangle) -> Option<Point> {
self.position_over(bounds)
.map(|p| p - Vector::new(bounds.x, bounds.y))
}
pub fn position_from(self, origin: Point) -> Option<Point> {
self.position().map(|p| p - Vector::new(origin.x, origin.y))
}
pub fn is_over(self, bounds: Rectangle) -> bool {
self.position_over(bounds).is_some()
}
pub fn is_levitating(self) -> bool {
matches!(self, Self::Levitating(_))
}
pub fn levitate(self) -> Self {
match self {
Self::Available(position) => Self::Levitating(position),
_ => self,
}
}
pub fn land(self) -> Self {
match self {
Cursor::Levitating(position) => Cursor::Available(position),
_ => self,
}
}
}
impl std::ops::Add<Vector> for Cursor {
type Output = Self;
fn add(self, translation: Vector) -> Self::Output {
match self {
Cursor::Available(point) => Cursor::Available(point + translation),
Cursor::Levitating(point) => {
Cursor::Levitating(point + translation)
}
Cursor::Unavailable => Cursor::Unavailable,
}
}
}
impl std::ops::Sub<Vector> for Cursor {
type Output = Self;
fn sub(self, translation: Vector) -> Self::Output {
match self {
Cursor::Available(point) => Cursor::Available(point - translation),
Cursor::Levitating(point) => {
Cursor::Levitating(point - translation)
}
Cursor::Unavailable => Cursor::Unavailable,
}
}
}
impl std::ops::Mul<Transformation> for Cursor {
type Output = Self;
fn mul(self, transformation: Transformation) -> Self {
match self {
Self::Available(position) => {
Self::Available(position * transformation)
}
Self::Levitating(position) => {
Self::Levitating(position * transformation)
}
Self::Unavailable => Self::Unavailable,
}
}
}