Implement Primitive::Cached
This commit is contained in:
parent
37f0d97159
commit
b74e7e7353
12 changed files with 151 additions and 105 deletions
|
|
@ -123,7 +123,10 @@ impl<'a, Message> Widget<Message, Renderer> for Canvas<'a> {
|
|||
primitives: self
|
||||
.layers
|
||||
.iter()
|
||||
.map(|layer| layer.draw(origin, size))
|
||||
.map(|layer| Primitive::Cached {
|
||||
origin,
|
||||
cache: layer.draw(size),
|
||||
})
|
||||
.collect(),
|
||||
},
|
||||
MouseCursor::Idle,
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
use iced_native::{Point, Size, Vector};
|
||||
use iced_native::{Point, Rectangle, Size, Vector};
|
||||
|
||||
use crate::{
|
||||
canvas::{Fill, Path, Stroke, Text},
|
||||
triangle, Primitive,
|
||||
};
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
/// The frame of a [`Canvas`].
|
||||
///
|
||||
/// [`Canvas`]: struct.Canvas.html
|
||||
|
|
@ -15,8 +13,8 @@ pub struct Frame {
|
|||
width: f32,
|
||||
height: f32,
|
||||
buffers: lyon::tessellation::VertexBuffers<triangle::Vertex2D, u32>,
|
||||
primitives: Vec<Primitive>,
|
||||
transforms: Transforms,
|
||||
texts: Vec<Text>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -43,7 +41,7 @@ impl Frame {
|
|||
width,
|
||||
height,
|
||||
buffers: lyon::tessellation::VertexBuffers::new(),
|
||||
texts: Vec::new(),
|
||||
primitives: Vec::new(),
|
||||
transforms: Transforms {
|
||||
previous: Vec::new(),
|
||||
current: Transform {
|
||||
|
|
@ -165,7 +163,23 @@ impl Frame {
|
|||
/// [`Frame`]: struct.Frame.html
|
||||
#[inline]
|
||||
pub fn fill_text(&mut self, text: Text) {
|
||||
self.texts.push(text);
|
||||
use std::f32;
|
||||
|
||||
// TODO: Use vectorial text instead of primitive
|
||||
self.primitives.push(Primitive::Text {
|
||||
content: text.content,
|
||||
bounds: Rectangle {
|
||||
x: text.position.x,
|
||||
y: text.position.y,
|
||||
width: f32::INFINITY,
|
||||
height: f32::INFINITY,
|
||||
},
|
||||
color: text.color,
|
||||
size: text.size,
|
||||
font: text.font,
|
||||
horizontal_alignment: text.horizontal_alignment,
|
||||
vertical_alignment: text.vertical_alignment,
|
||||
});
|
||||
}
|
||||
|
||||
/// Stores the current transform of the [`Frame`] and executes the given
|
||||
|
|
@ -226,35 +240,18 @@ impl Frame {
|
|||
/// Produces the primitive representing everything drawn on the [`Frame`].
|
||||
///
|
||||
/// [`Frame`]: struct.Frame.html
|
||||
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 {
|
||||
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,
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
Primitive::Group { primitives }
|
||||
Primitive::Group {
|
||||
primitives: self.primitives,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,21 +4,22 @@ mod cache;
|
|||
pub use cache::Cache;
|
||||
|
||||
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`].
|
||||
///
|
||||
/// [`Canvas`]: ../struct.Canvas.html
|
||||
pub trait Layer: std::fmt::Debug {
|
||||
/// Draws the [`Layer`] in the given bounds and produces [`Mesh2D`] as a
|
||||
/// result.
|
||||
/// Draws the [`Layer`] in the given bounds and produces a [`Primitive`] as
|
||||
/// a result.
|
||||
///
|
||||
/// The [`Layer`] may choose to store the produced [`Mesh2D`] locally and
|
||||
/// The [`Layer`] may choose to store the produced [`Primitive`] locally and
|
||||
/// only recompute it when the bounds change, its contents change, or is
|
||||
/// otherwise explicitly cleared by other means.
|
||||
///
|
||||
/// [`Layer`]: trait.Layer.html
|
||||
/// [`Mesh2D`]: ../../../triangle/struct.Mesh2D.html
|
||||
fn draw(&self, origin: Point, bounds: Size) -> Primitive;
|
||||
/// [`Primitive`]: ../../../enum.Primitive.html
|
||||
fn draw(&self, bounds: Size) -> Arc<Primitive>;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ use crate::{
|
|||
Primitive,
|
||||
};
|
||||
|
||||
use iced_native::{Point, Size};
|
||||
use std::{cell::RefCell, marker::PhantomData};
|
||||
use iced_native::Size;
|
||||
use std::{cell::RefCell, marker::PhantomData, sync::Arc};
|
||||
|
||||
/// A simple cache that stores generated geometry to avoid recomputation.
|
||||
///
|
||||
|
|
@ -22,7 +22,10 @@ pub struct Cache<T: Drawable> {
|
|||
#[derive(Debug)]
|
||||
enum State {
|
||||
Empty,
|
||||
Filled { bounds: Size, primitive: Primitive },
|
||||
Filled {
|
||||
bounds: Size,
|
||||
primitive: Arc<Primitive>,
|
||||
},
|
||||
}
|
||||
|
||||
impl<T> Cache<T>
|
||||
|
|
@ -70,7 +73,7 @@ impl<'a, T> Layer for Bind<'a, T>
|
|||
where
|
||||
T: Drawable + std::fmt::Debug,
|
||||
{
|
||||
fn draw(&self, origin: Point, current_bounds: Size) -> Primitive {
|
||||
fn draw(&self, current_bounds: Size) -> Arc<Primitive> {
|
||||
use std::ops::Deref;
|
||||
|
||||
if let State::Filled { bounds, primitive } =
|
||||
|
|
@ -84,7 +87,7 @@ where
|
|||
let mut frame = Frame::new(current_bounds.width, current_bounds.height);
|
||||
self.input.draw(&mut frame);
|
||||
|
||||
let primitive = frame.into_primitive(origin);
|
||||
let primitive = Arc::new(frame.into_primitive());
|
||||
|
||||
*self.cache.state.borrow_mut() = State::Filled {
|
||||
bounds: current_bounds,
|
||||
|
|
|
|||
|
|
@ -1,14 +1,12 @@
|
|||
use iced_native::{
|
||||
Color, Font, HorizontalAlignment, Rectangle, VerticalAlignment,
|
||||
};
|
||||
use iced_native::{Color, Font, HorizontalAlignment, Point, VerticalAlignment};
|
||||
|
||||
/// A bunch of text that can be drawn to a canvas
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Text {
|
||||
/// The contents of the text
|
||||
pub content: String,
|
||||
/// The bounds of the text
|
||||
pub bounds: Rectangle,
|
||||
/// The position where to begin drawing the text (top-left corner coordinates)
|
||||
pub position: Point,
|
||||
/// The color of the text
|
||||
pub color: Color,
|
||||
/// The size of the text
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue