mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 04:06:45 +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
54
output/src/align.rs
Normal file
54
output/src/align.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
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<T>(Alignment, T);
|
||||
|
||||
impl<T> Align<T> {
|
||||
pub fn c (a: T) -> Self { Self(Alignment::Center, a) }
|
||||
pub fn x (a: T) -> Self { Self(Alignment::X, a) }
|
||||
pub fn y (a: T) -> Self { Self(Alignment::Y, a) }
|
||||
pub fn n (a: T) -> Self { Self(Alignment::N, a) }
|
||||
pub fn s (a: T) -> Self { Self(Alignment::S, a) }
|
||||
pub fn e (a: T) -> Self { Self(Alignment::E, a) }
|
||||
pub fn w (a: T) -> Self { Self(Alignment::W, a) }
|
||||
pub fn nw (a: T) -> Self { Self(Alignment::NW, a) }
|
||||
pub fn sw (a: T) -> Self { Self(Alignment::SW, a) }
|
||||
pub fn ne (a: T) -> Self { Self(Alignment::NE, a) }
|
||||
pub fn se (a: T) -> Self { Self(Alignment::SE, a) }
|
||||
}
|
||||
|
||||
impl<E: Output, T: Content<E>> Content<E> for Align<T> {
|
||||
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 centered = on.center_xy(it.wh());
|
||||
let far_x = (on.x() + on.w()).minus(it.w());
|
||||
let far_y = (on.y() + on.h()).minus(it.h());
|
||||
let [x, y] = match self.0 {
|
||||
NW => [on.x(), on.y()],
|
||||
N => [centered.x(), on.y()],
|
||||
NE => [far_x, on.y()],
|
||||
E => [far_x, centered.y()],
|
||||
SE => [far_x, far_y ],
|
||||
S => [centered.x(), far_y ],
|
||||
SW => [on.x(), far_y ],
|
||||
W => [on.x(), centered.y()],
|
||||
|
||||
Center => centered.xy(),
|
||||
|
||||
X => [centered.x(), it.y()],
|
||||
Y => [it.x(), centered.y()],
|
||||
};
|
||||
[x, y, centered.w(), centered.h()].into()
|
||||
}
|
||||
fn render (&self, render: &mut E) {
|
||||
let content = &self.content();
|
||||
let it = Render::layout(content, render.area()).xywh();
|
||||
render.place(it.into(), content)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue