Introduce Translate primitive in iced_wgpu

This commit is contained in:
Héctor Ramón Jiménez 2020-04-28 03:18:31 +02:00
parent 2381a9310c
commit 59b1e90661
10 changed files with 127 additions and 104 deletions

View file

@ -10,7 +10,7 @@ use crate::{Defaults, Primitive, Renderer};
use iced_native::{
input::mouse, layout, Clipboard, Element, Hasher, Layout, Length,
MouseCursor, Point, Size, Widget,
MouseCursor, Point, Size, Vector, Widget,
};
use std::hash::Hash;
@ -190,20 +190,20 @@ impl<Message, S: State> Widget<Message, Renderer> for Canvas<S> {
_cursor_position: Point,
) -> (Primitive, MouseCursor) {
let bounds = layout.bounds();
let origin = Point::new(bounds.x, bounds.y);
let translation = Vector::new(bounds.x, bounds.y);
let size = Size::new(bounds.width, bounds.height);
(
Primitive::Group {
primitives: self
.state
.draw(size)
.into_iter()
.map(|geometry| Primitive::Cached {
origin,
cache: geometry.into_primitive(),
})
.collect(),
Primitive::Translate {
translation,
content: Box::new(Primitive::Group {
primitives: self
.state
.draw(size)
.into_iter()
.map(Geometry::into_primitive)
.collect(),
}),
},
MouseCursor::Idle,
)

View file

@ -57,21 +57,27 @@ impl Cache {
if let State::Filled { bounds, primitive } = self.state.borrow().deref()
{
if *bounds == new_bounds {
return Geometry::from_primitive(primitive.clone());
return Geometry::from_primitive(Primitive::Cached {
cache: primitive.clone(),
});
}
}
let mut frame = Frame::new(new_bounds);
input.draw(&mut frame);
let primitive = Arc::new(frame.into_primitive());
let primitive = {
let geometry = frame.into_geometry();
Arc::new(geometry.into_primitive())
};
*self.state.borrow_mut() = State::Filled {
bounds: new_bounds,
primitive: primitive.clone(),
};
Geometry::from_primitive(primitive)
Geometry::from_primitive(Primitive::Cached { cache: primitive })
}
pub fn with<'a, T>(&'a self, input: T) -> impl crate::canvas::State + 'a

View file

@ -1,7 +1,7 @@
use iced_native::{Point, Rectangle, Size, Vector};
use crate::{
canvas::{Fill, Path, Stroke, Text},
canvas::{Fill, Geometry, Path, Stroke, Text},
triangle, Primitive,
};
@ -260,13 +260,13 @@ impl Frame {
self.transforms.current.is_identity = false;
}
/// Produces the primitive representing everything drawn on the [`Frame`].
/// Produces the [`Geometry`] representing everything drawn on the [`Frame`].
///
/// [`Frame`]: struct.Frame.html
pub fn into_primitive(mut self) -> Primitive {
/// [`Geometry`]: struct.Geometry.html
pub fn into_geometry(mut self) -> Geometry {
if !self.buffers.indices.is_empty() {
self.primitives.push(Primitive::Mesh2D {
origin: Point::ORIGIN,
buffers: triangle::Mesh2D {
vertices: self.buffers.vertices,
indices: self.buffers.indices,
@ -274,9 +274,9 @@ impl Frame {
});
}
Primitive::Group {
Geometry::from_primitive(Primitive::Group {
primitives: self.primitives,
}
})
}
}

View file

@ -1,15 +1,20 @@
use crate::Primitive;
use std::sync::Arc;
#[derive(Debug)]
pub struct Geometry(Arc<Primitive>);
#[derive(Debug, Clone)]
pub struct Geometry(Primitive);
impl Geometry {
pub(crate) fn from_primitive(primitive: Arc<Primitive>) -> Self {
pub(crate) fn from_primitive(primitive: Primitive) -> Self {
Self(primitive)
}
pub(crate) fn into_primitive(self) -> Arc<Primitive> {
pub fn into_primitive(self) -> Primitive {
self.0
}
}
impl From<Geometry> for Primitive {
fn from(geometry: Geometry) -> Primitive {
geometry.0
}
}