diff options
author | 2020-04-28 06:24:12 +0200 | |
---|---|---|
committer | 2020-04-28 06:24:12 +0200 | |
commit | 2539042b71d70afd4d8f262783d441e768811ee9 (patch) | |
tree | f3af8a86b700308ff5fe9f28f88a508b2fd8cdb5 /wgpu | |
parent | 7f1e7aea07bb448471470093a47898b059d940d3 (diff) | |
download | iced-2539042b71d70afd4d8f262783d441e768811ee9.tar.gz iced-2539042b71d70afd4d8f262783d441e768811ee9.tar.bz2 iced-2539042b71d70afd4d8f262783d441e768811ee9.zip |
Remove `Drawable` and rename `State` to `Program`
Diffstat (limited to 'wgpu')
-rw-r--r-- | wgpu/src/widget/canvas.rs | 58 | ||||
-rw-r--r-- | wgpu/src/widget/canvas/cache.rs | 37 | ||||
-rw-r--r-- | wgpu/src/widget/canvas/drawable.rs | 36 | ||||
-rw-r--r-- | wgpu/src/widget/canvas/program.rs (renamed from wgpu/src/widget/canvas/state.rs) | 6 |
4 files changed, 37 insertions, 100 deletions
diff --git a/wgpu/src/widget/canvas.rs b/wgpu/src/widget/canvas.rs index 044bc3ec..c0506cf7 100644 --- a/wgpu/src/widget/canvas.rs +++ b/wgpu/src/widget/canvas.rs @@ -18,34 +18,27 @@ use std::marker::PhantomData; pub mod path; mod cache; -mod drawable; mod event; mod fill; mod frame; mod geometry; -mod state; +mod program; mod stroke; mod text; pub use cache::Cache; -pub use drawable::Drawable; pub use event::Event; pub use fill::Fill; pub use frame::Frame; pub use geometry::Geometry; pub use path::Path; -pub use state::State; +pub use program::Program; pub use stroke::{LineCap, LineJoin, Stroke}; pub use text::Text; /// A widget capable of drawing 2D graphics. /// -/// A [`Canvas`] may contain multiple layers. A [`Layer`] is drawn using the -/// painter's algorithm. In other words, layers will be drawn on top of each -/// other in the same order they are pushed into the [`Canvas`]. -/// /// [`Canvas`]: struct.Canvas.html -/// [`Layer`]: layer/trait.Layer.html /// /// # Examples /// The repository has a couple of [examples] showcasing how to use a @@ -66,10 +59,10 @@ pub use text::Text; /// ```no_run /// # mod iced { /// # pub use iced_wgpu::canvas; -/// # pub use iced_native::Color; +/// # pub use iced_native::{Color, Size}; /// # } -/// use iced::canvas::{self, Cache, Canvas, Drawable, Fill, Frame, Path}; -/// use iced::Color; +/// use iced::canvas::{self, Cache, Canvas, Fill, Frame, Geometry, Path, Program}; +/// use iced::{Color, Size}; /// /// // First, we define the data we need for drawing /// #[derive(Debug)] @@ -77,42 +70,45 @@ pub use text::Text; /// radius: f32, /// } /// -/// // Then, we implement the `Drawable` trait -/// impl Drawable for Circle { -/// fn draw(&self, frame: &mut Frame) { +/// // Then, we implement the `Program` trait +/// impl Program<()> for Circle { +/// fn draw(&self, bounds: Size) -> Vec<Geometry>{ +/// // We prepare a new `Frame` +/// let mut frame = Frame::new(bounds); +/// /// // We create a `Path` representing a simple circle /// let circle = Path::circle(frame.center(), self.radius); /// /// // And fill it with some color /// frame.fill(&circle, Fill::Color(Color::BLACK)); +/// +/// // Finally, we produce the geometry +/// vec![frame.into_geometry()] /// } /// } /// -/// // We can use a `Cache` to avoid unnecessary re-tessellation -/// let cache = Cache::new(); -/// /// // Finally, we simply use our `Cache` to create the `Canvas`! -/// let canvas: Canvas<_, ()> = Canvas::new(cache.with(Circle { radius: 50.0 })); +/// let canvas = Canvas::new(Circle { radius: 50.0 }); /// ``` #[derive(Debug)] -pub struct Canvas<S: State<Message>, Message> { +pub struct Canvas<Message, P: Program<Message>> { width: Length, height: Length, - state: S, + program: P, phantom: PhantomData<Message>, } -impl<Message, S: State<Message>> Canvas<S, Message> { +impl<Message, P: Program<Message>> Canvas<Message, P> { const DEFAULT_SIZE: u16 = 100; - /// Creates a new [`Canvas`] with no layers. + /// Creates a new [`Canvas`]. /// /// [`Canvas`]: struct.Canvas.html - pub fn new(state: S) -> Self { + pub fn new(program: P) -> Self { Canvas { width: Length::Units(Self::DEFAULT_SIZE), height: Length::Units(Self::DEFAULT_SIZE), - state, + program, phantom: PhantomData, } } @@ -134,8 +130,8 @@ impl<Message, S: State<Message>> Canvas<S, Message> { } } -impl<Message, S: State<Message>> Widget<Message, Renderer> - for Canvas<S, Message> +impl<Message, P: Program<Message>> Widget<Message, Renderer> + for Canvas<Message, P> { fn width(&self) -> Length { self.width @@ -184,7 +180,7 @@ impl<Message, S: State<Message>> Widget<Message, Renderer> if let Some(canvas_event) = canvas_event { if let Some(message) = - self.state.update(canvas_event, bounds.size()) + self.program.update(canvas_event, bounds.size()) { messages.push(message); } @@ -207,7 +203,7 @@ impl<Message, S: State<Message>> Widget<Message, Renderer> translation, content: Box::new(Primitive::Group { primitives: self - .state + .program .draw(size) .into_iter() .map(Geometry::into_primitive) @@ -227,12 +223,12 @@ impl<Message, S: State<Message>> Widget<Message, Renderer> } } -impl<'a, Message, S: State<Message> + 'a> From<Canvas<S, Message>> +impl<'a, Message, P: Program<Message> + 'a> From<Canvas<Message, P>> for Element<'a, Message, Renderer> where Message: 'static, { - fn from(canvas: Canvas<S, Message>) -> Element<'a, Message, Renderer> { + fn from(canvas: Canvas<Message, P>) -> Element<'a, Message, Renderer> { Element::new(canvas) } } diff --git a/wgpu/src/widget/canvas/cache.rs b/wgpu/src/widget/canvas/cache.rs index 2beed0f7..03643f74 100644 --- a/wgpu/src/widget/canvas/cache.rs +++ b/wgpu/src/widget/canvas/cache.rs @@ -1,5 +1,5 @@ use crate::{ - canvas::{Drawable, Frame, Geometry}, + canvas::{Frame, Geometry}, Primitive, }; @@ -48,10 +48,11 @@ impl Cache { *self.state.borrow_mut() = State::Empty; } - pub fn draw<T>(&self, new_bounds: Size, input: T) -> Geometry - where - T: Drawable + std::fmt::Debug, - { + pub fn draw( + &self, + new_bounds: Size, + draw_fn: impl Fn(&mut Frame), + ) -> Geometry { use std::ops::Deref; if let State::Filled { bounds, primitive } = self.state.borrow().deref() @@ -64,7 +65,7 @@ impl Cache { } let mut frame = Frame::new(new_bounds); - input.draw(&mut frame); + draw_fn(&mut frame); let primitive = { let geometry = frame.into_geometry(); @@ -79,30 +80,6 @@ impl Cache { Geometry::from_primitive(Primitive::Cached { cache: primitive }) } - - pub fn with<'a, T, Message>( - &'a self, - input: T, - ) -> impl crate::canvas::State<Message> + 'a - where - T: Drawable + std::fmt::Debug + 'a, - { - Bind { cache: self, input } - } -} - -struct Bind<'a, T> { - cache: &'a Cache, - input: T, -} - -impl<'a, T, Message> crate::canvas::State<Message> for Bind<'a, T> -where - T: Drawable + std::fmt::Debug + 'a, -{ - fn draw(&self, bounds: Size) -> Vec<Geometry> { - vec![self.cache.draw(bounds, &self.input)] - } } impl std::fmt::Debug for State { diff --git a/wgpu/src/widget/canvas/drawable.rs b/wgpu/src/widget/canvas/drawable.rs deleted file mode 100644 index 5209fa6f..00000000 --- a/wgpu/src/widget/canvas/drawable.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::canvas::Frame; - -/// A type that can be drawn on a [`Frame`]. -/// -/// [`Frame`]: struct.Frame.html -pub trait Drawable { - /// Draws the [`Drawable`] on the given [`Frame`]. - /// - /// [`Drawable`]: trait.Drawable.html - /// [`Frame`]: struct.Frame.html - fn draw(&self, frame: &mut Frame); -} - -impl<'a> Drawable for dyn Fn(&mut Frame) + 'a { - fn draw(&self, frame: &mut Frame) { - self(frame) - } -} - -impl<T> Drawable for &T -where - T: Drawable, -{ - fn draw(&self, frame: &mut Frame) { - T::draw(self, frame) - } -} - -impl<T> Drawable for &[T] -where - T: Drawable, -{ - fn draw(&self, frame: &mut Frame) { - self.iter().for_each(|drawable| drawable.draw(frame)); - } -} diff --git a/wgpu/src/widget/canvas/state.rs b/wgpu/src/widget/canvas/program.rs index ab433dd1..8e35fdfb 100644 --- a/wgpu/src/widget/canvas/state.rs +++ b/wgpu/src/widget/canvas/program.rs @@ -1,6 +1,6 @@ use crate::canvas::{Event, Geometry, Size}; -pub trait State<Message> { +pub trait Program<Message> { fn update(&mut self, _event: Event, _bounds: Size) -> Option<Message> { None } @@ -8,9 +8,9 @@ pub trait State<Message> { fn draw(&self, bounds: Size) -> Vec<Geometry>; } -impl<T, Message> State<Message> for &mut T +impl<T, Message> Program<Message> for &mut T where - T: State<Message>, + T: Program<Message>, { fn update(&mut self, event: Event, bounds: Size) -> Option<Message> { T::update(self, event, bounds) |