use crate::Vec2;
use derive_more::derive::Into;
use thiserror::Error;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Into)]
#[cfg_attr(
feature = "bevy_reflect",
derive(Reflect),
reflect(Debug, PartialEq, Clone)
)]
pub struct AspectRatio(f32);
impl AspectRatio {
pub const SIXTEEN_NINE: Self = Self(16.0 / 9.0);
pub const FOUR_THREE: Self = Self(4.0 / 3.0);
pub const ULTRAWIDE: Self = Self(21.0 / 9.0);
#[inline]
pub const fn try_new(width: f32, height: f32) -> Result<Self, AspectRatioError> {
match (width, height) {
(w, h) if w == 0.0 || h == 0.0 => Err(AspectRatioError::Zero),
(w, h) if w.is_infinite() || h.is_infinite() => Err(AspectRatioError::Infinite),
(w, h) if w.is_nan() || h.is_nan() => Err(AspectRatioError::NaN),
_ => Ok(Self(width / height)),
}
}
#[inline]
pub const fn try_from_pixels(x: u32, y: u32) -> Result<Self, AspectRatioError> {
Self::try_new(x as f32, y as f32)
}
#[inline]
pub const fn ratio(&self) -> f32 {
self.0
}
#[inline]
pub const fn inverse(&self) -> Self {
Self(1.0 / self.0)
}
#[inline]
pub const fn is_landscape(&self) -> bool {
self.0 > 1.0
}
#[inline]
pub const fn is_portrait(&self) -> bool {
self.0 < 1.0
}
#[inline]
pub const fn is_square(&self) -> bool {
self.0 == 1.0
}
}
impl TryFrom<Vec2> for AspectRatio {
type Error = AspectRatioError;
#[inline]
fn try_from(value: Vec2) -> Result<Self, Self::Error> {
Self::try_new(value.x, value.y)
}
}
#[derive(Error, Debug, PartialEq, Eq, Clone, Copy)]
pub enum AspectRatioError {
#[error("AspectRatio error: width or height is zero")]
Zero,
#[error("AspectRatio error: width or height is infinite")]
Infinite,
#[error("AspectRatio error: width or height is NaN")]
NaN,
}