summaryrefslogtreecommitdiffstats
path: root/wgpu/src/widget
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-05-01 04:33:17 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-05-01 04:34:09 +0200
commit08b376c6d7d2f3c1fab1328202325cb47ce369e5 (patch)
tree399349a78fe849785e38790e67b4274b75455a4c /wgpu/src/widget
parent345f0e13362aed3debe30226e28c4c4b870e9b8a (diff)
downloadiced-08b376c6d7d2f3c1fab1328202325cb47ce369e5.tar.gz
iced-08b376c6d7d2f3c1fab1328202325cb47ce369e5.tar.bz2
iced-08b376c6d7d2f3c1fab1328202325cb47ce369e5.zip
Implement `Frame::fill_rectangle`
This method greatly improves performance when drawing axis-aligned rectangles.
Diffstat (limited to 'wgpu/src/widget')
-rw-r--r--wgpu/src/widget/canvas/frame.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs
index 1c4a038a..5262ab4e 100644
--- a/wgpu/src/widget/canvas/frame.rs
+++ b/wgpu/src/widget/canvas/frame.rs
@@ -120,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.
///
@@ -283,6 +320,20 @@ impl Frame {
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
{