From 2539042b71d70afd4d8f262783d441e768811ee9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 06:24:12 +0200 Subject: Remove `Drawable` and rename `State` to `Program` --- wgpu/src/widget/canvas.rs | 58 ++++++++++++++++++-------------------- wgpu/src/widget/canvas/cache.rs | 37 +++++------------------- wgpu/src/widget/canvas/drawable.rs | 36 ----------------------- wgpu/src/widget/canvas/program.rs | 22 +++++++++++++++ wgpu/src/widget/canvas/state.rs | 22 --------------- 5 files changed, 56 insertions(+), 119 deletions(-) delete mode 100644 wgpu/src/widget/canvas/drawable.rs create mode 100644 wgpu/src/widget/canvas/program.rs delete mode 100644 wgpu/src/widget/canvas/state.rs (limited to 'wgpu/src/widget') 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{ +/// // 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, Message> { +pub struct Canvas> { width: Length, height: Length, - state: S, + program: P, phantom: PhantomData, } -impl> Canvas { +impl> Canvas { 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> Canvas { } } -impl> Widget - for Canvas +impl> Widget + for Canvas { fn width(&self) -> Length { self.width @@ -184,7 +180,7 @@ impl> Widget 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> Widget translation, content: Box::new(Primitive::Group { primitives: self - .state + .program .draw(size) .into_iter() .map(Geometry::into_primitive) @@ -227,12 +223,12 @@ impl> Widget } } -impl<'a, Message, S: State + 'a> From> +impl<'a, Message, P: Program + 'a> From> for Element<'a, Message, Renderer> where Message: 'static, { - fn from(canvas: Canvas) -> Element<'a, Message, Renderer> { + fn from(canvas: Canvas) -> 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(&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 + '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 for Bind<'a, T> -where - T: Drawable + std::fmt::Debug + 'a, -{ - fn draw(&self, bounds: Size) -> Vec { - 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 Drawable for &T -where - T: Drawable, -{ - fn draw(&self, frame: &mut Frame) { - T::draw(self, frame) - } -} - -impl 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/program.rs b/wgpu/src/widget/canvas/program.rs new file mode 100644 index 00000000..8e35fdfb --- /dev/null +++ b/wgpu/src/widget/canvas/program.rs @@ -0,0 +1,22 @@ +use crate::canvas::{Event, Geometry, Size}; + +pub trait Program { + fn update(&mut self, _event: Event, _bounds: Size) -> Option { + None + } + + fn draw(&self, bounds: Size) -> Vec; +} + +impl Program for &mut T +where + T: Program, +{ + fn update(&mut self, event: Event, bounds: Size) -> Option { + T::update(self, event, bounds) + } + + fn draw(&self, bounds: Size) -> Vec { + T::draw(self, bounds) + } +} diff --git a/wgpu/src/widget/canvas/state.rs b/wgpu/src/widget/canvas/state.rs deleted file mode 100644 index ab433dd1..00000000 --- a/wgpu/src/widget/canvas/state.rs +++ /dev/null @@ -1,22 +0,0 @@ -use crate::canvas::{Event, Geometry, Size}; - -pub trait State { - fn update(&mut self, _event: Event, _bounds: Size) -> Option { - None - } - - fn draw(&self, bounds: Size) -> Vec; -} - -impl State for &mut T -where - T: State, -{ - fn update(&mut self, event: Event, bounds: Size) -> Option { - T::update(self, event, bounds) - } - - fn draw(&self, bounds: Size) -> Vec { - T::draw(self, bounds) - } -} -- cgit