trying to get new Bsp to work; update docs

This commit is contained in:
🪞👃🪞 2024-12-31 19:21:48 +01:00
parent c9b81edb45
commit 62ce1776c0
11 changed files with 301 additions and 157 deletions

4
layout/README.md Normal file
View file

@ -0,0 +1,4 @@
# `tek_layout`
this crate exposes several layout operators
which are generic over `tek_engine::Engine`.

View file

@ -89,28 +89,28 @@ impl Direction {
pub enum Bsp<E: Engine, X: Content<E>, Y: Content<E>> {
/// X is north of Y
N(Option<f64>, Option<X>, Option<Y>),
North(Option<f64>, Option<X>, Option<Y>),
/// X is south of Y
S(Option<f64>, Option<X>, Option<Y>),
South(Option<f64>, Option<X>, Option<Y>),
/// X is east of Y
E(Option<f64>, Option<X>, Option<Y>),
East(Option<f64>, Option<X>, Option<Y>),
/// X is west of Y
W(Option<f64>, Option<X>, Option<Y>),
West(Option<f64>, Option<X>, Option<Y>),
/// X is above Y
A(Option<X>, Option<Y>),
Above(Option<X>, Option<Y>),
/// X is below Y
B(Option<X>, Option<Y>),
Below(Option<X>, Option<Y>),
/// Should be avoided.
Null(PhantomData<E>),
}
impl<E: Engine, X: Content<E>, Y: Content<E>> Bsp<E, X, Y> {
pub fn n (x: X, y: Y) -> Self { Self::N(None, Some(x), Some(y)) }
pub fn s (x: X, y: Y) -> Self { Self::S(None, Some(x), Some(y)) }
pub fn e (x: X, y: Y) -> Self { Self::E(None, Some(x), Some(y)) }
pub fn w (x: X, y: Y) -> Self { Self::W(None, Some(x), Some(y)) }
pub fn a (x: X, y: Y) -> Self { Self::A(Some(x), Some(y)) }
pub fn b (x: X, y: Y) -> Self { Self::B(Some(x), Some(y)) }
pub fn n (x: X, y: Y) -> Self { Self::North(None, Some(x), Some(y)) }
pub fn s (x: X, y: Y) -> Self { Self::South(None, Some(x), Some(y)) }
pub fn e (x: X, y: Y) -> Self { Self::East(None, Some(x), Some(y)) }
pub fn w (x: X, y: Y) -> Self { Self::West(None, Some(x), Some(y)) }
pub fn a (x: X, y: Y) -> Self { Self::Above(Some(x), Some(y)) }
pub fn b (x: X, y: Y) -> Self { Self::Below(Some(x), Some(y)) }
}
impl<E: Engine, X: Content<E>, Y: Content<E>> Default for Bsp<E, X, Y> {
@ -123,17 +123,27 @@ impl<E: Engine, X: Content<E>, Y: Content<E>> Content<E> for Bsp<E, X, Y> {
fn area (&self, outer: E::Area) -> E::Area {
match self {
Self::Null(_) => [0.into(), 0.into(), 0.into(), 0.into()].into(),
Self::N(_, a, b) | Self::S(_, a, b) => {
Self::North(_, a, b) => {
let a = a.area(outer);
let b = b.area(outer);
let b = b.area(North.split_fixed(outer, a.y() + a.h()).1.into());
[a.x().min(b.x()), a.y().min(b.y()), a.w().max(b.w()), a.h() + b.h()].into()
}
Self::South(_, a, b) => {
let a = a.area(outer);
let b = b.area(South.split_fixed(outer, a.y() + a.h()).1.into());
[a.x().min(b.x()), a.y().min(b.y()), a.w().max(b.w()), a.h() + b.h()].into()
},
Self::E(_, a, b) | Self::W(_, a, b) => {
Self::East(_, a, b) => {
let a = a.area(outer);
let b = b.area(outer);
let b = b.area(East.split_fixed(outer, a.x() + a.w()).1.into());
[a.x().min(b.x()), a.y().min(b.y()), a.w() + b.w(), a.h().max(b.h())].into()
},
Self::A(a, b) | Self::B(a, b) => {
Self::West(_, a, b) => {
let a = a.area(outer);
let b = b.area(West.split_fixed(outer, a.x() + a.w()).1.into());
[a.x().min(b.x()), a.y().min(b.y()), a.w() + b.w(), a.h().max(b.h())].into()
},
Self::Above(a, b) | Self::Below(a, b) => {
let a = a.area(outer);
let b = b.area(outer);
[a.x().min(b.x()), a.y().min(b.y()), a.w().max(b.w()), a.h().max(b.h())].into()
@ -141,38 +151,45 @@ impl<E: Engine, X: Content<E>, Y: Content<E>> Content<E> for Bsp<E, X, Y> {
}
}
fn render (&self, to: &mut E::Output) {
let n = E::Area::zero();
let area = to.area();
let area = to.area().clone();
match self {
Self::S(p, a, b) => {
let s_a = a.area(area);
let _ = b.area(area);
let h = s_a.h().into();
to.place(to.area().clip_h(h).into(), a);
to.place(to.area().shrink_y(h).push_y(h).into(), b);
Self::North(_, a, b) => {
let area_a = a.area(area);
let area_b = b.area(North.split_fixed(area, area_a.y() + area_a.h()).1.into());
to.place(area_a, a);
to.place(area_b, b);
},
Self::E(p, a, b) => {
let s_a = a.area(area);
let _ = b.area(area);
let w = s_a.w().into();
to.place(to.area().clip_w(w).into(), a);
to.place(to.area().push_x(w).shrink_x(w).into(), b);
Self::South(_, a, b) => {
let area_a = a.area(area).clone();
let area_b = b.area(South.split_fixed(area, area_a.y() + area_a.h()).1.into()).clone();
to.place(area_a, a);
to.place(area_b, b);
},
Self::W(p, a, b) => {
let s_a = a.area(area);
let _ = b.area(area);
let w = (to.area().w() - s_a.w()).into();
to.place(to.area().push_x(w).into(), a);
to.place(to.area().shrink_x(w).into(), b);
Self::East(_, a, b) => {
let area_a = a.area(area);
let area_b = b.area(East.split_fixed(area, area_a.x() + area_a.w()).1.into());
to.place(area_a, a);
to.place(area_b, b);
},
Self::N(p, a, b) => {
let s_a = a.area(area);
let _ = b.area(area);
let h = to.area().h() - s_a.h();
to.place(to.area().push_y(h).into(), a);
to.place(to.area().shrink_y(h).into(), b);
Self::West(_, a, b) => {
let area_a = a.area(area);
let area_b = b.area(West.split_fixed(area, area_a.x() + area_a.w()).1.into());
to.place(area_a, a);
to.place(area_b, b);
},
_ => todo!()
Self::Above(a, b) => {
let area_a = a.area(area);
let area_b = b.area(area);
to.place(area_b, b);
to.place(area_a, a);
},
Self::Below(a, b) => {
let area_a = a.area(area);
let area_b = b.area(area);
to.place(area_a, a);
to.place(area_b, b);
},
Self::Null(_) => {}
}
}
}

View file

@ -1,5 +1,6 @@
use crate::*;
use std::sync::{Arc, atomic::{AtomicUsize, Ordering::Relaxed}};
use ratatui::prelude::{Style, Color};
// TODO: 🡘 🡙 ←🡙→ indicator to expand window when too small
@ -79,3 +80,43 @@ impl<E: Engine> Measure<E> {
//}
//impl<E: Engine> ContentDebug<E> for E {}
impl Render<Tui> for Measure<Tui> {
fn min_size (&self, _: [u16;2]) -> Perhaps<[u16;2]> {
Ok(Some([0u16.into(), 0u16.into()].into()))
}
fn render (&self, to: &mut TuiOutput) -> Usually<()> {
self.set_w(to.area().w());
self.set_h(to.area().h());
Ok(())
}
}
impl Measure<Tui> {
pub fn debug (&self) -> ShowMeasure {
ShowMeasure(&self)
}
}
render!(<Tui>|self: ShowMeasure<'a>|render(|to: &mut TuiOutput|Ok({
let w = self.0.w();
let h = self.0.h();
to.blit(&format!(" {w} x {h} "), to.area.x(), to.area.y(), Some(
Style::default().bold().italic().bg(Color::Rgb(255, 0, 255)).fg(Color::Rgb(0,0,0))
))
})));
pub struct ShowMeasure<'a>(&'a Measure<Tui>);
pub struct DebugOverlay<E: Engine, W: Render<E>>(PhantomData<E>, pub W);
impl<T: Render<Tui>> Render<Tui> for DebugOverlay<Tui, T> {
fn min_size (&self, to: [u16;2]) -> Perhaps<[u16;2]> {
self.1.min_size(to)
}
fn render (&self, to: &mut TuiOutput) -> Usually<()> {
let [x, y, w, h] = to.area();
self.1.render(to)?;
Ok(to.blit(&format!("{w}x{h}+{x}+{y}"), x, y, Some(Style::default().green())))
}
}

View file

@ -92,46 +92,63 @@ macro_rules! transform_xy_unit {
}
}
transform_xy_unit!(|self: Fixed, to|match self {
Self::X(fw, _) => [to.x(), to.y(), *fw, to.h()],
Self::Y(fh, _) => [to.x(), to.y(), to.w(), *fh],
Self::XY(fw, fh, _) => [to.x(), to.y(), *fw, *fh], // tagn
transform_xy_unit!(|self: Fixed, area|{
let area = self.content().area(area);
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
}
});
transform_xy_unit!(|self: Shrink, to|
[to.x(), to.y(), to.w().minus(self.dx()), to.h().minus(self.dy())]);
transform_xy_unit!(|self: Expand, to|
[to.x(), to.y(), to.w() + self.dx(), to.h() + self.dy()]);
transform_xy_unit!(|self: Min, to|match self {
Self::X(mw, _) => [to.x(), to.y(), to.w().max(*mw), to.h()],
Self::Y(mh, _) => [to.x(), to.y(), to.w(), to.h().max(*mh)],
Self::XY(mw, mh, _) => [to.x(), to.y(), to.w().max(*mw), to.h().max(*mh)]
transform_xy_unit!(|self: Shrink, area|{
let area = self.content().area(area);
[area.x(), area.y(), area.w().minus(self.dx()), area.h().minus(self.dy())]
});
transform_xy_unit!(|self: Max, to|match self {
Self::X(mw, _) => [to.x(), to.y(), to.w().min(*mw), to.h()],
Self::Y(mh, _) => [to.x(), to.y(), to.w(), to.h().min(*mh)],
Self::XY(mw, mh, _) => [to.x(), to.y(), to.w().min(*mw), to.h().min(*mh)],
transform_xy_unit!(|self: Expand, area|{
let area = self.content().area(area);
[area.x(), area.y(), area.w() + self.dx(), area.h() + self.dy()]
});
transform_xy_unit!(|self: Push, to|
[to.x() + self.dx(), to.y() + self.dy(), to.w(), to.h()]);
transform_xy_unit!(|self: Min, area|{
let area = self.content().area(area);
match self {
Self::X(mw, _) => [area.x(), area.y(), area.w().max(*mw), area.h()],
Self::Y(mh, _) => [area.x(), area.y(), area.w(), area.h().max(*mh)],
Self::XY(mw, mh, _) => [area.x(), area.y(), area.w().max(*mw), area.h().max(*mh)]
}});
transform_xy_unit!(|self: Pull, to|
[to.x().minus(self.dx()), to.y().minus(self.dy()), to.w(), to.h()]);
transform_xy_unit!(|self: Max, area|{
let area = self.content().area(area);
match self {
Self::X(mw, _) => [area.x(), area.y(), area.w().min(*mw), area.h()],
Self::Y(mh, _) => [area.x(), area.y(), area.w(), area.h().min(*mh)],
Self::XY(mw, mh, _) => [area.x(), area.y(), area.w().min(*mw), area.h().min(*mh)],
}});
transform_xy_unit!(|self: Margin, to|{
transform_xy_unit!(|self: Push, area|{
let area = self.content().area(area);
[area.x() + self.dx(), area.y() + self.dy(), area.w(), area.h()]
});
transform_xy_unit!(|self: Pull, area|{
let area = self.content().area(area);
[area.x().minus(self.dx()), area.y().minus(self.dy()), area.w(), area.h()]
});
transform_xy_unit!(|self: Margin, area|{
let area = self.content().area(area);
let dx = self.dx();
let dy = self.dy();
[to.x().minus(dx), to.y().minus(dy), to.w() + dy + dy, to.h() + dy + dy]
[area.x().minus(dx), area.y().minus(dy), area.w() + dy + dy, area.h() + dy + dy]
});
transform_xy_unit!(|self: Padding, to|{
transform_xy_unit!(|self: Padding, area|{
let area = self.content().area(area);
let dx = self.dx();
let dy = self.dy();
[to.x() + dx, to.y() + dy, to.w().minus(dy + dy), to.h().minus(dy + dy), ]
[area.x() + dx, area.y() + dy, area.w().minus(dy + dy), area.h().minus(dy + dy), ]
});
content_enum!(Align: Center, X, Y, NW, N, NE, E, SE, S, SW, W);
@ -173,10 +190,26 @@ fn align<E: Engine, T: Content<E>, N: Coordinate, R: Area<N> + From<[N;4]>> (ali
}
impl<E: Engine, T: Content<E>> Content<E> for Align<E, T> {
fn render (&self, to: &mut E::Output) {
let outer = to.area();
fn area (&self, outer: E::Area) -> E::Area {
let inner = Content::area(&self.content(), outer);
let (oc, ic) = (outer.center(), inner.center());
match self {
Self::Center(_) => [0, 0, 0, 0],
Self::X(_) => [0, 0, 0, 0],
Self::Y(_) => [0, 0, 0, 0],
_ => [0, 0, 0, 0]
}.into()
}
fn render (&self, render: &mut E::Output) {
let outer = render.area();
let content = self.content();
let inner = Content::area(&content, outer);
let aligned = match self {
Self::Center(_) => {
let oc = outer.center();
let ic = inner.center();
}
}
if let Some(aligned) = align(&self, outer.into(), inner.into()) {
to.place(aligned, &content)
}

View file

@ -1,42 +0,0 @@
use crate::*;
use ratatui::prelude::{Style, Color};
impl Render<Tui> for Measure<Tui> {
fn min_size (&self, _: [u16;2]) -> Perhaps<[u16;2]> {
Ok(Some([0u16.into(), 0u16.into()].into()))
}
fn render (&self, to: &mut TuiOutput) -> Usually<()> {
self.set_w(to.area().w());
self.set_h(to.area().h());
Ok(())
}
}
impl Measure<Tui> {
pub fn debug (&self) -> ShowMeasure {
ShowMeasure(&self)
}
}
render!(<Tui>|self: ShowMeasure<'a>|render(|to: &mut TuiOutput|Ok({
let w = self.0.w();
let h = self.0.h();
to.blit(&format!(" {w} x {h} "), to.area.x(), to.area.y(), Some(
Style::default().bold().italic().bg(Color::Rgb(255, 0, 255)).fg(Color::Rgb(0,0,0))
))
})));
pub struct ShowMeasure<'a>(&'a Measure<Tui>);
pub struct DebugOverlay<E: Engine, W: Render<E>>(PhantomData<E>, pub W);
impl<T: Render<Tui>> Render<Tui> for DebugOverlay<Tui, T> {
fn min_size (&self, to: [u16;2]) -> Perhaps<[u16;2]> {
self.1.min_size(to)
}
fn render (&self, to: &mut TuiOutput) -> Usually<()> {
let [x, y, w, h] = to.area();
self.1.render(to)?;
Ok(to.blit(&format!("{w}x{h}+{x}+{y}"), x, y, Some(Style::default().green())))
}
}