use std::fmt;
use mitsuami_reactive::{IntoValue, Value};
#[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),
FollowHeight(f32),
FollowHeightUntilResized(f32),
}
impl From<Size> for WindowSize {
fn from(size: Size) -> WindowSize {
WindowSize::Fixed(size)
}
}
impl IntoValue<WindowSize> for WindowSize {
fn into_value(self) -> Value<WindowSize> {
Value::Static(self)
}
}
impl IntoValue<WindowSize> for Size {
fn into_value(self) -> Value<WindowSize> {
Value::Static(WindowSize::Fixed(self))
}
}
#[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 Insets {
pub top: f32,
pub right: f32,
pub bottom: f32,
pub left: f32,
}
impl Insets {
pub const ZERO: Insets = Insets { top: 0.0, right: 0.0, bottom: 0.0, left: 0.0 };
pub const fn new(top: f32, right: f32, bottom: f32, left: f32) -> Insets {
Insets { top, right, bottom, left }
}
}
#[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 inset(&self, by: Insets) -> Rect {
Rect::new(
self.x() + by.left,
self.y() + by.top,
(self.width() - by.left - by.right).max(0.0),
(self.height() - by.top - by.bottom).max(0.0),
)
}
pub fn union(&self, other: &Rect) -> Rect {
let (x, y) = (self.x().min(other.x()), self.y().min(other.y()));
Rect::new(x, y, self.max_x().max(other.max_x()) - x, self.max_y().max(other.max_y()) - y)
}
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}") }
}
}