mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
54 lines
2 KiB
Rust
54 lines
2 KiB
Rust
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: Engine, 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::Output) {
|
|
let content = &self.content();
|
|
let it = Render::layout(content, render.area()).xywh();
|
|
render.place(it.into(), content)
|
|
}
|
|
}
|