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

View File

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

View File

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

View File

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

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