use lyon::tessellation::FillOptions;
use crate::draw::{Draw, drawing};
pub trait SetFill: Sized {
fn fill_options_mut(&mut self) -> &mut FillOptions;
fn fill_opts(mut self, opts: FillOptions) -> Self {
*self.fill_options_mut() = opts;
self
}
fn fill_tolerance(mut self, tolerance: f32) -> Self {
self.fill_options_mut().tolerance = tolerance;
self
}
fn fill_rule(mut self, rule: lyon::tessellation::FillRule) -> Self {
self.fill_options_mut().fill_rule = rule;
self
}
fn fill_sweep_orientation(mut self, orientation: lyon::tessellation::Orientation) -> Self {
self.fill_options_mut().sweep_orientation = orientation;
self
}
fn handle_intersections(mut self, handle: bool) -> Self {
self.fill_options_mut().handle_intersections = handle;
self
}
}
impl SetFill for Option<FillOptions> {
fn fill_options_mut(&mut self) -> &mut FillOptions {
self.get_or_insert_with(Default::default)
}
}
pub(crate) enum Update {
Opts(FillOptions),
Tolerance(f32),
Rule(lyon::tessellation::FillRule),
SweepOrientation(lyon::tessellation::Orientation),
HandleIntersections(bool),
}
pub(crate) fn set_fill(draw: &Draw, index: usize, update: Update) {
drawing::with_primitive(draw, index, |prim| match prim.fill_options_mut() {
Some(opts) => apply_update(opts, update),
None => bevy::log::warn_once!("drawing primitive does not support `fill` options"),
})
}
fn apply_update(opts: &mut FillOptions, update: Update) {
match update {
Update::Opts(new) => *opts = new,
Update::Tolerance(tolerance) => opts.tolerance = tolerance,
Update::Rule(rule) => opts.fill_rule = rule,
Update::SweepOrientation(orientation) => opts.sweep_orientation = orientation,
Update::HandleIntersections(handle) => opts.handle_intersections = handle,
}
}