Skip to content

Instantly share code, notes, and snippets.

@ratmice
Last active November 12, 2023 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ratmice/2838bcdf44964de24f9b92f9d355ac5b to your computer and use it in GitHub Desktop.
Save ratmice/2838bcdf44964de24f9b92f9d355ac5b to your computer and use it in GitHub Desktop.
enumized shape painting ops...
#![allow(unused)]
use kurbo::{Affine, Point, Rect, Shape, Stroke};
use vello::peniko::{BlendMode, BrushRef, Fill};
use vello::SceneBuilder;
#[derive(Clone)]
pub enum ShapeOp<'a, 'b> {
Fill {
style: Fill,
transform: Affine,
brush: BrushRef<'b>,
brush_transform: Option<Affine>,
},
Stroke {
style: &'a Stroke,
transform: Affine,
brush: BrushRef<'b>,
brush_transform: Option<Affine>,
},
PushLayer {
blend: BlendMode,
alpha: f32,
transform: Affine,
},
}
pub trait SceneBuilderWhisperer {
fn paint_shape_op(&mut self, op: ShapeOp<'_, '_>, shape: &impl Shape);
fn paint_shape_ops<'a, 'b, I>(&mut self, ops: I, shape: &impl Shape)
where
I: IntoIterator<Item = ShapeOp<'a, 'b>>;
}
impl SceneBuilderWhisperer for SceneBuilder<'_> {
fn paint_shape_op(&mut self, op: ShapeOp<'_, '_>, shape: &impl Shape) {
match op {
ShapeOp::Fill {
style,
transform,
brush,
brush_transform,
} => self.fill(style, transform, brush, brush_transform, shape),
ShapeOp::Stroke {
style,
transform,
brush,
brush_transform,
} => self.stroke(style, transform, brush, brush_transform, shape),
ShapeOp::PushLayer {
blend,
alpha,
transform,
} => self.push_layer(blend, alpha, transform, shape),
}
}
fn paint_shape_ops<'a, 'b, I>(&mut self, ops: I, shape: &impl Shape)
where
I: IntoIterator<Item = ShapeOp<'a, 'b>>,
{
for op in ops {
self.paint_shape_op(op, shape)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment