mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-02-21 10:39:03 +01:00
refactor: remove Out::{Area, Size}. 7 errors left...
This commit is contained in:
parent
997610f23e
commit
07a1206c2f
4 changed files with 253 additions and 215 deletions
|
|
@ -16,7 +16,7 @@ pub(crate) use std::sync::{Arc, RwLock, atomic::{AtomicUsize, Ordering::Relaxed}
|
||||||
pub(crate) use std::marker::PhantomData;
|
pub(crate) use std::marker::PhantomData;
|
||||||
pub(crate) use dizzle::*;
|
pub(crate) use dizzle::*;
|
||||||
|
|
||||||
// Define macros first, so that private macros are available in private modules.
|
// Define macros first, so that private macros are available in private modules:
|
||||||
|
|
||||||
/// Clear a pre-allocated buffer, then write into it.
|
/// Clear a pre-allocated buffer, then write into it.
|
||||||
#[macro_export] macro_rules! rewrite {
|
#[macro_export] macro_rules! rewrite {
|
||||||
|
|
@ -117,8 +117,8 @@ macro_rules! layout_op_xy (
|
||||||
macro_rules! push_pull(($T:ident: $method: ident)=>{
|
macro_rules! push_pull(($T:ident: $method: ident)=>{
|
||||||
layout_op_xy!(1: $T);
|
layout_op_xy!(1: $T);
|
||||||
impl<O: Out, T: Layout<O>> Layout<O> for $T<O::Unit, T> {
|
impl<O: Out, T: Layout<O>> Layout<O> for $T<O::Unit, T> {
|
||||||
fn x (&self, area: O::Area) -> O::Unit { area.x().$method(self.dx()) }
|
fn x (&self, area: XYWH<O::Unit>) -> O::Unit { area.x().$method(self.dx()) }
|
||||||
fn y (&self, area: O::Area) -> O::Unit { area.y().$method(self.dy()) }
|
fn y (&self, area: XYWH<O::Unit>) -> O::Unit { area.y().$method(self.dy()) }
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,37 +49,62 @@ impl<N: Coord> WH<N> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<N: Coord> XYWH<N> {
|
impl<N: Coord> XYWH<N> {
|
||||||
fn with_w (&self, w: N) -> XYWH<N> { Self(self.x(), self.y(), w, self.h()) }
|
fn zero (&self) -> Self {
|
||||||
fn with_h (&self, h: N) -> XYWH<N> { Self(self.x(), self.y(), self.w(), h) }
|
Self(0.into(), 0.into(), 0.into(), 0.into())
|
||||||
fn lrtb (&self) -> XYWH<N> { Self(self.x(), self.x2(), self.y(), self.y2()) }
|
}
|
||||||
fn clipped_w (&self, w: N) -> XYWH<N> { Self(self.x(), self.y(), self.w().min(w), self.h()) }
|
fn x2 (&self) -> N {
|
||||||
fn clipped_h (&self, h: N) -> XYWH<N> { Self(self.x(), self.y(), self.w(), self.h().min(h)) }
|
self.x().plus(self.w())
|
||||||
fn clipped (&self, wh: impl HasWH<N>) -> XYWH<N> { Self(self.x(), self.y(), wh.w(), wh.h()) }
|
}
|
||||||
|
fn y2 (&self) -> N {
|
||||||
|
self.y().plus(self.h())
|
||||||
|
}
|
||||||
|
fn with_w (&self, w: N) -> XYWH<N> {
|
||||||
|
Self(self.x(), self.y(), w, self.h())
|
||||||
|
}
|
||||||
|
fn with_h (&self, h: N) -> XYWH<N> {
|
||||||
|
Self(self.x(), self.y(), self.w(), h)
|
||||||
|
}
|
||||||
|
fn lrtb (&self) -> XYWH<N> {
|
||||||
|
Self(self.x(), self.x2(), self.y(), self.y2())
|
||||||
|
}
|
||||||
|
fn clipped_w (&self, w: N) -> XYWH<N> {
|
||||||
|
Self(self.x(), self.y(), self.w().min(w), self.h())
|
||||||
|
}
|
||||||
|
fn clipped_h (&self, h: N) -> XYWH<N> {
|
||||||
|
Self(self.x(), self.y(), self.w(), self.h().min(h))
|
||||||
|
}
|
||||||
|
fn clipped (&self, wh: WH<N>) -> XYWH<N> {
|
||||||
|
Self(self.x(), self.y(), wh.w(), wh.h())
|
||||||
|
}
|
||||||
/// Iterate over every covered X coordinate.
|
/// Iterate over every covered X coordinate.
|
||||||
fn iter_x (&self) -> impl Iterator<Item = N> where N: std::iter::Step {
|
fn iter_x (&self) -> impl Iterator<Item = N> where N: std::iter::Step {
|
||||||
self.x()..(self.x()+self.w())
|
let Self(x, _, w, _) = *self;
|
||||||
|
x..(x+w)
|
||||||
}
|
}
|
||||||
/// Iterate over every covered Y coordinate.
|
/// Iterate over every covered Y coordinate.
|
||||||
fn iter_y (&self) -> impl Iterator<Item = N> where N: std::iter::Step {
|
fn iter_y (&self) -> impl Iterator<Item = N> where N: std::iter::Step {
|
||||||
self.y()..(self.y()+self.h())
|
let Self(_, y, _, h) = *self;
|
||||||
|
y..(y+h)
|
||||||
}
|
}
|
||||||
fn center (&self) -> XY<N> {
|
fn center (&self) -> XY<N> {
|
||||||
|
let Self(x, y, w, h) = self;
|
||||||
XY(self.x().plus(self.w()/2.into()), self.y().plus(self.h()/2.into()))
|
XY(self.x().plus(self.w()/2.into()), self.y().plus(self.h()/2.into()))
|
||||||
}
|
}
|
||||||
fn centered (&self) -> XY<N> {
|
fn centered (&self) -> XY<N> {
|
||||||
XY(self.x().minus(self.w()/2.into()), self.y().minus(self.h()/2.into()))
|
let Self(x, y, w, h) = *self;
|
||||||
|
XY(x.minus(w/2.into()), y.minus(h/2.into()))
|
||||||
}
|
}
|
||||||
fn centered_x (&self, n: N) -> XYWH<N> {
|
fn centered_x (&self, n: N) -> XYWH<N> {
|
||||||
let [x, y, w, h] = self.xywh();
|
let Self(x, y, w, h) = *self;
|
||||||
Self((x.plus(w / 2.into())).minus(n / 2.into()), y.plus(h / 2.into()), n, 1.into())
|
XYWH((x.plus(w / 2.into())).minus(n / 2.into()), y.plus(h / 2.into()), n, 1.into())
|
||||||
}
|
}
|
||||||
fn centered_y (&self, n: N) -> XYWH<N> {
|
fn centered_y (&self, n: N) -> XYWH<N> {
|
||||||
let [x, y, w, h] = self.xywh();
|
let Self(x, y, w, h) = *self;
|
||||||
Self(x.plus(w / 2.into()), (y.plus(h / 2.into())).minus(n / 2.into()), 1.into(), n)
|
XYWH(x.plus(w / 2.into()), (y.plus(h / 2.into())).minus(n / 2.into()), 1.into(), n)
|
||||||
}
|
}
|
||||||
fn centered_xy (&self, [n, m]: [N;2]) -> XYWH<N> {
|
fn centered_xy (&self, [n, m]: [N;2]) -> XYWH<N> {
|
||||||
let [x, y, w, h] = self.xywh();
|
let Self(x, y, w, h) = *self;
|
||||||
Self((x.plus(w / 2.into())).minus(n / 2.into()), (y.plus(h / 2.into())).minus(m / 2.into()), n, m)
|
XYWH((x.plus(w / 2.into())).minus(n / 2.into()), (y.plus(h / 2.into())).minus(m / 2.into()), n, m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -167,14 +192,14 @@ impl<T: PartialEq, U> Memo<T, U> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Direction {
|
impl Direction {
|
||||||
pub fn split_fixed <N: Coord> (self, area: impl HasXYWH<N>, a: N) -> ([N;4],[N;4]) {
|
pub fn split_fixed <N: Coord> (self, area: XYWH<N>, a: N) -> (XYWH<N>, XYWH<N>) {
|
||||||
let XYWH(x, y, w, h) = area.xywh();
|
let XYWH(x, y, w, h) = area;
|
||||||
match self {
|
match self {
|
||||||
North => ([x, y.plus(h).minus(a), w, a], [x, y, w, h.minus(a)]),
|
North => (XYWH(x, y.plus(h).minus(a), w, a), XYWH(x, y, w, h.minus(a))),
|
||||||
South => ([x, y, w, a], [x, y.plus(a), w, h.minus(a)]),
|
South => (XYWH(x, y, w, a), XYWH(x, y.plus(a), w, h.minus(a))),
|
||||||
East => ([x, y, a, h], [x.plus(a), y, w.minus(a), h]),
|
East => (XYWH(x, y, a, h), XYWH(x.plus(a), y, w.minus(a), h)),
|
||||||
West => ([x.plus(w).minus(a), y, a, h], [x, y, w.minus(a), h]),
|
West => (XYWH(x.plus(w).minus(a), y, a, h), XYWH(x, y, w.minus(a), h)),
|
||||||
Above | Below => (area.xywh(), area.xywh())
|
Above | Below => (area, area)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -221,7 +246,7 @@ impl<O: Out> Measure<O> {
|
||||||
pub fn format (&self) -> Arc<str> { format!("{}x{}", self.w(), self.h()).into() }
|
pub fn format (&self) -> Arc<str> { format!("{}x{}", self.w(), self.h()).into() }
|
||||||
pub fn of <T: Draw<O>> (&self, item: T) -> Bsp<Fill<&Self>, T> { Bsp::b(Fill::XY(self), item) }
|
pub fn of <T: Draw<O>> (&self, item: T) -> Bsp<Fill<&Self>, T> { Bsp::b(Fill::XY(self), item) }
|
||||||
pub fn new (x: O::Unit, y: O::Unit) -> Self {
|
pub fn new (x: O::Unit, y: O::Unit) -> Self {
|
||||||
Self { __: PhantomData::default(), x: Arc::new(x.into()), y: Arc::new(y.into()), }
|
Self { __: PhantomData::default(), x: Arc::new(x.atomic()), y: Arc::new(y.atomic()), }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -230,88 +255,91 @@ impl<O: Out> From<[O::Unit; 2]> for Measure<O> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O: Out> Layout<O> for () {
|
impl<O: Out> Layout<O> for () {
|
||||||
fn x (&self, a: O::Area) -> O::Unit { a.x() }
|
fn x (&self, a: XYWH<O::Unit>) -> O::Unit { a.x() }
|
||||||
fn y (&self, a: O::Area) -> O::Unit { a.y() }
|
fn y (&self, a: XYWH<O::Unit>) -> O::Unit { a.y() }
|
||||||
fn w (&self, _: O::Area) -> O::Unit { 0.into() }
|
fn w (&self, _: XYWH<O::Unit>) -> O::Unit { 0.into() }
|
||||||
fn w_min (&self, _: O::Area) -> O::Unit { 0.into() }
|
fn w_min (&self, _: XYWH<O::Unit>) -> O::Unit { 0.into() }
|
||||||
fn w_max (&self, _: O::Area) -> O::Unit { 0.into() }
|
fn w_max (&self, _: XYWH<O::Unit>) -> O::Unit { 0.into() }
|
||||||
fn h (&self, _: O::Area) -> O::Unit { 0.into() }
|
fn h (&self, _: XYWH<O::Unit>) -> O::Unit { 0.into() }
|
||||||
fn h_min (&self, _: O::Area) -> O::Unit { 0.into() }
|
fn h_min (&self, _: XYWH<O::Unit>) -> O::Unit { 0.into() }
|
||||||
fn h_max (&self, _: O::Area) -> O::Unit { 0.into() }
|
fn h_max (&self, _: XYWH<O::Unit>) -> O::Unit { 0.into() }
|
||||||
fn layout (&self, a: O::Area) -> O::Area { [a.x(), a.y(), 0.into(), 0.into()].into() }
|
fn layout (&self, a: XYWH<O::Unit>) -> XYWH<O::Unit> { XYWH(a.x(), a.y(), 0.into(), 0.into()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O: Out, L: Layout<O>> Layout<O> for &L {
|
impl<O: Out, L: Layout<O>> Layout<O> for &L {
|
||||||
fn x (&self, a: O::Area) -> O::Unit { (*self).x(a) }
|
fn x (&self, a: XYWH<O::Unit>) -> O::Unit { (*self).x(a) }
|
||||||
fn y (&self, a: O::Area) -> O::Unit { (*self).y(a) }
|
fn y (&self, a: XYWH<O::Unit>) -> O::Unit { (*self).y(a) }
|
||||||
fn w (&self, a: O::Area) -> O::Unit { (*self).w(a) }
|
fn w (&self, a: XYWH<O::Unit>) -> O::Unit { (*self).w(a) }
|
||||||
fn w_min (&self, a: O::Area) -> O::Unit { (*self).w_min(a) }
|
fn w_min (&self, a: XYWH<O::Unit>) -> O::Unit { (*self).w_min(a) }
|
||||||
fn w_max (&self, a: O::Area) -> O::Unit { (*self).w_max(a) }
|
fn w_max (&self, a: XYWH<O::Unit>) -> O::Unit { (*self).w_max(a) }
|
||||||
fn h (&self, a: O::Area) -> O::Unit { (*self).h(a) }
|
fn h (&self, a: XYWH<O::Unit>) -> O::Unit { (*self).h(a) }
|
||||||
fn h_min (&self, a: O::Area) -> O::Unit { (*self).h_min(a) }
|
fn h_min (&self, a: XYWH<O::Unit>) -> O::Unit { (*self).h_min(a) }
|
||||||
fn h_max (&self, a: O::Area) -> O::Unit { (*self).h_max(a) }
|
fn h_max (&self, a: XYWH<O::Unit>) -> O::Unit { (*self).h_max(a) }
|
||||||
fn layout (&self, a: O::Area) -> O::Area { (*self).layout(a) }
|
fn layout (&self, a: XYWH<O::Unit>) -> XYWH<O::Unit> { (*self).layout(a) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O: Out, L: Layout<O>> Layout<O> for &mut L {
|
impl<O: Out, L: Layout<O>> Layout<O> for &mut L {
|
||||||
fn x (&self, a: O::Area) -> O::Unit { (**self).x(a) }
|
fn x (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).x(a) }
|
||||||
fn y (&self, a: O::Area) -> O::Unit { (**self).y(a) }
|
fn y (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).y(a) }
|
||||||
fn w (&self, a: O::Area) -> O::Unit { (**self).w(a) }
|
fn w (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).w(a) }
|
||||||
fn w_min (&self, a: O::Area) -> O::Unit { (**self).w_min(a) }
|
fn w_min (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).w_min(a) }
|
||||||
fn w_max (&self, a: O::Area) -> O::Unit { (**self).w_max(a) }
|
fn w_max (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).w_max(a) }
|
||||||
fn h (&self, a: O::Area) -> O::Unit { (**self).h(a) }
|
fn h (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).h(a) }
|
||||||
fn h_min (&self, a: O::Area) -> O::Unit { (**self).h_min(a) }
|
fn h_min (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).h_min(a) }
|
||||||
fn h_max (&self, a: O::Area) -> O::Unit { (**self).h_max(a) }
|
fn h_max (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).h_max(a) }
|
||||||
fn layout (&self, a: O::Area) -> O::Area { (**self).layout(a) }
|
fn layout (&self, a: XYWH<O::Unit>) -> XYWH<O::Unit> { (**self).layout(a) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O: Out, L: Layout<O>> Layout<O> for Arc<L> {
|
impl<O: Out, L: Layout<O>> Layout<O> for Arc<L> {
|
||||||
fn x (&self, a: O::Area) -> O::Unit { (**self).x(a) }
|
fn x (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).x(a) }
|
||||||
fn y (&self, a: O::Area) -> O::Unit { (**self).y(a) }
|
fn y (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).y(a) }
|
||||||
fn w (&self, a: O::Area) -> O::Unit { (**self).w(a) }
|
fn w (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).w(a) }
|
||||||
fn w_min (&self, a: O::Area) -> O::Unit { (**self).w_min(a) }
|
fn w_min (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).w_min(a) }
|
||||||
fn w_max (&self, a: O::Area) -> O::Unit { (**self).w_max(a) }
|
fn w_max (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).w_max(a) }
|
||||||
fn h (&self, a: O::Area) -> O::Unit { (**self).h(a) }
|
fn h (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).h(a) }
|
||||||
fn h_min (&self, a: O::Area) -> O::Unit { (**self).h_min(a) }
|
fn h_min (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).h_min(a) }
|
||||||
fn h_max (&self, a: O::Area) -> O::Unit { (**self).h_max(a) }
|
fn h_max (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).h_max(a) }
|
||||||
fn layout (&self, a: O::Area) -> O::Area { (**self).layout(a) }
|
fn layout (&self, a: XYWH<O::Unit>) -> XYWH<O::Unit> { (**self).layout(a) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O: Out> Layout<O> for Box<dyn Layout<O>> {
|
impl<O: Out> Layout<O> for Box<dyn Layout<O>> {
|
||||||
fn x (&self, a: O::Area) -> O::Unit { (**self).x(a) }
|
fn x (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).x(a) }
|
||||||
fn y (&self, a: O::Area) -> O::Unit { (**self).y(a) }
|
fn y (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).y(a) }
|
||||||
fn w (&self, a: O::Area) -> O::Unit { (**self).w(a) }
|
fn w (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).w(a) }
|
||||||
fn w_min (&self, a: O::Area) -> O::Unit { (**self).w_min(a) }
|
fn w_min (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).w_min(a) }
|
||||||
fn w_max (&self, a: O::Area) -> O::Unit { (**self).w_max(a) }
|
fn w_max (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).w_max(a) }
|
||||||
fn h (&self, a: O::Area) -> O::Unit { (**self).h(a) }
|
fn h (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).h(a) }
|
||||||
fn h_min (&self, a: O::Area) -> O::Unit { (**self).h_min(a) }
|
fn h_min (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).h_min(a) }
|
||||||
fn h_max (&self, a: O::Area) -> O::Unit { (**self).h_max(a) }
|
fn h_max (&self, a: XYWH<O::Unit>) -> O::Unit { (**self).h_max(a) }
|
||||||
fn layout (&self, a: O::Area) -> O::Area { (**self).layout(a) }
|
fn layout (&self, a: XYWH<O::Unit>) -> XYWH<O::Unit> { (**self).layout(a) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O: Out, L: Layout<O>> Layout<O> for RwLock<L> {
|
impl<O: Out, L: Layout<O>> Layout<O> for RwLock<L> {
|
||||||
fn x (&self, a: O::Area) -> O::Unit { self.read().unwrap().x(a) }
|
fn x (&self, a: XYWH<O::Unit>) -> O::Unit { self.read().unwrap().x(a) }
|
||||||
fn y (&self, a: O::Area) -> O::Unit { self.read().unwrap().y(a) }
|
fn y (&self, a: XYWH<O::Unit>) -> O::Unit { self.read().unwrap().y(a) }
|
||||||
fn w (&self, a: O::Area) -> O::Unit { self.read().unwrap().w(a) }
|
fn w (&self, a: XYWH<O::Unit>) -> O::Unit { self.read().unwrap().w(a) }
|
||||||
fn w_min (&self, a: O::Area) -> O::Unit { self.read().unwrap().w_min(a) }
|
fn w_min (&self, a: XYWH<O::Unit>) -> O::Unit { self.read().unwrap().w_min(a) }
|
||||||
fn w_max (&self, a: O::Area) -> O::Unit { self.read().unwrap().w_max(a) }
|
fn w_max (&self, a: XYWH<O::Unit>) -> O::Unit { self.read().unwrap().w_max(a) }
|
||||||
fn h (&self, a: O::Area) -> O::Unit { self.read().unwrap().h(a) }
|
fn h (&self, a: XYWH<O::Unit>) -> O::Unit { self.read().unwrap().h(a) }
|
||||||
fn h_min (&self, a: O::Area) -> O::Unit { self.read().unwrap().h_min(a) }
|
fn h_min (&self, a: XYWH<O::Unit>) -> O::Unit { self.read().unwrap().h_min(a) }
|
||||||
fn h_max (&self, a: O::Area) -> O::Unit { self.read().unwrap().h_max(a) }
|
fn h_max (&self, a: XYWH<O::Unit>) -> O::Unit { self.read().unwrap().h_max(a) }
|
||||||
fn layout (&self, a: O::Area) -> O::Area { self.read().unwrap().layout(a) }
|
fn layout (&self, a: XYWH<O::Unit>) -> XYWH<O::Unit> { self.read().unwrap().layout(a) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O: Out, L: Layout<O>> Layout<O> for Option<L> {
|
impl<O: Out, L: Layout<O>> Layout<O> for Option<L> {
|
||||||
fn x (&self, to: O::Area) -> O::Unit { self.as_ref().map(|c|c.x(to)).unwrap_or(to.x()) }
|
fn x (&self, to: XYWH<O::Unit>) -> O::Unit { self.as_ref().map(|c|c.x(to)).unwrap_or(to.x()) }
|
||||||
fn y (&self, to: O::Area) -> O::Unit { self.as_ref().map(|c|c.y(to)).unwrap_or(to.y()) }
|
fn y (&self, to: XYWH<O::Unit>) -> O::Unit { self.as_ref().map(|c|c.y(to)).unwrap_or(to.y()) }
|
||||||
fn w_min (&self, to: O::Area) -> O::Unit { self.as_ref().map(|c|c.w_min(to)).unwrap_or(0.into()) }
|
fn w_min (&self, to: XYWH<O::Unit>) -> O::Unit { self.as_ref().map(|c|c.w_min(to)).unwrap_or(0.into()) }
|
||||||
fn w_max (&self, to: O::Area) -> O::Unit { self.as_ref().map(|c|c.w_max(to)).unwrap_or(0.into()) }
|
fn w_max (&self, to: XYWH<O::Unit>) -> O::Unit { self.as_ref().map(|c|c.w_max(to)).unwrap_or(0.into()) }
|
||||||
fn w (&self, to: O::Area) -> O::Unit { self.as_ref().map(|c|c.w(to)).unwrap_or(0.into()) }
|
fn w (&self, to: XYWH<O::Unit>) -> O::Unit { self.as_ref().map(|c|c.w(to)).unwrap_or(0.into()) }
|
||||||
fn h_min (&self, to: O::Area) -> O::Unit { self.as_ref().map(|c|c.h_min(to)).unwrap_or(0.into()) }
|
fn h_min (&self, to: XYWH<O::Unit>) -> O::Unit { self.as_ref().map(|c|c.h_min(to)).unwrap_or(0.into()) }
|
||||||
fn h_max (&self, to: O::Area) -> O::Unit { self.as_ref().map(|c|c.h_max(to)).unwrap_or(0.into()) }
|
fn h_max (&self, to: XYWH<O::Unit>) -> O::Unit { self.as_ref().map(|c|c.h_max(to)).unwrap_or(0.into()) }
|
||||||
fn h (&self, to: O::Area) -> O::Unit { self.as_ref().map(|c|c.h(to)).unwrap_or(0.into()) }
|
fn h (&self, to: XYWH<O::Unit>) -> O::Unit { self.as_ref().map(|c|c.h(to)).unwrap_or(0.into()) }
|
||||||
fn layout (&self, to: O::Area) -> O::Area { self.as_ref().map(|c|c.layout([self.x(to), self.y(to), self.w(to), self.h(to)].into()))
|
fn layout (&self, to: XYWH<O::Unit>) -> XYWH<O::Unit> {
|
||||||
.unwrap_or([to.x(), to.y(), 0.into(), 0.into()].into()) }
|
self.as_ref()
|
||||||
|
.map(|c|c.layout(XYWH(self.x(to), self.y(to), self.w(to), self.h(to)).into()))
|
||||||
|
.unwrap_or(XYWH(to.x(), to.y(), 0.into(), 0.into()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O: Out, D: Content<O>> HasContent<O> for Bounded<O, D> {
|
impl<O: Out, D: Content<O>> HasContent<O> for Bounded<O, D> {
|
||||||
|
|
@ -335,9 +363,9 @@ impl<O: Out, T: Content<O>> When<O, T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O: Out, T: Layout<O>> Layout<O> for When<O, T> {
|
impl<O: Out, T: Layout<O>> Layout<O> for When<O, T> {
|
||||||
fn layout (&self, to: O::Area) -> O::Area {
|
fn layout (&self, to: XYWH<O::Unit>) -> XYWH<O::Unit> {
|
||||||
let Self(cond, item, ..) = self;
|
let Self(cond, item, ..) = self;
|
||||||
if *cond { item.layout(to) } else { O::Area::zero().into() }
|
if *cond { item.layout(to) } else { XYWH::<O::Unit>::zero().into() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -356,7 +384,7 @@ impl<E: Out, A: Content<E>, B: Content<E>> Either<E, A, B> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Out, A: Layout<E>, B: Layout<E>> Layout<E> for Either<E, A, B> {
|
impl<E: Out, A: Layout<E>, B: Layout<E>> Layout<E> for Either<E, A, B> {
|
||||||
fn layout (&self, to: E::Area) -> E::Area {
|
fn layout (&self, to: XYWH<E::Unit>) -> XYWH<E::Unit> {
|
||||||
let Self(cond, a, b, ..) = self;
|
let Self(cond, a, b, ..) = self;
|
||||||
if *cond { a.layout(to) } else { b.layout(to) }
|
if *cond { a.layout(to) } else { b.layout(to) }
|
||||||
}
|
}
|
||||||
|
|
@ -376,14 +404,14 @@ impl<E: Out, A: Content<E>, B: Content<E>> Draw<E> for Either<E, A, B> {
|
||||||
|
|
||||||
layout_op_xy!(0: Fill);
|
layout_op_xy!(0: Fill);
|
||||||
impl<O: Out, T: Layout<O>> Layout<O> for Fill<T> {
|
impl<O: Out, T: Layout<O>> Layout<O> for Fill<T> {
|
||||||
fn x (&self, area: O::Area) -> O::Unit { if self.dx() { area.x() } else { self.inner().x(area) } }
|
fn x (&self, area: XYWH<O::Unit>) -> O::Unit { if self.dx() { area.x() } else { self.inner().x(area) } }
|
||||||
fn y (&self, area: O::Area) -> O::Unit { if self.dy() { area.y() } else { self.inner().y(area) } }
|
fn y (&self, area: XYWH<O::Unit>) -> O::Unit { if self.dy() { area.y() } else { self.inner().y(area) } }
|
||||||
fn w (&self, area: O::Area) -> O::Unit { if self.dx() { area.w() } else { self.inner().w(area) } }
|
fn w (&self, area: XYWH<O::Unit>) -> O::Unit { if self.dx() { area.w() } else { self.inner().w(area) } }
|
||||||
fn w_min (&self, area: O::Area) -> O::Unit { if self.dx() { area.w() } else { self.inner().w_min(area) } }
|
fn w_min (&self, area: XYWH<O::Unit>) -> O::Unit { if self.dx() { area.w() } else { self.inner().w_min(area) } }
|
||||||
fn w_max (&self, area: O::Area) -> O::Unit { if self.dx() { area.w() } else { self.inner().w_max(area) } }
|
fn w_max (&self, area: XYWH<O::Unit>) -> O::Unit { if self.dx() { area.w() } else { self.inner().w_max(area) } }
|
||||||
fn h (&self, area: O::Area) -> O::Unit { if self.dy() { area.h() } else { self.inner().h(area) } }
|
fn h (&self, area: XYWH<O::Unit>) -> O::Unit { if self.dy() { area.h() } else { self.inner().h(area) } }
|
||||||
fn h_min (&self, area: O::Area) -> O::Unit { if self.dy() { area.h() } else { self.inner().h_min(area) } }
|
fn h_min (&self, area: XYWH<O::Unit>) -> O::Unit { if self.dy() { area.h() } else { self.inner().h_min(area) } }
|
||||||
fn h_max (&self, area: O::Area) -> O::Unit { if self.dy() { area.h() } else { self.inner().h_max(area) } }
|
fn h_max (&self, area: XYWH<O::Unit>) -> O::Unit { if self.dy() { area.h() } else { self.inner().h_max(area) } }
|
||||||
}
|
}
|
||||||
impl<A> Fill<A> {
|
impl<A> Fill<A> {
|
||||||
#[inline] pub const fn dx (&self) -> bool { matches!(self, Self::X(_) | Self::XY(_)) }
|
#[inline] pub const fn dx (&self) -> bool { matches!(self, Self::X(_) | Self::XY(_)) }
|
||||||
|
|
@ -392,53 +420,57 @@ impl<E: Out, A: Content<E>, B: Content<E>> Draw<E> for Either<E, A, B> {
|
||||||
|
|
||||||
layout_op_xy!(1 opt: Fixed);
|
layout_op_xy!(1 opt: Fixed);
|
||||||
impl<O: Out, T: Layout<O>> Layout<O> for Fixed<O::Unit, T> {
|
impl<O: Out, T: Layout<O>> Layout<O> for Fixed<O::Unit, T> {
|
||||||
fn w (&self, area: O::Area) -> O::Unit { self.dx().unwrap_or(self.inner().w(area)) }
|
fn w (&self, area: XYWH<O::Unit>) -> O::Unit { self.dx().unwrap_or(self.inner().w(area)) }
|
||||||
fn w_min (&self, area: O::Area) -> O::Unit { self.dx().unwrap_or(self.inner().w_min(area)) }
|
fn w_min (&self, area: XYWH<O::Unit>) -> O::Unit { self.dx().unwrap_or(self.inner().w_min(area)) }
|
||||||
fn w_max (&self, area: O::Area) -> O::Unit { self.dx().unwrap_or(self.inner().w_max(area)) }
|
fn w_max (&self, area: XYWH<O::Unit>) -> O::Unit { self.dx().unwrap_or(self.inner().w_max(area)) }
|
||||||
fn h (&self, area: O::Area) -> O::Unit { self.dy().unwrap_or(self.inner().h(area)) }
|
fn h (&self, area: XYWH<O::Unit>) -> O::Unit { self.dy().unwrap_or(self.inner().h(area)) }
|
||||||
fn h_min (&self, area: O::Area) -> O::Unit { self.dy().unwrap_or(self.inner().h_min(area)) }
|
fn h_min (&self, area: XYWH<O::Unit>) -> O::Unit { self.dy().unwrap_or(self.inner().h_min(area)) }
|
||||||
fn h_max (&self, area: O::Area) -> O::Unit { self.dy().unwrap_or(self.inner().h_max(area)) }
|
fn h_max (&self, area: XYWH<O::Unit>) -> O::Unit { self.dy().unwrap_or(self.inner().h_max(area)) }
|
||||||
}
|
}
|
||||||
|
|
||||||
layout_op_xy!(1 opt: Max);
|
layout_op_xy!(1 opt: Max);
|
||||||
impl<E: Out, T: Layout<E>> Layout<E> for Max<E::Unit, T> {
|
impl<E: Out, T: Layout<E>> Layout<E> for Max<E::Unit, T> {
|
||||||
fn layout (&self, area: E::Area) -> E::Area {
|
fn layout (&self, area: XYWH<E::Unit>) -> XYWH<E::Unit> {
|
||||||
let [x, y, w, h] = self.inner().layout(area).xywh();
|
let XYWH(x, y, w, h) = self.inner().layout(area);
|
||||||
match self {
|
match self {
|
||||||
Self::X(mw, _) => [x, y, w.min(*mw), h ],
|
Self::X(mw, _) => XYWH(x, y, w.min(*mw), h ),
|
||||||
Self::Y(mh, _) => [x, y, w, h.min(*mh)],
|
Self::Y(mh, _) => XYWH(x, y, w, h.min(*mh)),
|
||||||
Self::XY(mw, mh, _) => [x, y, w.min(*mw), h.min(*mh)],
|
Self::XY(mw, mh, _) => XYWH(x, y, w.min(*mw), h.min(*mh)),
|
||||||
}.into()
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
layout_op_xy!(1 opt: Min);
|
layout_op_xy!(1 opt: Min);
|
||||||
impl<E: Out, T: Layout<E>> Layout<E> for Min<E::Unit, T> {
|
impl<E: Out, T: Layout<E>> Layout<E> for Min<E::Unit, T> {
|
||||||
fn layout (&self, area: E::Area) -> E::Area {
|
fn layout (&self, area: XYWH<E::Unit>) -> XYWH<E::Unit> {
|
||||||
let [x, y, w, h] = self.inner().layout(area).xywh();
|
let XYWH(x, y, w, h) = self.inner().layout(area);
|
||||||
match self {
|
match self {
|
||||||
Self::X(mw, _) => [x, y, w.max(*mw), h],
|
Self::X(mw, _) => XYWH(x, y, w.max(*mw), h),
|
||||||
Self::Y(mh, _) => [x, y, w, h.max(*mh)],
|
Self::Y(mh, _) => XYWH(x, y, w, h.max(*mh)),
|
||||||
Self::XY(mw, mh, _) => [x, y, w.max(*mw), h.max(*mh)],
|
Self::XY(mw, mh, _) => XYWH(x, y, w.max(*mw), h.max(*mh)),
|
||||||
}.into()
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
layout_op_xy!(1 opt: Expand);
|
layout_op_xy!(1 opt: Expand);
|
||||||
impl<O: Out, T: Layout<O>> Layout<O> for Expand<O::Unit, T> {
|
impl<O: Out, T: Layout<O>> Layout<O> for Expand<O::Unit, T> {
|
||||||
fn w (&self, to: O::Area) -> O::Unit { self.inner().w(to).plus(self.dx().unwrap_or_default()) }
|
fn w (&self, to: XYWH<O::Unit>) -> O::Unit {
|
||||||
fn h (&self, to: O::Area) -> O::Unit { self.inner().w(to).plus(self.dy().unwrap_or_default()) }
|
self.inner().w(to).plus(self.dx().unwrap_or_default())
|
||||||
|
}
|
||||||
|
fn h (&self, to: XYWH<O::Unit>) -> O::Unit {
|
||||||
|
self.inner().w(to).plus(self.dy().unwrap_or_default())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: why they differ?
|
// FIXME: why they differ?
|
||||||
|
|
||||||
layout_op_xy!(1 opt: Shrink);
|
layout_op_xy!(1 opt: Shrink);
|
||||||
impl<E: Out, T: Layout<E>> Layout<E> for Shrink<E::Unit, T> {
|
impl<E: Out, T: Layout<E>> Layout<E> for Shrink<E::Unit, T> {
|
||||||
fn layout (&self, to: E::Area) -> E::Area {
|
fn layout (&self, to: XYWH<E::Unit>) -> XYWH<E::Unit> {
|
||||||
let area = self.inner().layout(to);
|
let area = self.inner().layout(to);
|
||||||
let dx = self.dx().unwrap_or_default();
|
let dx = self.dx().unwrap_or_default();
|
||||||
let dy = self.dy().unwrap_or_default();
|
let dy = self.dy().unwrap_or_default();
|
||||||
[area.x(), area.y(), area.w().minus(dx), area.h().minus(dy)].into()
|
XYWH(area.x(), area.y(), area.w().minus(dx), area.h().minus(dy))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -461,7 +493,7 @@ impl<O: Out, T: Content<O>> Draw<O> for Align<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O: Out, T: Layout<O>> Layout<O> for Align<T> {
|
impl<O: Out, T: Layout<O>> Layout<O> for Align<T> {
|
||||||
fn x (&self, to: O::Area) -> O::Unit {
|
fn x (&self, to: XYWH<O::Unit>) -> O::Unit {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
NW | W | SW => to.x(),
|
NW | W | SW => to.x(),
|
||||||
N | Center | S => to.x().plus(to.w() / 2.into()).minus(self.1.w(to) / 2.into()),
|
N | Center | S => to.x().plus(to.w() / 2.into()).minus(self.1.w(to) / 2.into()),
|
||||||
|
|
@ -469,7 +501,7 @@ impl<O: Out, T: Layout<O>> Layout<O> for Align<T> {
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn y (&self, to: O::Area) -> O::Unit {
|
fn y (&self, to: XYWH<O::Unit>) -> O::Unit {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
NW | N | NE => to.y(),
|
NW | N | NE => to.y(),
|
||||||
W | Center | E => to.y().plus(to.h() / 2.into()).minus(self.1.h(to) / 2.into()),
|
W | Center | E => to.y().plus(to.h() / 2.into()).minus(self.1.h(to) / 2.into()),
|
||||||
|
|
@ -499,10 +531,10 @@ impl<O: Out, T: Content<O>> Draw<O> for Pad<O::Unit, T> {
|
||||||
fn draw (&self, to: &mut O) { Bounded(self.layout(to.area()), self.inner()).draw(to) }
|
fn draw (&self, to: &mut O) { Bounded(self.layout(to.area()), self.inner()).draw(to) }
|
||||||
}
|
}
|
||||||
impl<O: Out, T: Layout<O>> Layout<O> for Pad<O::Unit, T> {
|
impl<O: Out, T: Layout<O>> Layout<O> for Pad<O::Unit, T> {
|
||||||
fn x (&self, area: O::Area) -> O::Unit { area.x().plus(self.dx()) }
|
fn x (&self, area: XYWH<O::Unit>) -> O::Unit { area.x().plus(self.dx()) }
|
||||||
fn y (&self, area: O::Area) -> O::Unit { area.x().plus(self.dx()) }
|
fn y (&self, area: XYWH<O::Unit>) -> O::Unit { area.x().plus(self.dx()) }
|
||||||
fn w (&self, area: O::Area) -> O::Unit { area.w().minus(self.dx() * 2.into()) }
|
fn w (&self, area: XYWH<O::Unit>) -> O::Unit { area.w().minus(self.dx() * 2.into()) }
|
||||||
fn h (&self, area: O::Area) -> O::Unit { area.h().minus(self.dy() * 2.into()) }
|
fn h (&self, area: XYWH<O::Unit>) -> O::Unit { area.h().minus(self.dy() * 2.into()) }
|
||||||
}
|
}
|
||||||
impl<Head, Tail> Bsp<Head, Tail> {
|
impl<Head, Tail> Bsp<Head, Tail> {
|
||||||
#[inline] pub const fn n (a: Head, b: Tail) -> Self { Self(North, a, b) }
|
#[inline] pub const fn n (a: Head, b: Tail) -> Self { Self(North, a, b) }
|
||||||
|
|
@ -519,12 +551,10 @@ impl<O: Out, Head: Content<O>, Tail: Content<O>> Draw<O> for Bsp<Head, Tail> {
|
||||||
South => {
|
South => {
|
||||||
//panic!("{}", self.1.h(to.area()));
|
//panic!("{}", self.1.h(to.area()));
|
||||||
let area_1 = self.1.layout(to.area());
|
let area_1 = self.1.layout(to.area());
|
||||||
let area_2 = self.2.layout([
|
let area_2 = self.2.layout(XYWH(
|
||||||
to.area().x(),
|
to.area().x(), to.area().y().plus(area_1.h()),
|
||||||
to.area().y().plus(area_1.h()),
|
to.area().w(), to.area().h().minus(area_1.h())
|
||||||
to.area().w(),
|
));
|
||||||
to.area().h().minus(area_1.h())
|
|
||||||
].into());
|
|
||||||
//panic!("{area_1:?} {area_2:?}");
|
//panic!("{area_1:?} {area_2:?}");
|
||||||
to.place_at(area_1, &self.1);
|
to.place_at(area_1, &self.1);
|
||||||
to.place_at(area_2, &self.2);
|
to.place_at(area_2, &self.2);
|
||||||
|
|
@ -543,69 +573,75 @@ impl<O: Out, Head: Content<O>, Tail: Content<O>> Draw<O> for Bsp<Head, Tail> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<O: Out, Head: Layout<O>, Tail: Layout<O>> Layout<O> for Bsp<Head, Tail> {
|
impl<O: Out, Head: Layout<O>, Tail: Layout<O>> Layout<O> for Bsp<Head, Tail> {
|
||||||
fn w (&self, area: O::Area) -> O::Unit {
|
fn w (&self, area: XYWH<O::Unit>) -> O::Unit {
|
||||||
match self.0 { Above | Below | North | South => self.1.w(area).max(self.2.w(area)), East | West => self.1.w_min(area).plus(self.2.w(area)), }
|
match self.0 { Above | Below | North | South => self.1.w(area).max(self.2.w(area)), East | West => self.1.w_min(area).plus(self.2.w(area)), }
|
||||||
}
|
}
|
||||||
fn w_min (&self, area: O::Area) -> O::Unit {
|
fn w_min (&self, area: XYWH<O::Unit>) -> O::Unit {
|
||||||
match self.0 { Above | Below | North | South => self.1.w_min(area).max(self.2.w_min(area)), East | West => self.1.w_min(area).plus(self.2.w_min(area)), }
|
match self.0 { Above | Below | North | South => self.1.w_min(area).max(self.2.w_min(area)), East | West => self.1.w_min(area).plus(self.2.w_min(area)), }
|
||||||
}
|
}
|
||||||
fn w_max (&self, area: O::Area) -> O::Unit {
|
fn w_max (&self, area: XYWH<O::Unit>) -> O::Unit {
|
||||||
match self.0 { Above | Below | North | South => self.1.w_max(area).max(self.2.w_max(area)), East | West => self.1.w_max(area).plus(self.2.w_max(area)), }
|
match self.0 { Above | Below | North | South => self.1.w_max(area).max(self.2.w_max(area)), East | West => self.1.w_max(area).plus(self.2.w_max(area)), }
|
||||||
}
|
}
|
||||||
fn h (&self, area: O::Area) -> O::Unit {
|
fn h (&self, area: XYWH<O::Unit>) -> O::Unit {
|
||||||
match self.0 { Above | Below | East | West => self.1.h(area).max(self.2.h(area)), North | South => self.1.h(area).plus(self.2.h(area)), }
|
match self.0 { Above | Below | East | West => self.1.h(area).max(self.2.h(area)), North | South => self.1.h(area).plus(self.2.h(area)), }
|
||||||
}
|
}
|
||||||
fn h_min (&self, area: O::Area) -> O::Unit {
|
fn h_min (&self, area: XYWH<O::Unit>) -> O::Unit {
|
||||||
match self.0 { Above | Below | East | West => self.1.h_min(area).max(self.2.h_min(area)), North | South => self.1.h_min(area).plus(self.2.h_min(area)), }
|
match self.0 { Above | Below | East | West => self.1.h_min(area).max(self.2.h_min(area)), North | South => self.1.h_min(area).plus(self.2.h_min(area)), }
|
||||||
}
|
}
|
||||||
fn h_max (&self, area: O::Area) -> O::Unit {
|
fn h_max (&self, area: XYWH<O::Unit>) -> O::Unit {
|
||||||
match self.0 { Above | Below | North | South => self.1.h_max(area).max(self.2.h_max(area)), East | West => self.1.h_max(area).plus(self.2.h_max(area)), }
|
match self.0 { Above | Below | North | South => self.1.h_max(area).max(self.2.h_max(area)), East | West => self.1.h_max(area).plus(self.2.h_max(area)), }
|
||||||
}
|
}
|
||||||
fn layout (&self, area: O::Area) -> O::Area {
|
fn layout (&self, area: XYWH<O::Unit>) -> XYWH<O::Unit> {
|
||||||
bsp_areas(area, self.0, &self.1, &self.2)[2]
|
bsp_areas(area, self.0, &self.1, &self.2)[2]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bsp_areas <O: Out, A: Layout<O>, B: Layout<O>> (area: O::Area, direction: Direction, a: &A, b: &B,) -> [O::Area;3] {
|
fn bsp_areas <O: Out, A: Layout<O>, B: Layout<O>> (
|
||||||
let [x, y, w, h] = area.xywh();
|
area: XYWH<O::Unit>,
|
||||||
let [aw, ah] = a.layout(area).wh();
|
direction: Direction,
|
||||||
let [bw, bh] = b.layout(match direction {
|
a: &A,
|
||||||
Above | Below => area,
|
b: &B,
|
||||||
South => [x, y + ah, w, h.minus(ah)].into(),
|
) -> [XYWH<O::Unit>;3] {
|
||||||
North => [x, y, w, h.minus(ah)].into(),
|
let XYWH(x, y, w, h) = area;
|
||||||
East => [x + aw, y, w.minus(aw), h].into(),
|
let WH(aw, ah) = a.layout(area).wh();
|
||||||
West => [x, y, w.minus(aw), h].into(),
|
let WH(bw, bh) = b.layout(match direction {
|
||||||
|
South => XYWH(x, y + ah, w, h.minus(ah)),
|
||||||
|
North => XYWH(x, y, w, h.minus(ah)),
|
||||||
|
East => XYWH(x + aw, y, w.minus(aw), h),
|
||||||
|
West => XYWH(x, y, w.minus(aw), h),
|
||||||
|
Above => area,
|
||||||
|
Below => area,
|
||||||
}).wh();
|
}).wh();
|
||||||
match direction {
|
match direction {
|
||||||
Above | Below => {
|
Above | Below => {
|
||||||
let [x, y, w, h] = area.center_xy([aw.max(bw), ah.max(bh)]);
|
let XYWH(x, y, w, h) = area.centered_xy([aw.max(bw), ah.max(bh)]);
|
||||||
let a = [(x + w/2.into()).minus(aw/2.into()), (y + h/2.into()).minus(ah/2.into()), aw, ah];
|
let a = XYWH((x + w/2.into()).minus(aw/2.into()), (y + h/2.into()).minus(ah/2.into()), aw, ah);
|
||||||
let b = [(x + w/2.into()).minus(bw/2.into()), (y + h/2.into()).minus(bh/2.into()), bw, bh];
|
let b = XYWH((x + w/2.into()).minus(bw/2.into()), (y + h/2.into()).minus(bh/2.into()), bw, bh);
|
||||||
[a.into(), b.into(), [x, y, w, h].into()]
|
[a.into(), b.into(), XYWH(x, y, w, h)]
|
||||||
},
|
},
|
||||||
South => {
|
South => {
|
||||||
let [x, y, w, h] = area.center_xy([aw.max(bw), ah + bh]);
|
let XYWH(x, y, w, h) = area.centered_xy([aw.max(bw), ah + bh]);
|
||||||
let a = [(x + w/2.into()).minus(aw/2.into()), y, aw, ah];
|
let a = XYWH((x + w/2.into()).minus(aw/2.into()), y, aw, ah);
|
||||||
let b = [(x + w/2.into()).minus(bw/2.into()), y + ah, bw, bh];
|
let b = XYWH((x + w/2.into()).minus(bw/2.into()), y + ah, bw, bh);
|
||||||
[a.into(), b.into(), [x, y, w, h].into()]
|
[a.into(), b.into(), XYWH(x, y, w, h)]
|
||||||
},
|
},
|
||||||
North => {
|
North => {
|
||||||
let [x, y, w, h] = area.center_xy([aw.max(bw), ah + bh]);
|
let XYWH(x, y, w, h) = area.centered_xy([aw.max(bw), ah + bh]);
|
||||||
let a = [(x + (w/2.into())).minus(aw/2.into()), y + bh, aw, ah];
|
let a = XYWH((x + (w/2.into())).minus(aw/2.into()), y + bh, aw, ah);
|
||||||
let b = [(x + (w/2.into())).minus(bw/2.into()), y, bw, bh];
|
let b = XYWH((x + (w/2.into())).minus(bw/2.into()), y, bw, bh);
|
||||||
[a.into(), b.into(), [x, y, w, h].into()]
|
[a.into(), b.into(), XYWH(x, y, w, h)]
|
||||||
},
|
},
|
||||||
East => {
|
East => {
|
||||||
let [x, y, w, h] = area.center_xy([aw + bw, ah.max(bh)]);
|
let XYWH(x, y, w, h) = area.centered_xy([aw + bw, ah.max(bh)]);
|
||||||
let a = [x, (y + h/2.into()).minus(ah/2.into()), aw, ah];
|
let a = XYWH(x, (y + h/2.into()).minus(ah/2.into()), aw, ah);
|
||||||
let b = [x + aw, (y + h/2.into()).minus(bh/2.into()), bw, bh];
|
let b = XYWH(x + aw, (y + h/2.into()).minus(bh/2.into()), bw, bh);
|
||||||
[a.into(), b.into(), [x, y, w, h].into()]
|
[a.into(), b.into(), XYWH(x, y, w, h)]
|
||||||
},
|
},
|
||||||
West => {
|
West => {
|
||||||
let [x, y, w, h] = area.center_xy([aw + bw, ah.max(bh)]);
|
let XYWH(x, y, w, h) = area.centered_xy([aw + bw, ah.max(bh)]);
|
||||||
let a = [x + bw, (y + h/2.into()).minus(ah/2.into()), aw, ah];
|
let a = XYWH(x + bw, (y + h/2.into()).minus(ah/2.into()), aw, ah);
|
||||||
let b = [x, (y + h/2.into()).minus(bh/2.into()), bw, bh];
|
let b = XYWH(x, (y + h/2.into()).minus(bh/2.into()), bw, bh);
|
||||||
[a.into(), b.into(), [x, y, w, h].into()]
|
[a.into(), b.into(), XYWH(x, y, w, h)]
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -635,13 +671,13 @@ impl<'a, O, A, B, I, F, G> Layout<O> for Map<O, A, B, I, F, G> where
|
||||||
F: Fn() -> I + Send + Sync + 'a,
|
F: Fn() -> I + Send + Sync + 'a,
|
||||||
G: Fn(A, usize)->B + Send + Sync
|
G: Fn(A, usize)->B + Send + Sync
|
||||||
{
|
{
|
||||||
fn layout (&self, area: O::Area) -> O::Area {
|
fn layout (&self, area: XYWH<O::Unit>) -> XYWH<O::Unit> {
|
||||||
let Self { get_iter, get_item, .. } = self;
|
let Self { get_iter, get_item, .. } = self;
|
||||||
let mut index = 0;
|
let mut index = 0;
|
||||||
let [mut min_x, mut min_y] = area.center();
|
let [mut min_x, mut min_y] = area.center();
|
||||||
let [mut max_x, mut max_y] = area.center();
|
let [mut max_x, mut max_y] = area.center();
|
||||||
for item in get_iter() {
|
for item in get_iter() {
|
||||||
let [x,y,w,h] = get_item(item, index).layout(area).xywh();
|
let XYWH(x, y, w, h) = get_item(item, index).layout(area);
|
||||||
min_x = min_x.min(x);
|
min_x = min_x.min(x);
|
||||||
min_y = min_y.min(y);
|
min_y = min_y.min(y);
|
||||||
max_x = max_x.max(x + w);
|
max_x = max_x.max(x + w);
|
||||||
|
|
@ -651,7 +687,7 @@ impl<'a, O, A, B, I, F, G> Layout<O> for Map<O, A, B, I, F, G> where
|
||||||
let w = max_x - min_x;
|
let w = max_x - min_x;
|
||||||
let h = max_y - min_y;
|
let h = max_y - min_y;
|
||||||
//[min_x.into(), min_y.into(), w.into(), h.into()].into()
|
//[min_x.into(), min_y.into(), w.into(), h.into()].into()
|
||||||
area.center_xy([w.into(), h.into()]).into()
|
area.centered_xy([w.into(), h.into()])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<'a, O, A, B, I, F, G> Draw<O> for Map<O, A, B, I, F, G> where
|
impl<'a, O, A, B, I, F, G> Draw<O> for Map<O, A, B, I, F, G> where
|
||||||
|
|
@ -720,15 +756,11 @@ impl<A, B, C> Tryptich<A, B, C> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O: Out, Color, Item: Layout<O>> Layout<O> for Foreground<Color, Item> {
|
impl<O: Out, Color, Item: Layout<O>> Layout<O> for Foreground<Color, Item> {
|
||||||
fn layout (&self, to: O::Area) -> O::Area {
|
fn layout (&self, to: XYWH<O::Unit>) -> XYWH<O::Unit> { self.1.layout(to) }
|
||||||
self.1.layout(to)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O: Out, Color, Item: Layout<O>> Layout<O> for Background<Color, Item> {
|
impl<O: Out, Color, Item: Layout<O>> Layout<O> for Background<Color, Item> {
|
||||||
fn layout (&self, to: O::Area) -> O::Area {
|
fn layout (&self, to: XYWH<O::Unit>) -> XYWH<O::Unit> { self.1.layout(to) }
|
||||||
self.1.layout(to)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O: Out, T, L: Content<O>, V: Content<O>> HasContent<O> for FieldH<T, L, V> {
|
impl<O: Out, T, L: Content<O>, V: Content<O>> HasContent<O> for FieldH<T, L, V> {
|
||||||
|
|
@ -736,7 +768,7 @@ impl<O: Out, T, L: Content<O>, V: Content<O>> HasContent<O> for FieldH<T, L, V>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O: Out, T, L: Content<O>, V: Content<O>> Layout<O> for FieldH<T, L, V> {
|
impl<O: Out, T, L: Content<O>, V: Content<O>> Layout<O> for FieldH<T, L, V> {
|
||||||
fn layout (&self, to: O::Area) -> O::Area { self.content().layout(to) }
|
fn layout (&self, to: XYWH<O::Unit>) -> XYWH<O::Unit> { self.content().layout(to) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O: Out, T, L: Content<O>, V: Content<O>> Draw<O> for FieldH<T, L, V> {
|
impl<O: Out, T, L: Content<O>, V: Content<O>> Draw<O> for FieldH<T, L, V> {
|
||||||
|
|
@ -748,14 +780,14 @@ impl<O: Out, T, L: Content<O>, V: Content<O>> HasContent<O> for FieldV<T, L, V>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O: Out, T, L: Content<O>, V: Content<O>> Layout<O> for FieldV<T, L, V> {
|
impl<O: Out, T, L: Content<O>, V: Content<O>> Layout<O> for FieldV<T, L, V> {
|
||||||
fn layout (&self, to: O::Area) -> O::Area { self.content().layout(to) }
|
fn layout (&self, to: XYWH<O::Unit>) -> XYWH<O::Unit> { self.content().layout(to) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O: Out, T, L: Content<O>, V: Content<O>> Draw<O> for FieldV<T, L, V> {
|
impl<O: Out, T, L: Content<O>, V: Content<O>> Draw<O> for FieldV<T, L, V> {
|
||||||
fn draw (&self, to: &mut O) { self.content().draw(to) }
|
fn draw (&self, to: &mut O) { self.content().draw(to) }
|
||||||
}
|
}
|
||||||
impl<O: Out, S: Layout<O>> Layout<O> for Border<S> {
|
impl<O: Out, S: Layout<O>> Layout<O> for Border<S> {
|
||||||
fn layout (&self, area: O::Area) -> O::Area {
|
fn layout (&self, area: XYWH<O::Unit>) -> XYWH<O::Unit> {
|
||||||
self.1.layout(area)
|
self.1.layout(area)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,14 +5,16 @@ use crate::*;
|
||||||
/// ```
|
/// ```
|
||||||
/// let xy: XY<u16> = XY(0, 0);
|
/// let xy: XY<u16> = XY(0, 0);
|
||||||
/// ```
|
/// ```
|
||||||
pub struct XY<C: Coord>(pub C, pub C);
|
#[cfg_attr(test, derive(Arbitrary))]
|
||||||
|
#[derive(Copy, Clone)] pub struct XY<C: Coord>(pub C, pub C);
|
||||||
|
|
||||||
/// A size (Width, Height).
|
/// A size (Width, Height).
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let wh: WH<u16> = WH(0, 0);
|
/// let wh: WH<u16> = WH(0, 0);
|
||||||
/// ```
|
/// ```
|
||||||
pub struct WH<C: Coord>(pub C, pub C);
|
#[cfg_attr(test, derive(Arbitrary))]
|
||||||
|
#[derive(Copy, Clone)] pub struct WH<C: Coord>(pub C, pub C);
|
||||||
|
|
||||||
/// Point with size.
|
/// Point with size.
|
||||||
///
|
///
|
||||||
|
|
@ -22,15 +24,16 @@ pub struct WH<C: Coord>(pub C, pub C);
|
||||||
///
|
///
|
||||||
/// * [ ] TODO: anchor field (determines at which corner/side is X0 Y0)
|
/// * [ ] TODO: anchor field (determines at which corner/side is X0 Y0)
|
||||||
///
|
///
|
||||||
pub struct XYWH<C: Coord>(pub C, pub C, pub C, pub C);
|
#[cfg_attr(test, derive(Arbitrary))]
|
||||||
|
#[derive(Copy, Clone)] pub struct XYWH<C: Coord>(pub C, pub C, pub C, pub C);
|
||||||
|
|
||||||
/// A cardinal direction.
|
/// A cardinal direction.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let direction = Direction::Above;
|
/// let direction = Direction::Above;
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Copy, Clone, PartialEq, Debug)] #[cfg_attr(test, derive(Arbitrary))]
|
#[cfg_attr(test, derive(Arbitrary))]
|
||||||
pub enum Direction {
|
#[derive(Copy, Clone, PartialEq, Debug)] pub enum Direction {
|
||||||
North, South, East, West, Above, Below
|
North, South, East, West, Above, Below
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,8 +42,8 @@ pub enum Direction {
|
||||||
/// ```
|
/// ```
|
||||||
/// let alignment = Align::Center;
|
/// let alignment = Align::Center;
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Debug, Copy, Clone, Default)]
|
#[cfg_attr(test, derive(Arbitrary))]
|
||||||
pub enum Alignment {
|
#[derive(Debug, Copy, Clone, Default)] pub enum Alignment {
|
||||||
#[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W
|
#[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -130,8 +133,8 @@ pub enum Expand<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use ::tengri::{output::*, tui::*};
|
/// use ::tengri::{output::*, tui::*};
|
||||||
/// let area: [u16;4] = [10, 10, 20, 20];
|
/// let area = XYWH(10u16, 10, 20, 20);
|
||||||
/// fn test (area: [u16;4], item: &impl Draw<TuiOut>, expected: [u16;4]) {
|
/// fn test (area: XYWH<16>, item: &impl Draw<TuiOut>, expected: [u16;4]) {
|
||||||
/// assert_eq!(Content::layout(item, area), expected);
|
/// assert_eq!(Content::layout(item, area), expected);
|
||||||
/// assert_eq!(Draw::layout(item, area), expected);
|
/// assert_eq!(Draw::layout(item, area), expected);
|
||||||
/// };
|
/// };
|
||||||
|
|
@ -166,7 +169,7 @@ pub enum Pad<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
|
||||||
/// ```
|
/// ```
|
||||||
/// let bounded = Bounded(XYWH(0, 0, 0, 0), "");
|
/// let bounded = Bounded(XYWH(0, 0, 0, 0), "");
|
||||||
/// ```
|
/// ```
|
||||||
pub struct Bounded<O: Out, D>(pub O::Area, pub D);
|
pub struct Bounded<O: Out, D>(pub XYWH<O::Unit>, pub D);
|
||||||
|
|
||||||
/// Draws items from an iterator.
|
/// Draws items from an iterator.
|
||||||
///
|
///
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ use crate::*;
|
||||||
/// fn area_mut (&mut self) -> &mut [u16;4] {
|
/// fn area_mut (&mut self) -> &mut [u16;4] {
|
||||||
/// &mut self.0
|
/// &mut self.0
|
||||||
/// }
|
/// }
|
||||||
/// fn place_at <T: Draw<Self> + ?Sized> (&mut self, area: [u16;4], _: &T) {
|
/// fn place_at <T: Draw<Self> + ?Sized> (&mut self, area: XYWH<u16>, _: &T) {
|
||||||
/// println!("place_at: {area:?}");
|
/// println!("place_at: {area:?}");
|
||||||
/// ()
|
/// ()
|
||||||
/// }
|
/// }
|
||||||
|
|
@ -29,20 +29,14 @@ use crate::*;
|
||||||
pub trait Out: Send + Sync + Sized {
|
pub trait Out: Send + Sync + Sized {
|
||||||
/// Unit of length
|
/// Unit of length
|
||||||
type Unit: Coord;
|
type Unit: Coord;
|
||||||
/// Rectangle without offset
|
|
||||||
type Size: HasWH<Self::Unit>;
|
|
||||||
/// Rectangle with offset
|
|
||||||
type Area: HasXYWH<Self::Unit>;
|
|
||||||
/// Current output area
|
/// Current output area
|
||||||
fn area (&self) -> Self::Area;
|
fn area (&self) -> XYWH<Self::Unit>;
|
||||||
/// Mutable pointer to area.
|
/// Mutable pointer to area.
|
||||||
fn area_mut (&mut self) -> &mut Self::Area;
|
fn area_mut (&mut self) -> &mut XYWH<Self::Unit>;
|
||||||
/// Render drawable in area specified by `area`
|
/// Render drawable in area specified by `area`
|
||||||
fn place_at <'t, T: Draw<Self> + ?Sized> (&mut self, area: Self::Area, content: &'t T);
|
fn place_at <'t, T: Draw<Self> + ?Sized> (&mut self, area: XYWH<Self::Unit>, content: &'t T);
|
||||||
/// Render drawable in area specified by `T::layout(self.area())`
|
/// Render drawable in area specified by `T::layout(self.area())`
|
||||||
#[inline] fn place <'t, T: Content<Self> + ?Sized> (
|
#[inline] fn place <'t, T: Content<Self> + ?Sized> (&mut self, content: &'t T) {
|
||||||
&mut self, content: &'t T
|
|
||||||
) {
|
|
||||||
self.place_at(content.layout(self.area()), content)
|
self.place_at(content.layout(self.area()), content)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -79,15 +73,17 @@ pub trait Lay<O: Out>: Sized {}
|
||||||
|
|
||||||
/// Drawable area of display.
|
/// Drawable area of display.
|
||||||
pub trait Layout<O: Out> {
|
pub trait Layout<O: Out> {
|
||||||
fn x (&self, to: O::Area) -> O::Unit { to.x() }
|
fn x (&self, to: XYWH<O::Unit>) -> O::Unit { to.x() }
|
||||||
fn y (&self, to: O::Area) -> O::Unit { to.y() }
|
fn y (&self, to: XYWH<O::Unit>) -> O::Unit { to.y() }
|
||||||
fn w_min (&self, _t: O::Area) -> O::Unit { 0.into() }
|
fn w_min (&self, _t: XYWH<O::Unit>) -> O::Unit { 0.into() }
|
||||||
fn w_max (&self, to: O::Area) -> O::Unit { to.w() }
|
fn w_max (&self, to: XYWH<O::Unit>) -> O::Unit { to.w() }
|
||||||
fn w (&self, to: O::Area) -> O::Unit { to.w().max(self.w_min(to)).min(self.w_max(to)) }
|
fn w (&self, to: XYWH<O::Unit>) -> O::Unit { to.w().max(self.w_min(to)).min(self.w_max(to)) }
|
||||||
fn h_min (&self, _t: O::Area) -> O::Unit { 0.into() }
|
fn h_min (&self, _t: XYWH<O::Unit>) -> O::Unit { 0.into() }
|
||||||
fn h_max (&self, to: O::Area) -> O::Unit { to.h() }
|
fn h_max (&self, to: XYWH<O::Unit>) -> O::Unit { to.h() }
|
||||||
fn h (&self, to: O::Area) -> O::Unit { to.h().max(self.h_min(to)).min(self.h_max(to)) }
|
fn h (&self, to: XYWH<O::Unit>) -> O::Unit { to.h().max(self.h_min(to)).min(self.h_max(to)) }
|
||||||
fn layout (&self, to: O::Area) -> O::Area { [self.x(to), self.y(to), self.w(to), self.h(to)].into() }
|
fn layout (&self, to: XYWH<O::Unit>) -> XYWH<O::Unit> {
|
||||||
|
XYWH(self.x(to), self.y(to), self.w(to), self.h(to))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait HasContent<O: Out> {
|
pub trait HasContent<O: Out> {
|
||||||
|
|
@ -111,9 +107,16 @@ pub trait Coord: Send + Sync + Copy
|
||||||
+ Into<usize>
|
+ Into<usize>
|
||||||
+ Into<f64>
|
+ Into<f64>
|
||||||
{
|
{
|
||||||
fn zero () -> Self { 0.into() }
|
|
||||||
fn plus (self, other: Self) -> Self;
|
fn plus (self, other: Self) -> Self;
|
||||||
fn minus (self, other: Self) -> Self { if self >= other { self - other } else { 0.into() } }
|
fn minus (self, other: Self) -> Self {
|
||||||
|
if self >= other { self - other } else { 0.into() }
|
||||||
|
}
|
||||||
|
fn atomic (self) -> AtomicUsize {
|
||||||
|
AtomicUsize::new(self.into())
|
||||||
|
}
|
||||||
|
fn zero () -> Self {
|
||||||
|
0.into()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Something that has an origin point (X, Y).
|
// Something that has an origin point (X, Y).
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue