From 2a795faf4e3357397298d0c64dcc2c56169906b8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Apr 2020 06:37:27 +0200 Subject: Make `Frame::fill` take a generic `Into` This can be used to improve readability by using your own types. --- wgpu/src/widget/canvas/frame.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'wgpu/src/widget') diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs index 940be402..f04c6e78 100644 --- a/wgpu/src/widget/canvas/frame.rs +++ b/wgpu/src/widget/canvas/frame.rs @@ -89,14 +89,14 @@ impl Frame { /// /// [`Path`]: path/struct.Path.html /// [`Frame`]: struct.Frame.html - pub fn fill(&mut self, path: &Path, fill: Fill) { + pub fn fill(&mut self, path: &Path, fill: impl Into) { use lyon::tessellation::{ BuffersBuilder, FillOptions, FillTessellator, }; let mut buffers = BuffersBuilder::new( &mut self.buffers, - FillVertex(match fill { + FillVertex(match fill.into() { Fill::Color(color) => color.into_linear(), }), ); -- cgit From 81096ef454a9aacacf3853f7dfe96b8b9228f200 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Apr 2020 06:38:06 +0200 Subject: Implement `From` for `canvas::Fill` --- wgpu/src/widget/canvas/fill.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'wgpu/src/widget') diff --git a/wgpu/src/widget/canvas/fill.rs b/wgpu/src/widget/canvas/fill.rs index 5ce24cf3..a2010e45 100644 --- a/wgpu/src/widget/canvas/fill.rs +++ b/wgpu/src/widget/canvas/fill.rs @@ -12,3 +12,9 @@ impl Default for Fill { Fill::Color(Color::BLACK) } } + +impl From for Fill { + fn from(color: Color) -> Fill { + Fill::Color(color) + } +} -- cgit From dce1034699daee67138a54944c174c0aa5aec9dd Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Apr 2020 06:39:47 +0200 Subject: Make `Frame::stroke` take a generic `Into` --- wgpu/src/widget/canvas/frame.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'wgpu/src/widget') diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs index f04c6e78..f22d7887 100644 --- a/wgpu/src/widget/canvas/frame.rs +++ b/wgpu/src/widget/canvas/frame.rs @@ -127,11 +127,13 @@ impl Frame { /// /// [`Path`]: path/struct.Path.html /// [`Frame`]: struct.Frame.html - pub fn stroke(&mut self, path: &Path, stroke: Stroke) { + pub fn stroke(&mut self, path: &Path, stroke: impl Into) { use lyon::tessellation::{ BuffersBuilder, StrokeOptions, StrokeTessellator, }; + let stroke = stroke.into(); + let mut buffers = BuffersBuilder::new( &mut self.buffers, StrokeVertex(stroke.color.into_linear()), -- cgit From 6779fcf62134397671cdb7d3c406262911dcc4c8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Apr 2020 06:40:22 +0200 Subject: Make `Frame::fill_text` take a generic `Into` --- wgpu/src/widget/canvas/frame.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'wgpu/src/widget') diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs index f22d7887..de4717f1 100644 --- a/wgpu/src/widget/canvas/frame.rs +++ b/wgpu/src/widget/canvas/frame.rs @@ -175,9 +175,11 @@ impl Frame { /// [`Text`]: struct.Text.html /// [`Frame`]: struct.Frame.html /// [`Canvas`]: struct.Canvas.html - pub fn fill_text(&mut self, text: Text) { + pub fn fill_text(&mut self, text: impl Into) { use std::f32; + let text = text.into(); + let position = if self.transforms.current.is_identity { text.position } else { -- cgit From a2296b466bdb512aeabfed0619ce0f87049717e5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Apr 2020 06:43:43 +0200 Subject: Implement `From` for `canvas::Text` --- wgpu/src/widget/canvas/text.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'wgpu/src/widget') diff --git a/wgpu/src/widget/canvas/text.rs b/wgpu/src/widget/canvas/text.rs index d1cf1a0f..0589c35d 100644 --- a/wgpu/src/widget/canvas/text.rs +++ b/wgpu/src/widget/canvas/text.rs @@ -32,3 +32,12 @@ impl Default for Text { } } } + +impl From for Text { + fn from(content: String) -> Text { + Text { + content, + ..Default::default() + } + } +} -- cgit From 5c923fce4866bcda0d2d2c1023bb046cd92038cc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Apr 2020 06:43:58 +0200 Subject: Implement `From<&str>` for `canvas::Text` --- wgpu/src/widget/canvas/text.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'wgpu/src/widget') diff --git a/wgpu/src/widget/canvas/text.rs b/wgpu/src/widget/canvas/text.rs index 0589c35d..c4cae30e 100644 --- a/wgpu/src/widget/canvas/text.rs +++ b/wgpu/src/widget/canvas/text.rs @@ -41,3 +41,9 @@ impl From for Text { } } } + +impl From<&str> for Text { + fn from(content: &str) -> Text { + String::from(content).into() + } +} -- cgit From c545af35773307d16eca7ec03ed4794f26491da2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Apr 2020 06:49:15 +0200 Subject: Implement `canvas::Path::rectangle` helper method --- wgpu/src/widget/canvas/path.rs | 10 ++++++++++ wgpu/src/widget/canvas/path/builder.rs | 13 ++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) (limited to 'wgpu/src/widget') diff --git a/wgpu/src/widget/canvas/path.rs b/wgpu/src/widget/canvas/path.rs index e7ff47f3..d714ad05 100644 --- a/wgpu/src/widget/canvas/path.rs +++ b/wgpu/src/widget/canvas/path.rs @@ -7,6 +7,8 @@ mod builder; pub use arc::Arc; pub use builder::Builder; +use iced_native::{Point, Size}; + /// An immutable set of points that may or may not be connected. /// /// A single [`Path`] can represent different kinds of 2D shapes! @@ -33,6 +35,14 @@ impl Path { builder.build() } + /// Creates a new [`Path`] representing a rectangle given its top-left + /// corner coordinate and its `Size`. + /// + /// [`Path`]: struct.Path.html + pub fn rectangle(top_left: Point, size: Size) -> Self { + Self::new(|p| p.rectangle(top_left, size)) + } + #[inline] pub(crate) fn raw(&self) -> &lyon::path::Path { &self.raw diff --git a/wgpu/src/widget/canvas/path/builder.rs b/wgpu/src/widget/canvas/path/builder.rs index a013149e..6511fa52 100644 --- a/wgpu/src/widget/canvas/path/builder.rs +++ b/wgpu/src/widget/canvas/path/builder.rs @@ -133,11 +133,14 @@ impl Builder { /// /// [`Path`]: struct.Path.html #[inline] - pub fn rectangle(&mut self, p: Point, size: Size) { - self.move_to(p); - self.line_to(Point::new(p.x + size.width, p.y)); - self.line_to(Point::new(p.x + size.width, p.y + size.height)); - self.line_to(Point::new(p.x, p.y + size.height)); + pub fn rectangle(&mut self, top_left: Point, size: Size) { + self.move_to(top_left); + self.line_to(Point::new(top_left.x + size.width, top_left.y)); + self.line_to(Point::new( + top_left.x + size.width, + top_left.y + size.height, + )); + self.line_to(Point::new(top_left.x, top_left.y + size.height)); self.close(); } -- cgit From 46cd0891d25c2dd48e182747d8c1f9579b066490 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Apr 2020 06:54:12 +0200 Subject: Implement `canvas::Path::circle` helper method --- wgpu/src/widget/canvas/path.rs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'wgpu/src/widget') diff --git a/wgpu/src/widget/canvas/path.rs b/wgpu/src/widget/canvas/path.rs index d714ad05..19d8879a 100644 --- a/wgpu/src/widget/canvas/path.rs +++ b/wgpu/src/widget/canvas/path.rs @@ -43,6 +43,14 @@ impl Path { Self::new(|p| p.rectangle(top_left, size)) } + /// Creates a new [`Path`] representing a circle given its center + /// coordinate and its radius. + /// + /// [`Path`]: struct.Path.html + pub fn circle(center: Point, radius: f32) -> Self { + Self::new(|p| p.circle(center, radius)) + } + #[inline] pub(crate) fn raw(&self) -> &lyon::path::Path { &self.raw -- cgit From 3df49bebd47e8ff7c54934d6474336b7439176f9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Apr 2020 07:08:12 +0200 Subject: Implement `canvas::Path::line` helper method --- wgpu/src/widget/canvas/path.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'wgpu/src/widget') diff --git a/wgpu/src/widget/canvas/path.rs b/wgpu/src/widget/canvas/path.rs index 19d8879a..c26bf187 100644 --- a/wgpu/src/widget/canvas/path.rs +++ b/wgpu/src/widget/canvas/path.rs @@ -35,6 +35,17 @@ impl Path { builder.build() } + /// Creates a new [`Path`] representing a line segment given its starting + /// and end points. + /// + /// [`Path`]: struct.Path.html + pub fn line(from: Point, to: Point) -> Self { + Self::new(|p| { + p.move_to(from); + p.line_to(to); + }) + } + /// Creates a new [`Path`] representing a rectangle given its top-left /// corner coordinate and its `Size`. /// -- cgit