implement text support in canvas widget

This commit is contained in:
Artur Sapek 2020-03-05 22:05:05 -07:00 committed by Artur Sapek
parent 29219500b7
commit 1bb8555691
5 changed files with 80 additions and 31 deletions

View file

@ -20,6 +20,7 @@ mod drawable;
mod fill; mod fill;
mod frame; mod frame;
mod stroke; mod stroke;
mod text;
pub use drawable::Drawable; pub use drawable::Drawable;
pub use fill::Fill; pub use fill::Fill;
@ -27,6 +28,7 @@ pub use frame::Frame;
pub use layer::Layer; pub use layer::Layer;
pub use path::Path; pub use path::Path;
pub use stroke::{LineCap, LineJoin, Stroke}; pub use stroke::{LineCap, LineJoin, Stroke};
pub use text::TextNode;
/// A widget capable of drawing 2D graphics. /// A widget capable of drawing 2D graphics.
/// ///
@ -121,10 +123,7 @@ impl<'a, Message> Widget<Message, Renderer> for Canvas<'a> {
primitives: self primitives: self
.layers .layers
.iter() .iter()
.map(|layer| Primitive::Mesh2D { .map(|layer| layer.draw(origin, size))
origin,
buffers: layer.draw(size),
})
.collect(), .collect(),
}, },
MouseCursor::Idle, MouseCursor::Idle,

View file

@ -1,10 +1,12 @@
use iced_native::{Point, Size, Vector}; use iced_native::{Point, Size, Vector};
use crate::{ use crate::{
canvas::{Fill, Path, Stroke}, canvas::{Fill, Path, Stroke, TextNode},
triangle, triangle, Primitive,
}; };
use std::sync::Arc;
/// The frame of a [`Canvas`]. /// The frame of a [`Canvas`].
/// ///
/// [`Canvas`]: struct.Canvas.html /// [`Canvas`]: struct.Canvas.html
@ -14,6 +16,7 @@ pub struct Frame {
height: f32, height: f32,
buffers: lyon::tessellation::VertexBuffers<triangle::Vertex2D, u32>, buffers: lyon::tessellation::VertexBuffers<triangle::Vertex2D, u32>,
transforms: Transforms, transforms: Transforms,
texts: Vec<TextNode>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -40,6 +43,7 @@ impl Frame {
width, width,
height, height,
buffers: lyon::tessellation::VertexBuffers::new(), buffers: lyon::tessellation::VertexBuffers::new(),
texts: Vec::new(),
transforms: Transforms { transforms: Transforms {
previous: Vec::new(), previous: Vec::new(),
current: Transform { current: Transform {
@ -154,6 +158,14 @@ impl Frame {
let _ = result.expect("Stroke path"); 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 /// Stores the current transform of the [`Frame`] and executes the given
/// drawing operations, restoring the transform afterwards. /// drawing operations, restoring the transform afterwards.
/// ///
@ -209,14 +221,38 @@ impl Frame {
self.transforms.current.is_identity = false; 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 /// [`Frame`]: struct.Frame.html
pub fn into_mesh(self) -> triangle::Mesh2D { pub fn into_primitive(self, origin: Point) -> Primitive {
triangle::Mesh2D { let mut primitives: Vec<Primitive> = self
vertices: self.buffers.vertices, .texts
indices: self.buffers.indices, .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 }
} }
} }

View file

@ -3,10 +3,9 @@ mod cache;
pub use cache::Cache; pub use cache::Cache;
use crate::triangle; use crate::Primitive;
use iced_native::Size; use iced_native::{Point, Size};
use std::sync::Arc;
/// A layer that can be presented at a [`Canvas`]. /// A layer that can be presented at a [`Canvas`].
/// ///
@ -21,5 +20,5 @@ pub trait Layer: std::fmt::Debug {
/// ///
/// [`Layer`]: trait.Layer.html /// [`Layer`]: trait.Layer.html
/// [`Mesh2D`]: ../../../triangle/struct.Mesh2D.html /// [`Mesh2D`]: ../../../triangle/struct.Mesh2D.html
fn draw(&self, bounds: Size) -> Arc<triangle::Mesh2D>; fn draw(&self, origin: Point, bounds: Size) -> Primitive;
} }

View file

@ -1,12 +1,10 @@
use crate::{ use crate::{
canvas::{Drawable, Frame, Layer}, canvas::{Drawable, Frame, Layer},
triangle, Primitive,
}; };
use iced_native::Size; use iced_native::{Point, Size};
use std::cell::RefCell; use std::{cell::RefCell, marker::PhantomData};
use std::marker::PhantomData;
use std::sync::Arc;
/// A simple cache that stores generated geometry to avoid recomputation. /// A simple cache that stores generated geometry to avoid recomputation.
/// ///
@ -24,10 +22,7 @@ pub struct Cache<T: Drawable> {
#[derive(Debug)] #[derive(Debug)]
enum State { enum State {
Empty, Empty,
Filled { Filled { bounds: Size, primitive: Primitive },
mesh: Arc<triangle::Mesh2D>,
bounds: Size,
},
} }
impl<T> Cache<T> impl<T> Cache<T>
@ -75,27 +70,27 @@ impl<'a, T> Layer for Bind<'a, T>
where where
T: Drawable + std::fmt::Debug, 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; use std::ops::Deref;
if let State::Filled { mesh, bounds } = if let State::Filled { bounds, primitive } =
self.cache.state.borrow().deref() self.cache.state.borrow().deref()
{ {
if *bounds == current_bounds { if *bounds == current_bounds {
return mesh.clone(); return primitive.clone();
} }
} }
let mut frame = Frame::new(current_bounds.width, current_bounds.height); let mut frame = Frame::new(current_bounds.width, current_bounds.height);
self.input.draw(&mut frame); 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 { *self.cache.state.borrow_mut() = State::Filled {
mesh: mesh.clone(),
bounds: current_bounds, bounds: current_bounds,
primitive: primitive.clone(),
}; };
mesh primitive
} }
} }

View file

@ -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,
}