'and then not have to worry about layout, ever'

This commit is contained in:
🪞👃🪞 2024-12-31 23:58:51 +01:00
parent 21741ebc52
commit f7e6449324
8 changed files with 74 additions and 74 deletions

View file

@ -111,32 +111,18 @@ pub trait Area<N: Coordinate> {
#[inline] fn set_h (&self, h: N) -> [N;4] {
[self.x(), self.y(), self.w(), h]
}
fn x2 (&self) -> N {
#[inline] fn x2 (&self) -> N {
self.x() + self.w()
}
fn y2 (&self) -> N {
#[inline] fn y2 (&self) -> N {
self.y() + self.h()
}
#[inline] fn lrtb (&self) -> [N;4] {
[self.x(), self.x2(), self.y(), self.y2()]
}
#[inline] fn push_x (&self, x: N) -> [N;4] {
[self.x() + x, self.y(), self.w(), self.h()]
}
#[inline] fn push_y (&self, y: N) -> [N;4] {
[self.x(), self.y() + y, self.w(), self.h()]
}
#[inline] fn shrink_x (&self, x: N) -> [N;4] {
[self.x(), self.y(), self.w().minus(x), self.h()]
}
#[inline] fn shrink_y (&self, y: N) -> [N;4] {
[self.x(), self.y(), self.w(), self.h().minus(y)]
}
#[inline] fn center (&self) -> [N;2] {
[self.x() + self.w() / 2.into(), self.y() + self.h() / 2.into()]
}
fn zero () -> [N;4] {
[N::zero(), N::zero(), N::zero(), N::zero()]
}

View file

@ -22,27 +22,32 @@ pub trait Content<E: Engine>: Send + Sync {
fn content (&self) -> impl Content<E> {
()
}
fn area (&self, area: E::Area) -> E::Area {
self.content().area(area)
fn layout (&self, area: E::Area) -> E::Area {
self.content().layout(area)
}
fn render (&self, output: &mut E::Output) {
self.content().render(output)
output.place(self.layout(output.area()), &self.content())
}
}
/// The platonic ideal item of content: emptiness at dead center.
impl<E: Engine> Content<E> for () {
fn area (&self, _: E::Area) -> E::Area {
[0.into(), 0.into(), 0.into(), 0.into()].into()
fn layout (&self, area: E::Area) -> E::Area {
let [x, y, w, h] = area.xywh();
let x = x + w / 2.into();
let y = y + h / 2.into();
[x, y, 0.into(), 0.into()].into()
}
fn render (&self, _: &mut E::Output) {
}
fn render (&self, _: &mut E::Output) {}
}
impl<E: Engine, T: Content<E>> Content<E> for &T {
fn content (&self) -> impl Content<E> {
(*self).content()
}
fn area (&self, area: E::Area) -> E::Area {
(*self).area(area)
fn layout (&self, area: E::Area) -> E::Area {
(*self).layout(area)
}
fn render (&self, output: &mut E::Output) {
(*self).render(output)
@ -54,9 +59,9 @@ impl<E: Engine, T: Content<E>> Content<E> for Option<T> {
self.as_ref()
.map(|content|content.content())
}
fn area (&self, area: E::Area) -> E::Area {
fn layout (&self, area: E::Area) -> E::Area {
self.as_ref()
.map(|content|content.area(area))
.map(|content|content.layout(area))
.unwrap_or([0.into(), 0.into(), 0.into(), 0.into(),].into())
}
fn render (&self, output: &mut E::Output) {

View file

@ -70,8 +70,11 @@ impl TuiOut {
}
impl Content<Tui> for &str {
fn area (&self, to: [u16;4]) -> [u16;4] {
[to[0], to[1], self.chars().count() as u16, 1]
fn layout (&self, to: [u16;4]) -> [u16;4] {
let w = self.chars().count() as u16;
let y = to.y() + to.h() / 2;
let x = (to.x() + to.w() / 2).minus(w / 2);
[x, y, w, 1]
}
fn render (&self, to: &mut TuiOut) {
to.blit(self, to.area.x(), to.area.y(), None)
@ -79,8 +82,11 @@ impl Content<Tui> for &str {
}
impl Content<Tui> for String {
fn area (&self, to: [u16;4]) -> [u16;4] {
[to[0], to[1], self.chars().count() as u16, 1]
fn layout (&self, to: [u16;4]) -> [u16;4] {
let w = self.chars().count() as u16;
let y = to.y() + to.h() / 2;
let x = (to.x() + to.w() / 2).minus(w / 2);
[x, y, w, 1]
}
fn render (&self, to: &mut TuiOut) {
to.blit(self, to.area.x(), to.area.y(), None)