mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-07-17 15:56:57 +02:00
fokken wip
This commit is contained in:
parent
be14173126
commit
2dc501c184
11 changed files with 129 additions and 136 deletions
68
src/draw.rs
68
src/draw.rs
|
|
@ -1,5 +1,5 @@
|
|||
mod draw;
|
||||
pub use self::draw::*;
|
||||
use crate::*;
|
||||
pub use crate::space::*;
|
||||
|
||||
mod view;
|
||||
pub use self::view::*;
|
||||
|
|
@ -10,29 +10,57 @@ pub use self::thunk::*;
|
|||
mod screen;
|
||||
pub use self::screen::*;
|
||||
|
||||
use crate::*;
|
||||
pub use crate::space::*;
|
||||
|
||||
/// Only render when condition is true.
|
||||
/// Implement the [Draw] trait for a particular drawable and [Screen].
|
||||
///
|
||||
/// ```
|
||||
/// # use tengri::draw::*;
|
||||
/// # fn test () -> impl tengri::draw::Draw<tengri::term::Tui> {
|
||||
/// when(true, "Yes")
|
||||
/// # }
|
||||
/// use tengri::{*, draw::*, term::*};
|
||||
/// struct MyDrawable;
|
||||
/// impl_draw!(|self: MyDrawable, to: Tui|{
|
||||
/// todo!("your draw logic")
|
||||
/// });
|
||||
/// ```
|
||||
pub const fn when <T: Screen> (condition: bool, draw: impl Draw<T>) -> impl Draw<T> {
|
||||
thunk(move|to: &mut T|if condition { draw.draw(to) } else { Ok(Default::default()) })
|
||||
}
|
||||
#[macro_export] macro_rules! impl_draw ((|
|
||||
$self:ident:$Self:ty, $to:ident:$To:ty
|
||||
|$draw:block)=>{ impl Draw<$To> for $Self {
|
||||
fn draw ($self, $to: &mut $To) -> Usually<XYWH<u16>> $draw
|
||||
} });
|
||||
|
||||
/// Render one thing if a condition is true and another false.
|
||||
/// Drawable that supports dynamic dispatch.
|
||||
///
|
||||
/// Drawables are composable, e.g. the [when] and [either] conditionals
|
||||
/// or the layout constraints.
|
||||
///
|
||||
/// Drawables are consumable, i.e. the [Draw::draw] method receives an
|
||||
/// owned `self` and does not return it, consuming the drawable.
|
||||
///
|
||||
/// To draw a thing multiple times, instead of explicitly constructing it
|
||||
/// every time, implement the [View] trait instead, which will construct
|
||||
/// a [Draw]able.
|
||||
///
|
||||
/// ```
|
||||
/// # use tengri::draw::*;
|
||||
/// # fn test () -> impl tengri::draw::Draw<tengri::term::Tui> {
|
||||
/// either(true, "Yes", "No")
|
||||
/// # }
|
||||
/// use tengri::{*, draw::*, term::*};
|
||||
/// struct MyWidget(bool);
|
||||
/// impl Draw<Tui> for MyWidget {
|
||||
/// fn draw (self, to: &mut Tui) -> Usually<XYWH<u16>> {
|
||||
/// todo!("your draw logic")
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub const fn either <T: Screen> (condition: bool, a: impl Draw<T>, b: impl Draw<T>) -> impl Draw<T> {
|
||||
thunk(move|to: &mut T|if condition { a.draw(to) } else { b.draw(to) })
|
||||
pub trait Draw<T: Screen> {
|
||||
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>>;
|
||||
}
|
||||
impl<T: Screen, D: Draw<T>> Draw<T> for &D {
|
||||
fn draw (self, __: &mut T) -> Usually<XYWH<T::Unit>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
impl<T: Screen, D: Draw<T>> Draw<T> for Option<D> {
|
||||
fn draw (self, __: &mut T) -> Usually<XYWH<T::Unit>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
//impl<T: Screen, D: Draw<T>> Draw<T> for Arc<D> {
|
||||
//fn draw (self, __: &mut T) -> Usually<XYWH<T::Unit>> {
|
||||
//todo!()
|
||||
//}
|
||||
//}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue