mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-15 08:06:41 +01:00
63 lines
1.8 KiB
Rust
63 lines
1.8 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] }
|
|
}
|
|
|
|
#[cfg(test)] mod test_size {
|
|
use super::*;
|
|
use proptest::prelude::*;
|
|
proptest! {
|
|
#[test] fn test_size (
|
|
x in u16::MIN..u16::MAX,
|
|
y in u16::MIN..u16::MAX,
|
|
a in u16::MIN..u16::MAX,
|
|
b in u16::MIN..u16::MAX,
|
|
) {
|
|
let size = [x, y];
|
|
let _ = size.w();
|
|
let _ = size.h();
|
|
let _ = size.wh();
|
|
let _ = size.clip_w(a);
|
|
let _ = size.clip_h(b);
|
|
let _ = size.expect_min(a, b);
|
|
let _ = size.to_area_pos();
|
|
let _ = size.to_area_size();
|
|
}
|
|
}
|
|
}
|