use crate::overlay::{OverlayFill, OverlayFillKind, ShadowLayer};
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct BackdropEffects {
pub blur: f32,
pub saturation: f32,
pub brightness: f32,
pub hue_shift: f32,
}
impl Default for BackdropEffects {
fn default() -> Self {
Self {
blur: 0.0,
saturation: 1.0,
brightness: 1.0,
hue_shift: 0.0,
}
}
}
impl BackdropEffects {
pub fn is_active(&self) -> bool {
self.blur > 0.0
}
pub fn with_blur(mut self, blur: f32) -> Self {
self.blur = blur;
self
}
pub fn with_filters(mut self, saturation: f32, brightness: f32, hue_shift: f32) -> Self {
self.saturation = saturation;
self.brightness = brightness;
self.hue_shift = hue_shift;
self
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct OverlayStyle {
pub fill: OverlayFill,
pub shadows: Vec<ShadowLayer>,
pub inner_shadows: Vec<ShadowLayer>,
pub backdrop: BackdropEffects,
pub tint: [f32; 4],
pub opacity: f32,
}
impl Default for OverlayStyle {
fn default() -> Self {
Self {
fill: OverlayFill::none(),
shadows: Vec::new(),
inner_shadows: Vec::new(),
backdrop: BackdropEffects::default(),
tint: [1.0, 1.0, 1.0, 1.0],
opacity: 1.0,
}
}
}
impl OverlayStyle {
pub fn solid(colour: impl Into<crate::colour::Colour>) -> Self {
Self {
fill: OverlayFill::Solid(colour.into()),
..Default::default()
}
}
pub fn with_fill(mut self, fill: OverlayFill) -> Self {
self.fill = fill;
self
}
pub fn with_shadows(mut self, shadows: Vec<ShadowLayer>) -> Self {
self.shadows = shadows;
self
}
pub fn with_inner_shadows(mut self, shadows: Vec<ShadowLayer>) -> Self {
self.inner_shadows = shadows;
self
}
pub fn with_backdrop(mut self, backdrop: BackdropEffects) -> Self {
self.backdrop = backdrop;
self
}
pub fn with_tint(mut self, tint: [f32; 4]) -> Self {
self.tint = tint;
self
}
pub fn with_opacity(mut self, opacity: f32) -> Self {
self.opacity = opacity;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct OverlayStyleSupport {
pub fill: &'static [OverlayFillKind],
pub backdrop: bool,
}
const COLOUR_FILLS: &[OverlayFillKind] = &[OverlayFillKind::Solid, OverlayFillKind::Gradient];
const ALL_FILLS: &[OverlayFillKind] = &[
OverlayFillKind::Solid,
OverlayFillKind::Gradient,
OverlayFillKind::Texture,
];
impl OverlayStyleSupport {
pub const ALL: Self = Self {
fill: ALL_FILLS,
backdrop: true,
};
pub const NONE: Self = Self {
fill: &[],
backdrop: false,
};
pub const fn for_shape(_shape: &crate::overlay::OverlayShape) -> Self {
Self::ALL
}
pub const fn for_polyline(closed: bool) -> Self {
Self {
fill: if closed { ALL_FILLS } else { &[] },
backdrop: false,
}
}
pub const fn for_glyphs() -> Self {
Self {
fill: COLOUR_FILLS,
backdrop: false,
}
}
pub fn draws_fill(&self, fill: &OverlayFill) -> bool {
let kind = fill.kind();
self.fill.iter().any(|k| *k == kind)
}
pub fn inert_fields(&self, style: &OverlayStyle) -> Vec<&'static str> {
let mut out = Vec::new();
if style.fill != OverlayFill::none() && !self.draws_fill(&style.fill) {
out.push("fill");
}
if !self.backdrop && style.backdrop.is_active() {
out.push("backdrop");
}
out
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::colour::Colour;
use crate::overlay::{OverlayShape, ShadowLayer};
#[test]
fn vector_shapes_draw_every_cell() {
let style = OverlayStyle::solid(Colour::linear(1.0, 0.0, 0.0, 1.0))
.with_inner_shadows(vec![ShadowLayer::default()])
.with_backdrop(BackdropEffects::default().with_blur(4.0));
let analytic = OverlayStyleSupport::for_shape(&OverlayShape::Circle);
assert!(analytic.inert_fields(&style).is_empty());
let vector = OverlayStyleSupport::for_shape(&OverlayShape::Vector {
subpaths: Vec::new(),
fill_rule: crate::overlay::FillRule::NonZero,
});
assert!(vector.inert_fields(&style).is_empty());
assert!(vector.draws_fill(&OverlayFill::texture(crate::overlay::OverlayTextureId(0))));
}
#[test]
fn a_default_style_is_never_inert() {
let style = OverlayStyle::default();
assert!(
OverlayStyleSupport::for_glyphs()
.inert_fields(&style)
.is_empty()
);
assert!(OverlayStyleSupport::NONE.inert_fields(&style).is_empty());
}
}