From 59b1e90661ee9e479f404bae71029db824cc7b46 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 03:18:31 +0200 Subject: Introduce `Translate` primitive in `iced_wgpu` --- examples/bezier_tool/src/main.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'examples/bezier_tool/src/main.rs') diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index fcb7733c..8012ea0a 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -172,12 +172,14 @@ mod bezier { ) .unwrap(); - let mesh = Primitive::Mesh2D { - origin: Point::new(bounds.x, bounds.y), - buffers: Mesh2D { - vertices: buffer.vertices, - indices: buffer.indices, - }, + let mesh = Primitive::Translate { + translation: Vector::new(bounds.x, bounds.y), + content: Box::new(Primitive::Mesh2D { + buffers: Mesh2D { + vertices: buffer.vertices, + indices: buffer.indices, + }, + }), }; ( -- cgit From fd1ceac3633c4f60852eb9f75da9fbb5e1f35df3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 03:47:41 +0200 Subject: Port `bezier_tool` example to use `Canvas` --- examples/bezier_tool/src/main.rs | 439 +++++++++++++-------------------------- 1 file changed, 147 insertions(+), 292 deletions(-) (limited to 'examples/bezier_tool/src/main.rs') diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index 8012ea0a..a00a1005 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -1,290 +1,4 @@ -//! This example showcases a simple native custom widget that renders arbitrary -//! path with `lyon`. -mod bezier { - // For now, to implement a custom native widget you will need to add - // `iced_native` and `iced_wgpu` to your dependencies. - // - // Then, you simply need to define your widget type and implement the - // `iced_native::Widget` trait with the `iced_wgpu::Renderer`. - // - // Of course, you can choose to make the implementation renderer-agnostic, - // if you wish to, by creating your own `Renderer` trait, which could be - // implemented by `iced_wgpu` and other renderers. - use iced_native::{ - input, layout, Clipboard, Color, Element, Event, Font, Hasher, - HorizontalAlignment, Layout, Length, MouseCursor, Point, Rectangle, - Size, Vector, VerticalAlignment, Widget, - }; - use iced_wgpu::{ - triangle::{Mesh2D, Vertex2D}, - Defaults, Primitive, Renderer, - }; - use lyon::tessellation::{ - basic_shapes, BuffersBuilder, StrokeAttributes, StrokeOptions, - StrokeTessellator, VertexBuffers, - }; - - pub struct Bezier<'a, Message> { - state: &'a mut State, - curves: &'a [Curve], - // [from, to, ctrl] - on_click: Box Message>, - } - - #[derive(Debug, Clone, Copy)] - pub struct Curve { - from: Point, - to: Point, - control: Point, - } - - #[derive(Default)] - pub struct State { - pending: Option, - } - - enum Pending { - One { from: Point }, - Two { from: Point, to: Point }, - } - - impl<'a, Message> Bezier<'a, Message> { - pub fn new( - state: &'a mut State, - curves: &'a [Curve], - on_click: F, - ) -> Self - where - F: 'static + Fn(Curve) -> Message, - { - Self { - state, - curves, - on_click: Box::new(on_click), - } - } - } - - impl<'a, Message> Widget for Bezier<'a, Message> { - fn width(&self) -> Length { - Length::Fill - } - - fn height(&self) -> Length { - Length::Fill - } - - fn layout( - &self, - _renderer: &Renderer, - limits: &layout::Limits, - ) -> layout::Node { - let size = limits - .height(Length::Fill) - .width(Length::Fill) - .resolve(Size::ZERO); - layout::Node::new(size) - } - - fn draw( - &self, - _renderer: &mut Renderer, - defaults: &Defaults, - layout: Layout<'_>, - cursor_position: Point, - ) -> (Primitive, MouseCursor) { - let mut buffer: VertexBuffers = VertexBuffers::new(); - let mut path_builder = lyon::path::Path::builder(); - - let bounds = layout.bounds(); - - // Draw rectangle border with lyon. - basic_shapes::stroke_rectangle( - &lyon::math::Rect::new( - lyon::math::Point::new(0.5, 0.5), - lyon::math::Size::new( - bounds.width - 1.0, - bounds.height - 1.0, - ), - ), - &StrokeOptions::default().with_line_width(1.0), - &mut BuffersBuilder::new( - &mut buffer, - |pos: lyon::math::Point, _: StrokeAttributes| Vertex2D { - position: pos.to_array(), - color: [0.0, 0.0, 0.0, 1.0], - }, - ), - ) - .unwrap(); - - for curve in self.curves { - path_builder.move_to(lyon::math::Point::new( - curve.from.x, - curve.from.y, - )); - - path_builder.quadratic_bezier_to( - lyon::math::Point::new(curve.control.x, curve.control.y), - lyon::math::Point::new(curve.to.x, curve.to.y), - ); - } - - match self.state.pending { - None => {} - Some(Pending::One { from }) => { - path_builder - .move_to(lyon::math::Point::new(from.x, from.y)); - path_builder.line_to(lyon::math::Point::new( - cursor_position.x - bounds.x, - cursor_position.y - bounds.y, - )); - } - Some(Pending::Two { from, to }) => { - path_builder - .move_to(lyon::math::Point::new(from.x, from.y)); - path_builder.quadratic_bezier_to( - lyon::math::Point::new( - cursor_position.x - bounds.x, - cursor_position.y - bounds.y, - ), - lyon::math::Point::new(to.x, to.y), - ); - } - } - - let mut tessellator = StrokeTessellator::new(); - - // Draw strokes with lyon. - tessellator - .tessellate( - &path_builder.build(), - &StrokeOptions::default().with_line_width(3.0), - &mut BuffersBuilder::new( - &mut buffer, - |pos: lyon::math::Point, _: StrokeAttributes| { - Vertex2D { - position: pos.to_array(), - color: [0.0, 0.0, 0.0, 1.0], - } - }, - ), - ) - .unwrap(); - - let mesh = Primitive::Translate { - translation: Vector::new(bounds.x, bounds.y), - content: Box::new(Primitive::Mesh2D { - buffers: Mesh2D { - vertices: buffer.vertices, - indices: buffer.indices, - }, - }), - }; - - ( - Primitive::Clip { - bounds, - offset: Vector::new(0, 0), - content: Box::new( - if self.curves.is_empty() - && self.state.pending.is_none() - { - let instructions = Primitive::Text { - bounds: Rectangle { - x: bounds.center_x(), - y: bounds.center_y(), - ..bounds - }, - color: Color { - a: defaults.text.color.a * 0.7, - ..defaults.text.color - }, - content: String::from( - "Click to create bezier curves!", - ), - font: Font::Default, - size: 30.0, - horizontal_alignment: - HorizontalAlignment::Center, - vertical_alignment: VerticalAlignment::Center, - }; - - Primitive::Group { - primitives: vec![mesh, instructions], - } - } else { - mesh - }, - ), - }, - MouseCursor::OutOfBounds, - ) - } - - fn hash_layout(&self, _state: &mut Hasher) {} - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - _renderer: &Renderer, - _clipboard: Option<&dyn Clipboard>, - ) { - let bounds = layout.bounds(); - - if bounds.contains(cursor_position) { - match event { - Event::Mouse(input::mouse::Event::Input { - state: input::ButtonState::Pressed, - .. - }) => { - let new_point = Point::new( - cursor_position.x - bounds.x, - cursor_position.y - bounds.y, - ); - - match self.state.pending { - None => { - self.state.pending = - Some(Pending::One { from: new_point }); - } - Some(Pending::One { from }) => { - self.state.pending = Some(Pending::Two { - from, - to: new_point, - }); - } - Some(Pending::Two { from, to }) => { - self.state.pending = None; - - messages.push((self.on_click)(Curve { - from, - to, - control: new_point, - })); - } - } - } - _ => {} - } - } - } - } - - impl<'a, Message> Into> for Bezier<'a, Message> - where - Message: 'static, - { - fn into(self) -> Element<'a, Message, Renderer> { - Element::new(self) - } - } -} - -use bezier::Bezier; +//! This example showcases an interactive `Canvas` for drawing Bézier curves. use iced::{ button, Align, Button, Column, Container, Element, Length, Sandbox, Settings, Text, @@ -325,6 +39,7 @@ impl Sandbox for Example { match message { Message::AddCurve(curve) => { self.curves.push(curve); + self.bezier.request_redraw(); } Message::Clear => { self.bezier = bezier::State::default(); @@ -343,11 +58,7 @@ impl Sandbox for Example { .width(Length::Shrink) .size(50), ) - .push(Bezier::new( - &mut self.bezier, - self.curves.as_slice(), - Message::AddCurve, - )) + .push(self.bezier.view(&self.curves).map(Message::AddCurve)) .push( Button::new(&mut self.button_state, Text::new("Clear")) .padding(8) @@ -362,3 +73,147 @@ impl Sandbox for Example { .into() } } + +mod bezier { + use iced::{ + canvas::{ + self, Canvas, Drawable, Event, Frame, Geometry, Path, Stroke, + }, + mouse, ButtonState, Element, Length, Point, Size, + }; + + #[derive(Default)] + pub struct State { + pending: Option, + cursor_position: Point, + cache: canvas::Cache, + } + + impl State { + pub fn view<'a>( + &'a mut self, + curves: &'a [Curve], + ) -> Element<'a, Curve> { + Canvas::new(Bezier { + state: self, + curves, + }) + .width(Length::Fill) + .height(Length::Fill) + .into() + } + + pub fn request_redraw(&mut self) { + self.cache.clear() + } + } + + struct Bezier<'a> { + state: &'a mut State, + curves: &'a [Curve], + } + + impl<'a> canvas::State for Bezier<'a> { + fn update(&mut self, event: Event, _bounds: Size) -> Option { + match event { + Event::Mouse(mouse_event) => match mouse_event { + mouse::Event::CursorMoved { x, y } => { + self.state.cursor_position = Point::new(x, y); + + None + } + mouse::Event::Input { + button: mouse::Button::Left, + state: ButtonState::Pressed, + } => match self.state.pending { + None => { + self.state.pending = Some(Pending::One { + from: self.state.cursor_position, + }); + None + } + Some(Pending::One { from }) => { + self.state.pending = Some(Pending::Two { + from, + to: self.state.cursor_position, + }); + + None + } + Some(Pending::Two { from, to }) => { + self.state.pending = None; + + Some(Curve { + from, + to, + control: self.state.cursor_position, + }) + } + }, + _ => None, + }, + } + } + + fn draw(&self, bounds: Size) -> Vec { + let curves = self.state.cache.draw(bounds, &self.curves); + + if let Some(pending) = &self.state.pending { + let pending_curve = + pending.draw(bounds, self.state.cursor_position); + + vec![curves, pending_curve] + } else { + vec![curves] + } + } + } + + #[derive(Debug, Clone, Copy)] + pub struct Curve { + from: Point, + to: Point, + control: Point, + } + + impl Drawable for Curve { + fn draw(&self, frame: &mut Frame) { + let curve = Path::new(|p| { + p.move_to(self.from); + p.quadratic_curve_to(self.control, self.to); + }); + + frame.stroke(&curve, Stroke::default().with_width(2.0)); + } + } + + #[derive(Debug, Clone, Copy)] + enum Pending { + One { from: Point }, + Two { from: Point, to: Point }, + } + + impl Pending { + fn draw(&self, bounds: Size, cursor_position: Point) -> Geometry { + let mut frame = Frame::new(bounds); + + match *self { + Pending::One { from } => { + let line = Path::line(from, cursor_position); + frame.stroke(&line, Stroke::default().with_width(2.0)); + } + Pending::Two { from, to } => { + let curve = Curve { + from, + to, + control: cursor_position, + }; + + curve.draw(&mut frame); + } + }; + + frame.into_geometry() + } + } +} -- cgit From 7f1e7aea07bb448471470093a47898b059d940d3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 04:41:25 +0200 Subject: Remove unnecessary `Container` in `bezier_tool` --- examples/bezier_tool/src/main.rs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) (limited to 'examples/bezier_tool/src/main.rs') diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index a00a1005..5473bc07 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -1,7 +1,6 @@ //! This example showcases an interactive `Canvas` for drawing Bézier curves. use iced::{ - button, Align, Button, Column, Container, Element, Length, Sandbox, - Settings, Text, + button, Align, Button, Column, Element, Length, Sandbox, Settings, Text, }; pub fn main() { @@ -49,7 +48,7 @@ impl Sandbox for Example { } fn view(&mut self) -> Element { - let content = Column::new() + Column::new() .padding(20) .spacing(20) .align_items(Align::Center) @@ -63,13 +62,7 @@ impl Sandbox for Example { Button::new(&mut self.button_state, Text::new("Clear")) .padding(8) .on_press(Message::Clear), - ); - - Container::new(content) - .width(Length::Fill) - .height(Length::Fill) - .center_x() - .center_y() + ) .into() } } -- cgit 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` --- examples/bezier_tool/src/main.rs | 91 ++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 40 deletions(-) (limited to 'examples/bezier_tool/src/main.rs') diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index 5473bc07..2112f662 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -69,10 +69,8 @@ impl Sandbox for Example { mod bezier { use iced::{ - canvas::{ - self, Canvas, Drawable, Event, Frame, Geometry, Path, Stroke, - }, - mouse, ButtonState, Element, Length, Point, Size, + canvas::{self, Canvas, Event, Frame, Geometry, Path, Stroke}, + mouse, ButtonState, Element, Length, Point, Rectangle, Size, }; #[derive(Default)] @@ -106,8 +104,10 @@ mod bezier { curves: &'a [Curve], } - impl<'a> canvas::State for Bezier<'a> { - fn update(&mut self, event: Event, _bounds: Size) -> Option { + impl<'a> canvas::Program for Bezier<'a> { + fn update(&mut self, event: Event, bounds: Size) -> Option { + let bounds = Rectangle::new(Point::ORIGIN, bounds); + match event { Event::Mouse(mouse_event) => match mouse_event { mouse::Event::CursorMoved { x, y } => { @@ -118,46 +118,55 @@ mod bezier { mouse::Event::Input { button: mouse::Button::Left, state: ButtonState::Pressed, - } => match self.state.pending { - None => { - self.state.pending = Some(Pending::One { - from: self.state.cursor_position, - }); - None - } - Some(Pending::One { from }) => { - self.state.pending = Some(Pending::Two { - from, - to: self.state.cursor_position, - }); - - None - } - Some(Pending::Two { from, to }) => { - self.state.pending = None; - - Some(Curve { - from, - to, - control: self.state.cursor_position, - }) + } if bounds.contains(self.state.cursor_position) => { + match self.state.pending { + None => { + self.state.pending = Some(Pending::One { + from: self.state.cursor_position, + }); + None + } + Some(Pending::One { from }) => { + self.state.pending = Some(Pending::Two { + from, + to: self.state.cursor_position, + }); + + None + } + Some(Pending::Two { from, to }) => { + self.state.pending = None; + + Some(Curve { + from, + to, + control: self.state.cursor_position, + }) + } } - }, + } _ => None, }, } } fn draw(&self, bounds: Size) -> Vec { - let curves = self.state.cache.draw(bounds, &self.curves); + let content = self.state.cache.draw(bounds, |frame: &mut Frame| { + Curve::draw_all(self.curves, frame); + + frame.stroke( + &Path::rectangle(Point::ORIGIN, frame.size()), + Stroke::default(), + ); + }); if let Some(pending) = &self.state.pending { let pending_curve = pending.draw(bounds, self.state.cursor_position); - vec![curves, pending_curve] + vec![content, pending_curve] } else { - vec![curves] + vec![content] } } } @@ -169,14 +178,16 @@ mod bezier { control: Point, } - impl Drawable for Curve { - fn draw(&self, frame: &mut Frame) { - let curve = Path::new(|p| { - p.move_to(self.from); - p.quadratic_curve_to(self.control, self.to); + impl Curve { + fn draw_all(curves: &[Curve], frame: &mut Frame) { + let curves = Path::new(|p| { + for curve in curves { + p.move_to(curve.from); + p.quadratic_curve_to(curve.control, curve.to); + } }); - frame.stroke(&curve, Stroke::default().with_width(2.0)); + frame.stroke(&curves, Stroke::default().with_width(2.0)); } } @@ -202,7 +213,7 @@ mod bezier { control: cursor_position, }; - curve.draw(&mut frame); + Curve::draw_all(&[curve], &mut frame); } }; -- cgit From 5586034d6626e013cdd718aca1c4f19f6a060ff3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 29 Apr 2020 03:23:27 +0200 Subject: Display crosshair cursor in `bezier_tool` example --- examples/bezier_tool/src/main.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'examples/bezier_tool/src/main.rs') diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index 2112f662..8c9ebd75 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -70,7 +70,8 @@ impl Sandbox for Example { mod bezier { use iced::{ canvas::{self, Canvas, Event, Frame, Geometry, Path, Stroke}, - mouse, ButtonState, Element, Length, Point, Rectangle, Size, + mouse, ButtonState, Element, Length, MouseCursor, Point, Rectangle, + Size, }; #[derive(Default)] @@ -106,8 +107,6 @@ mod bezier { impl<'a> canvas::Program for Bezier<'a> { fn update(&mut self, event: Event, bounds: Size) -> Option { - let bounds = Rectangle::new(Point::ORIGIN, bounds); - match event { Event::Mouse(mouse_event) => match mouse_event { mouse::Event::CursorMoved { x, y } => { @@ -118,7 +117,9 @@ mod bezier { mouse::Event::Input { button: mouse::Button::Left, state: ButtonState::Pressed, - } if bounds.contains(self.state.cursor_position) => { + } if Rectangle::with_size(bounds) + .contains(self.state.cursor_position) => + { match self.state.pending { None => { self.state.pending = Some(Pending::One { @@ -169,6 +170,15 @@ mod bezier { vec![content] } } + + fn mouse_cursor(&self, bounds: Size) -> MouseCursor { + if Rectangle::with_size(bounds).contains(self.state.cursor_position) + { + MouseCursor::Crosshair + } else { + MouseCursor::default() + } + } } #[derive(Debug, Clone, Copy)] -- cgit From dc51080328caa12d2b1fc02febc72cab70bb9f50 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 29 Apr 2020 04:25:49 +0200 Subject: Introduce `Cursor` type in `canvas` --- examples/bezier_tool/src/main.rs | 137 ++++++++++++++++++++------------------- 1 file changed, 69 insertions(+), 68 deletions(-) (limited to 'examples/bezier_tool/src/main.rs') diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index 8c9ebd75..6473db75 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -69,15 +69,13 @@ impl Sandbox for Example { mod bezier { use iced::{ - canvas::{self, Canvas, Event, Frame, Geometry, Path, Stroke}, + canvas::{self, Canvas, Cursor, Event, Frame, Geometry, Path, Stroke}, mouse, ButtonState, Element, Length, MouseCursor, Point, Rectangle, - Size, }; #[derive(Default)] pub struct State { pending: Option, - cursor_position: Point, cache: canvas::Cache, } @@ -106,64 +104,62 @@ mod bezier { } impl<'a> canvas::Program for Bezier<'a> { - fn update(&mut self, event: Event, bounds: Size) -> Option { + fn update( + &mut self, + event: Event, + bounds: Rectangle, + cursor: Cursor, + ) -> Option { + let cursor_position = cursor.internal_position(&bounds)?; + match event { Event::Mouse(mouse_event) => match mouse_event { - mouse::Event::CursorMoved { x, y } => { - self.state.cursor_position = Point::new(x, y); - - None - } mouse::Event::Input { button: mouse::Button::Left, state: ButtonState::Pressed, - } if Rectangle::with_size(bounds) - .contains(self.state.cursor_position) => - { - match self.state.pending { - None => { - self.state.pending = Some(Pending::One { - from: self.state.cursor_position, - }); - None - } - Some(Pending::One { from }) => { - self.state.pending = Some(Pending::Two { - from, - to: self.state.cursor_position, - }); - - None - } - Some(Pending::Two { from, to }) => { - self.state.pending = None; - - Some(Curve { - from, - to, - control: self.state.cursor_position, - }) - } + } => match self.state.pending { + None => { + self.state.pending = Some(Pending::One { + from: cursor_position, + }); + None } - } + Some(Pending::One { from }) => { + self.state.pending = Some(Pending::Two { + from, + to: cursor_position, + }); + + None + } + Some(Pending::Two { from, to }) => { + self.state.pending = None; + + Some(Curve { + from, + to, + control: cursor_position, + }) + } + }, _ => None, }, } } - fn draw(&self, bounds: Size) -> Vec { - let content = self.state.cache.draw(bounds, |frame: &mut Frame| { - Curve::draw_all(self.curves, frame); + fn draw(&self, bounds: Rectangle, cursor: Cursor) -> Vec { + let content = + self.state.cache.draw(bounds.size(), |frame: &mut Frame| { + Curve::draw_all(self.curves, frame); - frame.stroke( - &Path::rectangle(Point::ORIGIN, frame.size()), - Stroke::default(), - ); - }); + frame.stroke( + &Path::rectangle(Point::ORIGIN, frame.size()), + Stroke::default(), + ); + }); if let Some(pending) = &self.state.pending { - let pending_curve = - pending.draw(bounds, self.state.cursor_position); + let pending_curve = pending.draw(bounds, cursor); vec![content, pending_curve] } else { @@ -171,9 +167,12 @@ mod bezier { } } - fn mouse_cursor(&self, bounds: Size) -> MouseCursor { - if Rectangle::with_size(bounds).contains(self.state.cursor_position) - { + fn mouse_cursor( + &self, + bounds: Rectangle, + cursor: Cursor, + ) -> MouseCursor { + if cursor.is_over(&bounds) { MouseCursor::Crosshair } else { MouseCursor::default() @@ -208,24 +207,26 @@ mod bezier { } impl Pending { - fn draw(&self, bounds: Size, cursor_position: Point) -> Geometry { - let mut frame = Frame::new(bounds); - - match *self { - Pending::One { from } => { - let line = Path::line(from, cursor_position); - frame.stroke(&line, Stroke::default().with_width(2.0)); - } - Pending::Two { from, to } => { - let curve = Curve { - from, - to, - control: cursor_position, - }; - - Curve::draw_all(&[curve], &mut frame); - } - }; + fn draw(&self, bounds: Rectangle, cursor: Cursor) -> Geometry { + let mut frame = Frame::new(bounds.size()); + + if let Some(cursor_position) = cursor.internal_position(&bounds) { + match *self { + Pending::One { from } => { + let line = Path::line(from, cursor_position); + frame.stroke(&line, Stroke::default().with_width(2.0)); + } + Pending::Two { from, to } => { + let curve = Curve { + from, + to, + control: cursor_position, + }; + + Curve::draw_all(&[curve], &mut frame); + } + }; + } frame.into_geometry() } -- cgit From 5d12e194f45b4a01034f3f52fae16c10bc0192dd Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 29 Apr 2020 20:58:59 +0200 Subject: Rename `Cursor::*_position` methods in `canvas` --- examples/bezier_tool/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples/bezier_tool/src/main.rs') diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index 6473db75..3cecd058 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -110,7 +110,7 @@ mod bezier { bounds: Rectangle, cursor: Cursor, ) -> Option { - let cursor_position = cursor.internal_position(&bounds)?; + let cursor_position = cursor.position_in(&bounds)?; match event { Event::Mouse(mouse_event) => match mouse_event { @@ -210,7 +210,7 @@ mod bezier { fn draw(&self, bounds: Rectangle, cursor: Cursor) -> Geometry { let mut frame = Frame::new(bounds.size()); - if let Some(cursor_position) = cursor.internal_position(&bounds) { + if let Some(cursor_position) = cursor.position_in(&bounds) { match *self { Pending::One { from } => { let line = Path::line(from, cursor_position); -- cgit From e55cd9652e7c7aea4dc2c6ccb83769246d1a808e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Apr 2020 04:53:15 +0200 Subject: Split `Input` mouse event by `ButtonState` --- examples/bezier_tool/src/main.rs | 55 ++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 28 deletions(-) (limited to 'examples/bezier_tool/src/main.rs') diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index 3cecd058..fe4136b4 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -70,7 +70,7 @@ impl Sandbox for Example { mod bezier { use iced::{ canvas::{self, Canvas, Cursor, Event, Frame, Geometry, Path, Stroke}, - mouse, ButtonState, Element, Length, MouseCursor, Point, Rectangle, + mouse, Element, Length, MouseCursor, Point, Rectangle, }; #[derive(Default)] @@ -114,34 +114,33 @@ mod bezier { match event { Event::Mouse(mouse_event) => match mouse_event { - mouse::Event::Input { - button: mouse::Button::Left, - state: ButtonState::Pressed, - } => match self.state.pending { - None => { - self.state.pending = Some(Pending::One { - from: cursor_position, - }); - None + mouse::Event::ButtonPressed(mouse::Button::Left) => { + match self.state.pending { + None => { + self.state.pending = Some(Pending::One { + from: cursor_position, + }); + None + } + Some(Pending::One { from }) => { + self.state.pending = Some(Pending::Two { + from, + to: cursor_position, + }); + + None + } + Some(Pending::Two { from, to }) => { + self.state.pending = None; + + Some(Curve { + from, + to, + control: cursor_position, + }) + } } - Some(Pending::One { from }) => { - self.state.pending = Some(Pending::Two { - from, - to: cursor_position, - }); - - None - } - Some(Pending::Two { from, to }) => { - self.state.pending = None; - - Some(Curve { - from, - to, - control: cursor_position, - }) - } - }, + } _ => None, }, } -- cgit From 98bc8cf2a7c4944d762a0148ca9f615d6ccc0d6e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Apr 2020 08:16:38 +0200 Subject: Rename `MouseCursor` to `mouse::Interaction` --- examples/bezier_tool/src/main.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'examples/bezier_tool/src/main.rs') diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index fe4136b4..fe41e1b2 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -70,7 +70,7 @@ impl Sandbox for Example { mod bezier { use iced::{ canvas::{self, Canvas, Cursor, Event, Frame, Geometry, Path, Stroke}, - mouse, Element, Length, MouseCursor, Point, Rectangle, + mouse, Element, Length, Point, Rectangle, }; #[derive(Default)] @@ -166,15 +166,15 @@ mod bezier { } } - fn mouse_cursor( + fn mouse_interaction( &self, bounds: Rectangle, cursor: Cursor, - ) -> MouseCursor { + ) -> mouse::Interaction { if cursor.is_over(&bounds) { - MouseCursor::Crosshair + mouse::Interaction::Crosshair } else { - MouseCursor::default() + mouse::Interaction::default() } } } -- cgit