fix and test alignments

This commit is contained in:
🪞👃🪞 2025-01-01 21:04:39 +01:00
parent 9125e04e07
commit 3c14456566
8 changed files with 135 additions and 100 deletions

View file

@ -1,59 +1,77 @@
use crate::*;
#[derive(Default, Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, Default)]
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()) }
pub fn c (a: T) -> Self { Self(Alignment::Center, a, Default::default()) }
pub fn x (a: T) -> Self { Self(Alignment::X, a, Default::default()) }
pub fn y (a: T) -> Self { Self(Alignment::Y, a, Default::default()) }
pub fn n (a: T) -> Self { Self(Alignment::N, a, Default::default()) }
pub fn s (a: T) -> Self { Self(Alignment::S, a, Default::default()) }
pub fn e (a: T) -> Self { Self(Alignment::E, a, Default::default()) }
pub fn w (a: T) -> Self { Self(Alignment::W, a, Default::default()) }
pub fn nw (a: T) -> Self { Self(Alignment::NW, a, Default::default()) }
pub fn sw (a: T) -> Self { Self(Alignment::SW, a, Default::default()) }
pub fn ne (a: T) -> Self { Self(Alignment::NE, a, Default::default()) }
pub fn se (a: T) -> Self { Self(Alignment::SE, a, Default::default()) }
}
impl<E: Engine, T: Content<E>> Content<E> for Align<E, T> {
fn layout (&self, outer: E::Area) -> E::Area {
align_areas(self.0, outer.xywh(), Content::layout(&self.content(), outer).xywh()).into()
fn content (&self) -> impl Content<E> {
&self.1
}
fn layout (&self, on: E::Area) -> E::Area {
use Alignment::*;
let it = self.content().layout(on).xywh();
let centered = on.center_xy(it.wh());
let far_x = (on.x() + on.w()).minus(it.w());
let far_y = (on.y() + on.h()).minus(it.h());
let [x, y] = match self.0 {
NW => [on.x(), on.y()],
N => [centered.x(), on.y()],
NE => [far_x, on.y()],
E => [far_x, centered.y()],
SE => [far_x, far_y ],
S => [centered.x(), far_y ],
SW => [on.x(), far_y ],
W => [on.x(), centered.y()],
Center => centered.xy(),
X => [centered.x(), it.y()],
Y => [it.x(), centered.y()],
};
[x, y, centered.w(), centered.h()].into()
//let [cfx, cfy, ..] = on.center();
//let [cmx, cmy, ..] = it.center();
////let center = |cf, cm, m|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());
//let [x, y] = match self.0 {
//Alignment::X => [center_x, it.y(), ],
//Alignment::Y => [it.x(), center_y,],
//Alignment::NW => [on.x(), on.y(), ],
//Alignment::N => [center_x, on.y(), ],
//Alignment::NE => [east_x, on.y(), ],
//Alignment::E => [east_x, center_y,],
//Alignment::SE => [east_x, south_y, ],
//Alignment::S => [center_x, south_y, ],
//Alignment::SW => [on.x(), south_y, ],
//Alignment::W => [on.x(), center_y,],
//};
//[x, y, it.w(), it.h()].into()
}
fn render (&self, render: &mut E::Output) {
render.place(self.layout(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());
let [x, y] = match alignment {
Alignment::Center => [center_x, center_y,],
Alignment::X => [center_x, it.y(), ],
Alignment::Y => [it.x(), center_y,],
Alignment::NW => [on.x(), on.y(), ],
Alignment::N => [center_x, on.y(), ],
Alignment::NE => [east_x, on.y(), ],
Alignment::E => [east_x, center_y,],
Alignment::SE => [east_x, south_y, ],
Alignment::S => [center_x, south_y, ],
Alignment::SW => [on.x(), south_y, ],
Alignment::W => [on.x(), center_y,],
};
[x, 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