use crate::AcrylicBrushResources;
use waft_embedder::valo::{
GradientStop, Matrix, Paint, PaintStyle, Point, Shader, SpreadMode, Stroke,
};
use waft_embedder::{
Canvas, ClipOp, Color, FillRule, PathBuilder, RRect, Radius, Rect, rrect_radii_elliptical,
};
use waft_painting::{draw_drrect, draw_oval, draw_rrect};
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Brush {
Solid(Color),
Acrylic(AcrylicBrushResources),
ControlElevation([(f64, Color); 2]),
CircleElevation([(f64, Color); 2]),
VerticalElevation {
stops: [(f64, Color); 2],
length: f64,
},
}
impl Brush {
pub fn representative_color(self) -> Color {
match self {
Brush::Solid(color) => color,
Brush::Acrylic(resources) => resources.fallback_color,
Brush::ControlElevation(stops) | Brush::CircleElevation(stops) => stops[1].1,
Brush::VerticalElevation { stops, .. } => stops[1].1,
}
}
fn fill(self, bounds: Rect) -> Option<Paint> {
Some(match self {
Brush::Acrylic(_) => return None,
Brush::Solid(color) => Paint::from_color(color.into()),
Brush::ControlElevation(stops) => Paint::from_shader(Shader::Linear {
start: Point::new(bounds.left as f32, bounds.bottom as f32),
end: Point::new(bounds.left as f32, bounds.bottom as f32 - 3.0),
stops: gradient_stops(stops),
spread: SpreadMode::Pad,
local: Matrix::IDENTITY,
}),
Brush::CircleElevation(stops) => Paint::from_shader(Shader::Linear {
start: Point::new(bounds.left as f32, bounds.top as f32),
end: Point::new(bounds.left as f32, bounds.bottom as f32),
stops: gradient_stops(stops),
spread: SpreadMode::Pad,
local: Matrix::IDENTITY,
}),
Brush::VerticalElevation { stops, length } => Paint::from_shader(Shader::Linear {
start: Point::new(bounds.left as f32, bounds.bottom as f32),
end: Point::new(bounds.left as f32, bounds.bottom as f32 - length as f32),
stops: gradient_stops(stops),
spread: SpreadMode::Pad,
local: Matrix::IDENTITY,
}),
})
}
pub fn paint_rrect(self, canvas: &mut Canvas, rect: RRect, bounds: Rect) {
if let Some(paint) = self.fill(bounds) {
draw_rrect(canvas, rect, &paint);
} else {
canvas.save();
canvas.clip_rrect_radii_elliptical(
rect.outer_rect(),
rrect_radii_elliptical(rect),
ClipOp::Intersect,
);
self.paint_material(canvas, rect.outer_rect());
canvas.restore();
}
}
pub fn paint_drrect(self, canvas: &mut Canvas, outer: RRect, inner: RRect, bounds: Rect) {
if let Some(paint) = self.fill(bounds) {
draw_drrect(canvas, outer, inner, &paint);
} else {
let mut path = PathBuilder::new();
path.rrect_radii_elliptical(outer.outer_rect(), rrect_radii_elliptical(outer));
path.rrect_radii_elliptical(inner.outer_rect(), rrect_radii_elliptical(inner));
canvas.save();
canvas.clip_path(&path.build(), FillRule::EvenOdd, ClipOp::Intersect);
self.paint_material(canvas, outer.outer_rect());
canvas.restore();
}
}
pub fn paint_rrect_stroke(self, canvas: &mut Canvas, rect: RRect, bounds: Rect, width: f64) {
if let Some(mut paint) = self.fill(bounds) {
paint.style = PaintStyle::Stroke(Stroke::new(width as f32));
draw_rrect(canvas, rect, &paint);
} else {
self.paint_drrect(
canvas,
rect.inflate(width / 2.0),
rect.deflate(width / 2.0),
bounds,
);
}
}
pub fn paint_oval(self, canvas: &mut Canvas, rect: Rect, bounds: Rect) {
if let Some(paint) = self.fill(bounds) {
draw_oval(canvas, rect, &paint);
} else {
self.paint_rrect(
canvas,
RRect::from_rect_and_radius(
rect,
Radius::elliptical(rect.width() / 2.0, rect.height() / 2.0),
),
bounds,
);
}
}
pub fn paint_oval_stroke(self, canvas: &mut Canvas, rect: Rect, bounds: Rect, width: f64) {
if let Some(mut paint) = self.fill(bounds) {
paint.style = PaintStyle::Stroke(Stroke::new(width as f32));
draw_oval(canvas, rect, &paint);
} else {
self.paint_rrect_stroke(
canvas,
RRect::from_rect_and_radius(
rect,
Radius::elliptical(rect.width() / 2.0, rect.height() / 2.0),
),
bounds,
width,
);
}
}
fn paint_material(self, canvas: &mut Canvas, bounds: Rect) {
if let Brush::Acrylic(resources) = self {
super::acrylic::paint_acrylic(canvas, bounds, resources);
}
}
}
fn gradient_stops(stops: [(f64, Color); 2]) -> Vec<GradientStop> {
stops
.into_iter()
.map(|(offset, color)| GradientStop {
offset: offset as f32,
color: color.into(),
})
.collect()
}