diff --git a/engine/src/engine.rs b/engine/src/engine.rs index 4371e7e6..886f9670 100644 --- a/engine/src/engine.rs +++ b/engine/src/engine.rs @@ -128,15 +128,15 @@ pub trait Area { } #[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())] diff --git a/engine/src/tui/tui_output.rs b/engine/src/tui/tui_output.rs index 387cc5dd..876bad8d 100644 --- a/engine/src/tui/tui_output.rs +++ b/engine/src/tui/tui_output.rs @@ -71,7 +71,7 @@ impl TuiOut { impl Content 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 for &str { impl Content 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) diff --git a/layout/src/lib.rs b/layout/src/lib.rs index 72b180bc..e57aee7d 100644 --- a/layout/src/lib.rs +++ b/layout/src/lib.rs @@ -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::::layout(&unit, area), [20, 20, 0, 0]); - - assert_eq!(Fill::::x(unit).layout(area), [10, 20, 20, 0]); - assert_eq!(Fill::::y(unit).layout(area), [20, 10, 0, 20]); - assert_eq!(Fill::::xy(unit).layout(area), area); - - assert_eq!(Align::::c(unit).layout(area), [20, 20, 0, 0]); - assert_eq!(Align::::c(" ").layout(area), [20, 20, 0, 0]); - + assert_eq!(Content::::layout(&(), area), + [20, 20, 0, 0]); + assert_eq!(Fill::::x(()).layout(area), + [10, 20, 20, 0]); + assert_eq!(Fill::::y(()).layout(area), + [20, 10, 0, 20]); + assert_eq!(Fill::::xy(()).layout(area), + area); + assert_eq!(Fixed::::xy(4, 4, ()).layout(area), + [18, 18, 4, 4]); + assert_eq!(Align::::c(()).layout(area), + [20, 20, 0, 0]); Ok(()) } diff --git a/layout/src/transform_xy_unit.rs b/layout/src/transform_xy_unit.rs index e2149d8d..435fbf09 100644 --- a/layout/src/transform_xy_unit.rs +++ b/layout/src/transform_xy_unit.rs @@ -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);