mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-09 13:16:44 +01:00
still dark; refactor and document layout crate
This commit is contained in:
parent
ed72ab1635
commit
675d376100
12 changed files with 510 additions and 659 deletions
41
layout/src/transform_xy.rs
Normal file
41
layout/src/transform_xy.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
use crate::*;
|
||||
|
||||
/// Defines an enum that transforms its content
|
||||
/// along either the X axis, the Y axis, or both.
|
||||
///
|
||||
/// The `_Unused` variant wraps the `Engine` type
|
||||
/// using `PhantomData` to permit the double generic.
|
||||
macro_rules! transform_xy {
|
||||
($self:ident : $Enum:ident |$to:ident|$area:expr) => {
|
||||
pub enum $Enum<E: Engine, T: Content<E>> { _Unused(PhantomData<E>), X(T), Y(T), XY(T) }
|
||||
impl<E: Engine, T: Content<E>> $Enum<E, T> {
|
||||
pub fn x (item: T) -> Self { Self::X(item) }
|
||||
pub fn y (item: T) -> Self { Self::Y(item) }
|
||||
pub fn xy (item: T) -> Self { Self::XY(item) }
|
||||
}
|
||||
impl<E: Engine, T: Content<E>> Content<E> for $Enum<E, T> {
|
||||
fn content (&self) -> impl Content<E> {
|
||||
match self {
|
||||
Self::X(item) => item,
|
||||
Self::Y(item) => item,
|
||||
Self::XY(item) => item,
|
||||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
fn area (&$self, $to: <E as Engine>::Area) -> <E as Engine>::Area {
|
||||
$area
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
transform_xy!(self: Fill |to|{
|
||||
let [x0, y0, wmax, hmax] = to.xywh();
|
||||
let [x, y, w, h] = Content::area(&self.content(), to).xywh();
|
||||
return match self {
|
||||
Self::X(_) => [x0, y, wmax, h],
|
||||
Self::Y(_) => [x, y0, w, hmax],
|
||||
Self::XY(_) => [x0, y0, wmax, hmax],
|
||||
_ => unreachable!()
|
||||
}.into()
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue