diff options
author | 2020-03-05 22:05:05 -0700 | |
---|---|---|
committer | 2020-03-06 22:01:21 -0700 | |
commit | 1bb85556915bb00057ef2ee66a596592c292b15b (patch) | |
tree | 4a624fb4bc225ecb72b434176da69096411efda7 /wgpu/src/widget/canvas/frame.rs | |
parent | 29219500b7144f31dbf50fcc64653f7d2ce806d0 (diff) | |
download | iced-1bb85556915bb00057ef2ee66a596592c292b15b.tar.gz iced-1bb85556915bb00057ef2ee66a596592c292b15b.tar.bz2 iced-1bb85556915bb00057ef2ee66a596592c292b15b.zip |
implement text support in canvas widget
Diffstat (limited to 'wgpu/src/widget/canvas/frame.rs')
-rw-r--r-- | wgpu/src/widget/canvas/frame.rs | 52 |
1 files changed, 44 insertions, 8 deletions
diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs index fa6d8c0a..cdb9d0e1 100644 --- a/wgpu/src/widget/canvas/frame.rs +++ b/wgpu/src/widget/canvas/frame.rs @@ -1,10 +1,12 @@ use iced_native::{Point, Size, Vector}; use crate::{ - canvas::{Fill, Path, Stroke}, - triangle, + canvas::{Fill, Path, Stroke, TextNode}, + triangle, Primitive, }; +use std::sync::Arc; + /// The frame of a [`Canvas`]. /// /// [`Canvas`]: struct.Canvas.html @@ -14,6 +16,7 @@ pub struct Frame { height: f32, buffers: lyon::tessellation::VertexBuffers<triangle::Vertex2D, u32>, transforms: Transforms, + texts: Vec<TextNode>, } #[derive(Debug)] @@ -40,6 +43,7 @@ impl Frame { width, height, buffers: lyon::tessellation::VertexBuffers::new(), + texts: Vec::new(), transforms: Transforms { previous: Vec::new(), current: Transform { @@ -154,6 +158,14 @@ impl Frame { let _ = result.expect("Stroke path"); } + /// Draws the text of the given [`TextNode`] on the [`Frame`] + /// + /// [`TextNode`]: struct.TextNode.html + /// [`Frame`]: struct.Frame.html + pub fn text(&mut self, text: TextNode) { + self.texts.push(text); + } + /// Stores the current transform of the [`Frame`] and executes the given /// drawing operations, restoring the transform afterwards. /// @@ -209,14 +221,38 @@ impl Frame { self.transforms.current.is_identity = false; } - /// Produces the geometry that has been drawn on the [`Frame`]. + /// Produces the primitive representing everything drawn on the [`Frame`]. /// /// [`Frame`]: struct.Frame.html - pub fn into_mesh(self) -> triangle::Mesh2D { - triangle::Mesh2D { - vertices: self.buffers.vertices, - indices: self.buffers.indices, - } + pub fn into_primitive(self, origin: Point) -> Primitive { + let mut primitives: Vec<Primitive> = self + .texts + .into_iter() + .map(|mut t| { + t.bounds.x += origin.x; + t.bounds.y += origin.y; + + Primitive::Text { + content: t.content, + bounds: t.bounds, + color: t.color, + size: t.size, + font: t.font, + horizontal_alignment: t.horizontal_alignment, + vertical_alignment: t.vertical_alignment, + } + }) + .collect(); + + primitives.push(Primitive::Mesh2D { + origin, + buffers: Arc::new(triangle::Mesh2D { + vertices: self.buffers.vertices, + indices: self.buffers.indices, + }), + }); + + Primitive::Group { primitives } } } |