mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 04:06:45 +01:00
rewrite align code. still dark
This commit is contained in:
parent
62ce1776c0
commit
ed72ab1635
2 changed files with 100 additions and 90 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
use std::sync::{Arc, atomic::{AtomicUsize, Ordering::Relaxed}};
|
use std::sync::{Arc, atomic::{AtomicUsize, Ordering::Relaxed}};
|
||||||
use ratatui::prelude::{Style, Color};
|
//use ratatui::prelude::{Style, Color};
|
||||||
|
|
||||||
// TODO: 🡘 🡙 ←🡙→ indicator to expand window when too small
|
// TODO: 🡘 🡙 ←🡙→ indicator to expand window when too small
|
||||||
|
|
||||||
|
|
@ -81,42 +81,42 @@ impl<E: Engine> Measure<E> {
|
||||||
|
|
||||||
//impl<E: Engine> ContentDebug<E> for E {}
|
//impl<E: Engine> ContentDebug<E> for E {}
|
||||||
|
|
||||||
impl Render<Tui> for Measure<Tui> {
|
//impl Render<Tui> for Measure<Tui> {
|
||||||
fn min_size (&self, _: [u16;2]) -> Perhaps<[u16;2]> {
|
//fn min_size (&self, _: [u16;2]) -> Perhaps<[u16;2]> {
|
||||||
Ok(Some([0u16.into(), 0u16.into()].into()))
|
//Ok(Some([0u16.into(), 0u16.into()].into()))
|
||||||
}
|
//}
|
||||||
fn render (&self, to: &mut TuiOutput) -> Usually<()> {
|
//fn render (&self, to: &mut TuiOutput) -> Usually<()> {
|
||||||
self.set_w(to.area().w());
|
//self.set_w(to.area().w());
|
||||||
self.set_h(to.area().h());
|
//self.set_h(to.area().h());
|
||||||
Ok(())
|
//Ok(())
|
||||||
}
|
//}
|
||||||
}
|
//}
|
||||||
|
|
||||||
impl Measure<Tui> {
|
//impl Measure<Tui> {
|
||||||
pub fn debug (&self) -> ShowMeasure {
|
//pub fn debug (&self) -> ShowMeasure {
|
||||||
ShowMeasure(&self)
|
//ShowMeasure(&self)
|
||||||
}
|
//}
|
||||||
}
|
//}
|
||||||
|
|
||||||
render!(<Tui>|self: ShowMeasure<'a>|render(|to: &mut TuiOutput|Ok({
|
//render!(<Tui>|self: ShowMeasure<'a>|render(|to: &mut TuiOutput|Ok({
|
||||||
let w = self.0.w();
|
//let w = self.0.w();
|
||||||
let h = self.0.h();
|
//let h = self.0.h();
|
||||||
to.blit(&format!(" {w} x {h} "), to.area.x(), to.area.y(), Some(
|
//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))
|
//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 ShowMeasure<'a>(&'a Measure<Tui>);
|
||||||
|
|
||||||
pub struct DebugOverlay<E: Engine, W: Render<E>>(PhantomData<E>, pub W);
|
//pub struct DebugOverlay<E: Engine, W: Render<E>>(PhantomData<E>, pub W);
|
||||||
|
|
||||||
impl<T: Render<Tui>> Render<Tui> for DebugOverlay<Tui, T> {
|
//impl<T: Render<Tui>> Render<Tui> for DebugOverlay<Tui, T> {
|
||||||
fn min_size (&self, to: [u16;2]) -> Perhaps<[u16;2]> {
|
//fn min_size (&self, to: [u16;2]) -> Perhaps<[u16;2]> {
|
||||||
self.1.min_size(to)
|
//self.1.min_size(to)
|
||||||
}
|
//}
|
||||||
fn render (&self, to: &mut TuiOutput) -> Usually<()> {
|
//fn render (&self, to: &mut TuiOutput) -> Usually<()> {
|
||||||
let [x, y, w, h] = to.area();
|
//let [x, y, w, h] = to.area();
|
||||||
self.1.render(to)?;
|
//self.1.render(to)?;
|
||||||
Ok(to.blit(&format!("{w}x{h}+{x}+{y}"), x, y, Some(Style::default().green())))
|
//Ok(to.blit(&format!("{w}x{h}+{x}+{y}"), x, y, Some(Style::default().green())))
|
||||||
}
|
//}
|
||||||
}
|
//}
|
||||||
|
|
|
||||||
|
|
@ -151,67 +151,77 @@ transform_xy_unit!(|self: Padding, area|{
|
||||||
[area.x() + dx, area.y() + dy, area.w().minus(dy + dy), area.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);
|
#[derive(Default, Debug, Copy, Clone)]
|
||||||
impl<E: Engine, T: Content<E>> Align<E, T> {
|
pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
|
||||||
pub fn c (w: T) -> Self { Self::Center(w) }
|
|
||||||
pub fn x (w: T) -> Self { Self::X(w) }
|
|
||||||
pub fn y (w: T) -> Self { Self::Y(w) }
|
|
||||||
pub fn n (w: T) -> Self { Self::N(w) }
|
|
||||||
pub fn s (w: T) -> Self { Self::S(w) }
|
|
||||||
pub fn e (w: T) -> Self { Self::E(w) }
|
|
||||||
pub fn w (w: T) -> Self { Self::W(w) }
|
|
||||||
pub fn nw (w: T) -> Self { Self::NW(w) }
|
|
||||||
pub fn sw (w: T) -> Self { Self::SW(w) }
|
|
||||||
pub fn ne (w: T) -> Self { Self::NE(w) }
|
|
||||||
pub fn se (w: T) -> Self { Self::SE(w) }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn align<E: Engine, T: Content<E>, N: Coordinate, R: Area<N> + From<[N;4]>> (align: &Align<E, T>, outer: R, content: R) -> Option<R> {
|
pub struct Align<E: Engine, T: Content<E>>(Alignment, T, PhantomData<E>);
|
||||||
if outer.w() < content.w() || outer.h() < content.h() {
|
|
||||||
None
|
impl<E: Engine, T: Content<E>> Align<E, T> {
|
||||||
} else {
|
pub fn c (w: T) -> Self { Self(Alignment::Center, w, Default::default()) }
|
||||||
let [ox, oy, ow, oh] = outer.xywh();
|
pub fn x (w: T) -> Self { Self(Alignment::X, w, Default::default()) }
|
||||||
let [ix, iy, iw, ih] = content.xywh();
|
pub fn y (w: T) -> Self { Self(Alignment::Y, w, Default::default()) }
|
||||||
Some(match align {
|
pub fn n (w: T) -> Self { Self(Alignment::N, w, Default::default()) }
|
||||||
Align::Center(_) => [ox + (ow - iw) / 2.into(), oy + (oh - ih) / 2.into(), iw, ih,].into(),
|
pub fn s (w: T) -> Self { Self(Alignment::S, w, Default::default()) }
|
||||||
Align::X(_) => [ox + (ow - iw) / 2.into(), iy, iw, ih,].into(),
|
pub fn e (w: T) -> Self { Self(Alignment::E, w, Default::default()) }
|
||||||
Align::Y(_) => [ix, oy + (oh - ih) / 2.into(), iw, ih,].into(),
|
pub fn w (w: T) -> Self { Self(Alignment::W, w, Default::default()) }
|
||||||
Align::NW(_) => [ox, oy, iw, ih,].into(),
|
pub fn nw (w: T) -> Self { Self(Alignment::NW, w, Default::default()) }
|
||||||
Align::N(_) => [ox + (ow - iw) / 2.into(), oy, iw, ih,].into(),
|
pub fn sw (w: T) -> Self { Self(Alignment::SW, w, Default::default()) }
|
||||||
Align::NE(_) => [ox + ow - iw, oy, iw, ih,].into(),
|
pub fn ne (w: T) -> Self { Self(Alignment::NE, w, Default::default()) }
|
||||||
Align::W(_) => [ox, oy + (oh - ih) / 2.into(), iw, ih,].into(),
|
pub fn se (w: T) -> Self { Self(Alignment::SE, w, Default::default()) }
|
||||||
Align::E(_) => [ox + ow - iw, oy + (oh - ih) / 2.into(), iw, ih,].into(),
|
|
||||||
Align::SW(_) => [ox, oy + oh - ih, iw, ih,].into(),
|
|
||||||
Align::S(_) => [ox + (ow - iw) / 2.into(), oy + oh - ih, iw, ih,].into(),
|
|
||||||
Align::SE(_) => [ox + ow - iw, oy + oh - ih, iw, ih,].into(),
|
|
||||||
_ => unreachable!()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Engine, T: Content<E>> Content<E> for Align<E, T> {
|
impl<E: Engine, T: Content<E>> Content<E> for Align<E, T> {
|
||||||
fn area (&self, outer: E::Area) -> E::Area {
|
fn area (&self, outer: E::Area) -> E::Area {
|
||||||
let inner = Content::area(&self.content(), outer);
|
align_areas(self.0, outer.xywh(), Content::area(&self.content(), outer).xywh()).into()
|
||||||
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) {
|
fn render (&self, render: &mut E::Output) {
|
||||||
let outer = render.area();
|
render.place(self.area(render.area()), &self.content())
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn align_areas<N: Coordinate>(alignment: Alignment, on: [N;4], it: [N;4]) -> [N;4] {
|
||||||
|
let [cfx, cfy] = on.center();
|
||||||
|
let [cmx, cmy] = it.center();
|
||||||
|
let center = |cf, cm, m: N|if cf >= cm { m + (cf - cm) } else { m.minus(cm - cf) };
|
||||||
|
let center_x = center(cfx, cmx, it.x());
|
||||||
|
let center_y = center(cfy, cmy, it.y());
|
||||||
|
let east_x = on.x() + on.w().minus(it.w());
|
||||||
|
let south_y = on.y() + on.h().minus(it.h());
|
||||||
|
match alignment {
|
||||||
|
Alignment::Center => [center_x, center_y, it.w(), it.h()],
|
||||||
|
Alignment::X => [center_x, it.y(), it.w(), it.h()],
|
||||||
|
Alignment::Y => [it.x(), center_y, it.w(), it.h()],
|
||||||
|
|
||||||
|
Alignment::NW => [on.x(), on.y(), it.w(), it.h()],
|
||||||
|
Alignment::N => [center_x, on.y(), it.w(), it.h()],
|
||||||
|
Alignment::NE => [east_x, on.y(), it.w(), it.h()],
|
||||||
|
Alignment::E => [east_x, center_y, it.w(), it.h()],
|
||||||
|
Alignment::SE => [east_x, south_y, it.w(), it.h()],
|
||||||
|
Alignment::S => [center_x, south_y, it.w(), it.h()],
|
||||||
|
Alignment::SW => [on.x(), south_y, it.w(), it.h()],
|
||||||
|
Alignment::W => [on.x(), center_y, it.w(), it.h()],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//fn align<E: Engine, T: Content<E>, N: Coordinate, R: Area<N> + From<[N;4]>> (align: &Align<E, T>, outer: R, content: R) -> Option<R> {
|
||||||
|
//if outer.w() < content.w() || outer.h() < content.h() {
|
||||||
|
//None
|
||||||
|
//} else {
|
||||||
|
//let [ox, oy, ow, oh] = outer.xywh();
|
||||||
|
//let [ix, iy, iw, ih] = content.xywh();
|
||||||
|
//Some(match align {
|
||||||
|
//Align::Center(_) => [ox + (ow - iw) / 2.into(), oy + (oh - ih) / 2.into(), iw, ih,].into(),
|
||||||
|
//Align::X(_) => [ox + (ow - iw) / 2.into(), iy, iw, ih,].into(),
|
||||||
|
//Align::Y(_) => [ix, oy + (oh - ih) / 2.into(), iw, ih,].into(),
|
||||||
|
//Align::NW(_) => [ox, oy, iw, ih,].into(),
|
||||||
|
//Align::N(_) => [ox + (ow - iw) / 2.into(), oy, iw, ih,].into(),
|
||||||
|
//Align::NE(_) => [ox + ow - iw, oy, iw, ih,].into(),
|
||||||
|
//Align::W(_) => [ox, oy + (oh - ih) / 2.into(), iw, ih,].into(),
|
||||||
|
//Align::E(_) => [ox + ow - iw, oy + (oh - ih) / 2.into(), iw, ih,].into(),
|
||||||
|
//Align::SW(_) => [ox, oy + oh - ih, iw, ih,].into(),
|
||||||
|
//Align::S(_) => [ox + (ow - iw) / 2.into(), oy + oh - ih, iw, ih,].into(),
|
||||||
|
//Align::SE(_) => [ox + ow - iw, oy + oh - ih, iw, ih,].into(),
|
||||||
|
//_ => unreachable!()
|
||||||
|
//})
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue