use bevy::prelude::*;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PathCommand {
MoveTo(Vec2),
LineTo(Vec2),
QuadTo { ctrl: Vec2, to: Vec2 },
CubicTo { ctrl1: Vec2, ctrl2: Vec2, to: Vec2 },
Close,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct GradientStop {
pub offset: f32,
pub color: LinearRgba,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Brush {
Solid(LinearRgba),
Linear {
start: Vec2,
end: Vec2,
stops: Vec<GradientStop>,
},
Radial {
center: Vec2,
radius: f32,
stops: Vec<GradientStop>,
},
}
impl From<LinearRgba> for Brush {
fn from(color: LinearRgba) -> Self {
Brush::Solid(color)
}
}
impl Brush {
pub fn is_opaque(&self) -> bool {
match self {
Brush::Solid(c) => c.alpha >= 1.0,
Brush::Linear { stops, .. } | Brush::Radial { stops, .. } => {
stops.iter().all(|s| s.color.alpha >= 1.0)
}
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum FillRule {
#[default]
NonZero,
EvenOdd,
}
#[derive(Clone, Debug, PartialEq)]
pub struct PathStyle {
pub fill: Option<Brush>,
pub stroke: Option<StrokeStyle>,
pub fill_rule: FillRule,
}
impl PathStyle {
pub fn fill(brush: impl Into<Brush>) -> Self {
Self { fill: Some(brush.into()), stroke: None, fill_rule: FillRule::default() }
}
pub fn stroke(stroke: StrokeStyle) -> Self {
Self { fill: None, stroke: Some(stroke), fill_rule: FillRule::default() }
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct DashPattern {
pub pattern: Vec<f32>,
pub offset: f32,
}
impl DashPattern {
pub fn new(pattern: impl Into<Vec<f32>>) -> Self {
Self { pattern: pattern.into(), offset: 0.0 }
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct StrokeStyle {
pub brush: Brush,
pub width: f32,
pub join: LineJoin,
pub cap: LineCap,
pub miter_limit: f32,
pub dash: Option<DashPattern>,
}
impl StrokeStyle {
pub fn new(brush: impl Into<Brush>, width: f32) -> Self {
Self {
brush: brush.into(),
width,
join: LineJoin::Miter,
cap: LineCap::Butt,
miter_limit: 4.0,
dash: None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LineJoin {
Miter,
Round,
Bevel,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LineCap {
Butt,
Round,
Square,
}