fix Fixed

This commit is contained in:
🪞👃🪞 2025-01-01 19:08:57 +01:00
parent 01dacd407d
commit 8c28ef2bd7
4 changed files with 31 additions and 29 deletions

View file

@ -128,15 +128,15 @@ pub trait Area<N: Coordinate> {
}
#[inline] fn center_x (&self, n: N) -> [N;4] {
let [x, y, w, h] = self.xywh();
[x + w / 2.into() - n, y + h / 2.into(), n, 1.into()]
[(x + w / 2.into()).minus(n / 2.into()), y + h / 2.into(), n, 1.into()]
}
#[inline] fn center_y (&self, m: N) -> [N;4] {
let [x, y, w, h] = self.xywh();
[x + w / 2.into(), y + h / 2.into() - m, 1.into(), m]
[x + w / 2.into(), (y + h / 2.into()).minus(m / 2.into()), 1.into(), m]
}
#[inline] fn center_xy (&self, [n, m]: [N;2]) -> [N;4] {
let [x, y, w, h] = self.xywh();
[x + w / 2.into() - n, y + h / 2.into() - n - m, n, m]
[(x + w / 2.into()).minus(n / 2.into()), (y + h / 2.into()).minus(m / 2.into()), n, m]
}
#[inline] fn centered (&self) -> [N;2] {
[self.x().minus(self.w()/2.into()), self.y().minus(self.h()/2.into())]

View file

@ -71,7 +71,7 @@ impl TuiOut {
impl Content<Tui> for &str {
fn layout (&self, to: [u16;4]) -> [u16;4] {
to.center_x(self.chars().count() as u16)
to.center_xy([self.chars().count() as u16, 1])
}
fn render (&self, to: &mut TuiOut) {
to.blit(self, to.area.x(), to.area.y(), None)
@ -80,7 +80,7 @@ impl Content<Tui> for &str {
impl Content<Tui> for String {
fn layout (&self, to: [u16;4]) -> [u16;4] {
to.center_x(self.chars().count() as u16)
to.center_xy([self.chars().count() as u16, 1])
}
fn render (&self, to: &mut TuiOut) {
to.blit(self, to.area.x(), to.area.y(), None)

View file

@ -13,18 +13,18 @@ pub(crate) use std::marker::PhantomData;
#[cfg(test)] #[test] fn test_layout () -> Usually<()> {
use crate::tui::Tui;
let area: [u16;4] = [10, 10, 20, 20];
let unit = ();
assert_eq!(Content::<Tui>::layout(&unit, area), [20, 20, 0, 0]);
assert_eq!(Fill::<Tui, _>::x(unit).layout(area), [10, 20, 20, 0]);
assert_eq!(Fill::<Tui, _>::y(unit).layout(area), [20, 10, 0, 20]);
assert_eq!(Fill::<Tui, _>::xy(unit).layout(area), area);
assert_eq!(Align::<Tui, _>::c(unit).layout(area), [20, 20, 0, 0]);
assert_eq!(Align::<Tui, _>::c(" ").layout(area), [20, 20, 0, 0]);
assert_eq!(Content::<Tui>::layout(&(), area),
[20, 20, 0, 0]);
assert_eq!(Fill::<Tui, _>::x(()).layout(area),
[10, 20, 20, 0]);
assert_eq!(Fill::<Tui, _>::y(()).layout(area),
[20, 10, 0, 20]);
assert_eq!(Fill::<Tui, _>::xy(()).layout(area),
area);
assert_eq!(Fixed::<Tui, _>::xy(4, 4, ()).layout(area),
[18, 18, 4, 4]);
assert_eq!(Align::<Tui, _>::c(()).layout(area),
[20, 20, 0, 0]);
Ok(())
}

View file

@ -42,23 +42,25 @@ macro_rules! transform_xy_unit {
}
transform_xy_unit!(|self: Fixed, area|{
let area = self.content().layout(area);
let [x, y, w, h] = self.content().layout(area.center_xy(match self {
Self::X(fw, _) => [*fw, area.h()],
Self::Y(fh, _) => [area.w(), *fh],
Self::XY(fw, fh, _) => [*fw, *fh],
}).into()).xywh();
match self {
Self::X(fw, _) => [area.x(), area.y(), *fw, area.h()],
Self::Y(fh, _) => [area.x(), area.y(), area.w(), *fh],
Self::XY(fw, fh, _) => [area.x(), area.y(), *fw, *fh], // tagn
Self::X(fw, _) => [x, y, *fw, h],
Self::Y(fh, _) => [x, y, w, *fh],
Self::XY(fw, fh, _) => [x, y, *fw, *fh],
}
});
transform_xy_unit!(|self: Shrink, area|{
let area = self.content().layout(area);
[area.x(), area.y(), area.w().minus(self.dx()), area.h().minus(self.dy())]
});
transform_xy_unit!(|self: Shrink, area|self.content().layout([
area.x(), area.y(), area.w().minus(self.dx()), area.h().minus(self.dy())
].into()));
transform_xy_unit!(|self: Expand, area|{
let area = self.content().layout(area);
[area.x(), area.y(), area.w() + self.dx(), area.h() + self.dy()]
});
transform_xy_unit!(|self: Expand, area|self.content().layout([
area.x(), area.y(), area.w() + self.dx(), area.h() + self.dy()
].into()));
transform_xy_unit!(|self: Min, area|{
let area = self.content().layout(area);