summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Artur Sapek <me@artur.co>2020-03-05 22:05:05 -0700
committerLibravatar Artur Sapek <artur@lich.io>2020-03-06 22:01:21 -0700
commit1bb85556915bb00057ef2ee66a596592c292b15b (patch)
tree4a624fb4bc225ecb72b434176da69096411efda7
parent29219500b7144f31dbf50fcc64653f7d2ce806d0 (diff)
downloadiced-1bb85556915bb00057ef2ee66a596592c292b15b.tar.gz
iced-1bb85556915bb00057ef2ee66a596592c292b15b.tar.bz2
iced-1bb85556915bb00057ef2ee66a596592c292b15b.zip
implement text support in canvas widget
-rw-r--r--wgpu/src/widget/canvas.rs7
-rw-r--r--wgpu/src/widget/canvas/frame.rs52
-rw-r--r--wgpu/src/widget/canvas/layer.rs7
-rw-r--r--wgpu/src/widget/canvas/layer/cache.rs25
-rw-r--r--wgpu/src/widget/canvas/text.rs20
5 files changed, 80 insertions, 31 deletions
diff --git a/wgpu/src/widget/canvas.rs b/wgpu/src/widget/canvas.rs
index 38c1ce62..7d0c5bf3 100644
--- a/wgpu/src/widget/canvas.rs
+++ b/wgpu/src/widget/canvas.rs
@@ -20,6 +20,7 @@ mod drawable;
mod fill;
mod frame;
mod stroke;
+mod text;
pub use drawable::Drawable;
pub use fill::Fill;
@@ -27,6 +28,7 @@ pub use frame::Frame;
pub use layer::Layer;
pub use path::Path;
pub use stroke::{LineCap, LineJoin, Stroke};
+pub use text::TextNode;
/// A widget capable of drawing 2D graphics.
///
@@ -121,10 +123,7 @@ impl<'a, Message> Widget<Message, Renderer> for Canvas<'a> {
primitives: self
.layers
.iter()
- .map(|layer| Primitive::Mesh2D {
- origin,
- buffers: layer.draw(size),
- })
+ .map(|layer| layer.draw(origin, size))
.collect(),
},
MouseCursor::Idle,
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 }
}
}
diff --git a/wgpu/src/widget/canvas/layer.rs b/wgpu/src/widget/canvas/layer.rs
index 82d647bb..95e2d0ee 100644
--- a/wgpu/src/widget/canvas/layer.rs
+++ b/wgpu/src/widget/canvas/layer.rs
@@ -3,10 +3,9 @@ mod cache;
pub use cache::Cache;
-use crate::triangle;
+use crate::Primitive;
-use iced_native::Size;
-use std::sync::Arc;
+use iced_native::{Point, Size};
/// A layer that can be presented at a [`Canvas`].
///
@@ -21,5 +20,5 @@ pub trait Layer: std::fmt::Debug {
///
/// [`Layer`]: trait.Layer.html
/// [`Mesh2D`]: ../../../triangle/struct.Mesh2D.html
- fn draw(&self, bounds: Size) -> Arc<triangle::Mesh2D>;
+ fn draw(&self, origin: Point, bounds: Size) -> Primitive;
}
diff --git a/wgpu/src/widget/canvas/layer/cache.rs b/wgpu/src/widget/canvas/layer/cache.rs
index 3071cce0..f05028da 100644
--- a/wgpu/src/widget/canvas/layer/cache.rs
+++ b/wgpu/src/widget/canvas/layer/cache.rs
@@ -1,12 +1,10 @@
use crate::{
canvas::{Drawable, Frame, Layer},
- triangle,
+ Primitive,
};
-use iced_native::Size;
-use std::cell::RefCell;
-use std::marker::PhantomData;
-use std::sync::Arc;
+use iced_native::{Point, Size};
+use std::{cell::RefCell, marker::PhantomData};
/// A simple cache that stores generated geometry to avoid recomputation.
///
@@ -24,10 +22,7 @@ pub struct Cache<T: Drawable> {
#[derive(Debug)]
enum State {
Empty,
- Filled {
- mesh: Arc<triangle::Mesh2D>,
- bounds: Size,
- },
+ Filled { bounds: Size, primitive: Primitive },
}
impl<T> Cache<T>
@@ -75,27 +70,27 @@ impl<'a, T> Layer for Bind<'a, T>
where
T: Drawable + std::fmt::Debug,
{
- fn draw(&self, current_bounds: Size) -> Arc<triangle::Mesh2D> {
+ fn draw(&self, origin: Point, current_bounds: Size) -> Primitive {
use std::ops::Deref;
- if let State::Filled { mesh, bounds } =
+ if let State::Filled { bounds, primitive } =
self.cache.state.borrow().deref()
{
if *bounds == current_bounds {
- return mesh.clone();
+ return primitive.clone();
}
}
let mut frame = Frame::new(current_bounds.width, current_bounds.height);
self.input.draw(&mut frame);
- let mesh = Arc::new(frame.into_mesh());
+ let primitive = frame.into_primitive(origin);
*self.cache.state.borrow_mut() = State::Filled {
- mesh: mesh.clone(),
bounds: current_bounds,
+ primitive: primitive.clone(),
};
- mesh
+ primitive
}
}
diff --git a/wgpu/src/widget/canvas/text.rs b/wgpu/src/widget/canvas/text.rs
new file mode 100644
index 00000000..5f6abe58
--- /dev/null
+++ b/wgpu/src/widget/canvas/text.rs
@@ -0,0 +1,20 @@
+use iced_native::{Color, Font, HorizontalAlignment, Rectangle, VerticalAlignment};
+
+/// A text node to be drawn to a canvas
+#[derive(Debug, Clone)]
+pub struct TextNode {
+ /// The contents of the text
+ pub content: String,
+ /// The bounds of the text
+ pub bounds: Rectangle,
+ /// The color of the text
+ pub color: Color,
+ /// The size of the text
+ pub size: f32,
+ /// The font of the text
+ pub font: Font,
+ /// The horizontal alignment of the text
+ pub horizontal_alignment: HorizontalAlignment,
+ /// The vertical alignment of the text
+ pub vertical_alignment: VerticalAlignment,
+}