mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-07-17 15:56:57 +02:00
fix Size, begin fixing View
This commit is contained in:
parent
a06ea2ac13
commit
a93fe92a59
3 changed files with 38 additions and 18 deletions
30
src/draw.rs
30
src/draw.rs
|
|
@ -1,5 +1,17 @@
|
|||
use crate::{*, lang::*, color::*, space::*};
|
||||
|
||||
/// 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, V: View<T>> Draw<T> for &V {
|
||||
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>> {
|
||||
self.view().draw(to)
|
||||
}
|
||||
}
|
||||
|
||||
/// Drawable that supports dynamic dispatch.
|
||||
///
|
||||
/// Drawables are composable, e.g. the [when] and [either] conditionals
|
||||
|
|
@ -7,6 +19,10 @@ use crate::{*, lang::*, color::*, space::*};
|
|||
///
|
||||
/// 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::*;
|
||||
|
|
@ -26,13 +42,12 @@ use crate::{*, lang::*, color::*, space::*};
|
|||
pub trait Draw<T: Screen> {
|
||||
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>>;
|
||||
}
|
||||
|
||||
/// Empty draw
|
||||
impl<T: Screen> Draw<T> for () {
|
||||
fn draw (self, __: &mut T) -> Usually<XYWH<T::Unit>> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Screen, D: Draw<T>> Draw<T> for Option<D> {
|
||||
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>> {
|
||||
Ok(self.map(|draw|draw.draw(to)).transpose()?.unwrap_or_default())
|
||||
|
|
@ -111,14 +126,3 @@ pub trait Screen: Space<Self::Unit> + Send + Sync + Sized {
|
|||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
/// Emit a [Draw]able.
|
||||
pub trait View<T: Screen> {
|
||||
fn view (&self) -> impl Draw<T>;
|
||||
}
|
||||
|
||||
impl<T: Screen, V: View<T>> Draw<T> for &V {
|
||||
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>> {
|
||||
self.view().draw(to)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue