use crate::*; pub trait Size: 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 Size for (N, N) { fn x (&self) -> N { self.0 } fn y (&self) -> N { self.1 } } impl Size for [N;2] { fn x (&self) -> N { self[0] } fn y (&self) -> N { self[1] } } pub trait HasSize { fn size (&self) -> &Measure; fn width (&self) -> usize { self.size().w() } fn height (&self) -> usize { self.size().h() } } impl>> HasSize for T { fn size (&self) -> &Measure { self.get() } }