mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-09-18 05:16:44 +02:00
This commit is contained in:
parent
92f992e2e6
commit
8e7286e409
6 changed files with 123 additions and 47 deletions
|
|
@ -19,6 +19,7 @@ pub struct Area<S: Screen, T>(
|
|||
|
||||
impl<'a, S: Screen, T: Draw<S>> Draw<S> for Area<S, T> {
|
||||
fn draw (&self, to: &mut S) -> Drawn<S::Unit> {
|
||||
#[cfg(feature = "prof")] profiling::scope!("area_draw");
|
||||
to.draw(self.0, &self.1)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -297,6 +297,7 @@ where
|
|||
|
||||
impl <'a, S: Screen, I: Draw<S>> Draw<S> for Full<'a, S, I> {
|
||||
fn draw (&self, to: &mut S) -> Drawn<S::Unit> {
|
||||
#[cfg(feature = "prof")] profiling::scope!("full_draw");
|
||||
let XYWH(x0, y0, w0, h0) = to.area();
|
||||
let item = match self {
|
||||
Self::W(i) => i, Self::H(i) => i, Self::WH(i) => i, _ => unreachable!()
|
||||
|
|
@ -315,6 +316,7 @@ impl <'a, S: Screen, I: Draw<S>> Draw<S> for Full<'a, S, I> {
|
|||
|
||||
impl <'a, S: Screen, I: Draw<S>, X: Into<Option<S::Unit>> + Copy> Draw<S> for Exact<'a, S, I, X> {
|
||||
fn draw (&self, to: &mut S) -> Drawn<S::Unit> {
|
||||
#[cfg(feature = "prof")] profiling::scope!("exact_draw");
|
||||
let XYWH(x0, y0, w0, h0) = to.area();
|
||||
let (item, w1, h1) = match self {
|
||||
Self::W(item, w1) => (item, (*w1).into(), None),
|
||||
|
|
@ -337,6 +339,7 @@ impl <'a, S: Screen, I: Draw<S>, X: Into<Option<S::Unit>> + Copy> Draw<S> for Ex
|
|||
|
||||
impl <'a, S: Screen, I: Draw<S>, X: Into<Option<S::Unit>> + Copy> Draw<S> for Min<'a, S, I, X> {
|
||||
fn draw (&self, to: &mut S) -> Drawn<S::Unit> {
|
||||
#[cfg(feature = "prof")] profiling::scope!("min_draw");
|
||||
let XYWH(x0, y0, w0, h0) = to.area();
|
||||
let (item, w1, h1) = match self {
|
||||
Self::W(item, w1) => (item, (*w1).into(), None),
|
||||
|
|
@ -358,6 +361,7 @@ impl <'a, S: Screen, I: Draw<S>, X: Into<Option<S::Unit>> + Copy> Draw<S> for Mi
|
|||
|
||||
impl <'a, S: Screen, I: Draw<S>, X: Into<Option<S::Unit>> + Copy> Draw<S> for Max<'a, S, I, X> {
|
||||
fn draw (&self, to: &mut S) -> Drawn<S::Unit> {
|
||||
#[cfg(feature = "prof")] profiling::scope!("max_draw");
|
||||
let XYWH(x, y, w0, h0) = to.area();
|
||||
let (item, w, h) = match self {
|
||||
Self::W(item, w) => (item, (*w).into().unwrap_or(w0), h0),
|
||||
|
|
@ -371,6 +375,7 @@ impl <'a, S: Screen, I: Draw<S>, X: Into<Option<S::Unit>> + Copy> Draw<S> for Ma
|
|||
|
||||
impl <'a, S: Screen, I: Draw<S>, X: Into<Option<S::Unit>> + Copy> Draw<S> for Push<'a, S, I, X> {
|
||||
fn draw (&self, to: &mut S) -> Drawn<S::Unit> {
|
||||
#[cfg(feature = "prof")] profiling::scope!("push_draw");
|
||||
match self {
|
||||
Self::__(_) => unreachable!(),
|
||||
Self::X(item, x1) if let Some(XYWH(x, y, w, h)) = to.size(None, item)? => {
|
||||
|
|
@ -389,6 +394,7 @@ impl <'a, S: Screen, I: Draw<S>, X: Into<Option<S::Unit>> + Copy> Draw<S> for Pu
|
|||
|
||||
impl <'a, S: Screen, I: Draw<S>, X: Into<Option<S::Unit>> + Copy> Draw<S> for Pull<'a, S, I, X> {
|
||||
fn draw (&self, to: &mut S) -> Drawn<S::Unit> {
|
||||
#[cfg(feature = "prof")] profiling::scope!("pull_draw");
|
||||
match self {
|
||||
Self::__(_) => unreachable!(),
|
||||
Self::X(item, x1) if let Some(XYWH(x, y, w, h)) = to.size(None, item)? => {
|
||||
|
|
@ -413,6 +419,7 @@ impl <'a, S: Screen, I: Draw<S>, X: Into<Option<S::Unit>> + Copy> Draw<S> for Pu
|
|||
|
||||
impl <'a, S: Screen, I: Draw<S>, X: Into<Option<S::Unit>> + Copy> Draw<S> for Pad<'a, S, I, X> {
|
||||
fn draw (&self, to: &mut S) -> Drawn<S::Unit> {
|
||||
#[cfg(feature = "prof")] profiling::scope!("pad_draw");
|
||||
let XYWH(x, y, w0, h0) = to.area();
|
||||
let (item, w, h) = match self {
|
||||
Self::X(item, w) => (item, (*w).into().unwrap_or_default(), Default::default()),
|
||||
|
|
|
|||
|
|
@ -116,16 +116,17 @@ pub trait CanAlign<'a, S: Screen>: Draw<S> + Sized {
|
|||
}
|
||||
|
||||
pub struct Align<T>(
|
||||
pub(crate) Option<Azimuth>,
|
||||
pub(crate) T,
|
||||
pub Option<Azimuth>,
|
||||
pub T,
|
||||
);
|
||||
|
||||
impl<'a, S: Screen, T: Draw<S>> Draw<S> for Align<T> {
|
||||
fn draw (&self, to: &mut S) -> Drawn<S::Unit> {
|
||||
#[cfg(feature = "prof")] profiling::scope!("align_draw");
|
||||
let Self(azimuth, item) = self;
|
||||
let area0 = to.area();
|
||||
let size = to.size(area0, item)?;
|
||||
let area1 = align::<S>(area0, size, *azimuth);
|
||||
let area1 = aligned::<S>(area0, size, *azimuth);
|
||||
//println!("\n\r{azimuth:?} {area0:?} {size:?}=>{area1:?}");
|
||||
Ok(if let Some(area) = area1 {
|
||||
to.draw(area, &item)?
|
||||
|
|
@ -135,9 +136,10 @@ impl<'a, S: Screen, T: Draw<S>> Draw<S> for Align<T> {
|
|||
}
|
||||
}
|
||||
|
||||
fn align <S: Screen> (
|
||||
fn aligned <S: Screen> (
|
||||
area0: XYWH<S::Unit>, area: Option<XYWH<S::Unit>>, azimuth: Option<Azimuth>
|
||||
) -> Option<XYWH<S::Unit>> {
|
||||
#[cfg(feature = "prof")] profiling::scope!("align");
|
||||
area.map(|XYWH(x, y, w, h)|{
|
||||
let XYWH(x0, y0, w0, h0) = area0;
|
||||
match azimuth {
|
||||
|
|
@ -158,6 +160,7 @@ fn align <S: Screen> (
|
|||
}
|
||||
|
||||
fn_kw_layout!(kw_split |state, output, expr| {
|
||||
#[cfg(feature = "prof")] profiling::scope!("kw_split");
|
||||
let thunk_a = draw(move|screen|ok_flat(expr.nth(1)?.map(|x|state.interpret(screen, x))));
|
||||
let thunk_b = draw(move|screen|ok_flat(expr.nth(2)?.map(|x|state.interpret(screen, x))));
|
||||
Ok(match expr.head()? {
|
||||
|
|
@ -205,6 +208,7 @@ pub fn split <'a, S: Screen, A: Draw<S>, B: Draw<S>> (
|
|||
|
||||
impl<'a, S: Screen, A: Draw<S>, B: Draw<S>> Draw<S> for Pair<A, B> {
|
||||
fn draw (&self, to: &mut S) -> Drawn<S::Unit> {
|
||||
#[cfg(feature = "prof")] profiling::scope!("pair_draw");
|
||||
let Self(split, a, b, ..) = self;
|
||||
let (area_a, area_b) = stack_areas(split, to, a, b)?;
|
||||
let (drawn_a, drawn_b) = draw_stacks(split, to, a, area_a, None, b, area_b, None)?;
|
||||
|
|
@ -222,6 +226,7 @@ fn draw_stacks <'a, S: Screen> (
|
|||
area_b: impl Into<Option<XYWH<S::Unit>>>,
|
||||
origin_b: impl Into<Option<Azimuth>>,
|
||||
) -> UsuallyRef<'a, (Option<XYWH<S::Unit>>, Option<XYWH<S::Unit>>)> {
|
||||
#[cfg(feature = "prof")] profiling::scope!("draw_stacks");
|
||||
let draw_a = |to: &mut S|Ok::<_, Box<dyn Error>>(match origin_a.into() {
|
||||
Some(origin_a) => to.draw(area_a.into(), a.align(origin_a))?,
|
||||
None => to.draw(area_a.into(), a)?
|
||||
|
|
@ -303,6 +308,7 @@ fn stack_drawn <S: Coord> (
|
|||
drawn_a: Option<XYWH<S>>,
|
||||
drawn_b: Option<XYWH<S>>,
|
||||
) -> Option<XYWH<S>> {
|
||||
#[cfg(feature = "prof")] profiling::scope!("stack_drawn");
|
||||
if let (Some(XYWH(xa, ya, wa, ha)), Some(XYWH(xb, yb, wb, hb))) = (drawn_a, drawn_b) {
|
||||
match split {
|
||||
Split::South => Some(XYWH(xa.min(xb), ya, wa.max(wb), ha + hb)),
|
||||
|
|
|
|||
|
|
@ -1,11 +1,28 @@
|
|||
use crate::*;
|
||||
|
||||
fn_kw_layout!(kw_when |state, output, expr| {
|
||||
let thunk = draw(move|screen|ok_flat(expr.nth(2)?.map(|x|state.interpret(screen, x))));
|
||||
ok_flat(matches!(expr.head()?, Some("when")).then(||{
|
||||
when(state.namespace(&expr.nth(1)?)?.unwrap(), thunk).draw(output)
|
||||
}))
|
||||
});
|
||||
pub struct When<T>(pub bool, pub T);
|
||||
|
||||
impl<S: Screen, T: Draw<S>> Draw<S> for When<T> {
|
||||
fn draw (&self, to: &mut S) -> Drawn<S::Unit> {
|
||||
#[cfg(feature = "prof")] profiling::scope!("when");
|
||||
if self.0 {
|
||||
self.1.draw(to)
|
||||
} else {
|
||||
Ok(Default::default())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> When<T> {
|
||||
|
||||
fn_kw_layout!(interpret |state, output, expr| {
|
||||
#[cfg(feature = "prof")] profiling::scope!("kw_when");
|
||||
let thunk = draw(move|screen|ok_flat(expr.nth(2)?.map(|x|state.interpret(screen, x))));
|
||||
ok_flat(matches!(expr.head()?, Some("when")).then(||{
|
||||
when(state.namespace(&expr.nth(1)?)?.unwrap(), thunk).draw(output)
|
||||
}))
|
||||
});
|
||||
}
|
||||
|
||||
/// Only render when condition is true.
|
||||
///
|
||||
|
|
@ -15,10 +32,11 @@ fn_kw_layout!(kw_when |state, output, expr| {
|
|||
/// # }
|
||||
/// ```
|
||||
pub const fn when <'a, T: Screen> (condition: bool, item: impl Draw<T>) -> impl Draw<T> {
|
||||
draw(move|to: &mut T|if condition { item.draw(to) } else { Ok(Default::default()) })
|
||||
When(condition, item)
|
||||
}
|
||||
|
||||
fn_kw_layout!(kw_either |state, output, expr| {
|
||||
#[cfg(feature = "prof")] profiling::scope!("kw_layout");
|
||||
let thunk_a = draw(move|screen|ok_flat(expr.nth(2)?.map(|x|state.interpret(screen, x))));
|
||||
let thunk_b = draw(move|screen|ok_flat(expr.nth(3)?.map(|x|state.interpret(screen, x))));
|
||||
ok_flat(matches!(expr.head()?, Some("either")).then(||{
|
||||
|
|
@ -36,5 +54,8 @@ fn_kw_layout!(kw_either |state, output, expr| {
|
|||
pub const fn either <'a, T: Screen> (
|
||||
condition: bool, a: impl Draw<T>, b: impl Draw<T>
|
||||
) -> impl Draw<T> {
|
||||
draw(move|to: &mut T|if condition { a.draw(to) } else { b.draw(to) })
|
||||
draw(move|to: &mut T|{
|
||||
#[cfg(feature = "prof")] profiling::scope!("either");
|
||||
if condition { a.draw(to) } else { b.draw(to) }
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue