still dark; refactor and document layout crate

This commit is contained in:
🪞👃🪞 2024-12-31 19:57:03 +01:00
parent ed72ab1635
commit 675d376100
12 changed files with 510 additions and 659 deletions

76
layout/src/align.rs Normal file
View file

@ -0,0 +1,76 @@
use crate::*;
#[derive(Default, Debug, Copy, Clone)]
pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
pub struct Align<E: Engine, T: Content<E>>(Alignment, T, PhantomData<E>);
impl<E: Engine, T: Content<E>> Align<E, T> {
pub fn c (w: T) -> Self { Self(Alignment::Center, w, Default::default()) }
pub fn x (w: T) -> Self { Self(Alignment::X, w, Default::default()) }
pub fn y (w: T) -> Self { Self(Alignment::Y, w, Default::default()) }
pub fn n (w: T) -> Self { Self(Alignment::N, w, Default::default()) }
pub fn s (w: T) -> Self { Self(Alignment::S, w, Default::default()) }
pub fn e (w: T) -> Self { Self(Alignment::E, w, Default::default()) }
pub fn w (w: T) -> Self { Self(Alignment::W, w, Default::default()) }
pub fn nw (w: T) -> Self { Self(Alignment::NW, w, Default::default()) }
pub fn sw (w: T) -> Self { Self(Alignment::SW, w, Default::default()) }
pub fn ne (w: T) -> Self { Self(Alignment::NE, w, Default::default()) }
pub fn se (w: T) -> Self { Self(Alignment::SE, w, Default::default()) }
}
impl<E: Engine, T: Content<E>> Content<E> for Align<E, T> {
fn area (&self, outer: E::Area) -> E::Area {
align_areas(self.0, outer.xywh(), Content::area(&self.content(), outer).xywh()).into()
}
fn render (&self, render: &mut E::Output) {
render.place(self.area(render.area()), &self.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!()
//})
//}
//}