mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
parent
31e84bf5b3
commit
f1b24d436a
20 changed files with 1081 additions and 1103 deletions
|
|
@ -3,8 +3,16 @@
|
|||
#![feature(impl_trait_in_assoc_type)]
|
||||
pub(crate) use tengri_core::*;
|
||||
pub(crate) use std::marker::PhantomData;
|
||||
#[cfg(feature = "dsl")] pub(crate) use ::tengri_dsl::*;
|
||||
mod space; pub use self::space::*;
|
||||
mod ops; pub use self::ops::*;
|
||||
mod output; pub use self::output::*;
|
||||
|
||||
#[cfg(feature = "dsl")]
|
||||
pub(crate) use ::tengri_dsl::*;
|
||||
|
||||
mod space; pub use self::space::*;
|
||||
|
||||
mod ops; pub use self::ops::*;
|
||||
|
||||
#[cfg(feature = "dsl")] mod ops_dsl;
|
||||
|
||||
mod output; pub use self::output::*;
|
||||
|
||||
#[cfg(test)] mod test;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,382 @@
|
|||
//mod reduce; pub use self::reduce::*;
|
||||
mod align; pub use self::align::*;
|
||||
mod bsp; pub use self::bsp::*;
|
||||
mod either; pub use self::either::*;
|
||||
//! Transform:
|
||||
//! ```
|
||||
//! use ::tengri::{output::*, tui::*};
|
||||
//! let area: [u16;4] = [10, 10, 20, 20];
|
||||
//! fn test (area: [u16;4], item: &impl Content<TuiOut>, expected: [u16;4]) {
|
||||
//! assert_eq!(Content::layout(item, area), expected);
|
||||
//! assert_eq!(Render::layout(item, area), expected);
|
||||
//! };
|
||||
//! test(area, &(), [20, 20, 0, 0]);
|
||||
//!
|
||||
//! test(area, &Fill::xy(()), area);
|
||||
//! test(area, &Fill::x(()), [10, 20, 20, 0]);
|
||||
//! test(area, &Fill::y(()), [20, 10, 0, 20]);
|
||||
//!
|
||||
//! //FIXME:test(area, &Fixed::x(4, ()), [18, 20, 4, 0]);
|
||||
//! //FIXME:test(area, &Fixed::y(4, ()), [20, 18, 0, 4]);
|
||||
//! //FIXME:test(area, &Fixed::xy(4, 4, unit), [18, 18, 4, 4]);
|
||||
//! ```
|
||||
//! Align:
|
||||
//! ```
|
||||
//! use ::tengri::{output::*, tui::*};
|
||||
//! let area: [u16;4] = [10, 10, 20, 20];
|
||||
//! fn test (area: [u16;4], item: &impl Content<TuiOut>, expected: [u16;4]) {
|
||||
//! assert_eq!(Content::layout(item, area), expected);
|
||||
//! assert_eq!(Render::layout(item, area), expected);
|
||||
//! };
|
||||
//!
|
||||
//! let four = ||Fixed::xy(4, 4, "");
|
||||
//! test(area, &Align::nw(four()), [10, 10, 4, 4]);
|
||||
//! test(area, &Align::n(four()), [18, 10, 4, 4]);
|
||||
//! test(area, &Align::ne(four()), [26, 10, 4, 4]);
|
||||
//! test(area, &Align::e(four()), [26, 18, 4, 4]);
|
||||
//! test(area, &Align::se(four()), [26, 26, 4, 4]);
|
||||
//! test(area, &Align::s(four()), [18, 26, 4, 4]);
|
||||
//! test(area, &Align::sw(four()), [10, 26, 4, 4]);
|
||||
//! test(area, &Align::w(four()), [10, 18, 4, 4]);
|
||||
//!
|
||||
//! let two_by_four = ||Fixed::xy(4, 2, "");
|
||||
//! test(area, &Align::nw(two_by_four()), [10, 10, 4, 2]);
|
||||
//! test(area, &Align::n(two_by_four()), [18, 10, 4, 2]);
|
||||
//! test(area, &Align::ne(two_by_four()), [26, 10, 4, 2]);
|
||||
//! test(area, &Align::e(two_by_four()), [26, 19, 4, 2]);
|
||||
//! test(area, &Align::se(two_by_four()), [26, 28, 4, 2]);
|
||||
//! test(area, &Align::s(two_by_four()), [18, 28, 4, 2]);
|
||||
//! test(area, &Align::sw(two_by_four()), [10, 28, 4, 2]);
|
||||
//! test(area, &Align::w(two_by_four()), [10, 19, 4, 2]);
|
||||
//! ```
|
||||
|
||||
use crate::*;
|
||||
use Direction::*;
|
||||
|
||||
mod map; pub use self::map::*;
|
||||
mod memo; pub use self::memo::*;
|
||||
mod stack; pub use self::stack::*;
|
||||
mod thunk; pub use self::thunk::*;
|
||||
mod transform; pub use self::transform::*;
|
||||
mod when; pub use self::when::*;
|
||||
|
||||
/// Renders multiple things on top of each other,
|
||||
#[macro_export] macro_rules! lay {
|
||||
($($expr:expr),* $(,)?) => {{ let bsp = (); $(let bsp = Bsp::b(bsp, $expr);)*; bsp }}
|
||||
}
|
||||
|
||||
/// Stack southward.
|
||||
#[macro_export] macro_rules! col {
|
||||
($($expr:expr),* $(,)?) => {{ let bsp = (); $(let bsp = Bsp::s(bsp, $expr);)*; bsp }};
|
||||
}
|
||||
|
||||
/// Stack northward.
|
||||
#[macro_export] macro_rules! col_up {
|
||||
($($expr:expr),* $(,)?) => {{ let bsp = (); $(let bsp = Bsp::n(bsp, $expr);)*; bsp }}
|
||||
}
|
||||
|
||||
/// Stack eastward.
|
||||
#[macro_export] macro_rules! row {
|
||||
($($expr:expr),* $(,)?) => {{ let bsp = (); $(let bsp = Bsp::e(bsp, $expr);)*; bsp }};
|
||||
}
|
||||
|
||||
/// Show an item only when a condition is true.
|
||||
pub struct When<A>(pub bool, pub A);
|
||||
impl<A> When<A> {
|
||||
/// Create a binary condition.
|
||||
pub const fn new (c: bool, a: A) -> Self { Self(c, a) }
|
||||
}
|
||||
impl<E: Output, A: Render<E>> Content<E> for When<A> {
|
||||
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.layout(to);
|
||||
area[0] = item_area.x();
|
||||
area[1] = item_area.y();
|
||||
area[2] = item_area.w();
|
||||
area[3] = item_area.h();
|
||||
}
|
||||
area.into()
|
||||
}
|
||||
fn render (&self, to: &mut E) {
|
||||
let Self(cond, item) = self;
|
||||
if *cond { item.render(to) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Show one item if a condition is true and another if the condition is false
|
||||
pub struct Either<A, B>(pub bool, pub A, pub B);
|
||||
impl<A, B> Either<A, B> {
|
||||
/// Create a ternary view condition.
|
||||
pub const fn new (c: bool, a: A, b: B) -> Self { Self(c, a, b) }
|
||||
}
|
||||
impl<E: Output, A: Render<E>, B: Render<E>> Content<E> for Either<A, B> {
|
||||
fn layout (&self, to: E::Area) -> E::Area {
|
||||
let Self(cond, a, b) = self;
|
||||
if *cond { a.layout(to) } else { b.layout(to) }
|
||||
}
|
||||
fn render (&self, to: &mut E) {
|
||||
let Self(cond, a, b) = self;
|
||||
if *cond { a.render(to) } else { b.render(to) }
|
||||
}
|
||||
}
|
||||
|
||||
/// 9th of area to place.
|
||||
#[derive(Debug, Copy, Clone, Default)]
|
||||
pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
|
||||
|
||||
pub struct Align<A>(Alignment, A);
|
||||
|
||||
impl<A> Align<A> {
|
||||
#[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) }
|
||||
#[inline] pub const fn x (a: A) -> Self { Self(Alignment::X, a) }
|
||||
#[inline] pub const fn y (a: A) -> Self { Self(Alignment::Y, a) }
|
||||
#[inline] pub const fn n (a: A) -> Self { Self(Alignment::N, a) }
|
||||
#[inline] pub const fn s (a: A) -> Self { Self(Alignment::S, a) }
|
||||
#[inline] pub const fn e (a: A) -> Self { Self(Alignment::E, a) }
|
||||
#[inline] pub const fn w (a: A) -> Self { Self(Alignment::W, a) }
|
||||
#[inline] pub const fn nw (a: A) -> Self { Self(Alignment::NW, a) }
|
||||
#[inline] pub const fn sw (a: A) -> Self { Self(Alignment::SW, a) }
|
||||
#[inline] pub const fn ne (a: A) -> Self { Self(Alignment::NE, a) }
|
||||
#[inline] pub const fn se (a: A) -> Self { Self(Alignment::SE, a) }
|
||||
}
|
||||
|
||||
impl<E: Output, A: Content<E>> Content<E> for Align<A> {
|
||||
fn content (&self) -> impl Render<E> + '_ {
|
||||
&self.1
|
||||
}
|
||||
fn layout (&self, on: E::Area) -> E::Area {
|
||||
use Alignment::*;
|
||||
let it = Render::layout(&self.content(), on).xywh();
|
||||
let cx = on.x()+(on.w().minus(it.w())/2.into());
|
||||
let cy = on.y()+(on.h().minus(it.h())/2.into());
|
||||
let fx = (on.x()+on.w()).minus(it.w());
|
||||
let fy = (on.y()+on.h()).minus(it.h());
|
||||
let [x, y] = match self.0 {
|
||||
Center => [cx, cy],
|
||||
X => [cx, it.y()],
|
||||
Y => [it.x(), cy],
|
||||
NW => [on.x(), on.y()],
|
||||
N => [cx, on.y()],
|
||||
NE => [fx, on.y()],
|
||||
W => [on.x(), cy],
|
||||
E => [fx, cy],
|
||||
SW => [on.x(), fy],
|
||||
S => [cx, fy],
|
||||
SE => [fx, fy],
|
||||
}.into();
|
||||
[x, y, it.w(), it.h()].into()
|
||||
}
|
||||
fn render (&self, to: &mut E) {
|
||||
to.place(Content::layout(self, to.area()), &self.content())
|
||||
}
|
||||
}
|
||||
|
||||
/// A split or layer.
|
||||
pub struct Bsp<A, B>(
|
||||
pub(crate) Direction,
|
||||
pub(crate) A,
|
||||
pub(crate) B,
|
||||
);
|
||||
impl<A, B> Bsp<A, B> {
|
||||
#[inline] pub const fn n (a: A, b: B) -> Self { Self(North, a, b) }
|
||||
#[inline] pub const fn s (a: A, b: B) -> Self { Self(South, a, b) }
|
||||
#[inline] pub const fn e (a: A, b: B) -> Self { Self(East, a, b) }
|
||||
#[inline] pub const fn w (a: A, b: B) -> Self { Self(West, a, b) }
|
||||
#[inline] pub const fn a (a: A, b: B) -> Self { Self(Above, a, b) }
|
||||
#[inline] pub const fn b (a: A, b: B) -> Self { Self(Below, a, b) }
|
||||
}
|
||||
impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
|
||||
fn layout (&self, outer: E::Area) -> E::Area { let [_, _, c] = self.areas(outer); c }
|
||||
fn render (&self, to: &mut E) {
|
||||
let [area_a, area_b, _] = self.areas(to.area());
|
||||
let (a, b) = self.contents();
|
||||
match self.0 {
|
||||
Below => { to.place(area_a, a); to.place(area_b, b); },
|
||||
_ => { to.place(area_b, b); to.place(area_a, a); }
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<E: Output, A: Content<E>, B: Content<E>> BspAreas<E, A, B> for Bsp<A, B> {
|
||||
fn direction (&self) -> Direction { self.0 }
|
||||
fn contents (&self) -> (&A, &B) { (&self.1, &self.2) }
|
||||
}
|
||||
pub trait BspAreas<E: Output, A: Content<E>, B: Content<E>> {
|
||||
fn direction (&self) -> Direction;
|
||||
fn contents (&self) -> (&A, &B);
|
||||
fn areas (&self, outer: E::Area) -> [E::Area;3] {
|
||||
let direction = self.direction();
|
||||
let [x, y, w, h] = outer.xywh();
|
||||
let (a, b) = self.contents();
|
||||
let [aw, ah] = a.layout(outer).wh();
|
||||
let [bw, bh] = b.layout(match direction {
|
||||
Above | Below => outer,
|
||||
South => [x, y + ah, w, h.minus(ah)].into(),
|
||||
North => [x, y, w, h.minus(ah)].into(),
|
||||
East => [x + aw, y, w.minus(aw), h].into(),
|
||||
West => [x, y, w.minus(aw), h].into(),
|
||||
}).wh();
|
||||
match direction {
|
||||
Above | Below => {
|
||||
let [x, y, w, h] = outer.center_xy([aw.max(bw), ah.max(bh)]);
|
||||
let a = [(x + w/2.into()).minus(aw/2.into()), (y + h/2.into()).minus(ah/2.into()), aw, ah];
|
||||
let b = [(x + w/2.into()).minus(bw/2.into()), (y + h/2.into()).minus(bh/2.into()), bw, bh];
|
||||
[a.into(), b.into(), [x, y, w, h].into()]
|
||||
},
|
||||
South => {
|
||||
let [x, y, w, h] = outer.center_xy([aw.max(bw), ah + bh]);
|
||||
let a = [(x + w/2.into()).minus(aw/2.into()), y, aw, ah];
|
||||
let b = [(x + w/2.into()).minus(bw/2.into()), y + ah, bw, bh];
|
||||
[a.into(), b.into(), [x, y, w, h].into()]
|
||||
},
|
||||
North => {
|
||||
let [x, y, w, h] = outer.center_xy([aw.max(bw), ah + bh]);
|
||||
let a = [(x + (w/2.into())).minus(aw/2.into()), y + bh, aw, ah];
|
||||
let b = [(x + (w/2.into())).minus(bw/2.into()), y, bw, bh];
|
||||
[a.into(), b.into(), [x, y, w, h].into()]
|
||||
},
|
||||
East => {
|
||||
let [x, y, w, h] = outer.center_xy([aw + bw, ah.max(bh)]);
|
||||
let a = [x, (y + h/2.into()).minus(ah/2.into()), aw, ah];
|
||||
let b = [x + aw, (y + h/2.into()).minus(bh/2.into()), bw, bh];
|
||||
[a.into(), b.into(), [x, y, w, h].into()]
|
||||
},
|
||||
West => {
|
||||
let [x, y, w, h] = outer.center_xy([aw + bw, ah.max(bh)]);
|
||||
let a = [x + bw, (y + h/2.into()).minus(ah/2.into()), aw, ah];
|
||||
let b = [x, (y + h/2.into()).minus(bh/2.into()), bw, bh];
|
||||
[a.into(), b.into(), [x, y, w, h].into()]
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Defines an enum that transforms its content
|
||||
/// along either the X axis, the Y axis, or both.
|
||||
macro_rules! transform_xy {
|
||||
($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$area:expr) => {
|
||||
pub enum $Enum<A> { X(A), Y(A), XY(A) }
|
||||
impl<A> $Enum<A> {
|
||||
#[inline] pub const fn x (item: A) -> Self { Self::X(item) }
|
||||
#[inline] pub const fn y (item: A) -> Self { Self::Y(item) }
|
||||
#[inline] pub const fn xy (item: A) -> Self { Self::XY(item) }
|
||||
}
|
||||
impl<E: Output, T: Content<E>> Content<E> for $Enum<T> {
|
||||
fn content (&self) -> impl Render<E> + '_ {
|
||||
match self {
|
||||
Self::X(item) => item,
|
||||
Self::Y(item) => item,
|
||||
Self::XY(item) => item,
|
||||
}
|
||||
}
|
||||
fn layout (&$self, $to: <E as Output>::Area) -> <E as Output>::Area {
|
||||
use $Enum::*;
|
||||
$area
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Defines an enum that parametrically transforms its content
|
||||
/// along either the X axis, the Y axis, or both.
|
||||
macro_rules! transform_xy_unit {
|
||||
($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$layout:expr) => {
|
||||
pub enum $Enum<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
|
||||
impl<U, A> $Enum<U, A> {
|
||||
#[inline] pub const fn x (x: U, item: A) -> Self { Self::X(x, item) }
|
||||
#[inline] pub const fn y (y: U, item: A) -> Self { Self::Y(y, item) }
|
||||
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self { Self::XY(x, y, item) }
|
||||
}
|
||||
impl<E: Output, T: Content<E>> Content<E> for $Enum<E::Unit, T> {
|
||||
fn layout (&$self, $to: E::Area) -> E::Area {
|
||||
$layout.into()
|
||||
}
|
||||
fn content (&self) -> impl Render<E> + '_ {
|
||||
use $Enum::*;
|
||||
Some(match self { X(_, c) => c, Y(_, c) => c, XY(_, _, c) => c, })
|
||||
}
|
||||
}
|
||||
impl<U: Coordinate, T> $Enum<U, T> {
|
||||
#[inline] pub fn dx (&self) -> U {
|
||||
use $Enum::*;
|
||||
match self { X(x, _) => *x, Y(_, _) => 0.into(), XY(x, _, _) => *x, }
|
||||
}
|
||||
#[inline] pub fn dy (&self) -> U {
|
||||
use $Enum::*;
|
||||
match self { X(_, _) => 0.into(), Y(y, _) => *y, XY(_, y, _) => *y, }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
transform_xy!("fill/x" "fill/y" "fill/xy" |self: Fill, to|{
|
||||
let [x0, y0, wmax, hmax] = to.xywh();
|
||||
let [x, y, w, h] = self.content().layout(to).xywh();
|
||||
match self {
|
||||
X(_) => [x0, y, wmax, h],
|
||||
Y(_) => [x, y0, w, hmax],
|
||||
XY(_) => [x0, y0, wmax, hmax],
|
||||
}.into()
|
||||
});
|
||||
|
||||
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],
|
||||
};
|
||||
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],
|
||||
};
|
||||
fixed_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)],
|
||||
}
|
||||
});
|
||||
|
||||
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],
|
||||
}.into())
|
||||
});
|
||||
|
||||
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!("expand/x" "expand/y" "expand/xy"|self: Expand, area|Render::layout(
|
||||
&self.content(),
|
||||
[area.x(), area.y(), area.w().plus(self.dx()), area.h().plus(self.dy())].into()));
|
||||
|
||||
transform_xy_unit!("push/x" "push/y" "push/xy"|self: Push, area|{
|
||||
let area = Render::layout(&self.content(), area);
|
||||
[area.x().plus(self.dx()), area.y().plus(self.dy()), area.w(), area.h()]
|
||||
});
|
||||
|
||||
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!("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().plus(dy.plus(dy)), area.h().plus(dy.plus(dy))]
|
||||
});
|
||||
|
||||
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();
|
||||
[area.x().plus(dx), area.y().plus(dy), area.w().minus(dy.plus(dy)), area.h().minus(dy.plus(dy))]
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,110 +0,0 @@
|
|||
//! Aligns things to the container. Comes with caveats.
|
||||
//! ```
|
||||
//! use ::tengri::{output::*, tui::*};
|
||||
//! let area: [u16;4] = [10, 10, 20, 20];
|
||||
//! fn test (area: [u16;4], item: &impl Content<TuiOut>, expected: [u16;4]) {
|
||||
//! assert_eq!(Content::layout(item, area), expected);
|
||||
//! assert_eq!(Render::layout(item, area), expected);
|
||||
//! };
|
||||
//!
|
||||
//! let four = ||Fixed::xy(4, 4, "");
|
||||
//! test(area, &Align::nw(four()), [10, 10, 4, 4]);
|
||||
//! test(area, &Align::n(four()), [18, 10, 4, 4]);
|
||||
//! test(area, &Align::ne(four()), [26, 10, 4, 4]);
|
||||
//! test(area, &Align::e(four()), [26, 18, 4, 4]);
|
||||
//! test(area, &Align::se(four()), [26, 26, 4, 4]);
|
||||
//! test(area, &Align::s(four()), [18, 26, 4, 4]);
|
||||
//! test(area, &Align::sw(four()), [10, 26, 4, 4]);
|
||||
//! test(area, &Align::w(four()), [10, 18, 4, 4]);
|
||||
//!
|
||||
//! let two_by_four = ||Fixed::xy(4, 2, "");
|
||||
//! test(area, &Align::nw(two_by_four()), [10, 10, 4, 2]);
|
||||
//! test(area, &Align::n(two_by_four()), [18, 10, 4, 2]);
|
||||
//! test(area, &Align::ne(two_by_four()), [26, 10, 4, 2]);
|
||||
//! test(area, &Align::e(two_by_four()), [26, 19, 4, 2]);
|
||||
//! test(area, &Align::se(two_by_four()), [26, 28, 4, 2]);
|
||||
//! test(area, &Align::s(two_by_four()), [18, 28, 4, 2]);
|
||||
//! test(area, &Align::sw(two_by_four()), [10, 28, 4, 2]);
|
||||
//! test(area, &Align::w(two_by_four()), [10, 19, 4, 2]);
|
||||
//! ```
|
||||
use crate::*;
|
||||
#[derive(Debug, Copy, Clone, Default)]
|
||||
pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
|
||||
pub struct Align<A>(Alignment, A);
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<'state, State: Give<'state, A>, A: 'state> Take<'state, State> for Align<A> {
|
||||
fn take <'source: 'state> (state: &State, words: TokenIter<'source>) -> Perhaps<Self> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
//give!(Align<A>, A|state, words|Ok(Some(match words.peek() {
|
||||
//Some(Token { value: Value::Key(key), .. }) => match key {
|
||||
//"align/c"|"align/x"|"align/y"|
|
||||
//"align/n"|"align/s"|"align/e"|"al;qign/w"|
|
||||
//"align/nw"|"align/sw"|"align/ne"|"align/se" => {
|
||||
//let _ = words.next().unwrap();
|
||||
//let content = Take::take_or_fail(state, &mut words.clone(), ||"expected content")?;
|
||||
//match key {
|
||||
//"align/c" => Self::c(content),
|
||||
//"align/x" => Self::x(content),
|
||||
//"align/y" => Self::y(content),
|
||||
//"align/n" => Self::n(content),
|
||||
//"align/s" => Self::s(content),
|
||||
//"align/e" => Self::e(content),
|
||||
//"align/w" => Self::w(content),
|
||||
//"align/nw" => Self::nw(content),
|
||||
//"align/ne" => Self::ne(content),
|
||||
//"align/sw" => Self::sw(content),
|
||||
//"align/se" => Self::se(content),
|
||||
//_ => unreachable!()
|
||||
//}
|
||||
//},
|
||||
//_ => return Ok(None)
|
||||
//},
|
||||
//_ => return Ok(None)
|
||||
//})));
|
||||
|
||||
impl<A> Align<A> {
|
||||
#[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) }
|
||||
#[inline] pub const fn x (a: A) -> Self { Self(Alignment::X, a) }
|
||||
#[inline] pub const fn y (a: A) -> Self { Self(Alignment::Y, a) }
|
||||
#[inline] pub const fn n (a: A) -> Self { Self(Alignment::N, a) }
|
||||
#[inline] pub const fn s (a: A) -> Self { Self(Alignment::S, a) }
|
||||
#[inline] pub const fn e (a: A) -> Self { Self(Alignment::E, a) }
|
||||
#[inline] pub const fn w (a: A) -> Self { Self(Alignment::W, a) }
|
||||
#[inline] pub const fn nw (a: A) -> Self { Self(Alignment::NW, a) }
|
||||
#[inline] pub const fn sw (a: A) -> Self { Self(Alignment::SW, a) }
|
||||
#[inline] pub const fn ne (a: A) -> Self { Self(Alignment::NE, a) }
|
||||
#[inline] pub const fn se (a: A) -> Self { Self(Alignment::SE, a) }
|
||||
}
|
||||
|
||||
impl<E: Output, A: Content<E>> Content<E> for Align<A> {
|
||||
fn content (&self) -> impl Render<E> + '_ {
|
||||
&self.1
|
||||
}
|
||||
fn layout (&self, on: E::Area) -> E::Area {
|
||||
use Alignment::*;
|
||||
let it = Render::layout(&self.content(), on).xywh();
|
||||
let cx = on.x()+(on.w().minus(it.w())/2.into());
|
||||
let cy = on.y()+(on.h().minus(it.h())/2.into());
|
||||
let fx = (on.x()+on.w()).minus(it.w());
|
||||
let fy = (on.y()+on.h()).minus(it.h());
|
||||
let [x, y] = match self.0 {
|
||||
Center => [cx, cy],
|
||||
X => [cx, it.y()],
|
||||
Y => [it.x(), cy],
|
||||
NW => [on.x(), on.y()],
|
||||
N => [cx, on.y()],
|
||||
NE => [fx, on.y()],
|
||||
W => [on.x(), cy],
|
||||
E => [fx, cy],
|
||||
SW => [on.x(), fy],
|
||||
S => [cx, fy],
|
||||
SE => [fx, fy],
|
||||
}.into();
|
||||
[x, y, it.w(), it.h()].into()
|
||||
}
|
||||
fn render (&self, to: &mut E) {
|
||||
to.place(Content::layout(self, to.area()), &self.content())
|
||||
}
|
||||
}
|
||||
|
|
@ -1,122 +0,0 @@
|
|||
use crate::*;
|
||||
pub use Direction::*;
|
||||
/// A split or layer.
|
||||
pub struct Bsp<A, B>(
|
||||
pub(crate) Direction,
|
||||
pub(crate) A,
|
||||
pub(crate) B,
|
||||
);
|
||||
impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
|
||||
fn layout (&self, outer: E::Area) -> E::Area {
|
||||
let [_, _, c] = self.areas(outer);
|
||||
c
|
||||
}
|
||||
fn render (&self, to: &mut E) {
|
||||
let [area_a, area_b, _] = self.areas(to.area());
|
||||
let (a, b) = self.contents();
|
||||
match self.0 {
|
||||
Below => { to.place(area_a, a); to.place(area_b, b); },
|
||||
_ => { to.place(area_b, b); to.place(area_a, a); }
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "dsl")] take!(Bsp<A, B>, A, B|state, words|Ok(if let Some(Token {
|
||||
value: Value::Key("bsp/n"|"bsp/s"|"bsp/e"|"bsp/w"|"bsp/a"|"bsp/b"),
|
||||
..
|
||||
}) = words.peek() {
|
||||
if let Value::Key(key) = words.next().unwrap().value() {
|
||||
let base = words.clone();
|
||||
let a: A = state.give_or_fail(words, ||"bsp: expected content 1")?;
|
||||
let b: B = state.give_or_fail(words, ||"bsp: expected content 2")?;
|
||||
return Ok(Some(match key {
|
||||
"bsp/n" => Self::n(a, b),
|
||||
"bsp/s" => Self::s(a, b),
|
||||
"bsp/e" => Self::e(a, b),
|
||||
"bsp/w" => Self::w(a, b),
|
||||
"bsp/a" => Self::a(a, b),
|
||||
"bsp/b" => Self::b(a, b),
|
||||
_ => unreachable!(),
|
||||
}))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}));
|
||||
impl<A, B> Bsp<A, B> {
|
||||
#[inline] pub const fn n (a: A, b: B) -> Self { Self(North, a, b) }
|
||||
#[inline] pub const fn s (a: A, b: B) -> Self { Self(South, a, b) }
|
||||
#[inline] pub const fn e (a: A, b: B) -> Self { Self(East, a, b) }
|
||||
#[inline] pub const fn w (a: A, b: B) -> Self { Self(West, a, b) }
|
||||
#[inline] pub const fn a (a: A, b: B) -> Self { Self(Above, a, b) }
|
||||
#[inline] pub const fn b (a: A, b: B) -> Self { Self(Below, a, b) }
|
||||
}
|
||||
pub trait BspAreas<E: Output, A: Content<E>, B: Content<E>> {
|
||||
fn direction (&self) -> Direction;
|
||||
fn contents (&self) -> (&A, &B);
|
||||
fn areas (&self, outer: E::Area) -> [E::Area;3] {
|
||||
let direction = self.direction();
|
||||
let [x, y, w, h] = outer.xywh();
|
||||
let (a, b) = self.contents();
|
||||
let [aw, ah] = a.layout(outer).wh();
|
||||
let [bw, bh] = b.layout(match direction {
|
||||
Above | Below => outer,
|
||||
South => [x, y + ah, w, h.minus(ah)].into(),
|
||||
North => [x, y, w, h.minus(ah)].into(),
|
||||
East => [x + aw, y, w.minus(aw), h].into(),
|
||||
West => [x, y, w.minus(aw), h].into(),
|
||||
}).wh();
|
||||
match direction {
|
||||
Above | Below => {
|
||||
let [x, y, w, h] = outer.center_xy([aw.max(bw), ah.max(bh)]);
|
||||
let a = [(x + w/2.into()).minus(aw/2.into()), (y + h/2.into()).minus(ah/2.into()), aw, ah];
|
||||
let b = [(x + w/2.into()).minus(bw/2.into()), (y + h/2.into()).minus(bh/2.into()), bw, bh];
|
||||
[a.into(), b.into(), [x, y, w, h].into()]
|
||||
},
|
||||
South => {
|
||||
let [x, y, w, h] = outer.center_xy([aw.max(bw), ah + bh]);
|
||||
let a = [(x + w/2.into()).minus(aw/2.into()), y, aw, ah];
|
||||
let b = [(x + w/2.into()).minus(bw/2.into()), y + ah, bw, bh];
|
||||
[a.into(), b.into(), [x, y, w, h].into()]
|
||||
},
|
||||
North => {
|
||||
let [x, y, w, h] = outer.center_xy([aw.max(bw), ah + bh]);
|
||||
let a = [(x + (w/2.into())).minus(aw/2.into()), y + bh, aw, ah];
|
||||
let b = [(x + (w/2.into())).minus(bw/2.into()), y, bw, bh];
|
||||
[a.into(), b.into(), [x, y, w, h].into()]
|
||||
},
|
||||
East => {
|
||||
let [x, y, w, h] = outer.center_xy([aw + bw, ah.max(bh)]);
|
||||
let a = [x, (y + h/2.into()).minus(ah/2.into()), aw, ah];
|
||||
let b = [x + aw, (y + h/2.into()).minus(bh/2.into()), bw, bh];
|
||||
[a.into(), b.into(), [x, y, w, h].into()]
|
||||
},
|
||||
West => {
|
||||
let [x, y, w, h] = outer.center_xy([aw + bw, ah.max(bh)]);
|
||||
let a = [x + bw, (y + h/2.into()).minus(ah/2.into()), aw, ah];
|
||||
let b = [x, (y + h/2.into()).minus(bh/2.into()), bw, bh];
|
||||
[a.into(), b.into(), [x, y, w, h].into()]
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<E: Output, A: Content<E>, B: Content<E>> BspAreas<E, A, B> for Bsp<A, B> {
|
||||
fn direction (&self) -> Direction { self.0 }
|
||||
fn contents (&self) -> (&A, &B) { (&self.1, &self.2) }
|
||||
}
|
||||
/// Renders multiple things on top of each other,
|
||||
#[macro_export] macro_rules! lay {
|
||||
($($expr:expr),* $(,)?) => {{ let bsp = (); $(let bsp = Bsp::b(bsp, $expr);)*; bsp }}
|
||||
}
|
||||
/// Stack southward.
|
||||
#[macro_export] macro_rules! col {
|
||||
($($expr:expr),* $(,)?) => {{ let bsp = (); $(let bsp = Bsp::s(bsp, $expr);)*; bsp }};
|
||||
}
|
||||
/// Stack northward.
|
||||
#[macro_export] macro_rules! col_up {
|
||||
($($expr:expr),* $(,)?) => {{ let bsp = (); $(let bsp = Bsp::n(bsp, $expr);)*; bsp }}
|
||||
}
|
||||
/// Stack eastward.
|
||||
#[macro_export] macro_rules! row {
|
||||
($($expr:expr),* $(,)?) => {{ let bsp = (); $(let bsp = Bsp::e(bsp, $expr);)*; bsp }};
|
||||
}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
use crate::*;
|
||||
|
||||
/// Show one item if a condition is true and another if the condition is false
|
||||
pub struct Either<A, B>(pub bool, pub A, pub B);
|
||||
impl<A, B> Either<A, B> {
|
||||
/// Create a ternary condition.
|
||||
pub const fn new (c: bool, a: A, b: B) -> Self {
|
||||
Self(c, a, b)
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "dsl")] take!(Either<A, B>, A, B|state, words|Ok(
|
||||
if let Some(Token { value: Value::Key("either"), .. }) = words.peek() {
|
||||
let base = words.clone();
|
||||
let _ = words.next().unwrap();
|
||||
return Ok(Some(Self(
|
||||
state.give_or_fail(words, ||"either: no condition")?,
|
||||
state.give_or_fail(words, ||"either: no content 1")?,
|
||||
state.give_or_fail(words, ||"either: no content 2")?,
|
||||
)))
|
||||
} else {
|
||||
None
|
||||
}));
|
||||
impl<E: Output, A: Render<E>, B: Render<E>> Content<E> for Either<A, B> {
|
||||
fn layout (&self, to: E::Area) -> E::Area {
|
||||
let Self(cond, a, b) = self;
|
||||
if *cond { a.layout(to) } else { b.layout(to) }
|
||||
}
|
||||
fn render (&self, to: &mut E) {
|
||||
let Self(cond, a, b) = self;
|
||||
if *cond { a.render(to) } else { b.render(to) }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,192 +0,0 @@
|
|||
//! [Content] items that modify the inherent
|
||||
//! dimensions of their inner [Render]ables.
|
||||
//!
|
||||
//! Transform may also react to the [Area] taked.
|
||||
//! ```
|
||||
//! use ::tengri::{output::*, tui::*};
|
||||
//! let area: [u16;4] = [10, 10, 20, 20];
|
||||
//! fn test (area: [u16;4], item: &impl Content<TuiOut>, expected: [u16;4]) {
|
||||
//! assert_eq!(Content::layout(item, area), expected);
|
||||
//! assert_eq!(Render::layout(item, area), expected);
|
||||
//! };
|
||||
//! test(area, &(), [20, 20, 0, 0]);
|
||||
//!
|
||||
//! test(area, &Fill::xy(()), area);
|
||||
//! test(area, &Fill::x(()), [10, 20, 20, 0]);
|
||||
//! test(area, &Fill::y(()), [20, 10, 0, 20]);
|
||||
//!
|
||||
//! //FIXME:test(area, &Fixed::x(4, ()), [18, 20, 4, 0]);
|
||||
//! //FIXME:test(area, &Fixed::y(4, ()), [20, 18, 0, 4]);
|
||||
//! //FIXME:test(area, &Fixed::xy(4, 4, unit), [18, 18, 4, 4]);
|
||||
//! ```
|
||||
|
||||
use crate::*;
|
||||
|
||||
/// Defines an enum that transforms its content
|
||||
/// along either the X axis, the Y axis, or both.
|
||||
macro_rules! transform_xy {
|
||||
($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$area:expr) => {
|
||||
pub enum $Enum<A> { X(A), Y(A), XY(A) }
|
||||
impl<A> $Enum<A> {
|
||||
#[inline] pub const fn x (item: A) -> Self { Self::X(item) }
|
||||
#[inline] pub const fn y (item: A) -> Self { Self::Y(item) }
|
||||
#[inline] pub const fn xy (item: A) -> Self { Self::XY(item) }
|
||||
}
|
||||
#[cfg(feature = "dsl")] take!($Enum<A>, A|state, words|Ok(
|
||||
if let Some(Token { value: Value::Key(k), .. }) = words.peek() {
|
||||
let mut base = words.clone();
|
||||
let content = state.give_or_fail(words, ||format!("{k}: no content"))?;
|
||||
return Ok(Some(match words.next() {
|
||||
Some(Token{value: Value::Key($x),..}) => Self::x(content),
|
||||
Some(Token{value: Value::Key($y),..}) => Self::y(content),
|
||||
Some(Token{value: Value::Key($xy),..}) => Self::xy(content),
|
||||
_ => unreachable!()
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
}));
|
||||
impl<E: Output, T: Content<E>> Content<E> for $Enum<T> {
|
||||
fn content (&self) -> impl Render<E> + '_ {
|
||||
match self {
|
||||
Self::X(item) => item,
|
||||
Self::Y(item) => item,
|
||||
Self::XY(item) => item,
|
||||
}
|
||||
}
|
||||
fn layout (&$self, $to: <E as Output>::Area) -> <E as Output>::Area {
|
||||
use $Enum::*;
|
||||
$area
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Defines an enum that parametrically transforms its content
|
||||
/// along either the X axis, the Y axis, or both.
|
||||
macro_rules! transform_xy_unit {
|
||||
($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$layout:expr) => {
|
||||
pub enum $Enum<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
|
||||
impl<U, A> $Enum<U, A> {
|
||||
#[inline] pub const fn x (x: U, item: A) -> Self { Self::X(x, item) }
|
||||
#[inline] pub const fn y (y: U, item: A) -> Self { Self::Y(y, item) }
|
||||
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self { Self::XY(x, y, item) }
|
||||
}
|
||||
#[cfg(feature = "dsl")] take!($Enum<U, A>, U, A|state, words|Ok(
|
||||
if let Some(Token { value: Value::Key($x|$y|$xy), .. }) = words.peek() {
|
||||
let mut base = words.clone();
|
||||
Some(match words.next() {
|
||||
Some(Token { value: Value::Key($x), .. }) => Self::x(
|
||||
state.give_or_fail(words, ||"x: no unit")?,
|
||||
state.give_or_fail(words, ||"x: no content")?,
|
||||
),
|
||||
Some(Token { value: Value::Key($y), .. }) => Self::y(
|
||||
state.give_or_fail(words, ||"y: no unit")?,
|
||||
state.give_or_fail(words, ||"y: no content")?,
|
||||
),
|
||||
Some(Token { value: Value::Key($x), .. }) => Self::xy(
|
||||
state.give_or_fail(words, ||"xy: no unit x")?,
|
||||
state.give_or_fail(words, ||"xy: no unit y")?,
|
||||
state.give_or_fail(words, ||"xy: no content")?
|
||||
),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}));
|
||||
impl<E: Output, T: Content<E>> Content<E> for $Enum<E::Unit, T> {
|
||||
fn layout (&$self, $to: E::Area) -> E::Area {
|
||||
$layout.into()
|
||||
}
|
||||
fn content (&self) -> impl Render<E> + '_ {
|
||||
use $Enum::*;
|
||||
Some(match self { X(_, c) => c, Y(_, c) => c, XY(_, _, c) => c, })
|
||||
}
|
||||
}
|
||||
impl<U: Coordinate, T> $Enum<U, T> {
|
||||
#[inline] pub fn dx (&self) -> U {
|
||||
use $Enum::*;
|
||||
match self { X(x, _) => *x, Y(_, _) => 0.into(), XY(x, _, _) => *x, }
|
||||
}
|
||||
#[inline] pub fn dy (&self) -> U {
|
||||
use $Enum::*;
|
||||
match self { X(_, _) => 0.into(), Y(y, _) => *y, XY(_, y, _) => *y, }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
transform_xy!("fill/x" "fill/y" "fill/xy" |self: Fill, to|{
|
||||
let [x0, y0, wmax, hmax] = to.xywh();
|
||||
let [x, y, w, h] = self.content().layout(to).xywh();
|
||||
match self {
|
||||
X(_) => [x0, y, wmax, h],
|
||||
Y(_) => [x, y0, w, hmax],
|
||||
XY(_) => [x0, y0, wmax, hmax],
|
||||
}.into()
|
||||
});
|
||||
|
||||
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],
|
||||
};
|
||||
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],
|
||||
};
|
||||
fixed_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)],
|
||||
}
|
||||
});
|
||||
|
||||
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],
|
||||
}.into())
|
||||
});
|
||||
|
||||
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!("expand/x" "expand/y" "expand/xy"|self: Expand, area|Render::layout(
|
||||
&self.content(),
|
||||
[area.x(), area.y(), area.w().plus(self.dx()), area.h().plus(self.dy())].into()));
|
||||
|
||||
transform_xy_unit!("push/x" "push/y" "push/xy"|self: Push, area|{
|
||||
let area = Render::layout(&self.content(), area);
|
||||
[area.x().plus(self.dx()), area.y().plus(self.dy()), area.w(), area.h()]
|
||||
});
|
||||
|
||||
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!("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().plus(dy.plus(dy)), area.h().plus(dy.plus(dy))]
|
||||
});
|
||||
|
||||
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();
|
||||
[area.x().plus(dx), area.y().plus(dy), area.w().minus(dy.plus(dy)), area.h().minus(dy.plus(dy))]
|
||||
});
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
use crate::*;
|
||||
/// Show an item only when a condition is true.
|
||||
pub struct When<A>(pub bool, pub A);
|
||||
impl<A> When<A> {
|
||||
/// Create a binary condition.
|
||||
pub const fn new (c: bool, a: A) -> Self {
|
||||
Self(c, a)
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "dsl")]take!(When<A>, A|state, words|Ok(Some(match words.peek() {
|
||||
Some(Token { value: Value::Key("when"), .. }) => {
|
||||
let _ = words.next();
|
||||
let base = words.clone();
|
||||
let cond = state.give_or_fail(words, ||"cond: no condition")?;
|
||||
let cont = state.give_or_fail(words, ||"cond: no content")?;
|
||||
Self(cond, cont)
|
||||
},
|
||||
_ => return Ok(None)
|
||||
})));
|
||||
|
||||
impl<E: Output, A: Render<E>> Content<E> for When<A> {
|
||||
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.layout(to);
|
||||
area[0] = item_area.x();
|
||||
area[1] = item_area.y();
|
||||
area[2] = item_area.w();
|
||||
area[3] = item_area.h();
|
||||
}
|
||||
area.into()
|
||||
}
|
||||
fn render (&self, to: &mut E) {
|
||||
let Self(cond, item) = self;
|
||||
if *cond { item.render(to) }
|
||||
}
|
||||
}
|
||||
185
output/src/ops_dsl.rs
Normal file
185
output/src/ops_dsl.rs
Normal file
|
|
@ -0,0 +1,185 @@
|
|||
use crate::*;
|
||||
|
||||
impl<I: Ast, S, A> Eval<I, When<A>> for S where
|
||||
S: Eval<AstValue, bool> + Eval<AstValue, A>
|
||||
{
|
||||
fn eval (&self, source: I) -> Perhaps<Self> {
|
||||
Ok(match source.peek() {
|
||||
Some(Value::Key("when")) => Some(Self(
|
||||
self.provide(source, ||"when: expected condition")?,
|
||||
self.provide(source, ||"when: expected content")?,
|
||||
)),
|
||||
_ => None
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Ast, S, A, B> Eval<I, Either<A, B>> for S where
|
||||
S: Eval<AstValue, bool> + Eval<AstValue, A> + Eval<AstValue, B>
|
||||
{
|
||||
fn eval (&self, source: I) -> Perhaps<Self> {
|
||||
Ok(match source.peek() {
|
||||
Some(Value::Key("either")) => Some(Self(
|
||||
self.provide(source, ||"either: expected condition")?,
|
||||
self.provide(source, ||"either: expected content 1")?,
|
||||
self.provide(source, ||"either: expected content 2")?
|
||||
)),
|
||||
_ => None
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Ast, S, A, B> Eval<I, Bsp<A, B>> for S where
|
||||
S: Eval<AstValue, A> + Eval<AstValue, B>
|
||||
{
|
||||
fn eval (&self, source: I) -> Perhaps<Self> {
|
||||
Ok(if let Some(Value::Key(key)) = source.peek() {
|
||||
Some(match key {
|
||||
"bsp/n" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/n: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/n: expected content 2")?;
|
||||
Self::n(a, b)
|
||||
},
|
||||
"bsp/s" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/s: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/s: expected content 2")?;
|
||||
Self::s(a, b)
|
||||
},
|
||||
"bsp/e" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/e: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/e: expected content 2")?;
|
||||
Self::e(a, b)
|
||||
},
|
||||
"bsp/w" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/w: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/w: expected content 2")?;
|
||||
Self::w(a, b)
|
||||
},
|
||||
"bsp/a" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/a: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/a: expected content 2")?;
|
||||
Self::a(a, b)
|
||||
},
|
||||
"bsp/b" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/b: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/b: expected content 2")?;
|
||||
Self::b(a, b)
|
||||
},
|
||||
_ => return Ok(None),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Ast, S, A> Eval<I, Align<A>> for S where
|
||||
S: Eval<AstValue, A>
|
||||
{
|
||||
fn eval (&self, source: I) -> Perhaps<Self> {
|
||||
Ok(if let Some(Value::Key(key)) = source.peek() {
|
||||
Some(match key {
|
||||
"align/c" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/c: expected content")?;
|
||||
Self::c(content)
|
||||
},
|
||||
"align/x" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/x: expected content")?;
|
||||
Self::x(content)
|
||||
},
|
||||
"align/y" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/y: expected content")?;
|
||||
Self::y(content)
|
||||
},
|
||||
"align/n" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/n: expected content")?;
|
||||
Self::n(content)
|
||||
},
|
||||
"align/s" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/s: expected content")?;
|
||||
Self::s(content)
|
||||
},
|
||||
"align/e" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/e: expected content")?;
|
||||
Self::e(content)
|
||||
},
|
||||
"align/w" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/w: expected content")?;
|
||||
Self::w(content)
|
||||
},
|
||||
"align/nw" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/nw: expected content")?;
|
||||
Self::nw(content)
|
||||
},
|
||||
"align/ne" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/ne: expected content")?;
|
||||
Self::ne(content)
|
||||
},
|
||||
"align/sw" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/sw: expected content")?;
|
||||
Self::sw(content)
|
||||
},
|
||||
"align/se" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/se: expected content")?;
|
||||
Self::se(content)
|
||||
},
|
||||
_ => return Ok(None),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
//#[cfg(feature = "dsl")] take!($Enum<A>, A|state, words|Ok(
|
||||
//if let Some(Token { value: Value::Key(k), .. }) = words.peek() {
|
||||
//let mut base = words.clone();
|
||||
//let content = state.give_or_fail(words, ||format!("{k}: no content"))?;
|
||||
//return Ok(Some(match words.next() {
|
||||
//Some(Token{value: Value::Key($x),..}) => Self::x(content),
|
||||
//Some(Token{value: Value::Key($y),..}) => Self::y(content),
|
||||
//Some(Token{value: Value::Key($xy),..}) => Self::xy(content),
|
||||
//_ => unreachable!()
|
||||
//}))
|
||||
//} else {
|
||||
//None
|
||||
//}));
|
||||
//#[cfg(feature = "dsl")] take!($Enum<U, A>, U, A|state, words|Ok(
|
||||
//if let Some(Token { value: Value::Key($x|$y|$xy), .. }) = words.peek() {
|
||||
//let mut base = words.clone();
|
||||
//Some(match words.next() {
|
||||
//Some(Token { value: Value::Key($x), .. }) => Self::x(
|
||||
//state.give_or_fail(words, ||"x: no unit")?,
|
||||
//state.give_or_fail(words, ||"x: no content")?,
|
||||
//),
|
||||
//Some(Token { value: Value::Key($y), .. }) => Self::y(
|
||||
//state.give_or_fail(words, ||"y: no unit")?,
|
||||
//state.give_or_fail(words, ||"y: no content")?,
|
||||
//),
|
||||
//Some(Token { value: Value::Key($x), .. }) => Self::xy(
|
||||
//state.give_or_fail(words, ||"xy: no unit x")?,
|
||||
//state.give_or_fail(words, ||"xy: no unit y")?,
|
||||
//state.give_or_fail(words, ||"xy: no content")?
|
||||
//),
|
||||
//_ => unreachable!(),
|
||||
//})
|
||||
//} else {
|
||||
//None
|
||||
//}));
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
use crate::*;
|
||||
use crate::Direction::*;
|
||||
|
||||
/// A cardinal direction.
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue