use std::sync::Arc;
use kurbo::BezPath;
use crate::NodeId;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum FillRule {
#[default]
NonZero,
EvenOdd,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum BoolOp {
#[default]
Union,
Subtract,
Intersect,
Exclude,
}
impl BoolOp {
pub const ALL: [Self; 4] = [Self::Union, Self::Subtract, Self::Intersect, Self::Exclude];
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Union => "union",
Self::Subtract => "subtract",
Self::Intersect => "intersect",
Self::Exclude => "exclude",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TextSizing {
Fixed,
#[default]
AutoHeight,
AutoWidth,
}
impl TextSizing {
pub const ALL: [Self; 3] = [Self::Fixed, Self::AutoHeight, Self::AutoWidth];
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Fixed => "fixed",
Self::AutoHeight => "auto-height",
Self::AutoWidth => "auto-width",
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Shape {
None,
Rect,
Ellipse {
start: f64,
sweep: f64,
inner: f64,
},
Polygon {
points: u32,
ratio: f64,
},
Path {
path: Arc<BezPath>,
rule: FillRule,
},
Boolean(BoolOp),
Text(TextSizing),
Instance {
component: NodeId,
},
}
impl Shape {
#[must_use]
pub const fn ellipse() -> Self {
Self::Ellipse {
start: 0.0,
sweep: 360.0,
inner: 0.0,
}
}
#[must_use]
pub const fn polygon(points: u32) -> Self {
Self::Polygon { points, ratio: 1.0 }
}
#[must_use]
pub fn path(path: BezPath) -> Self {
Self::Path {
path: Arc::new(path),
rule: FillRule::NonZero,
}
}
#[must_use]
pub fn default_for(kind: crate::NodeKind) -> Self {
use crate::NodeKind;
match kind {
NodeKind::Frame | NodeKind::Rectangle | NodeKind::Image => Self::Rect,
NodeKind::Ellipse => Self::ellipse(),
NodeKind::Polygon => Self::polygon(3),
NodeKind::Path => Self::path(BezPath::new()),
NodeKind::Boolean => Self::Boolean(BoolOp::Union),
NodeKind::Text => Self::Text(TextSizing::AutoHeight),
NodeKind::Page
| NodeKind::Group
| NodeKind::Component
| NodeKind::Instance
| NodeKind::Custom => Self::None,
}
}
#[must_use]
pub fn fits(&self, kind: crate::NodeKind) -> bool {
use crate::NodeKind;
match (self, kind) {
(Self::Rect, NodeKind::Frame | NodeKind::Rectangle | NodeKind::Image)
| (Self::Ellipse { .. }, NodeKind::Ellipse)
| (Self::Polygon { .. }, NodeKind::Polygon)
| (Self::Path { .. }, NodeKind::Path)
| (Self::Boolean(_), NodeKind::Boolean)
| (Self::Text(_), NodeKind::Text)
| (Self::Instance { .. }, NodeKind::Instance) => true,
(Self::None, kind) => {
matches!(
kind,
NodeKind::Page | NodeKind::Group | NodeKind::Component | NodeKind::Custom
)
}
_ => false,
}
}
#[must_use]
pub fn is_valid(&self) -> bool {
match self {
Self::Ellipse {
start,
sweep,
inner,
} => start.is_finite() && sweep.is_finite() && (0.0..=1.0).contains(inner),
Self::Polygon { points, ratio } => *points >= 3 && (0.0..=1.0).contains(ratio),
_ => true,
}
}
}