mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 04:06:45 +01:00
40 lines
1.2 KiB
Rust
40 lines
1.2 KiB
Rust
use crate::*;
|
|
use std::fmt::Debug;
|
|
|
|
pub trait Size<N: Coordinate>: From<[N;2]> + Debug + Copy {
|
|
fn x (&self) -> N;
|
|
fn y (&self) -> N;
|
|
#[inline] fn w (&self) -> N { self.x() }
|
|
#[inline] fn h (&self) -> N { self.y() }
|
|
#[inline] fn wh (&self) -> [N;2] { [self.x(), self.y()] }
|
|
#[inline] fn clip_w (&self, w: N) -> [N;2] { [self.w().min(w), self.h()] }
|
|
#[inline] fn clip_h (&self, h: N) -> [N;2] { [self.w(), self.h().min(h)] }
|
|
#[inline] 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)
|
|
}
|
|
}
|
|
#[inline] fn zero () -> [N;2] {
|
|
[N::zero(), N::zero()]
|
|
}
|
|
#[inline] fn to_area_pos (&self) -> [N;4] {
|
|
let [x, y] = self.wh();
|
|
[x, y, 0.into(), 0.into()]
|
|
}
|
|
#[inline] 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) {
|
|
#[inline] fn x (&self) -> N { self.0 }
|
|
#[inline] fn y (&self) -> N { self.1 }
|
|
}
|
|
|
|
impl<N: Coordinate> Size<N> for [N;2] {
|
|
#[inline] fn x (&self) -> N { self[0] }
|
|
#[inline] fn y (&self) -> N { self[1] }
|
|
}
|