mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-08-07 14:16:56 +02:00
make Draw::clip take drawable instead of fn
This commit is contained in:
parent
cc4a428143
commit
4c25dcc702
9 changed files with 35 additions and 33 deletions
|
|
@ -11,10 +11,11 @@ impl<S: Screen, T: Draw<S>> Draw<S> for Align<T> {
|
|||
Ok(align::<S>(area, self.1.layout(area)?, self.0))
|
||||
}
|
||||
fn draw (self, to: &mut S) -> Perhaps<XYWH<S::Unit>> {
|
||||
Ok(self.layout(to.area())?
|
||||
.map(|area|to.clip(area, |to|self.1.draw(to)))
|
||||
.transpose()?
|
||||
.flatten())
|
||||
Ok(if let Some(area) = self.layout(to.area())? {
|
||||
to.clip(area, self.1)?
|
||||
} else {
|
||||
None
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ pub struct Area<S: Screen, T: Draw<S>>(
|
|||
pub Option<XYWH<S::Unit>>,
|
||||
pub T
|
||||
);
|
||||
|
||||
impl_draw!(<S: Screen, T: Draw<S>,>|self: Area<S, T>, to: S|{
|
||||
to.clip(self.0, |to|self.1.draw(to))
|
||||
to.clip(self.0, self.1)
|
||||
});
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ impl <S: Screen, I: Draw<S>, X: Into<Option<S::Unit>> + Copy> Draw<S> for Exact<
|
|||
fn draw (self, to: &mut S) -> Perhaps<XYWH<S::Unit>> {
|
||||
let area = layout_exact(&self, to.area());
|
||||
let item = match self { Self::W(i, ..) => i, Self::H(i, ..) => i, Self::WH(i, ..) => i, _ => unreachable!() };
|
||||
to.clip(area, |to|item.draw(to))
|
||||
to.clip(area, item)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,17 +22,17 @@ impl_draw!(<T: Screen, I: Draw<T>,>|self: Full<T, I>, to: T|{
|
|||
let XYWH(x0, y0, w0, h0) = to.area();
|
||||
match self {
|
||||
Self::W(item) => if let Some(XYWH(_, y, _, h)) = item.layout(to.area())? {
|
||||
to.clip(XYWH(x0, y, w0, h), |to|item.draw(to))
|
||||
to.clip(XYWH(x0, y, w0, h), item)
|
||||
} else {
|
||||
Ok(None)
|
||||
},
|
||||
Self::H(item) => if let Some(XYWH(x, _, w, _)) = item.layout(to.area())? {
|
||||
to.clip(XYWH(x, y0, w, h0), |to|item.draw(to))
|
||||
to.clip(XYWH(x, y0, w, h0), item)
|
||||
} else {
|
||||
Ok(None)
|
||||
},
|
||||
Self::WH(item) => if let Some(XYWH(..)) = item.layout(to.area())? {
|
||||
to.clip(XYWH(x0, y0, w0, h0), |to|item.draw(to))
|
||||
to.clip(XYWH(x0, y0, w0, h0), item)
|
||||
} else {
|
||||
Ok(None)
|
||||
},
|
||||
|
|
|
|||
|
|
@ -50,6 +50,6 @@ impl<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>> + Copy> Draw<T> for Max<T,
|
|||
)),
|
||||
_ => return Ok(None)
|
||||
};
|
||||
to.clip(area, |to|item.draw(to))
|
||||
to.clip(area, item)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,16 +24,16 @@ impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Min<T, I, X>
|
|||
Self::__(_) => unreachable!(),
|
||||
Self::W(item, w1) if let Some(XYWH(x, y, w, h)) = item.layout(to.area())? => {
|
||||
let w = w1.into().map(|w1|w.max(w1)).unwrap_or(w);
|
||||
to.clip(XYWH(x, y, w, h), |to|item.draw(to))
|
||||
to.clip(XYWH(x, y, w, h), item)
|
||||
},
|
||||
Self::H(item, h1) if let Some(XYWH(x, y, w, h)) = item.layout(to.area())? => {
|
||||
let h = h1.into().map(|h1|h.max(h1)).unwrap_or(h);
|
||||
to.clip(XYWH(x, y, w, h), |to|item.draw(to))
|
||||
to.clip(XYWH(x, y, w, h), item)
|
||||
},
|
||||
Self::WH(item, w1, h1) if let Some(XYWH(x, y, w, h)) = item.layout(to.area())? => {
|
||||
let w = w1.into().map(|w1|w.max(w1)).unwrap_or(w);
|
||||
let h = h1.into().map(|h1|h.max(h1)).unwrap_or(h);
|
||||
to.clip(XYWH(x, y, w, h), |to|item.draw(to))
|
||||
to.clip(XYWH(x, y, w, h), item)
|
||||
},
|
||||
_ => Ok(None)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,17 +25,17 @@ impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Push<T, I, X
|
|||
Self::X(item, x1) if let Some(XYWH(x, y, w, h)) = item.layout(to.area())? => {
|
||||
to.clip(XYWH(
|
||||
x + x1.into().unwrap_or_default(), y, w, h
|
||||
), |to|item.draw(to))
|
||||
), item)
|
||||
},
|
||||
Self::Y(item, y1) if let Some(XYWH(x, y, w, h)) = item.layout(to.area())? => {
|
||||
to.clip(XYWH(
|
||||
x, y + y1.into().unwrap_or_default(), w, h
|
||||
), |to|item.draw(to))
|
||||
), item)
|
||||
},
|
||||
Self::XY(item, x1, y1) if let Some(XYWH(x, y, w, h)) = item.layout(to.area())? => {
|
||||
to.clip(XYWH(
|
||||
x + x1.into().unwrap_or_default(), y + y1.into().unwrap_or_default(), w, h
|
||||
), |to|item.draw(to))
|
||||
), item)
|
||||
},
|
||||
_ => Ok(None)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -122,14 +122,14 @@ fn draw_stacks <S: Screen> (
|
|||
origin_b: impl Into<Option<Azimuth>>,
|
||||
) -> Usually<(Option<XYWH<S::Unit>>, Option<XYWH<S::Unit>>)> {
|
||||
let draw_a = |to: &mut S|Ok::<_, Box<dyn Error>>(if let Some(origin_a) = origin_a.into() {
|
||||
to.clip(area_a.into(), |to|a.align(origin_a).draw(to))?
|
||||
to.clip(area_a.into(), a.align(origin_a))?
|
||||
} else {
|
||||
to.clip(area_a.into(), |to|a.draw(to))?
|
||||
to.clip(area_a.into(), a)?
|
||||
});
|
||||
let draw_b = |to: &mut S|Ok::<_, Box<dyn Error>>(if let Some(origin_b) = origin_b.into() {
|
||||
to.clip(area_b.into(), |to|b.align(origin_b).draw(to))?
|
||||
to.clip(area_b.into(), b.align(origin_b))?
|
||||
} else {
|
||||
to.clip(area_b.into(), |to|b.draw(to))?
|
||||
to.clip(area_b.into(), b)?
|
||||
});
|
||||
Ok(if matches!(split, Split::Below) {
|
||||
let drawn_b = draw_b(to)?;
|
||||
|
|
|
|||
26
src/lib.rs
26
src/lib.rs
|
|
@ -1366,12 +1366,12 @@ macro_rules! eval_xy (
|
|||
/// fn area (&self) -> XYWH<Self::Unit> {
|
||||
/// Default::default()
|
||||
/// }
|
||||
/// fn clip <T> (
|
||||
/// fn clip (
|
||||
/// &mut self,
|
||||
/// area: impl Into<Option<XYWH<u16>>>,
|
||||
/// draw: impl FnOnce(&mut Self)->T
|
||||
/// ) -> T {
|
||||
/// draw(self)
|
||||
/// draw: impl Draw<Self>
|
||||
/// ) -> Perhaps<XYWH<Self::Unit>> {
|
||||
/// draw.to(self)
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
|
|
@ -1387,11 +1387,11 @@ macro_rules! eval_xy (
|
|||
/// Get current clipping area
|
||||
fn area (&self) -> XYWH<Self::Unit>;
|
||||
/// Set clipping area
|
||||
fn clip <T> (
|
||||
fn clip (
|
||||
&mut self,
|
||||
area: impl Into<Option<XYWH<Self::Unit>>>,
|
||||
draw: impl FnOnce(&mut Self)->T
|
||||
) -> T;
|
||||
draw: impl Draw<Self>
|
||||
) -> Perhaps<XYWH<Self::Unit>>;
|
||||
}
|
||||
|
||||
/// Implement the [Draw] trait for a particular drawable and [Screen].
|
||||
|
|
@ -2645,16 +2645,16 @@ macro_rules! eval_xy (
|
|||
}
|
||||
}
|
||||
|
||||
fn clip <T> (
|
||||
fn clip (
|
||||
&mut self,
|
||||
area: impl Into<Option<XYWH<u16>>>,
|
||||
draw: impl FnOnce(&mut Self)->T
|
||||
) -> T {
|
||||
draw: impl Draw<Self>
|
||||
) -> Perhaps<XYWH<Self::Unit>> {
|
||||
let prev = self.area();
|
||||
if let Some(area) = area.into() {
|
||||
*self.area_mut() = area.into();
|
||||
}
|
||||
let result = draw(self);
|
||||
let result = draw.draw(self);
|
||||
*self.area_mut() = prev;
|
||||
result
|
||||
}
|
||||
|
|
@ -2815,10 +2815,10 @@ macro_rules! eval_xy (
|
|||
self.1.layout(area)
|
||||
}
|
||||
fn draw (self, to: &mut Tui) -> Drawn<u16> {
|
||||
self.layout(to.area()).map(|area|to.clip(area, |to|{
|
||||
self.layout(to.area()).map(|area|to.clip(area, draw(|to: &mut Tui|{
|
||||
to.update(&|cell,_,_|{ cell.set_bg(self.0); });
|
||||
self.1.draw(to)
|
||||
}))?
|
||||
})))?
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue