mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-09-18 05:16:44 +02:00
This commit is contained in:
parent
41ef62146b
commit
4172fa2577
13 changed files with 250 additions and 248 deletions
88
src/lib.rs
88
src/lib.rs
|
|
@ -1268,14 +1268,14 @@ pub trait AsMutOpt<T> { fn as_mut_opt (&mut self) -> Option<&mut T>; }
|
|||
fn size <'a> (
|
||||
&mut self,
|
||||
area: impl Into<Option<XYWH<Self::Unit>>>,
|
||||
draw: impl Draw<'a, Self>
|
||||
) -> Drawn<'a, Self::Unit>;
|
||||
draw: impl Draw<Self>
|
||||
) -> Drawn<Self::Unit>;
|
||||
/// Draw
|
||||
fn draw <'a> (
|
||||
&mut self,
|
||||
area: impl Into<Option<XYWH<Self::Unit>>>,
|
||||
draw: impl Draw<'a, Self>
|
||||
) -> Drawn<'a, Self::Unit>;
|
||||
draw: impl Draw<Self>
|
||||
) -> Drawn<Self::Unit>;
|
||||
}
|
||||
|
||||
/// Implement the [Draw] trait for a particular drawable and [Screen].
|
||||
|
|
@ -1290,7 +1290,7 @@ pub trait AsMutOpt<T> { fn as_mut_opt (&mut self) -> Option<&mut T>; }
|
|||
#[macro_export] macro_rules! impl_draw (
|
||||
($(<$($T:ident: $Trait:path,)+>)?|
|
||||
$self:ident:$Self:path, $to:ident:$To:ty
|
||||
|$draw:block)=>{ impl$(<$($T:$Trait),+>)? Draw<'_, $To> for $Self {
|
||||
|$draw:block)=>{ impl$(<$($T:$Trait),+>)? Draw<$To> for $Self {
|
||||
fn draw (&$self, $to: &mut $To) -> Perhaps<XYWH<<$To as Screen>::Unit>> $draw
|
||||
} };
|
||||
($(<$($T:ident: $Trait:path,)+>)?|
|
||||
|
|
@ -1321,48 +1321,48 @@ pub trait AsMutOpt<T> { fn as_mut_opt (&mut self) -> Option<&mut T>; }
|
|||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub trait Draw<'a, S: Screen> {
|
||||
fn draw (&self, to: &mut S) -> Drawn<'a, S::Unit>;
|
||||
pub trait Draw<S: Screen> {
|
||||
fn draw (&self, to: &mut S) -> Drawn<S::Unit>;
|
||||
}
|
||||
|
||||
/// Emit a [Draw]able.
|
||||
///
|
||||
/// Speculative. How to avoid conflicts with [Draw] proper?
|
||||
pub trait View<T: Screen> {
|
||||
fn view (&self) -> impl Draw<T>;
|
||||
}
|
||||
///// Emit a [Draw]able.
|
||||
/////
|
||||
///// Speculative. How to avoid conflicts with [Draw] proper?
|
||||
//pub trait View<T: Screen> {
|
||||
//fn view (&self) -> impl Draw<T> ;
|
||||
//}
|
||||
|
||||
impl<T: Screen> View<T> for () {
|
||||
fn view (&self) -> impl Draw<T> {
|
||||
()
|
||||
}
|
||||
}
|
||||
//impl<T: Screen> View<T> for () {
|
||||
//fn view (&self) -> impl Draw<T> {
|
||||
//()
|
||||
//}
|
||||
//}
|
||||
|
||||
/// Return a [Draw]able.
|
||||
///
|
||||
/// ```
|
||||
/// # use tengri::*;
|
||||
/// let _ = view::<Tui, _, _>(||"drawable");
|
||||
/// let _ = view::<Tui, _, _>(||Some("drawable"));
|
||||
/// ```
|
||||
pub const fn view <S: Screen, T: for<'a> Draw<'a, S>, F: Fn()->T> (view: F) -> impl View<S> {
|
||||
ViewThunk(view, PhantomData)
|
||||
}
|
||||
///// Return a [Draw]able.
|
||||
/////
|
||||
///// ```
|
||||
///// # use tengri::*;
|
||||
///// let _ = view::<Tui, _, _>(||"drawable");
|
||||
///// let _ = view::<Tui, _, _>(||Some("drawable"));
|
||||
///// ```
|
||||
//pub const fn view <S: Screen, T: for<'a> Draw<S>, F: Fn()->T> (view: F) -> impl View<S> {
|
||||
//ViewThunk(view, PhantomData)
|
||||
//}
|
||||
|
||||
/// Because we can't implement [Draw] for `F: FnOnce...` without conflicts.
|
||||
pub struct ViewThunk<S: Screen, F>(pub F, std::marker::PhantomData<S>);
|
||||
///// Because we can't implement [Draw] for `F: FnOnce...` without conflicts.
|
||||
//pub struct ViewThunk<S: Screen, F>(pub F, std::marker::PhantomData<S>);
|
||||
|
||||
impl<S: Screen, T: for<'a> Draw<'a, S>, F: Fn()->T> View<S> for ViewThunk<S, F> {
|
||||
fn view (&self) -> impl Draw<S> {
|
||||
self.0()
|
||||
}
|
||||
}
|
||||
//impl<S: Screen, T: for<'a> Draw<S>, F: Fn()->T> View<S> for ViewThunk<S, F> {
|
||||
//fn view (&self) -> impl Draw<S> {
|
||||
//self.0()
|
||||
//}
|
||||
//}
|
||||
|
||||
/// Because we can't implement [Draw] for `F: FnOnce...` without conflicts.
|
||||
pub struct DrawThunk<S: Screen, F>(pub F, std::marker::PhantomData<S>);
|
||||
|
||||
impl<'a, T: Screen, F: Fn(&mut T)->PerhapsRef<'a, XYWH<T::Unit>>> Draw<'a, T> for DrawThunk<T, F> {
|
||||
fn draw (&self, to: &mut T) -> PerhapsRef<'a, XYWH<T::Unit>> {
|
||||
impl<T: Screen, F: Fn(&mut T)->Drawn<T::Unit>> Draw<T> for DrawThunk<T, F> {
|
||||
fn draw (&self, to: &mut T) -> Drawn<T::Unit> {
|
||||
to.clip(None, &self.0)
|
||||
}
|
||||
}
|
||||
|
|
@ -1379,16 +1379,16 @@ pub trait AsMutOpt<T> { fn as_mut_opt (&mut self) -> Option<&mut T>; }
|
|||
DrawThunk(item, std::marker::PhantomData)
|
||||
}
|
||||
|
||||
pub type Drawn<'a, U> = PerhapsRef<'a, XYWH<U>>;
|
||||
pub type Drawn<U> = Perhaps<XYWH<U>>;
|
||||
|
||||
impl<'a, S: Screen> Draw<'a, S> for () {
|
||||
fn draw (&self, _: &mut S) -> Drawn<'a, S::Unit> {
|
||||
impl<'a, S: Screen> Draw<S> for () {
|
||||
fn draw (&self, _: &mut S) -> Drawn<S::Unit> {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S: Screen, D: Draw<'a, S>> Draw<'a, S> for Option<D> {
|
||||
fn draw (&self, to: &mut S) -> Drawn<'a, S::Unit> {
|
||||
impl<'a, S: Screen, D: Draw<S>> Draw<S> for Option<D> {
|
||||
fn draw (&self, to: &mut S) -> Drawn<S::Unit> {
|
||||
self.as_ref().map(|it|it.draw(to)).transpose().map(Option::unwrap_or_default)
|
||||
}
|
||||
}
|
||||
|
|
@ -1405,8 +1405,8 @@ pub trait AsMutOpt<T> { fn as_mut_opt (&mut self) -> Option<&mut T>; }
|
|||
//}
|
||||
//}
|
||||
|
||||
impl<'a, T: Screen, V: Draw<'a, T>> Draw<'a, T> for &V {
|
||||
fn draw (&self, to: &mut T) -> Drawn<'a, T::Unit> {
|
||||
impl<'a, T: Screen, V: Draw<T>> Draw<T> for &V {
|
||||
fn draw (&self, to: &mut T) -> Drawn<T::Unit> {
|
||||
(*self).draw(to)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue