use crate::{
Background, Color, CustomShader, CustomShaderPrimitive, Path, PathPrimitive, Point, Quad, Rect,
Scene, ShaderParameters, Vector, scene::PaintLayerKey,
};
pub struct Canvas<'a> {
scene: &'a mut Scene,
layer: PaintLayerKey,
origin: Point,
bounds: Rect,
clip: Rect,
}
impl<'a> Canvas<'a> {
pub(crate) fn new(
scene: &'a mut Scene,
layer: PaintLayerKey,
origin: Point,
size: crate::Size,
clip: Rect,
) -> Self {
Self {
scene,
layer,
origin,
bounds: Rect::from_size(size),
clip,
}
}
pub const fn bounds(&self) -> Rect {
self.bounds
}
pub fn fill_rect(&mut self, rect: Rect, color: Color) {
self.scene.push_quad_in(
self.layer,
Quad::new(self.absolute_rect(rect), color).clip(self.clip),
);
}
pub fn fill_rounded_rect(&mut self, rect: Rect, radius: f32, color: Color) {
self.scene.push_quad_in(
self.layer,
Quad::new(self.absolute_rect(rect), color)
.radius(radius)
.clip(self.clip),
);
}
pub fn paint_path(&mut self, path: impl Into<Path>, background: impl Into<Background>) {
self.paint_path_at(path, Point::ZERO, background);
}
pub fn paint_path_at(
&mut self,
path: impl Into<Path>,
position: Point,
background: impl Into<Background>,
) {
self.scene.push_path_in(
self.layer,
PathPrimitive::new(path, background)
.translate(self.origin.x + position.x, self.origin.y + position.y)
.clip(self.clip),
);
}
pub fn paint_path_transformed(
&mut self,
path: impl Into<Path>,
scale: [f32; 2],
translation: Point,
background: impl Into<Background>,
) {
self.scene.push_path_in(
self.layer,
PathPrimitive::new(path, background)
.scale_xy(scale[0], scale[1])
.translate(self.origin.x + translation.x, self.origin.y + translation.y)
.clip(self.clip),
);
}
pub fn paint_shader(
&mut self,
rect: Rect,
shader: &CustomShader,
parameters: impl Into<ShaderParameters>,
) {
self.scene.push_custom_shader_in(
self.layer,
CustomShaderPrimitive::new(shader.clone(), self.absolute_rect(rect))
.parameters(parameters)
.clip(self.clip),
);
}
fn absolute_rect(&self, rect: Rect) -> Rect {
rect.translate(Vector::new(self.origin.x, self.origin.y))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{PathBuilder, Size};
#[test]
fn local_commands_translate_and_inherit_the_canvas_clip() {
let mut scene = Scene::new();
let mut builder = PathBuilder::fill();
builder.move_to(Point::new(0.0, 0.0));
builder.line_to(Point::new(10.0, 0.0));
builder.line_to(Point::new(0.0, 10.0));
builder.close();
let path = builder.build().unwrap();
let clip = Rect::new(12.0, 24.0, 30.0, 20.0);
{
let mut canvas = Canvas::new(
&mut scene,
PaintLayerKey::default(),
Point::new(10.0, 20.0),
Size::new(40.0, 30.0),
clip,
);
assert_eq!(canvas.bounds(), Rect::new(0.0, 0.0, 40.0, 30.0));
canvas.fill_rect(Rect::new(1.0, 2.0, 5.0, 6.0), Color::WHITE);
canvas.paint_path_at(&path, Point::new(3.0, 4.0), Color::WHITE);
let shader = CustomShader::new(
r#"
fn quickgui_fragment(input: QuickGuiShaderInput) -> vec4<f32> {
return vec4<f32>(input.uv, 0.0, 1.0);
}
"#,
)
.unwrap();
canvas.paint_shader(
Rect::new(2.0, 3.0, 8.0, 9.0),
&shader,
ShaderParameters::new().float(0, 0.75),
);
}
assert_eq!(scene.quads()[0].rect, Rect::new(11.0, 22.0, 5.0, 6.0));
assert_eq!(scene.quads()[0].clip, Some(clip));
assert_eq!(
scene.paths()[0].render_bounds(),
Rect::new(13.0, 24.0, 10.0, 10.0)
);
assert_eq!(scene.paths()[0].clip, Some(clip));
assert_eq!(
scene.custom_shaders()[0].rect,
Rect::new(12.0, 23.0, 8.0, 9.0)
);
assert_eq!(scene.custom_shaders()[0].clip, Some(clip));
assert_eq!(scene.custom_shaders()[0].parameters.vectors()[0][0], 0.75);
}
}