mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
wip: implement TryFromEdn for other x/y/xy operators
This commit is contained in:
parent
8eecd75592
commit
57fda5c7ad
1 changed files with 50 additions and 15 deletions
|
|
@ -3,19 +3,20 @@ 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> {
|
||||
($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$layout:expr) => {
|
||||
pub enum $Enum<E, U, T> { _Unused(E), X(U, T), Y(U, T), XY(U, U, T), }
|
||||
impl<E, U, T> $Enum<E, 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> {
|
||||
impl<E, U: Copy + Coordinate, T> $Enum<E, U, T> {
|
||||
pub fn dx (&self) -> U {
|
||||
match self {
|
||||
Self::X(x, _) => *x,
|
||||
Self::Y(_, _) => 0.into(),
|
||||
Self::XY(x, _, _) => *x,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
pub fn dy (&self) -> U {
|
||||
|
|
@ -23,75 +24,109 @@ macro_rules! transform_xy_unit {
|
|||
Self::X(_, _) => 0.into(),
|
||||
Self::Y(y, _) => *y,
|
||||
Self::XY(_, y, _) => *y,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<E: Output, T: Content<E>> Content<E> for $Enum<E::Unit, T> {
|
||||
impl<E: Output, T: Content<E>> Content<E> for $Enum<E, E::Unit, T> {
|
||||
fn content (&self) -> impl Render<E> {
|
||||
Some(match self {
|
||||
Self::X(_, content) => content,
|
||||
Self::Y(_, content) => content,
|
||||
Self::XY(_, _, content) => content,
|
||||
_ => unreachable!(),
|
||||
})
|
||||
}
|
||||
fn layout (&$self, $to: E::Area) -> E::Area {
|
||||
$layout.into()
|
||||
}
|
||||
}
|
||||
impl<'a, T, E, A> TryFromEdn<'a, T> for $Enum<E, E::Unit, A>
|
||||
where
|
||||
T: EdnProvide<'a, bool> + EdnProvide<'a, A> + 'a,
|
||||
E: Output,
|
||||
A: Render<E> + 'a,
|
||||
{
|
||||
fn try_from_edn (
|
||||
state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]
|
||||
) -> Option<Self> {
|
||||
use EdnItem::*;
|
||||
Some(match (head, tail) {
|
||||
(Key($x), [x, a]) => Self::x(
|
||||
state.get(x).expect("no content"),
|
||||
state.get(a).expect("no content"),
|
||||
),
|
||||
(Key($y), [y, a]) => Self::y(
|
||||
state.get(y).expect("no content"),
|
||||
state.get(a).expect("no content"),
|
||||
),
|
||||
(Key($xy), [x, y, a]) => Self::xy(
|
||||
state.get(x).expect("no content"),
|
||||
state.get(y).expect("no content"),
|
||||
state.get(a).expect("no content"),
|
||||
),
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
transform_xy_unit!(|self: Fixed, area|{
|
||||
transform_xy_unit!("fixed/x" "fixed/y" "fixed/xy"|self: Fixed, area|{
|
||||
let [x, y, w, h] = area.xywh();
|
||||
let fixed_area = match self {
|
||||
Self::X(fw, _) => [x, y, *fw, h],
|
||||
Self::Y(fh, _) => [x, y, w, *fh],
|
||||
Self::XY(fw, fh, _) => [x, y, *fw, *fh],
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let [x, y, w, h] = Render::layout(&self.content(), fixed_area.into()).xywh();
|
||||
let fixed_area = match self {
|
||||
Self::X(fw, _) => [x, y, *fw, h],
|
||||
Self::Y(fh, _) => [x, y, w, *fh],
|
||||
Self::XY(fw, fh, _) => [x, y, *fw, *fh],
|
||||
_ => unreachable!(),
|
||||
};
|
||||
fixed_area
|
||||
});
|
||||
transform_xy_unit!(|self: Min, area|{
|
||||
transform_xy_unit!("min/x" "min/y" "min/xy"|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)]
|
||||
Self::XY(mw, mh, _) => [area.x(), area.y(), area.w().max(*mw), area.h().max(*mh)],
|
||||
_ => unreachable!(),
|
||||
}});
|
||||
transform_xy_unit!(|self: Max, area|{
|
||||
transform_xy_unit!("max/x" "max/y" "max/xy"|self: Max, area|{
|
||||
let [x, y, w, h] = area.xywh();
|
||||
Render::layout(&self.content(), match self {
|
||||
Self::X(fw, _) => [x, y, *fw, h],
|
||||
Self::Y(fh, _) => [x, y, w, *fh],
|
||||
Self::XY(fw, fh, _) => [x, y, *fw, *fh],
|
||||
_ => unreachable!(),
|
||||
}.into())});
|
||||
|
||||
transform_xy_unit!(|self: Shrink, area|Render::layout(&self.content(), [
|
||||
transform_xy_unit!("shrink/x" "shrink/y" "shrink/xy"|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(), [
|
||||
transform_xy_unit!("expand/x" "expand/y" "expand/xy"|self: Expand, area|Render::layout(&self.content(), [
|
||||
area.x(), area.y(), area.w() + self.dx(), area.h() + self.dy()
|
||||
].into()));
|
||||
transform_xy_unit!(|self: Push, area|{
|
||||
transform_xy_unit!("push/x" "push/y" "push/xy"|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|{
|
||||
transform_xy_unit!("pull/x" "pull/y" "pull/xy"|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|{
|
||||
transform_xy_unit!("margin/x" "margin/y" "margin/xy"|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|{
|
||||
transform_xy_unit!("padding/x" "padding/y" "padding/xy"|self: Padding, area|{
|
||||
let area = Render::layout(&self.content(), area);
|
||||
let dx = self.dx();
|
||||
let dy = self.dy();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue