mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-10 21:56:42 +01:00
wip: generic layout!
This commit is contained in:
parent
0bbf74e915
commit
93ba611e33
11 changed files with 267 additions and 82 deletions
46
crates/tek_core/src/engine/collect.rs
Normal file
46
crates/tek_core/src/engine/collect.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
use crate::*;
|
||||
|
||||
pub enum Collected<'a, E: Engine> {
|
||||
Box(Box<dyn Render<E> + 'a>),
|
||||
Ref(&'a (dyn Render<E> + 'a)),
|
||||
}
|
||||
|
||||
impl<'a, E: Engine> Render<E> for Collected<'a, E> {
|
||||
fn render (&self, to: &mut E::RenderInput) -> Perhaps<E::Rendered> {
|
||||
match self {
|
||||
Self::Box(item) => (*item).render(to),
|
||||
Self::Ref(item) => (*item).render(to),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Collection<'a, E: Engine>(
|
||||
pub Vec<Collected<'a, E>>
|
||||
);
|
||||
|
||||
impl<'a, E: Engine> Collection<'a, E> {
|
||||
pub fn new () -> Self {
|
||||
Self(vec![])
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Collect<'a, E: Engine> {
|
||||
fn add_box (self, item: Box<dyn Render<E> + 'a>) -> Self;
|
||||
fn add_ref (self, item: &'a dyn Render<E>) -> Self;
|
||||
fn add <R: Render<E> + Sized + 'a> (self, item: R) -> Self
|
||||
where Self: Sized
|
||||
{
|
||||
self.add_box(Box::new(item))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, E: Engine> Collect<'a, E> for Collection<'a, E> {
|
||||
fn add_box (mut self, item: Box<dyn Render<E> + 'a>) -> Self {
|
||||
self.0.push(Collected::Box(item));
|
||||
self
|
||||
}
|
||||
fn add_ref (mut self, item: &'a dyn Render<E>) -> Self {
|
||||
self.0.push(Collected::Ref(item));
|
||||
self
|
||||
}
|
||||
}
|
||||
7
crates/tek_core/src/engine/component.rs
Normal file
7
crates/tek_core/src/engine/component.rs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
use crate::*;
|
||||
|
||||
/// A UI component.
|
||||
pub trait Component<E: Engine>: Render<E> + Handle<E> {}
|
||||
|
||||
/// Everything that implements [Render] and [Handle] is a [Component].
|
||||
impl<E: Engine, C: Render<E> + Handle<E>> Component<E> for C {}
|
||||
20
crates/tek_core/src/engine/layered.rs
Normal file
20
crates/tek_core/src/engine/layered.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
use crate::*;
|
||||
|
||||
pub struct Layered<'a, E: Engine>(pub Collection<'a, E>);
|
||||
|
||||
impl<'a, E: Engine> Layered<'a, E> {
|
||||
pub fn new () -> Self {
|
||||
Self(Collection::new())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, E: Engine> Collect<'a, E> for Layered<'a, E> {
|
||||
fn add_box (mut self, item: Box<dyn Render<E> + 'a>) -> Self {
|
||||
self.0 = self.0.add_box(item);
|
||||
self
|
||||
}
|
||||
fn add_ref (mut self, item: &'a dyn Render<E>) -> Self {
|
||||
self.0 = self.0.add_ref(item);
|
||||
self
|
||||
}
|
||||
}
|
||||
100
crates/tek_core/src/engine/layout.rs
Normal file
100
crates/tek_core/src/engine/layout.rs
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
use crate::*;
|
||||
|
||||
/// Compute drawing area before rendering
|
||||
pub trait Layout<E: Engine> {
|
||||
fn layout (&self, area: impl Rectangle<E::Unit>) -> Perhaps<impl Rectangle<E::Unit>>;
|
||||
}
|
||||
/// Enforce minimum size of drawing area
|
||||
pub enum Min<U: Number, L> { W(U, L), H(U, L), WH(U, U, L), }
|
||||
/// Enforce maximum size of drawing area
|
||||
pub enum Max<U: Number, L> { W(U, L), H(U, L), WH(U, U, L), }
|
||||
/// Expand drawing area
|
||||
pub enum Outset<U: Number, L> { W(U, L), H(U, L), WH(U, U, L), }
|
||||
/// Shrink drawing area
|
||||
pub enum Inset<U: Number, L> { W(U, L), H(U, L), WH(U, U, L), }
|
||||
/// Move origin point of drawing area
|
||||
pub enum Offset<U: Number, L> { X(U, L), Y(U, L), XY(U, U, L), }
|
||||
|
||||
impl<E: Engine, L: Layout<E>> Layout<E> for Min<E:: Unit, L> {
|
||||
fn layout (&self, area: impl Rectangle<E::Unit>) -> Perhaps<impl Rectangle<E::Unit>> {
|
||||
match self {
|
||||
Self::W(w, item) => if area.w() < *w { Ok(None) } else {
|
||||
// TODO: axis clamp (subtract from x if width goes out of area
|
||||
item.layout([area.x(), area.y(), area.w().max(*w), area.h()])
|
||||
},
|
||||
Self::H(h, item) => if area.w() < *h { Ok(None) } else {
|
||||
// TODO: axis clamp (subtract from x if width goes out of area
|
||||
item.layout([area.x(), area.y(), area.w(), area.h().max(*h)])
|
||||
},
|
||||
Self::WH(w, h, item) => if area.w() < *w || area.h() < *h { Ok(None) } else {
|
||||
item.layout([area.x(), area.y(), area.w().max(*w), area.h().max(*h)])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Engine, L: Layout<E>> Layout<E> for Max<E:: Unit, L> {
|
||||
fn layout (&self, area: impl Rectangle<E::Unit>) -> Perhaps<impl Rectangle<E::Unit>> {
|
||||
match self {
|
||||
Self::W(w, item) => {
|
||||
// TODO: axis clamp (subtract from x if width goes out of area
|
||||
item.layout([area.x(), area.y(), area.w().min(*w), area.h()])
|
||||
},
|
||||
Self::H(h, item) => {
|
||||
// TODO: axis clamp (subtract from x if width goes out of area
|
||||
item.layout([area.x(), area.y(), area.w(), area.h().min(*h)])
|
||||
},
|
||||
Self::WH(w, h, item) => {
|
||||
item.layout([area.x(), area.y(), area.w().min(*w), area.h().min(*h)])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Engine, L: Layout<E>> Layout<E> for Outset<E::Unit, L> {
|
||||
fn layout (&self, area: impl Rectangle<E::Unit>) -> Perhaps<impl Rectangle<E::Unit>> {
|
||||
match self {
|
||||
Self::W(w, item) => if area.x() < *w { Ok(None) } else {
|
||||
item.layout([area.x() - *w, area.y(), area.w() + *w, area.h()])
|
||||
},
|
||||
Self::H(h, item) => if area.y() < *h { Ok(None) } else {
|
||||
item.layout([area.x(), area.y() - *h, area.w(), area.h() + *h])
|
||||
},
|
||||
Self::WH(w, h, item) => if area.x() < *w || area.y() < *h { Ok(None) } else {
|
||||
item.layout([area.x()-*w, area.y() - *h, area.w() + *w, area.h() + *h])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Engine, L: Layout<E>> Layout<E> for Inset<E::Unit, L> {
|
||||
fn layout (&self, area: impl Rectangle<E::Unit>) -> Perhaps<impl Rectangle<E::Unit>> {
|
||||
match self {
|
||||
Self::W(w, item) => if area.w() < *w { Ok(None) } else {
|
||||
item.layout([area.x() + *w, area.y(), area.w() - *w, area.h()])
|
||||
},
|
||||
Self::H(h, item) => if area.h() < *h { Ok(None) } else {
|
||||
item.layout([area.x(), area.y() + *h, area.w(), area.h() - *h])
|
||||
},
|
||||
Self::WH(w, h, item) => if area.w() < *w || area.h() < *h { Ok(None) } else {
|
||||
item.layout([area.x() - *w, area.y() - *h, area.w() + *w, area.h() + *h])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Engine, L: Layout<E>> Layout<E> for Offset<E::Unit, L> {
|
||||
fn layout (&self, area: impl Rectangle<E::Unit>) -> Perhaps<impl Rectangle<E::Unit>> {
|
||||
match self {
|
||||
Self::X(x, item) => if area.w() < *x { Ok(None) } else {
|
||||
item.layout([area.x() + *x, area.y(), area.w() - *x, area.h()])
|
||||
},
|
||||
Self::Y(y, item) => if area.h() < *y { Ok(None) } else {
|
||||
item.layout([area.x(), area.y() + *y, area.w(), area.h() - *y])
|
||||
},
|
||||
Self::XY(x, y, item) => if area.w() < *x || area.h() < *y { Ok(None) } else {
|
||||
item.layout([area.x() + *x, area.y() + *y, area.w() - *x, area.h() - *y])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
50
crates/tek_core/src/engine/split.rs
Normal file
50
crates/tek_core/src/engine/split.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
use crate::*;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum Direction { Up, Down, Left, Right }
|
||||
|
||||
impl Direction {
|
||||
pub fn is_down (&self) -> bool {
|
||||
match self { Self::Down => true, _ => false }
|
||||
}
|
||||
pub fn is_right (&self) -> bool {
|
||||
match self { Self::Right => true, _ => false }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Split<'a, E: Engine> {
|
||||
pub items: Collection<'a, E>,
|
||||
pub direction: Direction,
|
||||
pub focus: Option<usize>
|
||||
}
|
||||
|
||||
impl<'a, E: Engine> Split<'a, E> {
|
||||
pub fn new (direction: Direction) -> Self {
|
||||
Self {
|
||||
items: Collection::new(),
|
||||
direction,
|
||||
focus: None
|
||||
}
|
||||
}
|
||||
pub fn down () -> Self {
|
||||
Self::new(Direction::Down)
|
||||
}
|
||||
pub fn right () -> Self {
|
||||
Self::new(Direction::Right)
|
||||
}
|
||||
pub fn focus (mut self, focus: Option<usize>) -> Self {
|
||||
self.focus = focus;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, E: Engine> Collect<'a, E> for Split<'a, E> {
|
||||
fn add_box (mut self, item: Box<dyn Render<E> + 'a>) -> Self {
|
||||
self.items = self.items.add_box(item);
|
||||
self
|
||||
}
|
||||
fn add_ref (mut self, item: &'a dyn Render<E>) -> Self {
|
||||
self.items = self.items.add_ref(item);
|
||||
self
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue