mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
refactor engine and layout into input and output
This commit is contained in:
parent
f052891473
commit
4d0f98acd2
40 changed files with 104 additions and 109 deletions
96
output/src/transform_xy_unit.rs
Normal file
96
output/src/transform_xy_unit.rs
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
use crate::*;
|
||||
|
||||
/// Defines an enum that parametrically transforms its content
|
||||
/// along either the X axis, the Y axis, or both.
|
||||
macro_rules! transform_xy_unit {
|
||||
(|$self:ident : $Enum:ident, $to:ident|$layout:expr) => {
|
||||
pub enum $Enum<U, T> { X(U, T), Y(U, T), XY(U, U, T), }
|
||||
impl<U, T> $Enum<U, T> {
|
||||
pub fn x (x: U, item: T) -> Self { Self::X(x, item) }
|
||||
pub fn y (y: U, item: T) -> Self { Self::Y(y, item) }
|
||||
pub fn xy (x: U, y: U, item: T) -> Self { Self::XY(x, y, item) }
|
||||
}
|
||||
impl<U: Copy + Coordinate, T> $Enum<U, T> {
|
||||
pub fn dx (&self) -> U {
|
||||
match self {
|
||||
Self::X(x, _) => *x,
|
||||
Self::Y(_, _) => 0.into(),
|
||||
Self::XY(x, _, _) => *x,
|
||||
}
|
||||
}
|
||||
pub fn dy (&self) -> U {
|
||||
match self {
|
||||
Self::X(_, _) => 0.into(),
|
||||
Self::Y(y, _) => *y,
|
||||
Self::XY(_, y, _) => *y,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<E: Output, T: Content<E>> Content<E> for $Enum<E::Unit, T> {
|
||||
fn content (&self) -> impl Render<E> {
|
||||
Some(match self {
|
||||
Self::X(_, content) => content,
|
||||
Self::Y(_, content) => content,
|
||||
Self::XY(_, _, content) => content,
|
||||
})
|
||||
}
|
||||
fn layout (&$self, $to: E::Area) -> E::Area {
|
||||
$layout.into()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
transform_xy_unit!(|self: Fixed, area|{
|
||||
let [w, h] = Render::layout(&self.content(), area.center_xy(match self {
|
||||
Self::X(fw, _) => [*fw, area.h()],
|
||||
Self::Y(fh, _) => [area.w(), *fh],
|
||||
Self::XY(fw, fh, _) => [*fw, *fh],
|
||||
}).into()).wh();
|
||||
area.center_xy(match self {
|
||||
Self::X(fw, _) => [*fw, h],
|
||||
Self::Y(fh, _) => [w, *fh],
|
||||
Self::XY(fw, fh, _) => [*fw, *fh],
|
||||
})
|
||||
});
|
||||
|
||||
transform_xy_unit!(|self: Shrink, area|Render::layout(&self.content(), [
|
||||
area.x(), area.y(), area.w().minus(self.dx()), area.h().minus(self.dy())
|
||||
].into()));
|
||||
transform_xy_unit!(|self: Expand, area|Render::layout(&self.content(), [
|
||||
area.x(), area.y(), area.w() + self.dx(), area.h() + self.dy()
|
||||
].into()));
|
||||
transform_xy_unit!(|self: Min, area|{
|
||||
let area = Render::layout(&self.content(), 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: Max, area|{
|
||||
let area = Render::layout(&self.content(), 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: Push, area|{
|
||||
let area = Render::layout(&self.content(), area);
|
||||
[area.x() + self.dx(), area.y() + self.dy(), area.w(), area.h()]
|
||||
});
|
||||
transform_xy_unit!(|self: Pull, area|{
|
||||
let area = Render::layout(&self.content(), area);
|
||||
[area.x().minus(self.dx()), area.y().minus(self.dy()), area.w(), area.h()]
|
||||
});
|
||||
transform_xy_unit!(|self: Margin, area|{
|
||||
let area = Render::layout(&self.content(), area);
|
||||
let dx = self.dx();
|
||||
let dy = self.dy();
|
||||
[area.x().minus(dx), area.y().minus(dy), area.w() + dy + dy, area.h() + dy + dy]
|
||||
});
|
||||
transform_xy_unit!(|self: Padding, area|{
|
||||
let area = Render::layout(&self.content(), area);
|
||||
let dx = self.dx();
|
||||
let dy = self.dy();
|
||||
[area.x() + dx, area.y() + dy, area.w().minus(dy + dy), area.h().minus(dy + dy), ]
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue