tengri/output/src/space/space_size.rs

55 lines
1.4 KiB
Rust

use crate::*;
pub trait Size<N: Coordinate>: From<[N;2]> + Debug + Copy {
fn x (&self) -> N;
fn y (&self) -> N;
fn w (&self) -> N { self.x() }
fn h (&self) -> N { self.y() }
fn wh (&self) -> [N;2] { [self.x(), self.y()] }
fn clip_w (&self, w: N) -> [N;2] { [self.w().min(w), self.h()] }
fn clip_h (&self, h: N) -> [N;2] { [self.w(), self.h().min(h)] }
fn expect_min (&self, w: N, h: N) -> Usually<&Self> {
if self.w() < w || self.h() < h {
Err(format!("min {w}x{h}").into())
} else {
Ok(self)
}
}
fn zero () -> [N;2] {
[N::zero(), N::zero()]
}
fn to_area_pos (&self) -> [N;4] {
let [x, y] = self.wh();
[x, y, 0.into(), 0.into()]
}
fn to_area_size (&self) -> [N;4] {
let [w, h] = self.wh();
[0.into(), 0.into(), w, h]
}
}
impl<N: Coordinate> Size<N> for (N, N) {
fn x (&self) -> N { self.0 }
fn y (&self) -> N { self.1 }
}
impl<N: Coordinate> Size<N> for [N;2] {
fn x (&self) -> N { self[0] }
fn y (&self) -> N { self[1] }
}
pub trait HasSize<E: Out> {
fn size (&self) -> &Measure<E>;
fn width (&self) -> usize {
self.size().w()
}
fn height (&self) -> usize {
self.size().h()
}
}
impl<E: Out, T: Has<Measure<E>>> HasSize<E> for T {
fn size (&self) -> &Measure<E> {
self.get()
}
}