From 0cddb3c1b55017cda29d32924514e917a389f11b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 9 Mar 2022 18:59:40 +0700 Subject: Implement `pure` version of `Canvas` widget --- graphics/src/widget/pure/canvas.rs | 229 +++++++++++++++++++++++++++++++++++++ 1 file changed, 229 insertions(+) create mode 100644 graphics/src/widget/pure/canvas.rs (limited to 'graphics/src/widget/pure/canvas.rs') diff --git a/graphics/src/widget/pure/canvas.rs b/graphics/src/widget/pure/canvas.rs new file mode 100644 index 00000000..614553ad --- /dev/null +++ b/graphics/src/widget/pure/canvas.rs @@ -0,0 +1,229 @@ +//! Draw 2D graphics for your users. +//! +//! A [`Canvas`] widget can be used to draw different kinds of 2D shapes in a +//! [`Frame`]. It can be used for animation, data visualization, game graphics, +//! and more! +mod program; + +pub use crate::widget::canvas::{Canvas as _, Program as _, *}; + +pub use program::Program; + +use crate::{Backend, Primitive, Renderer}; + +use iced_native::layout::{self, Layout}; +use iced_native::mouse; +use iced_native::renderer; +use iced_native::{Clipboard, Length, Point, Rectangle, Shell, Size, Vector}; +use iced_pure::widget::tree::{self, Tree}; +use iced_pure::widget::{Element, Widget}; + +/// A widget capable of drawing 2D graphics. +/// +/// ## Drawing a simple circle +/// If you want to get a quick overview, here's how we can draw a simple circle: +/// +/// ```no_run +/// # mod iced { +/// # pub mod pure { +/// # pub use iced_graphics::pure::canvas; +/// # } +/// # pub use iced_native::{Color, Rectangle}; +/// # } +/// use iced::pure::canvas::{self, Canvas, Cursor, Fill, Frame, Geometry, Path, Program}; +/// use iced::{Color, Rectangle}; +/// +/// // First, we define the data we need for drawing +/// #[derive(Debug)] +/// struct Circle { +/// radius: f32, +/// } +/// +/// // Then, we implement the `Program` trait +/// impl Program for Circle { +/// type Message = (); +/// type State = (); +/// +/// fn draw(&self, _state: &(), bounds: Rectangle, _cursor: Cursor) -> Vec{ +/// // We prepare a new `Frame` +/// let mut frame = Frame::new(bounds.size()); +/// +/// // 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, Color::BLACK); +/// +/// // Finally, we produce the geometry +/// vec![frame.into_geometry()] +/// } +/// } +/// +/// // Finally, we simply use our `Circle` to create the `Canvas`! +/// let canvas = Canvas::new(Circle { radius: 50.0 }); +/// ``` +#[derive(Debug)] +pub struct Canvas

+where + P: Program, +{ + width: Length, + height: Length, + program: P, +} + +impl

Canvas

+where + P: Program, +{ + const DEFAULT_SIZE: u16 = 100; + + /// Creates a new [`Canvas`]. + pub fn new(program: P) -> Self { + Canvas { + width: Length::Units(Self::DEFAULT_SIZE), + height: Length::Units(Self::DEFAULT_SIZE), + program, + } + } + + /// Sets the width of the [`Canvas`]. + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } + + /// Sets the height of the [`Canvas`]. + pub fn height(mut self, height: Length) -> Self { + self.height = height; + self + } +} + +impl Widget> for Canvas

+where + P: Program, + B: Backend, +{ + fn state(&self) -> tree::State { + tree::State::new(P::State::default()) + } + + fn width(&self) -> Length { + self.width + } + + fn height(&self) -> Length { + self.height + } + + fn layout( + &self, + _renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { + let limits = limits.width(self.width).height(self.height); + let size = limits.resolve(Size::ZERO); + + layout::Node::new(size) + } + + fn on_event( + &mut self, + tree: &mut Tree, + event: iced_native::Event, + layout: Layout<'_>, + cursor_position: Point, + _renderer: &Renderer, + _clipboard: &mut dyn Clipboard, + shell: &mut Shell<'_, P::Message>, + ) -> event::Status { + let bounds = layout.bounds(); + + let canvas_event = match event { + iced_native::Event::Mouse(mouse_event) => { + Some(Event::Mouse(mouse_event)) + } + iced_native::Event::Keyboard(keyboard_event) => { + Some(Event::Keyboard(keyboard_event)) + } + _ => None, + }; + + let cursor = Cursor::from_window_position(cursor_position); + + if let Some(canvas_event) = canvas_event { + let state = tree.state.downcast_mut::(); + + let (event_status, message) = + self.program.update(state, canvas_event, bounds, cursor); + + if let Some(message) = message { + shell.publish(message); + } + + return event_status; + } + + event::Status::Ignored + } + + fn mouse_interaction( + &self, + tree: &Tree, + layout: Layout<'_>, + cursor_position: Point, + _viewport: &Rectangle, + _renderer: &Renderer, + ) -> mouse::Interaction { + let bounds = layout.bounds(); + let cursor = Cursor::from_window_position(cursor_position); + let state = tree.state.downcast_ref::(); + + self.program.mouse_interaction(state, bounds, cursor) + } + + fn draw( + &self, + tree: &Tree, + renderer: &mut Renderer, + _style: &renderer::Style, + layout: Layout<'_>, + cursor_position: Point, + _viewport: &Rectangle, + ) { + use iced_native::Renderer as _; + + let bounds = layout.bounds(); + + if bounds.width < 1.0 || bounds.height < 1.0 { + return; + } + + let translation = Vector::new(bounds.x, bounds.y); + let cursor = Cursor::from_window_position(cursor_position); + let state = tree.state.downcast_ref::(); + + renderer.with_translation(translation, |renderer| { + renderer.draw_primitive(Primitive::Group { + primitives: self + .program + .draw(state, bounds, cursor) + .into_iter() + .map(Geometry::into_primitive) + .collect(), + }); + }); + } +} + +impl<'a, P, B> From> for Element<'a, P::Message, Renderer> +where + P::Message: 'static, + P: Program + 'a, + B: Backend, +{ + fn from(canvas: Canvas

) -> Element<'a, P::Message, Renderer> { + Element::new(canvas) + } +} -- cgit From 31d814b43c167ac63aa29e7a65820e3c83eb4b56 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 9 Mar 2022 19:19:21 +0700 Subject: Implement `Widget::tag` for `pure::Canvas` --- graphics/src/widget/pure/canvas.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'graphics/src/widget/pure/canvas.rs') diff --git a/graphics/src/widget/pure/canvas.rs b/graphics/src/widget/pure/canvas.rs index 614553ad..91214392 100644 --- a/graphics/src/widget/pure/canvas.rs +++ b/graphics/src/widget/pure/canvas.rs @@ -105,6 +105,10 @@ where P: Program, B: Backend, { + fn tag(&self) -> tree::Tag { + tree::Tag::of::() + } + fn state(&self) -> tree::State { tree::State::new(P::State::default()) } -- cgit From d7100fd2597da82d97eaf196d50573ea64f3f8ff Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 16 Mar 2022 17:37:19 +0700 Subject: Export widget modules in `iced_pure` ... and fix collisions with the new `helpers` --- graphics/src/widget/pure/canvas.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'graphics/src/widget/pure/canvas.rs') diff --git a/graphics/src/widget/pure/canvas.rs b/graphics/src/widget/pure/canvas.rs index 91214392..c48dea6c 100644 --- a/graphics/src/widget/pure/canvas.rs +++ b/graphics/src/widget/pure/canvas.rs @@ -16,7 +16,7 @@ use iced_native::mouse; use iced_native::renderer; use iced_native::{Clipboard, Length, Point, Rectangle, Shell, Size, Vector}; use iced_pure::widget::tree::{self, Tree}; -use iced_pure::widget::{Element, Widget}; +use iced_pure::{Element, Widget}; /// A widget capable of drawing 2D graphics. /// -- cgit From 32fd8dadda6636b11d4a1d9f59b467e57b3706a8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 18 Mar 2022 22:13:52 +0700 Subject: Reintroduce generic `Message` type for `canvas::Program` As it is useful to make the `Message` completely free in many implementations. --- graphics/src/widget/pure/canvas.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'graphics/src/widget/pure/canvas.rs') diff --git a/graphics/src/widget/pure/canvas.rs b/graphics/src/widget/pure/canvas.rs index c48dea6c..2e3e7ede 100644 --- a/graphics/src/widget/pure/canvas.rs +++ b/graphics/src/widget/pure/canvas.rs @@ -18,6 +18,8 @@ use iced_native::{Clipboard, Length, Point, Rectangle, Shell, Size, Vector}; use iced_pure::widget::tree::{self, Tree}; use iced_pure::{Element, Widget}; +use std::marker::PhantomData; + /// A widget capable of drawing 2D graphics. /// /// ## Drawing a simple circle @@ -40,8 +42,7 @@ use iced_pure::{Element, Widget}; /// } /// /// // Then, we implement the `Program` trait -/// impl Program for Circle { -/// type Message = (); +/// impl Program<()> for Circle { /// type State = (); /// /// fn draw(&self, _state: &(), bounds: Rectangle, _cursor: Cursor) -> Vec{ @@ -63,18 +64,19 @@ use iced_pure::{Element, Widget}; /// let canvas = Canvas::new(Circle { radius: 50.0 }); /// ``` #[derive(Debug)] -pub struct Canvas

+pub struct Canvas where - P: Program, + P: Program, { width: Length, height: Length, program: P, + message_: PhantomData, } -impl

Canvas

+impl Canvas where - P: Program, + P: Program, { const DEFAULT_SIZE: u16 = 100; @@ -84,6 +86,7 @@ where width: Length::Units(Self::DEFAULT_SIZE), height: Length::Units(Self::DEFAULT_SIZE), program, + message_: PhantomData, } } @@ -100,9 +103,9 @@ where } } -impl Widget> for Canvas

+impl Widget> for Canvas where - P: Program, + P: Program, B: Backend, { fn tag(&self) -> tree::Tag { @@ -140,7 +143,7 @@ where cursor_position: Point, _renderer: &Renderer, _clipboard: &mut dyn Clipboard, - shell: &mut Shell<'_, P::Message>, + shell: &mut Shell<'_, Message>, ) -> event::Status { let bounds = layout.bounds(); @@ -221,13 +224,14 @@ where } } -impl<'a, P, B> From> for Element<'a, P::Message, Renderer> +impl<'a, Message, P, B> From> + for Element<'a, Message, Renderer> where - P::Message: 'static, - P: Program + 'a, + Message: 'a, + P: Program + 'a, B: Backend, { - fn from(canvas: Canvas

) -> Element<'a, P::Message, Renderer> { + fn from(canvas: Canvas) -> Element<'a, Message, Renderer> { Element::new(canvas) } } -- cgit