diff options
Diffstat (limited to 'wgpu')
-rw-r--r-- | wgpu/Cargo.toml | 2 | ||||
-rw-r--r-- | wgpu/src/renderer.rs | 25 | ||||
-rw-r--r-- | wgpu/src/renderer/widget/checkbox.rs | 6 | ||||
-rw-r--r-- | wgpu/src/renderer/widget/text.rs | 14 | ||||
-rw-r--r-- | wgpu/src/renderer/widget/text_input.rs | 25 | ||||
-rw-r--r-- | wgpu/src/widget/canvas/fill.rs | 6 | ||||
-rw-r--r-- | wgpu/src/widget/canvas/frame.rs | 28 | ||||
-rw-r--r-- | wgpu/src/widget/canvas/layer/cache.rs | 31 | ||||
-rw-r--r-- | wgpu/src/widget/canvas/path.rs | 29 | ||||
-rw-r--r-- | wgpu/src/widget/canvas/path/builder.rs | 13 | ||||
-rw-r--r-- | wgpu/src/widget/canvas/text.rs | 15 |
11 files changed, 140 insertions, 54 deletions
diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index de496aa9..0794b970 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_wgpu" -version = "0.2.0" +version = "0.2.1" authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"] edition = "2018" description = "A wgpu renderer for Iced" diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index e847cb64..c886bed0 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -152,31 +152,14 @@ impl Renderer { horizontal_alignment, vertical_alignment, } => { - let x = match horizontal_alignment { - iced_native::HorizontalAlignment::Left => bounds.x, - iced_native::HorizontalAlignment::Center => { - bounds.x + bounds.width / 2.0 - } - iced_native::HorizontalAlignment::Right => { - bounds.x + bounds.width - } - }; - - let y = match vertical_alignment { - iced_native::VerticalAlignment::Top => bounds.y, - iced_native::VerticalAlignment::Center => { - bounds.y + bounds.height / 2.0 - } - iced_native::VerticalAlignment::Bottom => { - bounds.y + bounds.height - } - }; - let layer = layers.last_mut().unwrap(); layer.text.push(wgpu_glyph::Section { text: &content, - screen_position: (x + translation.x, y + translation.y), + screen_position: ( + bounds.x + translation.x, + bounds.y + translation.y, + ), bounds: (bounds.width, bounds.height), scale: wgpu_glyph::Scale { x: *size, y: *size }, color: color.into_linear(), diff --git a/wgpu/src/renderer/widget/checkbox.rs b/wgpu/src/renderer/widget/checkbox.rs index ecacf1de..c0f1bf21 100644 --- a/wgpu/src/renderer/widget/checkbox.rs +++ b/wgpu/src/renderer/widget/checkbox.rs @@ -38,7 +38,11 @@ impl checkbox::Renderer for Renderer { content: crate::text::CHECKMARK_ICON.to_string(), font: crate::text::BUILTIN_ICONS, size: bounds.height * 0.7, - bounds, + bounds: Rectangle { + x: bounds.center_x(), + y: bounds.center_y(), + ..bounds + }, color: style.checkmark_color, horizontal_alignment: HorizontalAlignment::Center, vertical_alignment: VerticalAlignment::Center, diff --git a/wgpu/src/renderer/widget/text.rs b/wgpu/src/renderer/widget/text.rs index 33e549cd..80bff574 100644 --- a/wgpu/src/renderer/widget/text.rs +++ b/wgpu/src/renderer/widget/text.rs @@ -31,11 +31,23 @@ impl text::Renderer for Renderer { horizontal_alignment: HorizontalAlignment, vertical_alignment: VerticalAlignment, ) -> Self::Output { + let x = match horizontal_alignment { + iced_native::HorizontalAlignment::Left => bounds.x, + iced_native::HorizontalAlignment::Center => bounds.center_x(), + iced_native::HorizontalAlignment::Right => bounds.x + bounds.width, + }; + + let y = match vertical_alignment { + iced_native::VerticalAlignment::Top => bounds.y, + iced_native::VerticalAlignment::Center => bounds.center_y(), + iced_native::VerticalAlignment::Bottom => bounds.y + bounds.height, + }; + ( Primitive::Text { content: content.to_string(), size: f32::from(size), - bounds, + bounds: Rectangle { x, y, ..bounds }, color: color.unwrap_or(defaults.text.color), font, horizontal_alignment, diff --git a/wgpu/src/renderer/widget/text_input.rs b/wgpu/src/renderer/widget/text_input.rs index 9093b0c6..6f72db68 100644 --- a/wgpu/src/renderer/widget/text_input.rs +++ b/wgpu/src/renderer/widget/text_input.rs @@ -23,11 +23,11 @@ impl text_input::Renderer for Renderer { Size::INFINITY, ); - let spaces_at_the_end = value.len() - value.trim_end().len(); + let spaces_around = value.len() - value.trim().len(); - if spaces_at_the_end > 0 { + if spaces_around > 0 { let space_width = self.text_pipeline.space_width(size as f32); - width += spaces_at_the_end as f32 * space_width; + width += spaces_around as f32 * space_width; } width @@ -109,6 +109,7 @@ impl text_input::Renderer for Renderer { }, font, bounds: Rectangle { + y: text_bounds.center_y(), width: f32::INFINITY, ..text_bounds }, @@ -210,10 +211,20 @@ impl text_input::Renderer for Renderer { (text_value, Vector::new(0, 0)) }; - let contents = Primitive::Clip { - bounds: text_bounds, - offset, - content: Box::new(contents_primitive), + let text_width = self.measure_value( + if text.is_empty() { placeholder } else { &text }, + size, + font, + ); + + let contents = if text_width > text_bounds.width { + Primitive::Clip { + bounds: text_bounds, + offset, + content: Box::new(contents_primitive), + } + } else { + contents_primitive }; ( 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<Color> for Fill { + fn from(color: Color) -> Fill { + Fill::Color(color) + } +} diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs index f6495bdc..24fd0d7e 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<Fill>) { 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(), }), ); @@ -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<Stroke>) { use lyon::tessellation::{ BuffersBuilder, StrokeOptions, StrokeTessellator, }; + let stroke = stroke.into(); + let mut buffers = BuffersBuilder::new( &mut self.buffers, StrokeVertex(stroke.color.into_linear()), @@ -173,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<Text>) { use std::f32; + let text = text.into(); + let position = if self.transforms.current.is_identity { text.position } else { @@ -262,13 +266,15 @@ impl Frame { /// /// [`Frame`]: struct.Frame.html pub fn into_primitive(mut self) -> Primitive { - self.primitives.push(Primitive::Mesh2D { - origin: Point::ORIGIN, - buffers: triangle::Mesh2D { - vertices: self.buffers.vertices, - indices: self.buffers.indices, - }, - }); + if !self.buffers.indices.is_empty() { + self.primitives.push(Primitive::Mesh2D { + origin: Point::ORIGIN, + buffers: triangle::Mesh2D { + vertices: self.buffers.vertices, + indices: self.buffers.indices, + }, + }); + } Primitive::Group { primitives: self.primitives, diff --git a/wgpu/src/widget/canvas/layer/cache.rs b/wgpu/src/widget/canvas/layer/cache.rs index 20a095bd..4f8c2bec 100644 --- a/wgpu/src/widget/canvas/layer/cache.rs +++ b/wgpu/src/widget/canvas/layer/cache.rs @@ -6,6 +6,19 @@ use crate::{ use iced_native::Size; use std::{cell::RefCell, marker::PhantomData, sync::Arc}; +enum State { + Empty, + Filled { + bounds: Size, + primitive: Arc<Primitive>, + }, +} + +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 @@ -19,12 +32,16 @@ pub struct Cache<T: Drawable> { state: RefCell<State>, } -enum State { - Empty, - Filled { - bounds: Size, - primitive: Arc<Primitive>, - }, +impl<T> Default for Cache<T> +where + T: Drawable, +{ + fn default() -> Self { + Self { + input: PhantomData, + state: Default::default(), + } + } } impl<T> Cache<T> @@ -37,7 +54,7 @@ where pub fn new() -> Self { Cache { input: PhantomData, - state: RefCell::new(State::Empty), + state: Default::default(), } } diff --git a/wgpu/src/widget/canvas/path.rs b/wgpu/src/widget/canvas/path.rs index e7ff47f3..c26bf187 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,33 @@ 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`. + /// + /// [`Path`]: struct.Path.html + pub fn rectangle(top_left: Point, size: Size) -> Self { + 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 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(); } diff --git a/wgpu/src/widget/canvas/text.rs b/wgpu/src/widget/canvas/text.rs index d1cf1a0f..c4cae30e 100644 --- a/wgpu/src/widget/canvas/text.rs +++ b/wgpu/src/widget/canvas/text.rs @@ -32,3 +32,18 @@ impl Default for Text { } } } + +impl From<String> for Text { + fn from(content: String) -> Text { + Text { + content, + ..Default::default() + } + } +} + +impl From<&str> for Text { + fn from(content: &str) -> Text { + String::from(content).into() + } +} |