summaryrefslogtreecommitdiffstats
path: root/wgpu/src/widget
diff options
context:
space:
mode:
Diffstat (limited to 'wgpu/src/widget')
-rw-r--r--wgpu/src/widget/canvas.rs21
-rw-r--r--wgpu/src/widget/canvas/frame.rs19
-rw-r--r--wgpu/src/widget/canvas/layer.rs63
-rw-r--r--wgpu/src/widget/canvas/path.rs2
4 files changed, 88 insertions, 17 deletions
diff --git a/wgpu/src/widget/canvas.rs b/wgpu/src/widget/canvas.rs
index 6bfeed9a..c984fee9 100644
--- a/wgpu/src/widget/canvas.rs
+++ b/wgpu/src/widget/canvas.rs
@@ -68,7 +68,6 @@ impl<'a, Message> Widget<Message, Renderer> for Canvas<'a> {
limits: &layout::Limits,
) -> layout::Node {
let limits = limits.width(self.width).height(self.height);
-
let size = limits.resolve(Size::ZERO);
layout::Node::new(size)
@@ -78,10 +77,26 @@ impl<'a, Message> Widget<Message, Renderer> for Canvas<'a> {
&self,
_renderer: &mut Renderer,
_defaults: &Defaults,
- _layout: Layout<'_>,
+ layout: Layout<'_>,
_cursor_position: Point,
) -> (Primitive, MouseCursor) {
- (Primitive::None, MouseCursor::Idle)
+ let bounds = layout.bounds();
+ let origin = Point::new(bounds.x, bounds.y);
+ let size = Size::new(bounds.width, bounds.height);
+
+ (
+ Primitive::Group {
+ primitives: self
+ .layers
+ .iter()
+ .map(|layer| Primitive::Mesh2D {
+ origin,
+ buffers: layer.draw(size),
+ })
+ .collect(),
+ },
+ MouseCursor::Idle,
+ )
}
fn hash_layout(&self, state: &mut Hasher) {
diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs
index 82ff526b..3c667426 100644
--- a/wgpu/src/widget/canvas/frame.rs
+++ b/wgpu/src/widget/canvas/frame.rs
@@ -7,13 +7,13 @@ use crate::{
#[derive(Debug)]
pub struct Frame {
- width: u32,
- height: u32,
+ width: f32,
+ height: f32,
buffers: lyon::tessellation::VertexBuffers<triangle::Vertex2D, u16>,
}
impl Frame {
- pub(crate) fn new(width: u32, height: u32) -> Frame {
+ pub fn new(width: f32, height: f32) -> Frame {
Frame {
width,
height,
@@ -21,16 +21,16 @@ impl Frame {
}
}
- pub fn width(&self) -> u32 {
+ pub fn width(&self) -> f32 {
self.width
}
- pub fn height(&self) -> u32 {
+ pub fn height(&self) -> f32 {
self.height
}
pub fn center(&self) -> Point {
- Point::new(self.width as f32 / 2.0, self.height as f32 / 2.0)
+ Point::new(self.width / 2.0, self.height / 2.0)
}
pub fn fill(&mut self, path: &Path, fill: Fill) {
@@ -74,6 +74,13 @@ impl Frame {
.tessellate_path(path.raw(), &options, &mut buffers)
.expect("Stroke path");
}
+
+ pub fn into_mesh(self) -> triangle::Mesh2D {
+ triangle::Mesh2D {
+ vertices: self.buffers.vertices,
+ indices: self.buffers.indices,
+ }
+ }
}
struct FillVertex([f32; 4]);
diff --git a/wgpu/src/widget/canvas/layer.rs b/wgpu/src/widget/canvas/layer.rs
index f97634e4..c239a254 100644
--- a/wgpu/src/widget/canvas/layer.rs
+++ b/wgpu/src/widget/canvas/layer.rs
@@ -1,13 +1,28 @@
-use crate::canvas::Frame;
+use crate::{canvas::Frame, triangle};
-pub trait Layer: std::fmt::Debug {}
+use iced_native::Size;
+use std::cell::RefCell;
+use std::sync::Arc;
+
+pub trait Layer: std::fmt::Debug {
+ fn draw(&self, bounds: Size) -> Arc<triangle::Mesh2D>;
+}
use std::marker::PhantomData;
-use std::sync::{Arc, Weak};
#[derive(Debug)]
pub struct Cached<T: Drawable> {
input: PhantomData<T>,
+ cache: RefCell<Cache>,
+}
+
+#[derive(Debug)]
+enum Cache {
+ Empty,
+ Filled {
+ mesh: Arc<triangle::Mesh2D>,
+ bounds: Size,
+ },
}
impl<T> Cached<T>
@@ -15,14 +30,19 @@ where
T: Drawable + std::fmt::Debug,
{
pub fn new() -> Self {
- Cached { input: PhantomData }
+ Cached {
+ input: PhantomData,
+ cache: RefCell::new(Cache::Empty),
+ }
}
- pub fn clear(&mut self) {}
+ pub fn clear(&mut self) {
+ *self.cache.borrow_mut() = Cache::Empty;
+ }
pub fn with<'a>(&'a self, input: &'a T) -> impl Layer + 'a {
Bind {
- cache: self,
+ layer: self,
input: input,
}
}
@@ -30,11 +50,38 @@ where
#[derive(Debug)]
struct Bind<'a, T: Drawable> {
- cache: &'a Cached<T>,
+ layer: &'a Cached<T>,
input: &'a T,
}
-impl<'a, T> Layer for Bind<'a, T> where T: Drawable + std::fmt::Debug {}
+impl<'a, T> Layer for Bind<'a, T>
+where
+ T: Drawable + std::fmt::Debug,
+{
+ fn draw(&self, current_bounds: Size) -> Arc<triangle::Mesh2D> {
+ use std::ops::Deref;
+
+ if let Cache::Filled { mesh, bounds } =
+ self.layer.cache.borrow().deref()
+ {
+ if *bounds == current_bounds {
+ return mesh.clone();
+ }
+ }
+
+ let mut frame = Frame::new(current_bounds.width, current_bounds.height);
+ self.input.draw(&mut frame);
+
+ let mesh = Arc::new(frame.into_mesh());
+
+ *self.layer.cache.borrow_mut() = Cache::Filled {
+ mesh: mesh.clone(),
+ bounds: current_bounds,
+ };
+
+ mesh
+ }
+}
pub trait Drawable {
fn draw(&self, frame: &mut Frame);
diff --git a/wgpu/src/widget/canvas/path.rs b/wgpu/src/widget/canvas/path.rs
index 96206256..c8ba10e1 100644
--- a/wgpu/src/widget/canvas/path.rs
+++ b/wgpu/src/widget/canvas/path.rs
@@ -58,6 +58,8 @@ impl Builder {
sweep_angle: lyon::math::Angle::radians(ellipse.end_angle),
};
+ let _ = self.raw.move_to(arc.sample(0.0));
+
arc.for_each_quadratic_bezier(&mut |curve| {
let _ = self.raw.quadratic_bezier_to(curve.ctrl, curve.to);
});