mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-07-17 07:56:56 +02:00
115 lines
3.3 KiB
Rust
115 lines
3.3 KiB
Rust
use super::*;
|
|
use Split::*;
|
|
|
|
pub trait Xy<N: Coord> {
|
|
fn x (&self) -> N;
|
|
fn y (&self) -> N;
|
|
}
|
|
|
|
pub trait Wh<N: Coord>: Wide<N> + Tall<N> {
|
|
fn wh (&self) -> [N;2];
|
|
}
|
|
|
|
pub trait Xywh<N: Coord>: Xy<N> + Wh<N> {
|
|
fn xywh (&self) -> XYWH<N> {
|
|
XYWH(self.x(), self.y(), self.w(), self.h())
|
|
}
|
|
}
|
|
|
|
pub trait Wide<N: Coord>: Xy<N> {
|
|
fn w (&self) -> N { N::zero() }
|
|
fn w_min (&self) -> N { self.w() }
|
|
fn w_max (&self) -> N { self.w() }
|
|
}
|
|
|
|
pub trait Tall<N: Coord> {
|
|
fn h (&self) -> N { N::zero() }
|
|
fn h_min (&self) -> N { self.h() }
|
|
fn h_max (&self) -> N { self.h() }
|
|
}
|
|
|
|
/// Point with size.
|
|
///
|
|
/// ```
|
|
/// # use tengri::*;
|
|
/// let xywh = XYWH(0u16, 0, 0, 0);
|
|
/// assert_eq!(XYWH(10u16, 10, 20, 20).center(), (20, 20));
|
|
/// ```
|
|
///
|
|
/// * [ ] TODO: origin field (determines at which corner/side is X0 Y0)
|
|
///
|
|
#[cfg_attr(test, derive(Arbitrary))] #[derive(Copy, Clone, Debug, Default, PartialEq)]
|
|
pub struct XYWH<N: Coord>(pub N, pub N, pub N, pub N);
|
|
|
|
impl<N: Coord> Xy<N> for XYWH<N> {
|
|
fn x (&self) -> N { self.0 }
|
|
fn y (&self) -> N { self.1 }
|
|
}
|
|
|
|
impl<N: Coord> Wide<N> for XYWH<N> { fn w (&self) -> N { self.2 } }
|
|
|
|
impl<N: Coord> Tall<N> for XYWH<N> { fn h (&self) -> N { self.3 } }
|
|
|
|
impl<N: Coord> XYWH<N> {
|
|
|
|
pub fn zero () -> Self {
|
|
Self(0.into(), 0.into(), 0.into(), 0.into())
|
|
}
|
|
|
|
pub fn center (&self) -> (N, N) {
|
|
let Self(x, y, w, h) = *self;
|
|
(x.plus(w/2.into()), y.plus(h/2.into()))
|
|
}
|
|
|
|
pub fn centered (&self) -> (N, N) {
|
|
let Self(x, y, w, h) = *self;
|
|
(x.minus(w/2.into()), y.minus(h/2.into()))
|
|
}
|
|
|
|
pub fn centered_x (&self, n: N) -> Self {
|
|
let Self(x, y, w, h) = *self;
|
|
let x_center = (x.plus(w / 2.into())).minus(n / 2.into());
|
|
let y_center = y.plus(h / 2.into());
|
|
XYWH(x_center, y_center, n, 1.into())
|
|
}
|
|
|
|
pub fn centered_y (&self, n: N) -> Self {
|
|
let Self(x, y, w, h) = *self;
|
|
let x_center = x.plus(w / 2.into());
|
|
let y_corner = (y.plus(h / 2.into())).minus(n / 2.into());
|
|
XYWH(x_center, y_corner, 1.into(), n)
|
|
}
|
|
|
|
pub fn centered_xy (&self, [n, m]: [N;2]) -> Self {
|
|
let Self(x, y, w, h) = *self;
|
|
let x_center = (x.plus(w / 2.into())).minus(n / 2.into());
|
|
let y_corner = (y.plus(h / 2.into())).minus(m / 2.into());
|
|
XYWH(x_center, y_corner, n, m)
|
|
}
|
|
|
|
pub fn split_half (&self, direction: &Split) -> (Self, Self) {
|
|
let XYWH(x, y, w, h) = self.xywh();
|
|
match direction {
|
|
South => (XYWH(x, y, w, h - h / 2.into()), XYWH(x, y + h / 2.into(), w, h / 2.into())),
|
|
East => (XYWH(x, y, w - w / 2.into(), h), XYWH(x + w / 2.into(), y, w / 2.into(), h)),
|
|
North => (XYWH(x, y + h / 2.into(), w, h - h / 2.into()), XYWH(x, y, w, h / 2.into())),
|
|
West => (XYWH(x + w / 2.into(), y, w - w / 2.into(), h), XYWH(x, y, w / 2.into(), h)),
|
|
Above | Below => (XYWH(x, y, w, h), XYWH(x, y, w, h))
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
impl From<&ratatui::prelude::Rect> for XYWH<u16> {
|
|
fn from (rect: &ratatui::prelude::Rect) -> Self {
|
|
Self(rect.x, rect.y, rect.width, rect.height)
|
|
}
|
|
}
|
|
|
|
impl<N: Coord, T: Wide<N> + Tall<N>> Wh<N> for T {
|
|
fn wh (&self) -> [N;2] {
|
|
[self.w(), self.h()]
|
|
}
|
|
}
|
|
|
|
impl<N: Coord, T: Xy<N> + Wh<N>> Xywh<N> for T {}
|