use std::fmt;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Size {
pub width: f32,
pub height: f32,
}
impl Size {
pub const ZERO: Size = Size { width: 0.0, height: 0.0 };
pub const fn new(width: f32, height: f32) -> Size {
Size { width, height }
}
pub fn is_empty(&self) -> bool {
self.width <= 0.0 || self.height <= 0.0
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum WindowSize {
Fixed(Size),
FitHeight(f32),
}
impl From<Size> for WindowSize {
fn from(size: Size) -> WindowSize {
WindowSize::Fixed(size)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Point {
pub x: f32,
pub y: f32,
}
impl Point {
pub const ZERO: Point = Point { x: 0.0, y: 0.0 };
pub const fn new(x: f32, y: f32) -> Point {
Point { x, y }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Rect {
pub origin: Point,
pub size: Size,
}
impl Rect {
pub const ZERO: Rect = Rect { origin: Point::ZERO, size: Size::ZERO };
pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Rect {
Rect { origin: Point { x, y }, size: Size { width, height } }
}
pub fn x(&self) -> f32 {
self.origin.x
}
pub fn y(&self) -> f32 {
self.origin.y
}
pub fn width(&self) -> f32 {
self.size.width
}
pub fn height(&self) -> f32 {
self.size.height
}
pub fn max_x(&self) -> f32 {
self.origin.x + self.size.width
}
pub fn max_y(&self) -> f32 {
self.origin.y + self.size.height
}
pub fn offset(&self, by: Point) -> Rect {
Rect { origin: Point::new(self.origin.x + by.x, self.origin.y + by.y), size: self.size }
}
pub fn intersection(&self, other: &Rect) -> Option<Rect> {
let x = self.x().max(other.x());
let y = self.y().max(other.y());
let max_x = self.max_x().min(other.max_x());
let max_y = self.max_y().min(other.max_y());
(max_x > x && max_y > y).then(|| Rect::new(x, y, max_x - x, max_y - y))
}
}
impl fmt::Display for Rect {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{},{} {}×{}", Num(self.origin.x), Num(self.origin.y), Num(self.size.width), Num(self.size.height))
}
}
pub struct Num(pub f32);
impl fmt::Display for Num {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let rounded = (self.0 * 100.0).round() / 100.0;
if rounded == rounded.trunc() { write!(f, "{}", rounded as i64) } else { write!(f, "{rounded}") }
}
}