use super::*; /// Split along an axis. Direction determines order. #[cfg_attr(test, derive(Arbitrary))] #[derive(Copy, Clone, PartialEq, Debug, Default)] pub enum Split { North, South, East, West, Above, #[default] Below } pub struct Pair, B: Draw>(Split, A, B, PhantomData); pub fn split , B: Draw> ( split: Split, a: A, b: B ) -> Pair { Pair(split, a, b, PhantomData) } impl, B: Draw> Draw for Pair { fn layout (&self, to: XYWH) -> Drawn { let Self(split, a, b, ..) = self; let (area_a, area_b) = stack_areas(split, to, a, b)?; Ok(stack_drawn(split, area_a, area_b)) } fn draw (self, to: &mut S) -> Drawn { let Self(ref split, a, b, ..) = self; let (area_a, area_b) = stack_areas(split, to.area(), &a, &b)?; let (drawn_a, drawn_b) = draw_stacks(split, to, a, area_a, None, b, area_b, None)?; Ok(stack_drawn(split, drawn_a, drawn_b)) } } impl Split { /// ``` /// use tengri::*; /// let _ = Split::Above.stack("", ""); /// let _ = Split::Below.stack("", ""); /// let _ = Split::North.stack("", ""); /// let _ = Split::South.stack("", ""); /// let _ = Split::East.stack("", ""); /// let _ = Split::West.stack("", ""); /// ``` pub const fn stack , B: Draw> (&self, a: A, b: B) -> impl Draw { Pair(*self, a, b, PhantomData) } /// ``` /// use tengri::*; /// let _ = Split::Above.half("", ""); /// let _ = Split::Below.half("", ""); /// let _ = Split::North.half("", ""); /// let _ = Split::South.half("", ""); /// let _ = Split::East.half("", ""); /// let _ = Split::West.half("", ""); /// ``` pub const fn half , B: Draw> (&self, a: A, b: B) -> impl Draw { draw(move|to: &mut S|{ let (area_a, area_b) = to.xywh().split_half(self); let (origin_a, origin_b) = self.origins(); let (drawn_a, drawn_b) = draw_stacks(self, to, a, area_a, origin_a, b, area_b, origin_b)?; Ok(stack_drawn(self, drawn_a, drawn_b)) }) } /// Newly split areas begin at the center of the split /// to maintain centeredness in the user's field of view. /// /// Use [align] to override that and always start /// at the top, bottom, etc. /// /// ``` /// /* /// /// Split east: Split south: /// | | | | A | /// | <-A|B-> | |---------| /// | | | | B | /// /// */ /// ``` const fn origins (&self) -> (Azimuth, Azimuth) { use Azimuth::*; match self { Self::South => (S, N), Self::East => (E, W), Self::North => (N, S), Self::West => (W, E), Self::Above => (C, C), Self::Below => (C, C), } } ///// ``` ///// use tengri::*; ///// let _ = Split::Below.iter([ ///// "Leftbar" ///// .min_w(10).max_w(15).align(Azimuth::NW), ///// "Rightbar" ///// .min_w(10).max_w(12).align(Azimuth::NE), ///// "Center" ///// .min_w(20).max_w(40).align(Azimuth::C), ///// ].iter()); ///// ``` //pub fn iter > (&self, _: impl Iterator) { //todo!() //} } fn draw_stacks ( split: &Split, to: &mut S, a: impl Draw, area_a: impl Into>>, origin_a: impl Into>, b: impl Draw, area_b: impl Into>>, origin_b: impl Into>, ) -> Usually<(Option>, Option>)> { let draw_a = |to: &mut S|Ok::<_, Box>(if let Some(origin_a) = origin_a.into() { to.clip(area_a.into(), a.align(origin_a))? } else { to.clip(area_a.into(), a)? }); let draw_b = |to: &mut S|Ok::<_, Box>(if let Some(origin_b) = origin_b.into() { to.clip(area_b.into(), b.align(origin_b))? } else { to.clip(area_b.into(), b)? }); Ok(if matches!(split, Split::Below) { let drawn_b = draw_b(to)?; let drawn_a = draw_a(to)?; (drawn_a, drawn_b) } else { (draw_a(to)?, draw_b(to)?) }) } pub fn stack_areas ( split: &Split, area: XYWH, a: &impl Draw, b: &impl Draw, ) -> Usually<(Option>, Option>)> { let area_a = a.layout(area)?; Ok(match split { Split::South => ( area_a, if let Some(used) = area_a { b.layout(XYWH(area.x(), area.y() + used.h(), area.w(), area.h().minus(used.h())))? } else { None } ), Split::East => ( area_a, if let Some(used) = area_a { b.layout(XYWH(area.x() + used.w(), area.y(), area.w().minus(used.w()), area.h()))? } else { None } ), Split::North => ( if let Some(used) = area_a { Some(XYWH(used.x(), (area.y() + area.h()).minus(used.h()), used.w(), used.h())) } else { None }, if let Some(used) = area_a { b.layout(XYWH(area.x(), area.y(), area.w(), area.h().minus(used.h())))? } else { b.layout(area)?.map(|area_b|XYWH( area_b.x(), area_b.y() + area_b.h(), area_b.w(), area_b.h() )) } ), Split::West => ( if let Some(used) = area_a { Some(XYWH((area.x() + area.w()).minus(used.w()), used.y(), used.w(), used.h())) } else { None }, if let Some(used) = area_a { b.layout(XYWH(area.x(), area.y(), area.w().minus(used.w()), area.h()))? } else { b.layout(area)?.map(|area_b|XYWH( area_b.x() + area_b.w(), area_b.y(), area_b.w(), area_b.h() )) } ), Split::Above | Split::Below => ( area_a, b.layout(area)?, ), }) } fn stack_drawn ( split: &Split, drawn_a: Option>, drawn_b: Option>, ) -> Option> { if let (Some(XYWH(xa, ya, wa, ha)), Some(XYWH(xb, yb, wb, hb))) = (drawn_a, drawn_b) { match split { Split::South => Some(XYWH(xa.min(xb), ya, wa.max(wb), ha + hb)), Split::East => Some(XYWH(xa, ya.min(yb), wa + wb, ha.max(hb))), Split::North => Some(XYWH(xa.min(xb), yb, wa.max(wb), ha + hb)), Split::West => Some(XYWH(xb, ya.min(yb), wa + wb, ha.max(hb))), Split::Above | Split::Below => Some(XYWH(xa.min(xb), ya.min(yb), wa.max(wb), ha.max(hb))), } } else if let Some(a) = drawn_a { Some(a) } else if let Some(b) = drawn_b { Some(b) } else { None } } #[macro_export] macro_rules! north { ($head:expr $(,)?) => { $head }; ($head:expr, $($tail:expr),* $(,)?) => { north($head, north!($($tail,)*)) }; } #[macro_export] macro_rules! south { ($head:expr $(,)?) => { $head }; ($head:expr, $($tail:expr),* $(,)?) => { south($head, south!($($tail,)*)) }; } #[macro_export] macro_rules! east { ($head:expr $(,)?) => { $head }; ($head:expr, $($tail:expr),* $(,)?) => { east($head, east!($($tail,)*)) }; } #[macro_export] macro_rules! west { ($head:expr $(,)?) => { $head }; ($head:expr $(, $tail:expr)* $(,)?) => { west($head, west!($($tail,)*)) }; } #[macro_export] macro_rules! above { ($head:expr $(,)?) => { $head }; ($head:expr $(, $tail:expr)* $(,)?) => { above($head, above!($($tail,)*)) }; } #[macro_export] macro_rules! below { ($head:expr $(,)?) => { $head }; ($head:expr, $($tail:expr),* $(,)?) => { below($head, below!($($tail,)*)) }; } pub const fn east , B: Draw> (a: A, b: B) -> impl Draw { Pair(Split::East, a, b, PhantomData) } pub const fn north , B: Draw> (a: A, b: B) -> impl Draw { Pair(Split::North, a, b, PhantomData) } pub const fn west , B: Draw> (a: A, b: B) -> impl Draw { Pair(Split::West, a, b, PhantomData) } pub const fn south , B: Draw> (a: A, b: B) -> impl Draw { Pair(Split::South, a, b, PhantomData) } pub const fn above , B: Draw> (a: A, b: B) -> impl Draw { Pair(Split::Above, a, b, PhantomData) } pub const fn below , B: Draw> (a: A, b: B) -> impl Draw { Pair(Split::Below, a, b, PhantomData) } #[cfg(test)] mod test { use crate::*; #[test] fn test_stack_areas () -> Usually<()> { let area = XYWH(0u16, 0, 80, 25); assert_eq!(stack_areas(&Split::East, area, &"foo", &"bar")?, ( Some(XYWH(0u16, 0, 3, 1)), Some(XYWH(3u16, 0, 3, 1)), )); assert_eq!(stack_areas(&Split::South, area, &"foo", &"bar")?, ( Some(XYWH(0u16, 0, 3, 1)), Some(XYWH(0u16, 1, 3, 1)), )); Ok(()) } #[test] fn test_split_stack () -> Usually<()> { use tengri::{*, Split::*}; let stack = split(East, "foo", "bar"); assert_eq!(stack.layout(XYWH(0, 0, 80, 25))?, Some(XYWH(0, 0, 6, 1))); let stack = split(South, "foo", "bar"); assert_eq!(stack.layout(XYWH(0, 0, 80, 25))?, Some(XYWH(0, 0, 3, 2))); let stack = split(South, split(East, "foo", "bar"), "baz"); assert_eq!(stack.layout(XYWH(0, 0, 80, 25))?, Some(XYWH(0, 0, 6, 2))); let stack = split(East, split(South, "foo", "bar"), "baz"); assert_eq!(stack.layout(XYWH(0, 0, 80, 25))?, Some(XYWH(0, 0, 6, 2))); Ok(()) } }