use crate::math::{Point, Quad, Rect, Size, Vector}; use core::f32; use num_traits::{Float, NumCast, ToPrimitive}; pub struct LogicalScreenSpace; pub struct PhysicalScreenSpace; pub struct Normalized; pub type LogicalPoint = Point; pub type LogicalVector = Vector; pub type LogicalSize = Size; pub type LogicalRect = Rect; pub type LogicalQuad = Quad; pub type PhysicalPoint = Point; pub type PhysicalVector = Vector; pub type PhysicalSize = Size; pub type PhysicalRect = Rect; pub type NormalizedPoint = Point; pub type NormalizedRect = Rect; pub type NormalizedQuad = Quad; pub trait Normalize { type Output; fn normalize(&self, size: Size) -> Self::Output; } impl Normalize for Point where T: Copy + std::ops::Div + ToPrimitive, { type Output = Point; #[inline] fn normalize(&self, size: Size) -> Self::Output { Point::new( F::from(self.x).unwrap() / F::from(size.width).unwrap(), F::from(self.y).unwrap() / F::from(size.height).unwrap(), ) } } impl Normalize for Rect where T: Copy + std::ops::Div + ToPrimitive, { type Output = Rect; #[inline] fn normalize(&self, size: Size) -> Self::Output { Rect::new(self.min.normalize(size), self.max.normalize(size)) } } pub trait ConvertToLogical { type Output; fn to_logical(&self, scale_factor: F) -> Self::Output; } pub trait ConvertToPhysical { type Output; fn to_physical(&self, scale_factor: F) -> Self::Output; } #[inline] pub fn validate_scale_factor(scale_factor: F) -> bool { scale_factor.is_sign_positive() && scale_factor.is_normal() } impl ConvertToLogical for PhysicalPoint where T: Copy + std::ops::Div + ToPrimitive, { type Output = LogicalPoint; #[inline] fn to_logical(&self, scale_factor: F) -> Self::Output { assert!(validate_scale_factor(scale_factor)); let x = F::from(self.x).unwrap() / scale_factor; let y = F::from(self.y).unwrap() / scale_factor; LogicalPoint::new(x, y).cast() } } impl ConvertToLogical for PhysicalSize where T: Copy + std::ops::Div + ToPrimitive, { type Output = LogicalSize; #[inline] fn to_logical(&self, scale_factor: F) -> Self::Output { assert!(validate_scale_factor(scale_factor)); let width = F::from(self.width).unwrap() / scale_factor; let height = F::from(self.height).unwrap() / scale_factor; LogicalSize::new(width, height).cast() } } impl ConvertToLogical for PhysicalRect where T: Copy + std::ops::Div + ToPrimitive, { type Output = LogicalRect; #[inline] fn to_logical(&self, scale_factor: F) -> Self::Output { assert!(validate_scale_factor(scale_factor)); let min = self.min.to_logical(scale_factor); let max = self.max.to_logical(scale_factor); LogicalRect::new(min, max) } } impl ConvertToPhysical for LogicalPoint where T: Copy + std::ops::Div + ToPrimitive, { type Output = PhysicalPoint; #[inline] fn to_physical(&self, scale_factor: F) -> Self::Output { assert!(validate_scale_factor(scale_factor)); let x = F::from(self.x).unwrap() * scale_factor; let y = F::from(self.y).unwrap() * scale_factor; PhysicalPoint::new(x, y).cast() } } impl ConvertToPhysical for LogicalRect where T: Copy + std::ops::Div + ToPrimitive, { type Output = PhysicalRect; #[inline] fn to_physical(&self, scale_factor: F) -> Self::Output { assert!(validate_scale_factor(scale_factor)); let min = self.min.to_physical(scale_factor); let max = self.max.to_physical(scale_factor); PhysicalRect::new(min, max) } } pub trait Uv { fn default_uv() -> Self; fn white_uv() -> Self; } impl Uv for NormalizedPoint { #[inline] fn default_uv() -> Self { Self::zero() } #[inline] fn white_uv() -> Self { Self::zero() } } impl Uv for NormalizedRect { #[inline] fn default_uv() -> Self { Self::from_size(Size::new(1.0, 1.0)) } #[inline] fn white_uv() -> Self { Self::zero() } } impl Uv for NormalizedQuad { #[inline] fn default_uv() -> Self { Self::from_size(Size::new(1.0, 1.0)) } #[inline] fn white_uv() -> Self { Self::zero() } }