mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 04:06:45 +01:00
'and then not have to worry about layout, ever'
This commit is contained in:
parent
21741ebc52
commit
f7e6449324
8 changed files with 74 additions and 74 deletions
|
|
@ -1,7 +1,8 @@
|
|||
# `tek_layout`
|
||||
|
||||
this crate exposes several layout operators
|
||||
which are generic over `tek_engine::Engine`.
|
||||
which work entirely in unsigned coordinates
|
||||
and are generic over `tek_engine::Engine`.
|
||||
|
||||
* `Fill` makes the content's dimension equal to the container's.
|
||||
* `Fixed` assigns a fixed dimension to its content.
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ impl<E: Engine, T: Content<E>> Align<E, T> {
|
|||
}
|
||||
|
||||
impl<E: Engine, T: Content<E>> Content<E> for Align<E, T> {
|
||||
fn area (&self, outer: E::Area) -> E::Area {
|
||||
fn layout (&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) {
|
||||
|
|
@ -36,20 +36,22 @@ pub fn align_areas<N: Coordinate>(alignment: Alignment, on: [N;4], it: [N;4]) ->
|
|||
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()],
|
||||
let [x, y] = match alignment {
|
||||
Alignment::Center => [center_x, center_y,],
|
||||
|
||||
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()],
|
||||
}
|
||||
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> {
|
||||
|
|
|
|||
|
|
@ -69,32 +69,32 @@ impl<E: Engine, X: Content<E>, Y: Content<E>> Default for Bsp<E, X, Y> {
|
|||
}
|
||||
|
||||
impl<E: Engine, X: Content<E>, Y: Content<E>> Content<E> for Bsp<E, X, Y> {
|
||||
fn area (&self, outer: E::Area) -> E::Area {
|
||||
fn layout (&self, outer: E::Area) -> E::Area {
|
||||
match self {
|
||||
Self::Null(_) => [0.into(), 0.into(), 0.into(), 0.into()].into(),
|
||||
Self::North(_, a, b) => {
|
||||
let a = a.area(outer);
|
||||
let b = b.area(North.split_fixed(outer, a.y() + a.h()).1.into());
|
||||
let a = a.layout(outer);
|
||||
let b = b.layout(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());
|
||||
let a = a.layout(outer);
|
||||
let b = b.layout(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::East(_, a, b) => {
|
||||
let a = a.area(outer);
|
||||
let b = b.area(East.split_fixed(outer, a.x() + a.w()).1.into());
|
||||
let a = a.layout(outer);
|
||||
let b = b.layout(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::West(_, a, b) => {
|
||||
let a = a.area(outer);
|
||||
let b = b.area(West.split_fixed(outer, a.x() + a.w()).1.into());
|
||||
let a = a.layout(outer);
|
||||
let b = b.layout(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);
|
||||
let a = a.layout(outer);
|
||||
let b = b.layout(outer);
|
||||
[a.x().min(b.x()), a.y().min(b.y()), a.w().max(b.w()), a.h().max(b.h())].into()
|
||||
}
|
||||
}
|
||||
|
|
@ -103,38 +103,38 @@ impl<E: Engine, X: Content<E>, Y: Content<E>> Content<E> for Bsp<E, X, Y> {
|
|||
let area = to.area().clone();
|
||||
match self {
|
||||
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());
|
||||
let area_a = a.layout(area);
|
||||
let area_b = b.layout(North.split_fixed(area, area_a.y() + area_a.h()).1.into());
|
||||
to.place(area_a, a);
|
||||
to.place(area_b, 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();
|
||||
let area_a = a.layout(area).clone();
|
||||
let area_b = b.layout(South.split_fixed(area, area_a.y() + area_a.h()).1.into()).clone();
|
||||
to.place(area_a, a);
|
||||
to.place(area_b, 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());
|
||||
let area_a = a.layout(area);
|
||||
let area_b = b.layout(East.split_fixed(area, area_a.x() + area_a.w()).1.into());
|
||||
to.place(area_a, a);
|
||||
to.place(area_b, 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());
|
||||
let area_a = a.layout(area);
|
||||
let area_b = b.layout(West.split_fixed(area, area_a.x() + area_a.w()).1.into());
|
||||
to.place(area_a, a);
|
||||
to.place(area_b, b);
|
||||
},
|
||||
Self::Above(a, b) => {
|
||||
let area_a = a.area(area);
|
||||
let area_b = b.area(area);
|
||||
let area_a = a.layout(area);
|
||||
let area_b = b.layout(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);
|
||||
let area_a = a.layout(area);
|
||||
let area_b = b.layout(area);
|
||||
to.place(area_a, a);
|
||||
to.place(area_b, b);
|
||||
},
|
||||
|
|
|
|||
|
|
@ -45,11 +45,11 @@ pub struct Opt<E: Engine, A, F: Fn(A)->R, R: Content<E>>(Option<A>, F, PhantomDa
|
|||
pub struct When<E: Engine, A>(bool, A, PhantomData<E>);
|
||||
|
||||
impl<E: Engine, A: Content<E>> Content<E> for When<E, A> {
|
||||
fn area (&self, to: E::Area) -> E::Area {
|
||||
fn layout (&self, to: E::Area) -> E::Area {
|
||||
let Self(cond, item, ..) = self;
|
||||
let mut area = E::Area::zero();
|
||||
if *cond {
|
||||
let item_area = item.area(to);
|
||||
let item_area = item.layout(to);
|
||||
area[0] = item_area.x();
|
||||
area[1] = item_area.y();
|
||||
area[2] = item_area.w();
|
||||
|
|
@ -67,9 +67,9 @@ impl<E: Engine, A: Content<E>> Content<E> for When<E, A> {
|
|||
pub struct Either<E: Engine, A, B>(bool, A, B, PhantomData<E>);
|
||||
|
||||
impl<E: Engine, A: Content<E>, B: Content<E>> Content<E> for Either<E, A, B> {
|
||||
fn area (&self, to: E::Area) -> E::Area {
|
||||
fn layout (&self, to: E::Area) -> E::Area {
|
||||
let Self(cond, a, b, ..) = self;
|
||||
if *cond { a.area(to) } else { b.area(to) }
|
||||
if *cond { a.layout(to) } else { b.layout(to) }
|
||||
}
|
||||
fn render (&self, to: &mut E::Output) {
|
||||
let Self(cond, a, b, ..) = self;
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ macro_rules! transform_xy {
|
|||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
fn area (&$self, $to: <E as Engine>::Area) -> <E as Engine>::Area {
|
||||
fn layout (&$self, $to: <E as Engine>::Area) -> <E as Engine>::Area {
|
||||
$area
|
||||
}
|
||||
}
|
||||
|
|
@ -31,7 +31,7 @@ macro_rules! transform_xy {
|
|||
|
||||
transform_xy!(self: Fill |to|{
|
||||
let [x0, y0, wmax, hmax] = to.xywh();
|
||||
let [x, y, w, h] = Content::area(&self.content(), to).xywh();
|
||||
let [x, y, w, h] = Content::layout(&self.content(), to).xywh();
|
||||
return match self {
|
||||
Self::X(_) => [x0, y, wmax, h],
|
||||
Self::Y(_) => [x, y0, w, hmax],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue