summaryrefslogtreecommitdiffstats
path: root/wgpu/src/widget/canvas
diff options
context:
space:
mode:
Diffstat (limited to 'wgpu/src/widget/canvas')
-rw-r--r--wgpu/src/widget/canvas/cache.rs108
-rw-r--r--wgpu/src/widget/canvas/cursor.rs72
-rw-r--r--wgpu/src/widget/canvas/drawable.rs12
-rw-r--r--wgpu/src/widget/canvas/event.rs10
-rw-r--r--wgpu/src/widget/canvas/frame.rs80
-rw-r--r--wgpu/src/widget/canvas/geometry.rs34
-rw-r--r--wgpu/src/widget/canvas/layer.rs25
-rw-r--r--wgpu/src/widget/canvas/layer/cache.rs128
-rw-r--r--wgpu/src/widget/canvas/program.rs85
-rw-r--r--wgpu/src/widget/canvas/stroke.rs32
10 files changed, 406 insertions, 180 deletions
diff --git a/wgpu/src/widget/canvas/cache.rs b/wgpu/src/widget/canvas/cache.rs
new file mode 100644
index 00000000..4b28d164
--- /dev/null
+++ b/wgpu/src/widget/canvas/cache.rs
@@ -0,0 +1,108 @@
+use crate::{
+ canvas::{Frame, Geometry},
+ Primitive,
+};
+
+use iced_native::Size;
+use std::{cell::RefCell, sync::Arc};
+
+enum State {
+ Empty,
+ Filled {
+ bounds: Size,
+ primitive: Arc<Primitive>,
+ },
+}
+
+impl Default for State {
+ fn default() -> Self {
+ State::Empty
+ }
+}
+/// A simple cache that stores generated [`Geometry`] to avoid recomputation.
+///
+/// A [`Cache`] will not redraw its geometry unless the dimensions of its layer
+/// change or it is explicitly cleared.
+///
+/// [`Layer`]: ../trait.Layer.html
+/// [`Cache`]: struct.Cache.html
+/// [`Geometry`]: struct.Geometry.html
+#[derive(Debug, Default)]
+pub struct Cache {
+ state: RefCell<State>,
+}
+
+impl Cache {
+ /// Creates a new empty [`Cache`].
+ ///
+ /// [`Cache`]: struct.Cache.html
+ pub fn new() -> Self {
+ Cache {
+ state: Default::default(),
+ }
+ }
+
+ /// Clears the [`Cache`], forcing a redraw the next time it is used.
+ ///
+ /// [`Cache`]: struct.Cache.html
+ pub fn clear(&mut self) {
+ *self.state.borrow_mut() = State::Empty;
+ }
+
+ /// Draws [`Geometry`] using the provided closure and stores it in the
+ /// [`Cache`].
+ ///
+ /// The closure will only be called when
+ /// - the bounds have changed since the previous draw call.
+ /// - the [`Cache`] is empty or has been explicitly cleared.
+ ///
+ /// Otherwise, the previously stored [`Geometry`] will be returned. The
+ /// [`Cache`] is not cleared in this case. In other words, it will keep
+ /// returning the stored [`Geometry`] if needed.
+ ///
+ /// [`Cache`]: struct.Cache.html
+ pub fn draw(&self, bounds: Size, draw_fn: impl Fn(&mut Frame)) -> Geometry {
+ use std::ops::Deref;
+
+ if let State::Filled {
+ bounds: cached_bounds,
+ primitive,
+ } = self.state.borrow().deref()
+ {
+ if *cached_bounds == bounds {
+ return Geometry::from_primitive(Primitive::Cached {
+ cache: primitive.clone(),
+ });
+ }
+ }
+
+ let mut frame = Frame::new(bounds);
+ draw_fn(&mut frame);
+
+ let primitive = {
+ let geometry = frame.into_geometry();
+
+ Arc::new(geometry.into_primitive())
+ };
+
+ *self.state.borrow_mut() = State::Filled {
+ bounds,
+ primitive: primitive.clone(),
+ };
+
+ Geometry::from_primitive(Primitive::Cached { cache: primitive })
+ }
+}
+
+impl std::fmt::Debug for State {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ State::Empty => write!(f, "Empty"),
+ State::Filled { primitive, bounds } => f
+ .debug_struct("Filled")
+ .field("primitive", primitive)
+ .field("bounds", bounds)
+ .finish(),
+ }
+ }
+}
diff --git a/wgpu/src/widget/canvas/cursor.rs b/wgpu/src/widget/canvas/cursor.rs
new file mode 100644
index 00000000..456760ea
--- /dev/null
+++ b/wgpu/src/widget/canvas/cursor.rs
@@ -0,0 +1,72 @@
+use iced_native::{Point, Rectangle};
+
+/// The mouse cursor state.
+#[derive(Debug, Clone, Copy, PartialEq)]
+pub enum Cursor {
+ /// The cursor has a defined position.
+ Available(Point),
+
+ /// The cursor is currently unavailable (i.e. out of bounds or busy).
+ Unavailable,
+}
+
+impl Cursor {
+ // TODO: Remove this once this type is used in `iced_native` to encode
+ // proper cursor availability
+ pub(crate) fn from_window_position(position: Point) -> Self {
+ if position.x < 0.0 || position.y < 0.0 {
+ Cursor::Unavailable
+ } else {
+ Cursor::Available(position)
+ }
+ }
+
+ /// Returns the absolute position of the [`Cursor`], if available.
+ ///
+ /// [`Cursor`]: enum.Cursor.html
+ pub fn position(&self) -> Option<Point> {
+ match self {
+ Cursor::Available(position) => Some(*position),
+ Cursor::Unavailable => None,
+ }
+ }
+
+ /// Returns the relative position of the [`Cursor`] inside the given bounds,
+ /// if available.
+ ///
+ /// If the [`Cursor`] is not over the provided bounds, this method will
+ /// return `None`.
+ ///
+ /// [`Cursor`]: enum.Cursor.html
+ pub fn position_in(&self, bounds: &Rectangle) -> Option<Point> {
+ if self.is_over(bounds) {
+ self.position_from(bounds.position())
+ } else {
+ None
+ }
+ }
+
+ /// Returns the relative position of the [`Cursor`] from the given origin,
+ /// if available.
+ ///
+ /// [`Cursor`]: enum.Cursor.html
+ pub fn position_from(&self, origin: Point) -> Option<Point> {
+ match self {
+ Cursor::Available(position) => {
+ Some(Point::new(position.x - origin.x, position.y - origin.y))
+ }
+ Cursor::Unavailable => None,
+ }
+ }
+
+ /// Returns whether the [`Cursor`] is currently over the provided bounds
+ /// or not.
+ ///
+ /// [`Cursor`]: enum.Cursor.html
+ pub fn is_over(&self, bounds: &Rectangle) -> bool {
+ match self {
+ Cursor::Available(position) => bounds.contains(*position),
+ Cursor::Unavailable => false,
+ }
+ }
+}
diff --git a/wgpu/src/widget/canvas/drawable.rs b/wgpu/src/widget/canvas/drawable.rs
deleted file mode 100644
index 6c74071c..00000000
--- a/wgpu/src/widget/canvas/drawable.rs
+++ /dev/null
@@ -1,12 +0,0 @@
-use crate::canvas::Frame;
-
-/// A type that can be drawn on a [`Frame`].
-///
-/// [`Frame`]: struct.Frame.html
-pub trait Drawable {
- /// Draws the [`Drawable`] on the given [`Frame`].
- ///
- /// [`Drawable`]: trait.Drawable.html
- /// [`Frame`]: struct.Frame.html
- fn draw(&self, frame: &mut Frame);
-}
diff --git a/wgpu/src/widget/canvas/event.rs b/wgpu/src/widget/canvas/event.rs
new file mode 100644
index 00000000..ad11f51e
--- /dev/null
+++ b/wgpu/src/widget/canvas/event.rs
@@ -0,0 +1,10 @@
+use iced_native::mouse;
+
+/// A [`Canvas`] event.
+///
+/// [`Canvas`]: struct.Event.html
+#[derive(Debug, Clone, Copy, PartialEq)]
+pub enum Event {
+ /// A mouse event.
+ Mouse(mouse::Event),
+}
diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs
index de4717f1..5262ab4e 100644
--- a/wgpu/src/widget/canvas/frame.rs
+++ b/wgpu/src/widget/canvas/frame.rs
@@ -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,
};
@@ -10,8 +10,7 @@ use crate::{
/// [`Canvas`]: struct.Canvas.html
#[derive(Debug)]
pub struct Frame {
- width: f32,
- height: f32,
+ size: Size,
buffers: lyon::tessellation::VertexBuffers<triangle::Vertex2D, u32>,
primitives: Vec<Primitive>,
transforms: Transforms,
@@ -36,10 +35,9 @@ impl Frame {
/// top-left corner of its bounds.
///
/// [`Frame`]: struct.Frame.html
- pub fn new(width: f32, height: f32) -> Frame {
+ pub fn new(size: Size) -> Frame {
Frame {
- width,
- height,
+ size,
buffers: lyon::tessellation::VertexBuffers::new(),
primitives: Vec::new(),
transforms: Transforms {
@@ -57,7 +55,7 @@ impl Frame {
/// [`Frame`]: struct.Frame.html
#[inline]
pub fn width(&self) -> f32 {
- self.width
+ self.size.width
}
/// Returns the width of the [`Frame`].
@@ -65,7 +63,7 @@ impl Frame {
/// [`Frame`]: struct.Frame.html
#[inline]
pub fn height(&self) -> f32 {
- self.height
+ self.size.height
}
/// Returns the dimensions of the [`Frame`].
@@ -73,7 +71,7 @@ impl Frame {
/// [`Frame`]: struct.Frame.html
#[inline]
pub fn size(&self) -> Size {
- Size::new(self.width, self.height)
+ self.size
}
/// Returns the coordinate of the center of the [`Frame`].
@@ -81,7 +79,7 @@ impl Frame {
/// [`Frame`]: struct.Frame.html
#[inline]
pub fn center(&self) -> Point {
- Point::new(self.width / 2.0, self.height / 2.0)
+ Point::new(self.size.width / 2.0, self.size.height / 2.0)
}
/// Draws the given [`Path`] on the [`Frame`] by filling it with the
@@ -122,6 +120,43 @@ impl Frame {
let _ = result.expect("Tessellate path");
}
+ /// Draws an axis-aligned rectangle given its top-left corner coordinate and
+ /// its `Size` on the [`Frame`] by filling it with the provided style.
+ ///
+ /// [`Frame`]: struct.Frame.html
+ pub fn fill_rectangle(
+ &mut self,
+ top_left: Point,
+ size: Size,
+ fill: impl Into<Fill>,
+ ) {
+ use lyon::tessellation::{BuffersBuilder, FillOptions};
+
+ let mut buffers = BuffersBuilder::new(
+ &mut self.buffers,
+ FillVertex(match fill.into() {
+ Fill::Color(color) => color.into_linear(),
+ }),
+ );
+
+ let top_left =
+ self.transforms.current.raw.transform_point(
+ lyon::math::Point::new(top_left.x, top_left.y),
+ );
+
+ let size =
+ self.transforms.current.raw.transform_vector(
+ lyon::math::Vector::new(size.width, size.height),
+ );
+
+ let _ = lyon::tessellation::basic_shapes::fill_rectangle(
+ &lyon::math::Rect::new(top_left, size.into()),
+ &FillOptions::default(),
+ &mut buffers,
+ )
+ .expect("Fill rectangle");
+ }
+
/// Draws the stroke of the given [`Path`] on the [`Frame`] with the
/// provided style.
///
@@ -262,13 +297,14 @@ 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,
+ size: self.size,
buffers: triangle::Mesh2D {
vertices: self.buffers.vertices,
indices: self.buffers.indices,
@@ -276,14 +312,28 @@ impl Frame {
});
}
- Primitive::Group {
+ Geometry::from_primitive(Primitive::Group {
primitives: self.primitives,
- }
+ })
}
}
struct FillVertex([f32; 4]);
+impl lyon::tessellation::BasicVertexConstructor<triangle::Vertex2D>
+ for FillVertex
+{
+ fn new_vertex(
+ &mut self,
+ position: lyon::math::Point,
+ ) -> triangle::Vertex2D {
+ triangle::Vertex2D {
+ position: [position.x, position.y],
+ color: self.0,
+ }
+ }
+}
+
impl lyon::tessellation::FillVertexConstructor<triangle::Vertex2D>
for FillVertex
{
diff --git a/wgpu/src/widget/canvas/geometry.rs b/wgpu/src/widget/canvas/geometry.rs
new file mode 100644
index 00000000..4cadee39
--- /dev/null
+++ b/wgpu/src/widget/canvas/geometry.rs
@@ -0,0 +1,34 @@
+use crate::Primitive;
+
+/// A bunch of shapes that can be drawn.
+///
+/// [`Geometry`] can be easily generated with a [`Frame`] or stored in a
+/// [`Cache`].
+///
+/// [`Geometry`]: struct.Geometry.html
+/// [`Frame`]: struct.Frame.html
+/// [`Cache`]: struct.Cache.html
+#[derive(Debug, Clone)]
+pub struct Geometry(Primitive);
+
+impl Geometry {
+ pub(crate) fn from_primitive(primitive: Primitive) -> Self {
+ Self(primitive)
+ }
+
+ /// Turns the [`Geometry`] into a [`Primitive`].
+ ///
+ /// This can be useful if you are building a custom widget.
+ ///
+ /// [`Geometry`]: struct.Geometry.html
+ /// [`Primitive`]: ../enum.Primitive.html
+ pub fn into_primitive(self) -> Primitive {
+ self.0
+ }
+}
+
+impl From<Geometry> for Primitive {
+ fn from(geometry: Geometry) -> Primitive {
+ geometry.0
+ }
+}
diff --git a/wgpu/src/widget/canvas/layer.rs b/wgpu/src/widget/canvas/layer.rs
deleted file mode 100644
index a46b7fb1..00000000
--- a/wgpu/src/widget/canvas/layer.rs
+++ /dev/null
@@ -1,25 +0,0 @@
-//! Produce, store, and reuse geometry.
-mod cache;
-
-pub use cache::Cache;
-
-use crate::Primitive;
-use iced_native::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 a [`Primitive`] as
- /// a result.
- ///
- /// 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
- /// [`Primitive`]: ../../../enum.Primitive.html
- fn draw(&self, bounds: Size) -> Arc<Primitive>;
-}
diff --git a/wgpu/src/widget/canvas/layer/cache.rs b/wgpu/src/widget/canvas/layer/cache.rs
deleted file mode 100644
index 4f8c2bec..00000000
--- a/wgpu/src/widget/canvas/layer/cache.rs
+++ /dev/null
@@ -1,128 +0,0 @@
-use crate::{
- canvas::{Drawable, Frame, Layer},
- Primitive,
-};
-
-use iced_native::Size;
-use std::{cell::RefCell, marker::PhantomData, sync::Arc};
-
-enum State {
- Empty,
- Filled {
- bounds: Size,
- primitive: Arc<Primitive>,
- },
-}
-
-impl Default for State {
- fn default() -> Self {
- State::Empty
- }
-}
-/// A simple cache that stores generated geometry to avoid recomputation.
-///
-/// A [`Cache`] will not redraw its geometry unless the dimensions of its layer
-/// change or it is explicitly cleared.
-///
-/// [`Layer`]: ../trait.Layer.html
-/// [`Cache`]: struct.Cache.html
-#[derive(Debug)]
-pub struct Cache<T: Drawable> {
- input: PhantomData<T>,
- state: RefCell<State>,
-}
-
-impl<T> Default for Cache<T>
-where
- T: Drawable,
-{
- fn default() -> Self {
- Self {
- input: PhantomData,
- state: Default::default(),
- }
- }
-}
-
-impl<T> Cache<T>
-where
- T: Drawable + std::fmt::Debug,
-{
- /// Creates a new empty [`Cache`].
- ///
- /// [`Cache`]: struct.Cache.html
- pub fn new() -> Self {
- Cache {
- input: PhantomData,
- state: Default::default(),
- }
- }
-
- /// Clears the cache, forcing a redraw the next time it is used.
- ///
- /// [`Cached`]: struct.Cached.html
- pub fn clear(&mut self) {
- *self.state.borrow_mut() = State::Empty;
- }
-
- /// Binds the [`Cache`] with some data, producing a [`Layer`] that can be
- /// added to a [`Canvas`].
- ///
- /// [`Cache`]: struct.Cache.html
- /// [`Layer`]: ../trait.Layer.html
- /// [`Canvas`]: ../../struct.Canvas.html
- pub fn with<'a>(&'a self, input: &'a T) -> impl Layer + 'a {
- Bind {
- cache: self,
- input: input,
- }
- }
-}
-
-#[derive(Debug)]
-struct Bind<'a, T: Drawable> {
- cache: &'a Cache<T>,
- input: &'a T,
-}
-
-impl<'a, T> Layer for Bind<'a, T>
-where
- T: Drawable + std::fmt::Debug,
-{
- fn draw(&self, current_bounds: Size) -> Arc<Primitive> {
- use std::ops::Deref;
-
- if let State::Filled { bounds, primitive } =
- self.cache.state.borrow().deref()
- {
- if *bounds == current_bounds {
- return primitive.clone();
- }
- }
-
- let mut frame = Frame::new(current_bounds.width, current_bounds.height);
- self.input.draw(&mut frame);
-
- let primitive = Arc::new(frame.into_primitive());
-
- *self.cache.state.borrow_mut() = State::Filled {
- bounds: current_bounds,
- primitive: primitive.clone(),
- };
-
- primitive
- }
-}
-
-impl std::fmt::Debug for State {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- match self {
- State::Empty => write!(f, "Empty"),
- State::Filled { primitive, bounds } => f
- .debug_struct("Filled")
- .field("primitive", primitive)
- .field("bounds", bounds)
- .finish(),
- }
- }
-}
diff --git a/wgpu/src/widget/canvas/program.rs b/wgpu/src/widget/canvas/program.rs
new file mode 100644
index 00000000..725d9d72
--- /dev/null
+++ b/wgpu/src/widget/canvas/program.rs
@@ -0,0 +1,85 @@
+use crate::canvas::{Cursor, Event, Geometry};
+use iced_native::{mouse, Rectangle};
+
+/// The state and logic of a [`Canvas`].
+///
+/// A [`Program`] can mutate internal state and produce messages for an
+/// application.
+///
+/// [`Canvas`]: struct.Canvas.html
+/// [`Program`]: trait.Program.html
+pub trait Program<Message> {
+ /// Updates the state of the [`Program`].
+ ///
+ /// When a [`Program`] is used in a [`Canvas`], the runtime will call this
+ /// method for each [`Event`].
+ ///
+ /// This method can optionally return a `Message` to notify an application
+ /// of any meaningful interactions.
+ ///
+ /// By default, this method does and returns nothing.
+ ///
+ /// [`Program`]: trait.Program.html
+ /// [`Canvas`]: struct.Canvas.html
+ /// [`Event`]: enum.Event.html
+ fn update(
+ &mut self,
+ _event: Event,
+ _bounds: Rectangle,
+ _cursor: Cursor,
+ ) -> Option<Message> {
+ None
+ }
+
+ /// Draws the state of the [`Program`], producing a bunch of [`Geometry`].
+ ///
+ /// [`Geometry`] can be easily generated with a [`Frame`] or stored in a
+ /// [`Cache`].
+ ///
+ /// [`Program`]: trait.Program.html
+ /// [`Geometry`]: struct.Geometry.html
+ /// [`Frame`]: struct.Frame.html
+ /// [`Cache`]: struct.Cache.html
+ fn draw(&self, bounds: Rectangle, cursor: Cursor) -> Vec<Geometry>;
+
+ /// Returns the current mouse interaction of the [`Program`].
+ ///
+ /// The interaction returned will be in effect even if the cursor position
+ /// is out of bounds of the program's [`Canvas`].
+ ///
+ /// [`Program`]: trait.Program.html
+ /// [`Canvas`]: struct.Canvas.html
+ fn mouse_interaction(
+ &self,
+ _bounds: Rectangle,
+ _cursor: Cursor,
+ ) -> mouse::Interaction {
+ mouse::Interaction::default()
+ }
+}
+
+impl<T, Message> Program<Message> for &mut T
+where
+ T: Program<Message>,
+{
+ fn update(
+ &mut self,
+ event: Event,
+ bounds: Rectangle,
+ cursor: Cursor,
+ ) -> Option<Message> {
+ T::update(self, event, bounds, cursor)
+ }
+
+ fn draw(&self, bounds: Rectangle, cursor: Cursor) -> Vec<Geometry> {
+ T::draw(self, bounds, cursor)
+ }
+
+ fn mouse_interaction(
+ &self,
+ bounds: Rectangle,
+ cursor: Cursor,
+ ) -> mouse::Interaction {
+ T::mouse_interaction(self, bounds, cursor)
+ }
+}
diff --git a/wgpu/src/widget/canvas/stroke.rs b/wgpu/src/widget/canvas/stroke.rs
index 46d669c4..5b6fc56a 100644
--- a/wgpu/src/widget/canvas/stroke.rs
+++ b/wgpu/src/widget/canvas/stroke.rs
@@ -14,6 +14,38 @@ pub struct Stroke {
pub line_join: LineJoin,
}
+impl Stroke {
+ /// Sets the color of the [`Stroke`].
+ ///
+ /// [`Stroke`]: struct.Stroke.html
+ pub fn with_color(self, color: Color) -> Stroke {
+ Stroke { color, ..self }
+ }
+
+ /// Sets the width of the [`Stroke`].
+ ///
+ /// [`Stroke`]: struct.Stroke.html
+ pub fn with_width(self, width: f32) -> Stroke {
+ Stroke { width, ..self }
+ }
+
+ /// Sets the [`LineCap`] of the [`Stroke`].
+ ///
+ /// [`LineCap`]: enum.LineCap.html
+ /// [`Stroke`]: struct.Stroke.html
+ pub fn with_line_cap(self, line_cap: LineCap) -> Stroke {
+ Stroke { line_cap, ..self }
+ }
+
+ /// Sets the [`LineJoin`] of the [`Stroke`].
+ ///
+ /// [`LineJoin`]: enum.LineJoin.html
+ /// [`Stroke`]: struct.Stroke.html
+ pub fn with_line_join(self, line_join: LineJoin) -> Stroke {
+ Stroke { line_join, ..self }
+ }
+}
+
impl Default for Stroke {
fn default() -> Stroke {
Stroke {