From 0b5028b1ab47707a469176e9bf20cacdd3a19861 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 19 Apr 2020 14:39:30 +0200 Subject: Draft `Program` interactivity for `Canvas` --- wgpu/src/widget/canvas/layer/cache.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'wgpu/src/widget/canvas/layer/cache.rs') diff --git a/wgpu/src/widget/canvas/layer/cache.rs b/wgpu/src/widget/canvas/layer/cache.rs index 4f8c2bec..2e87297c 100644 --- a/wgpu/src/widget/canvas/layer/cache.rs +++ b/wgpu/src/widget/canvas/layer/cache.rs @@ -1,5 +1,5 @@ use crate::{ - canvas::{Drawable, Frame, Layer}, + canvas::{Drawable, Frame, Layer, Program}, Primitive, }; @@ -79,6 +79,20 @@ where } } +impl Program for Cache +where + T: Drawable + std::fmt::Debug, +{ + type Input = T; + + fn layers<'a>( + &'a self, + input: &'a Self::Input, + ) -> Vec> { + vec![Box::new(self.with(input))] + } +} + #[derive(Debug)] struct Bind<'a, T: Drawable> { cache: &'a Cache, -- cgit From a97acd8fa854a470f26e7c39a62b90f2e2c2c664 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 19 Apr 2020 17:59:32 +0200 Subject: Use `Borrow` when binding in `layer::Cache` --- wgpu/src/widget/canvas/layer/cache.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'wgpu/src/widget/canvas/layer/cache.rs') diff --git a/wgpu/src/widget/canvas/layer/cache.rs b/wgpu/src/widget/canvas/layer/cache.rs index 2e87297c..e8d62b63 100644 --- a/wgpu/src/widget/canvas/layer/cache.rs +++ b/wgpu/src/widget/canvas/layer/cache.rs @@ -4,7 +4,7 @@ use crate::{ }; use iced_native::Size; -use std::{cell::RefCell, marker::PhantomData, sync::Arc}; +use std::{borrow::Borrow, cell::RefCell, marker::PhantomData, sync::Arc}; enum State { Empty, @@ -71,7 +71,10 @@ where /// [`Cache`]: struct.Cache.html /// [`Layer`]: ../trait.Layer.html /// [`Canvas`]: ../../struct.Canvas.html - pub fn with<'a>(&'a self, input: &'a T) -> impl Layer + 'a { + pub fn with<'a>( + &'a self, + input: impl Borrow + std::fmt::Debug + 'a, + ) -> impl Layer + 'a { Bind { cache: self, input: input, @@ -94,14 +97,15 @@ where } #[derive(Debug)] -struct Bind<'a, T: Drawable> { +struct Bind<'a, T: Drawable, I: Borrow + 'a> { cache: &'a Cache, - input: &'a T, + input: I, } -impl<'a, T> Layer for Bind<'a, T> +impl<'a, T, I> Layer for Bind<'a, T, I> where T: Drawable + std::fmt::Debug, + I: Borrow + std::fmt::Debug + 'a, { fn draw(&self, current_bounds: Size) -> Arc { use std::ops::Deref; @@ -115,7 +119,7 @@ where } let mut frame = Frame::new(current_bounds.width, current_bounds.height); - self.input.draw(&mut frame); + self.input.borrow().draw(&mut frame); let primitive = Arc::new(frame.into_primitive()); -- cgit From bb424e54c5083402225a0fdda6130de575592dca Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 19 Apr 2020 18:48:30 +0200 Subject: Add interactivity to `solar_system` example --- wgpu/src/widget/canvas/layer/cache.rs | 50 +++++++++-------------------------- 1 file changed, 12 insertions(+), 38 deletions(-) (limited to 'wgpu/src/widget/canvas/layer/cache.rs') diff --git a/wgpu/src/widget/canvas/layer/cache.rs b/wgpu/src/widget/canvas/layer/cache.rs index e8d62b63..4ecebb48 100644 --- a/wgpu/src/widget/canvas/layer/cache.rs +++ b/wgpu/src/widget/canvas/layer/cache.rs @@ -1,5 +1,5 @@ use crate::{ - canvas::{Drawable, Frame, Layer, Program}, + canvas::{Drawable, Frame, Layer}, Primitive, }; @@ -26,34 +26,17 @@ impl Default for State { /// /// [`Layer`]: ../trait.Layer.html /// [`Cache`]: struct.Cache.html -#[derive(Debug)] -pub struct Cache { - input: PhantomData, +#[derive(Debug, Default)] +pub struct Cache { state: RefCell, } -impl Default for Cache -where - T: Drawable, -{ - fn default() -> Self { - Self { - input: PhantomData, - state: Default::default(), - } - } -} - -impl Cache -where - T: Drawable + std::fmt::Debug, -{ +impl Cache { /// Creates a new empty [`Cache`]. /// /// [`Cache`]: struct.Cache.html pub fn new() -> Self { Cache { - input: PhantomData, state: Default::default(), } } @@ -71,35 +54,26 @@ where /// [`Cache`]: struct.Cache.html /// [`Layer`]: ../trait.Layer.html /// [`Canvas`]: ../../struct.Canvas.html - pub fn with<'a>( + pub fn with<'a, T>( &'a self, input: impl Borrow + std::fmt::Debug + 'a, - ) -> impl Layer + 'a { + ) -> impl Layer + 'a + where + T: Drawable + std::fmt::Debug + 'a, + { Bind { cache: self, input: input, + drawable: PhantomData, } } } -impl Program for Cache -where - T: Drawable + std::fmt::Debug, -{ - type Input = T; - - fn layers<'a>( - &'a self, - input: &'a Self::Input, - ) -> Vec> { - vec![Box::new(self.with(input))] - } -} - #[derive(Debug)] struct Bind<'a, T: Drawable, I: Borrow + 'a> { - cache: &'a Cache, + cache: &'a Cache, input: I, + drawable: PhantomData, } impl<'a, T, I> Layer for Bind<'a, T, I> -- cgit From 592cc685067c36cbba87e4db14f4ebc71d65b951 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 19 Apr 2020 21:55:23 +0200 Subject: Remove `Layer` trait and simplify `Canvas` --- wgpu/src/widget/canvas/layer/cache.rs | 120 ---------------------------------- 1 file changed, 120 deletions(-) delete mode 100644 wgpu/src/widget/canvas/layer/cache.rs (limited to 'wgpu/src/widget/canvas/layer/cache.rs') diff --git a/wgpu/src/widget/canvas/layer/cache.rs b/wgpu/src/widget/canvas/layer/cache.rs deleted file mode 100644 index 4ecebb48..00000000 --- a/wgpu/src/widget/canvas/layer/cache.rs +++ /dev/null @@ -1,120 +0,0 @@ -use crate::{ - canvas::{Drawable, Frame, Layer}, - Primitive, -}; - -use iced_native::Size; -use std::{borrow::Borrow, cell::RefCell, marker::PhantomData, sync::Arc}; - -enum State { - Empty, - Filled { - bounds: Size, - primitive: Arc, - }, -} - -impl Default for State { - fn default() -> Self { - State::Empty - } -} -/// A simple cache that stores generated geometry to avoid recomputation. -/// -/// A [`Cache`] will not redraw its geometry unless the dimensions of its layer -/// change or it is explicitly cleared. -/// -/// [`Layer`]: ../trait.Layer.html -/// [`Cache`]: struct.Cache.html -#[derive(Debug, Default)] -pub struct Cache { - state: RefCell, -} - -impl Cache { - /// Creates a new empty [`Cache`]. - /// - /// [`Cache`]: struct.Cache.html - pub fn new() -> Self { - Cache { - state: Default::default(), - } - } - - /// Clears the cache, forcing a redraw the next time it is used. - /// - /// [`Cached`]: struct.Cached.html - pub fn clear(&mut self) { - *self.state.borrow_mut() = State::Empty; - } - - /// Binds the [`Cache`] with some data, producing a [`Layer`] that can be - /// added to a [`Canvas`]. - /// - /// [`Cache`]: struct.Cache.html - /// [`Layer`]: ../trait.Layer.html - /// [`Canvas`]: ../../struct.Canvas.html - pub fn with<'a, T>( - &'a self, - input: impl Borrow + std::fmt::Debug + 'a, - ) -> impl Layer + 'a - where - T: Drawable + std::fmt::Debug + 'a, - { - Bind { - cache: self, - input: input, - drawable: PhantomData, - } - } -} - -#[derive(Debug)] -struct Bind<'a, T: Drawable, I: Borrow + 'a> { - cache: &'a Cache, - input: I, - drawable: PhantomData, -} - -impl<'a, T, I> Layer for Bind<'a, T, I> -where - T: Drawable + std::fmt::Debug, - I: Borrow + std::fmt::Debug + 'a, -{ - fn draw(&self, current_bounds: Size) -> Arc { - use std::ops::Deref; - - if let State::Filled { bounds, primitive } = - self.cache.state.borrow().deref() - { - if *bounds == current_bounds { - return primitive.clone(); - } - } - - let mut frame = Frame::new(current_bounds.width, current_bounds.height); - self.input.borrow().draw(&mut frame); - - let primitive = Arc::new(frame.into_primitive()); - - *self.cache.state.borrow_mut() = State::Filled { - bounds: current_bounds, - primitive: primitive.clone(), - }; - - primitive - } -} - -impl std::fmt::Debug for State { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - State::Empty => write!(f, "Empty"), - State::Filled { primitive, bounds } => f - .debug_struct("Filled") - .field("primitive", primitive) - .field("bounds", bounds) - .finish(), - } - } -} -- cgit