diff --git a/engine/src/engine.rs b/engine/src/engine.rs index 713c802f..4371e7e6 100644 --- a/engine/src/engine.rs +++ b/engine/src/engine.rs @@ -126,6 +126,18 @@ pub trait Area { #[inline] fn center (&self) -> [N;2] { [self.x() + self.w()/2.into(), self.y() + self.h()/2.into()] } + #[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()] + } + #[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] + } + #[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] + } #[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/lib.rs b/engine/src/lib.rs index 00fb8356..9a89169b 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -13,6 +13,10 @@ pub type Usually = Result>; /// Standard optional result type. pub type Perhaps = Result, Box>; +#[cfg(test)] #[test] fn test_dimensions () { + assert_eq!(Area::center(&[10u16, 10, 20, 20]), [20, 20]); +} + #[cfg(test)] #[test] fn test_stub_engine () -> Usually<()> { struct TestEngine(bool); struct TestInput(bool); @@ -46,11 +50,11 @@ pub type Perhaps = Result, Box>; fn area_mut (&mut self) -> &mut [u16;4] { &mut self.0 } - fn place (&mut self, _: [u16;4], _: &impl Layout) { + fn place (&mut self, _: [u16;4], _: &impl Content) { () } } - impl Layout for String { + impl Content for String { fn render (&self, to: &mut TestOutput) { to.area_mut().set_w(self.len() as u16); } @@ -62,8 +66,8 @@ pub type Perhaps = Result, Box>; use crate::tui::*; use std::sync::{Arc, RwLock}; struct TestComponent(String); - impl Layout for TestComponent { - fn layout (&self) -> Option> { + impl Content for TestComponent { + fn content (&self) -> Option> { Some(self.0.as_str()) } } diff --git a/engine/src/tui/tui_output.rs b/engine/src/tui/tui_output.rs index ceab4c8a..387cc5dd 100644 --- a/engine/src/tui/tui_output.rs +++ b/engine/src/tui/tui_output.rs @@ -71,10 +71,7 @@ impl TuiOut { impl Content for &str { fn layout (&self, to: [u16;4]) -> [u16;4] { - let w = self.chars().count() as u16; - let y = to.y() + to.h() / 2; - let x = (to.x() + to.w() / 2).minus(w / 2); - [x, y, w, 1] + to.center_x(self.chars().count() as u16) } fn render (&self, to: &mut TuiOut) { to.blit(self, to.area.x(), to.area.y(), None) @@ -83,10 +80,7 @@ impl Content for &str { impl Content for String { fn layout (&self, to: [u16;4]) -> [u16;4] { - let w = self.chars().count() as u16; - let y = to.y() + to.h() / 2; - let x = (to.x() + to.w() / 2).minus(w / 2); - [x, y, w, 1] + to.center_x(self.chars().count() as u16) } fn render (&self, to: &mut TuiOut) { to.blit(self, to.area.x(), to.area.y(), None) diff --git a/layout/src/direction.rs b/layout/src/direction.rs index 8c189e3f..1b1f7c14 100644 --- a/layout/src/direction.rs +++ b/layout/src/direction.rs @@ -20,7 +20,7 @@ impl Direction { Self::East => Self::South, } } - /// Return next direction counterclockwise + /// Return next direction counterclockwise01. pub fn ccw (&self) -> Self { match self { Self::North => Self::West, diff --git a/layout/src/lib.rs b/layout/src/lib.rs index 5f4455be..0f293e73 100644 --- a/layout/src/lib.rs +++ b/layout/src/lib.rs @@ -14,12 +14,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 = (); - // should be independent over E, isn't - assert_eq!(Content::::layout(&unit, area), [15, 15, 0, 0]); - assert_eq!(Fill::::x(unit).layout(area), [10, 15, 20, 0]); - assert_eq!(Fill::::y(unit).layout(area), [15, 10, 0, 20]); + + 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]); + Ok(()) }