GitCorner

Code

ossartcrafteffectcraft🌐
665bfac0b339
//! Vector icons drawn in code on a 16×16 design grid (crisp at any DPI, recolourable). Original
//! drawings following generic motion-graphics conventions; no third-party or Adobe artwork.

use egui::epaint::{PathShape, PathStroke};
use egui::{Color32, Painter, Pos2, Rect, Stroke, pos2, vec2};
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Icon {
    // tools
    Home,
    Selection,
    Hand,
    Zoom,
    Orbit,
    PanCamera,
    Dolly,
    Rotate,
    PanBehind,
    Rectangle,
    RoundedRect,
    Ellipse,
    Polygon,
    Star,
    StarFilled,
    Pen,
    PenAdd,
    PenDelete,
    PenConvert,
    MaskFeather,
    Type,
    TypeVertical,
    Brush,
    Clone,
    Eraser,
    RotoBrush,
    RefineEdge,
    /// Object Matte: a selection box's corners around a solid object, with a click point.
    ObjectMatte,
    /// Puppet Position Pin: a pushpin. The other pin tools add a badge at the lower right.
    Puppet,
    PuppetAdvanced,
    PuppetBend,
    PuppetStarch,
    PuppetOverlap,
    // switches & timeline
    Eye,
    Speaker,
    Solo,
    Lock,
    /// An open padlock: the shackle lifted clear of the body on the right.
    LockOpen,
    Shy,
    Collapse,
    Quality,
    Fx,
    FrameBlend,
    MotionBlur,
    Adjustment,
    Cube,
    Draft3D,
    Graph,
    Stopwatch,
    Keyframe,
    PickWhip,
    Flowchart,
    /// The Timeline's composition marker bin: a marker tab resting in an open tray.
    MarkerBin,
    // project items
    Folder,
    Comp,
    Footage,
    Image,
    Audio,
    Solid,
    TextLayer,
    ShapeLayer,
    Null,
    Camera,
    Light,
    // ui
    Search,
    Trash,
    NewFolder,
    NewComp,
    Gear,
    Hamburger,
    ChevronDown,
    ChevronRight,
    ChevronLeft,
    Play,
    Pause,
    StepBack,
    StepFwd,
    First,
    Last,
    Loop,
    Snapshot,
    ShowSnapshot,
    Grid,
    MaskVis,
    Checker,
    Region,
    /// Extended Viewer: a small frame with corner ticks reaching outward.
    ExtendedViewer,
    Plus,
    Minus,
    Close,
    Link,
    MountainSmall,
    MountainLarge,
    Fullscreen,
    Channel,
    Exposure,
    /// Stroke width: three bars, thin to thick.
    StrokeWidth,
    // links
    Chat,
    Globe,
    Code,
    Sparkle,
    // appearance modes
    /// Sync with System: a display on a stand.
    Monitor,
    /// Light: a disc with eight rays.
    Sun,
    /// Dark: a crescent.
    Moon,
}

pub struct Pen16<'a> {
    painter: &'a Painter,
    rect: Rect,
    color: Color32,
    width: f32,
}

impl Pen16<'_> {
    fn s(&self) -> f32 {
        self.rect.width().min(self.rect.height()) / 16.0
    }
    fn p(&self, x: f32, y: f32) -> Pos2 {
        let s = self.s();
        let o = self.rect.center() - vec2(8.0 * s, 8.0 * s);
        pos2(o.x + x * s, o.y + y * s)
    }
    fn line(&self, pts: &[(f32, f32)]) {
        let v: Vec<Pos2> = pts.iter().map(|(x, y)| self.p(*x, *y)).collect();
        self.painter.add(PathShape::line(v, PathStroke::new(self.width * self.s(), self.color)));
    }
    fn closed(&self, pts: &[(f32, f32)]) {
        let v: Vec<Pos2> = pts.iter().map(|(x, y)| self.p(*x, *y)).collect();
        self.painter.add(PathShape::closed_line(v, PathStroke::new(self.width * self.s(), self.color)));
    }
    /// Fill a **convex** outline: the tessellator fans a concave one into slivers across its
    /// notches, so a concave glyph is drawn as overlapping convex pieces (see [`SELECTION_ARROW`]).
    fn fill(&self, pts: &[(f32, f32)]) {
        let v: Vec<Pos2> = pts.iter().map(|(x, y)| self.p(*x, *y)).collect();
        self.painter.add(PathShape::convex_polygon(v, self.color, Stroke::NONE));
    }
    fn circle(&self, x: f32, y: f32, r: f32) {
        self.painter.circle_stroke(self.p(x, y), r * self.s(), Stroke::new(self.width * self.s(), self.color));
    }
    fn dot(&self, x: f32, y: f32, r: f32) {
        self.painter.circle_filled(self.p(x, y), r * self.s(), self.color);
    }
    /// A filled dot in its own colour, for the icons that are coloured in After Effects too (the
    /// channel picker's red, green and blue). The tint's alpha is kept, so a dimmed button stays
    /// dimmed.
    fn dot_colored(&self, x: f32, y: f32, r: f32, color: Color32) {
        // `gamma_multiply` keeps the premultiplied channels consistent, so a dimmed tint dims the
        // circle instead of darkening it twice.
        self.painter.circle_filled(self.p(x, y), r * self.s(), color.gamma_multiply(self.color.a() as f32 / 255.0));
    }
    fn rect(&self, x0: f32, y0: f32, x1: f32, y1: f32) {
        let r = Rect::from_min_max(self.p(x0, y0), self.p(x1, y1));
        self.painter.rect_stroke(r, 1.0 * self.s(), Stroke::new(self.width * self.s(), self.color), egui::StrokeKind::Middle);
    }
    fn rrect(&self, x0: f32, y0: f32, x1: f32, y1: f32, rad: f32) {
        let r = Rect::from_min_max(self.p(x0, y0), self.p(x1, y1));
        self.painter.rect_stroke(r, rad * self.s(), Stroke::new(self.width * self.s(), self.color), egui::StrokeKind::Middle);
    }
    fn rect_fill(&self, x0: f32, y0: f32, x1: f32, y1: f32) {
        let r = Rect::from_min_max(self.p(x0, y0), self.p(x1, y1));
        self.painter.rect_filled(r, 0.8 * self.s(), self.color);
    }
    fn arc(&self, cx: f32, cy: f32, r: f32, a0: f32, a1: f32) {
        let n = 24;
        let pts: Vec<(f32, f32)> = (0..=n)
            .map(|i| {
                let a = (a0 + (a1 - a0) * i as f32 / n as f32).to_radians();
                (cx + r * a.cos(), cy + r * a.sin())
            })
            .collect();
        self.line(&pts);
    }
    /// An upright pushpin centred on `cx`: a cap, a body flaring to its base and a needle.
    fn pushpin(&self, cx: f32) {
        self.fill(&[(cx - 3.0, 1.5), (cx + 3.0, 1.5), (cx + 2.0, 3.0), (cx - 2.0, 3.0)]);
        self.closed(&[(cx - 2.0, 3.0), (cx + 2.0, 3.0), (cx + 2.4, 7.0), (cx + 4.0, 8.8), (cx - 4.0, 8.8), (cx - 2.4, 7.0)]);
        self.line(&[(cx, 8.8), (cx, 14.8)]);
    }
    fn ellipse(&self, cx: f32, cy: f32, rx: f32, ry: f32) {
        let n = 32;
        let pts: Vec<(f32, f32)> = (0..n)
            .map(|i| {
                let a = std::f32::consts::TAU * i as f32 / n as f32;
                (cx + rx * a.cos(), cy + ry * a.sin())
            })
            .collect();
        self.closed(&pts);
    }
    fn star(&self, cx: f32, cy: f32, ro: f32, ri: f32, n: usize) -> Vec<(f32, f32)> {
        (0..n * 2)
            .map(|i| {
                let a = std::f32::consts::PI * i as f32 / n as f32 - std::f32::consts::FRAC_PI_2;
                let r = if i % 2 == 0 { ro } else { ri };
                (cx + r * a.cos(), cy + r * a.sin())
            })
            .collect()
    }
}

/// The convex pieces of each concave filled glyph. Pieces overlap rather than abut: abutting
/// antialiased edges would leave a faint seam.
type Pieces = &'static [&'static [(f32, f32)]];

/// The Selection tool's arrow pointer (tip upper left, a notch each side of the tail): the head
/// with its left wing, the right wing, and the tail. Only the head reaches the tip: slivers
/// meeting there would antialias into a spike past it.
const SELECTION_ARROW: Pieces = &[
    &[(4.5, 2.0), (4.5, 13.0), (7.2, 10.5), (10.1, 7.39)],
    &[(7.5, 9.0), (8.9, 9.9), (12.5, 9.7), (10.1, 7.39)],
    &[(7.8, 8.6), (7.2, 10.5), (9.2, 14.6), (10.8, 13.9), (8.9, 9.9)],
];
/// A loudspeaker: the magnet box, running a little into the cone.
const SPEAKER: Pieces = &[&[(2.0, 6.0), (5.5, 6.0), (5.5, 10.0), (2.0, 10.0)], &[(5.0, 6.0), (9.0, 2.5), (9.0, 13.5), (5.0, 10.0)]];
/// A four-pointed star: an upright and a level diamond, whose edges cross at the inner corners.
const SPARKLE: Pieces = &[&[(8.0, 1.5), (9.784, 8.0), (8.0, 14.5), (6.216, 8.0)], &[(1.5, 8.0), (8.0, 6.216), (14.5, 8.0), (8.0, 9.784)]];
#[cfg(test)]
const CONCAVE: [Pieces; 3] = [SELECTION_ARROW, SPEAKER, SPARKLE];

/// Paint `icon` centred in `rect`.
pub fn paint(painter: &Painter, rect: Rect, icon: Icon, color: Color32) {
    let pen = Pen16 { painter, rect, color, width: 1.25 };
    use Icon::*;
    match icon {
        Home => {
            pen.closed(&[(2.5, 7.5), (8.0, 2.5), (13.5, 7.5), (13.5, 13.5), (2.5, 13.5)]);
            pen.rect(6.5, 9.5, 9.5, 13.5);
        }
        Selection => SELECTION_ARROW.iter().for_each(|p| pen.fill(p)),
        Hand => {
            pen.line(&[(5.0, 9.0), (5.0, 4.0)]);
            pen.line(&[(7.3, 8.0), (7.3, 2.6)]);
            pen.line(&[(9.6, 8.0), (9.6, 3.2)]);
            pen.line(&[(11.8, 8.8), (11.8, 5.0)]);
            pen.line(&[(5.0, 9.0), (3.2, 7.4), (2.4, 8.4), (5.6, 13.4), (11.0, 14.0), (12.4, 11.0), (11.8, 8.8)]);
        }
        Zoom => {
            pen.circle(6.8, 6.8, 4.3);
            pen.line(&[(10.0, 10.0), (14.0, 14.0)]);
        }
        Orbit => {
            pen.ellipse(8.0, 8.0, 6.0, 2.8);
            pen.dot(8.0, 8.0, 1.6);
            pen.fill(&[(12.0, 3.0), (14.5, 5.2), (11.4, 6.0)]);
        }
        PanCamera => {
            pen.line(&[(8.0, 1.8), (8.0, 14.2)]);
            pen.line(&[(1.8, 8.0), (14.2, 8.0)]);
            pen.fill(&[(8.0, 1.0), (6.2, 3.5), (9.8, 3.5)]);
            pen.fill(&[(8.0, 15.0), (6.2, 12.5), (9.8, 12.5)]);
            pen.fill(&[(1.0, 8.0), (3.5, 6.2), (3.5, 9.8)]);
            pen.fill(&[(15.0, 8.0), (12.5, 6.2), (12.5, 9.8)]);
        }
        Dolly => {
            pen.line(&[(8.0, 1.8), (8.0, 14.2)]);
            pen.fill(&[(8.0, 1.0), (5.5, 4.5), (10.5, 4.5)]);
            pen.fill(&[(8.0, 15.0), (5.5, 11.5), (10.5, 11.5)]);
            pen.rect(5.0, 6.5, 11.0, 9.5);
        }
        Rotate => {
            pen.arc(8.0, 8.0, 5.0, -60.0, 220.0);
            pen.fill(&[(10.2, 1.8), (13.6, 4.6), (9.6, 5.8)]);
        }
        PanBehind => {
            pen.circle(8.0, 8.0, 3.0);
            pen.line(&[(8.0, 1.5), (8.0, 5.0)]);
            pen.line(&[(8.0, 11.0), (8.0, 14.5)]);
            pen.line(&[(1.5, 8.0), (5.0, 8.0)]);
            pen.line(&[(11.0, 8.0), (14.5, 8.0)]);
        }
        Rectangle => pen.rect(2.5, 3.5, 13.5, 12.5),
        RoundedRect => pen.rrect(2.5, 3.5, 13.5, 12.5, 3.0),
        Ellipse => pen.ellipse(8.0, 8.0, 5.8, 4.8),
        Polygon => {
            let pts: Vec<(f32, f32)> = (0..6)
                .map(|i| {
                    let a = std::f32::consts::TAU * i as f32 / 6.0 - std::f32::consts::FRAC_PI_2;
                    (8.0 + 6.0 * a.cos(), 8.4 + 6.0 * a.sin())
                })
                .collect();
            pen.closed(&pts);
        }
        Star => {
            let pts = pen.star(8.0, 8.6, 6.4, 2.7, 5);
            pen.closed(&pts);
        }
        StarFilled => {
            // Five triangles fill the concave star without a convex-polygon approximation.
            let pts = pen.star(8.0, 8.6, 6.4, 2.7, 5);
            for pair in pts.windows(2) {
                if let [a, b] = pair {
                    pen.fill(&[(8.0, 8.6), *a, *b]);
                }
            }
            if let (Some(first), Some(last)) = (pts.first(), pts.last()) {
                pen.fill(&[(8.0, 8.6), *last, *first]);
            }
        }
        Pen => {
            pen.closed(&[(8.0, 1.8), (12.0, 9.0), (8.0, 14.0), (4.0, 9.0)]);
            pen.dot(8.0, 8.6, 1.1);
            pen.line(&[(8.0, 1.8), (8.0, 7.6)]);
        }
        Type => {
            pen.line(&[(3.0, 3.5), (13.0, 3.5)]);
            pen.line(&[(8.0, 3.5), (8.0, 13.5)]);
            pen.line(&[(6.0, 13.5), (10.0, 13.5)]);
        }
        TypeVertical => {
            pen.line(&[(12.5, 3.0), (12.5, 13.0)]);
            pen.line(&[(12.5, 8.0), (3.0, 8.0)]);
            pen.line(&[(3.0, 6.0), (3.0, 10.0)]);
        }
        Brush => {
            pen.line(&[(13.5, 2.5), (7.0, 9.0)]);
            pen.fill(&[(7.6, 8.4), (4.4, 9.4), (2.6, 13.6), (6.8, 12.0), (7.8, 9.0)]);
        }
        Clone => {
            pen.rect(6.0, 8.0, 10.0, 14.0);
            pen.line(&[(8.0, 8.0), (8.0, 5.5)]);
            pen.ellipse(8.0, 3.8, 3.0, 1.8);
        }
        Eraser => {
            pen.closed(&[(9.0, 2.5), (14.0, 7.5), (8.0, 13.5), (3.0, 8.5)]);
            pen.line(&[(5.5, 6.0), (11.5, 11.0)]);
            pen.line(&[(6.0, 14.0), (14.0, 14.0)]);
        }
        RotoBrush => {
            pen.line(&[(13.0, 2.5), (8.0, 7.5)]);
            pen.ellipse(6.0, 10.0, 3.6, 3.6);
            pen.dot(6.0, 10.0, 1.0);
        }
        RefineEdge => {
            // A brush over a soft (dashed) edge.
            pen.line(&[(13.5, 2.5), (9.0, 7.0)]);
            pen.ellipse(7.0, 9.0, 2.6, 2.6);
            pen.line(&[(2.0, 14.0), (3.5, 12.5)]);
            pen.line(&[(5.0, 14.0), (6.5, 12.5)]);
            pen.line(&[(8.0, 14.0), (9.5, 12.5)]);
            pen.line(&[(11.0, 14.0), (12.5, 12.5)]);
        }
        ObjectMatte => {
            // Corner brackets of a box around a filled object, and the click on it.
            for (x, y, dx, dy) in [(2.0, 2.0, 1.0, 1.0), (14.0, 2.0, -1.0, 1.0), (14.0, 14.0, -1.0, -1.0), (2.0, 14.0, 1.0, -1.0)] {
                pen.line(&[(x, y + 3.0 * dy), (x, y), (x + 3.0 * dx, y)]);
            }
            pen.dot(8.0, 8.0, 2.2);
            pen.circle(8.0, 8.0, 4.4);
        }
        Puppet => pen.pushpin(8.0),
        PuppetAdvanced => {
            // Rotation ring with its square scale handle.
            pen.pushpin(6.5);
            pen.circle(12.0, 12.0, 2.4);
            pen.rect_fill(13.6, 11.2, 15.2, 12.8);
        }
        PuppetBend => {
            // A curved arrow.
            pen.pushpin(6.5);
            pen.arc(11.8, 13.2, 3.0, 200.0, 340.0);
            pen.fill(&[(14.9, 11.0), (15.4, 13.8), (12.9, 12.6)]);
        }
        PuppetStarch => {
            // Stiff bars.
            pen.pushpin(6.5);
            for y in [10.5, 12.75, 15.0] {
                pen.line(&[(10.5, y), (15.0, y)]);
            }
        }
        PuppetOverlap => {
            // Two overlapping sheets.
            pen.pushpin(6.5);
            pen.rect(10.0, 9.8, 13.4, 13.2);
            pen.rect_fill(11.8, 11.6, 15.2, 15.0);
        }
        Eye => {
            pen.line(&[(1.5, 8.0), (4.0, 5.0), (8.0, 3.8), (12.0, 5.0), (14.5, 8.0), (12.0, 11.0), (8.0, 12.2), (4.0, 11.0), (1.5, 8.0)]);
            pen.dot(8.0, 8.0, 2.2);
        }
        Speaker => {
            SPEAKER.iter().for_each(|p| pen.fill(p));
            pen.arc(9.0, 8.0, 3.0, -50.0, 50.0);
            pen.arc(9.0, 8.0, 5.5, -50.0, 50.0);
        }
        Solo => {
            pen.circle(8.0, 8.0, 4.5);
            pen.dot(8.0, 8.0, 2.4);
        }
        Lock => {
            pen.rect(3.5, 7.0, 12.5, 14.0);
            pen.arc(8.0, 7.0, 3.0, 180.0, 360.0);
            pen.dot(8.0, 10.5, 1.0);
        }
        LockOpen => {
            pen.rect(3.5, 7.0, 12.5, 14.0);
            pen.arc(8.0, 4.0, 3.0, 180.0, 360.0);
            pen.line(&[(5.0, 4.0), (5.0, 7.0)]);
            pen.dot(8.0, 10.5, 1.0);
        }
        Shy => {
            pen.circle(8.0, 8.0, 5.8);
            pen.dot(6.0, 7.0, 0.8);
            pen.dot(10.0, 7.0, 0.8);
            pen.arc(8.0, 8.5, 2.6, 30.0, 150.0);
        }
        Collapse => {
            pen.circle(8.0, 8.0, 2.8);
            for i in 0..8 {
                let a = std::f32::consts::TAU * i as f32 / 8.0;
                pen.line(&[(8.0 + 4.3 * a.cos(), 8.0 + 4.3 * a.sin()), (8.0 + 6.3 * a.cos(), 8.0 + 6.3 * a.sin())]);
            }
        }
        Quality => pen.line(&[(4.0, 2.5), (12.0, 13.5)]),
        Fx => {
            pen.line(&[(7.0, 3.0), (5.6, 3.0), (4.6, 4.2), (4.6, 13.0)]);
            pen.line(&[(3.0, 7.0), (7.0, 7.0)]);
            pen.line(&[(8.5, 7.0), (13.0, 13.0)]);
            pen.line(&[(13.0, 7.0), (8.5, 13.0)]);
        }
        FrameBlend => {
            pen.rect(2.0, 4.0, 9.0, 11.0);
            pen.rect(7.0, 5.0, 14.0, 12.0);
        }
        MotionBlur => {
            pen.circle(10.5, 8.0, 3.5);
            pen.circle(7.0, 8.0, 3.5);
            pen.circle(4.0, 8.0, 3.0);
        }
        Adjustment => {
            pen.circle(8.0, 8.0, 5.5);
            pen.fill(&[(8.0, 2.5), (8.0, 13.5), (5.2, 12.7), (3.2, 10.6), (2.5, 8.0), (3.2, 5.4), (5.2, 3.3)]);
        }
        Cube => {
            pen.closed(&[(8.0, 2.0), (13.5, 5.0), (13.5, 11.0), (8.0, 14.0), (2.5, 11.0), (2.5, 5.0)]);
            pen.line(&[(2.5, 5.0), (8.0, 8.0), (13.5, 5.0)]);
            pen.line(&[(8.0, 8.0), (8.0, 14.0)]);
        }
        Draft3D => {
            pen.closed(&[(8.0, 2.0), (13.5, 5.0), (13.5, 11.0), (8.0, 14.0), (2.5, 11.0), (2.5, 5.0)]);
            pen.line(&[(5.0, 9.0), (11.0, 9.0)]);
        }
        Graph => {
            pen.line(&[(2.0, 13.0), (5.0, 12.0), (7.0, 8.0), (9.0, 4.5), (11.5, 3.2), (14.0, 3.0)]);
            pen.line(&[(2.0, 2.0), (2.0, 14.0), (14.0, 14.0)]);
        }
        Stopwatch => {
            pen.circle(8.0, 9.0, 5.2);
            pen.line(&[(8.0, 9.0), (8.0, 5.8)]);
            pen.line(&[(6.5, 2.0), (9.5, 2.0)]);
            pen.line(&[(8.0, 2.0), (8.0, 3.8)]);
        }
        Keyframe => pen.fill(&[(8.0, 2.5), (13.5, 8.0), (8.0, 13.5), (2.5, 8.0)]),
        PickWhip => {
            pen.arc(8.0, 8.0, 5.5, 0.0, 300.0);
            pen.arc(8.0, 8.0, 3.0, 40.0, 340.0);
            pen.dot(8.0, 8.0, 1.0);
        }
        Flowchart => {
            pen.rect(1.5, 2.5, 6.0, 6.5);
            pen.rect(10.0, 9.5, 14.5, 13.5);
            pen.line(&[(6.0, 4.5), (12.25, 4.5), (12.25, 9.5)]);
        }
        MarkerBin => {
            pen.fill(&[(5.0, 2.0), (11.0, 2.0), (11.0, 7.5), (8.0, 10.5), (5.0, 7.5)]);
            pen.line(&[(2.0, 9.0), (2.0, 14.0), (14.0, 14.0), (14.0, 9.0)]);
        }
        Folder => pen.closed(&[(1.5, 4.0), (6.0, 4.0), (7.5, 5.5), (14.5, 5.5), (14.5, 13.0), (1.5, 13.0)]),
        Comp => {
            pen.rect(2.0, 3.0, 14.0, 13.0);
            pen.rect_fill(4.0, 5.0, 8.0, 9.0);
            pen.rect(7.0, 7.5, 12.0, 11.0);
        }
        Footage => {
            pen.rect(2.0, 3.0, 14.0, 13.0);
            for y in [5.0, 8.0, 11.0] {
                pen.dot(3.8, y, 0.6);
                pen.dot(12.2, y, 0.6);
            }
            pen.line(&[(5.5, 3.0), (5.5, 13.0)]);
            pen.line(&[(10.5, 3.0), (10.5, 13.0)]);
        }
        Image => {
            pen.rect(2.0, 3.0, 14.0, 13.0);
            pen.line(&[(2.5, 11.5), (6.0, 8.0), (9.0, 10.5), (11.0, 9.0), (13.5, 11.5)]);
            pen.dot(10.5, 5.8, 1.1);
        }
        Audio => {
            pen.line(&[(6.0, 12.0), (6.0, 3.5), (13.0, 2.5), (13.0, 10.5)]);
            pen.dot(4.4, 12.0, 1.8);
            pen.dot(11.4, 10.5, 1.8);
        }
        Solid => pen.rect_fill(3.0, 3.0, 13.0, 13.0),
        TextLayer => {
            pen.line(&[(3.5, 3.5), (12.5, 3.5)]);
            pen.line(&[(8.0, 3.5), (8.0, 13.0)]);
        }
        ShapeLayer => {
            let pts = pen.star(8.0, 8.6, 6.4, 2.7, 5);
            pen.closed(&pts);
        }
        Null => pen.rect(3.0, 3.0, 13.0, 13.0),
        Camera => {
            pen.rect(1.5, 5.0, 10.5, 12.0);
            pen.closed(&[(10.5, 7.5), (14.5, 5.5), (14.5, 11.5), (10.5, 9.5)]);
        }
        Light => {
            pen.circle(8.0, 6.5, 3.8);
            pen.line(&[(6.3, 10.0), (6.3, 12.5), (9.7, 12.5), (9.7, 10.0)]);
            pen.line(&[(6.8, 14.2), (9.2, 14.2)]);
        }
        Search => {
            pen.circle(6.8, 6.8, 4.0);
            pen.line(&[(9.8, 9.8), (13.5, 13.5)]);
        }
        Trash => {
            pen.line(&[(2.5, 4.0), (13.5, 4.0)]);
            pen.closed(&[(4.0, 4.0), (5.0, 14.0), (11.0, 14.0), (12.0, 4.0)]);
            pen.line(&[(6.0, 4.0), (6.5, 2.0), (9.5, 2.0), (10.0, 4.0)]);
        }
        NewFolder => {
            pen.closed(&[(1.5, 4.0), (6.0, 4.0), (7.5, 5.5), (14.5, 5.5), (14.5, 13.0), (1.5, 13.0)]);
            pen.line(&[(8.0, 7.5), (8.0, 11.5)]);
            pen.line(&[(6.0, 9.5), (10.0, 9.5)]);
        }
        NewComp => {
            pen.rect(2.0, 3.0, 14.0, 13.0);
            pen.line(&[(8.0, 5.5), (8.0, 10.5)]);
            pen.line(&[(5.5, 8.0), (10.5, 8.0)]);
        }
        Gear => {
            pen.circle(8.0, 8.0, 2.2);
            for i in 0..8 {
                let a = std::f32::consts::TAU * i as f32 / 8.0;
                pen.line(&[(8.0 + 4.0 * a.cos(), 8.0 + 4.0 * a.sin()), (8.0 + 6.0 * a.cos(), 8.0 + 6.0 * a.sin())]);
            }
            pen.circle(8.0, 8.0, 4.2);
        }
        StrokeWidth => {
            pen.rect_fill(2.5, 3.0, 13.5, 3.75);
            pen.rect_fill(2.5, 6.5, 13.5, 8.0);
            pen.rect_fill(2.5, 10.5, 13.5, 13.0);
        }
        Hamburger => {
            for y in [4.5, 8.0, 11.5] {
                pen.line(&[(3.0, y), (13.0, y)]);
            }
        }
        ChevronDown => pen.fill(&[(4.0, 6.0), (12.0, 6.0), (8.0, 11.0)]),
        ChevronRight => pen.fill(&[(6.0, 4.0), (11.0, 8.0), (6.0, 12.0)]),
        ChevronLeft => pen.fill(&[(10.0, 4.0), (5.0, 8.0), (10.0, 12.0)]),
        Play => pen.fill(&[(4.5, 2.5), (13.0, 8.0), (4.5, 13.5)]),
        Pause => {
            pen.rect_fill(4.0, 3.0, 6.8, 13.0);
            pen.rect_fill(9.2, 3.0, 12.0, 13.0);
        }
        StepBack => {
            pen.fill(&[(12.5, 3.5), (6.0, 8.0), (12.5, 12.5)]);
            pen.rect_fill(3.5, 3.5, 5.0, 12.5);
        }
        StepFwd => {
            pen.fill(&[(3.5, 3.5), (10.0, 8.0), (3.5, 12.5)]);
            pen.rect_fill(11.0, 3.5, 12.5, 12.5);
        }
        First => {
            pen.fill(&[(13.5, 3.5), (8.0, 8.0), (13.5, 12.5)]);
            pen.fill(&[(8.5, 3.5), (3.0, 8.0), (8.5, 12.5)]);
            pen.rect_fill(1.8, 3.5, 3.0, 12.5);
        }
        Last => {
            pen.fill(&[(2.5, 3.5), (8.0, 8.0), (2.5, 12.5)]);
            pen.fill(&[(7.5, 3.5), (13.0, 8.0), (7.5, 12.5)]);
            pen.rect_fill(13.0, 3.5, 14.2, 12.5);
        }
        Loop => {
            pen.arc(8.0, 8.0, 5.0, 200.0, 520.0);
            pen.fill(&[(1.8, 5.5), (5.2, 5.5), (3.2, 8.5)]);
        }
        Snapshot => {
            pen.closed(&[(2.0, 5.0), (5.0, 5.0), (6.0, 3.5), (10.0, 3.5), (11.0, 5.0), (14.0, 5.0), (14.0, 12.5), (2.0, 12.5)]);
            pen.circle(8.0, 8.7, 2.3);
        }
        ShowSnapshot => {
            pen.line(&[(1.5, 8.0), (4.0, 5.0), (8.0, 3.8), (12.0, 5.0), (14.5, 8.0), (12.0, 11.0), (8.0, 12.2), (4.0, 11.0), (1.5, 8.0)]);
            pen.rect(6.0, 6.5, 10.0, 9.5);
        }
        Grid => {
            pen.rect(2.0, 2.0, 14.0, 14.0);
            pen.line(&[(6.0, 2.0), (6.0, 14.0)]);
            pen.line(&[(10.0, 2.0), (10.0, 14.0)]);
            pen.line(&[(2.0, 6.0), (14.0, 6.0)]);
            pen.line(&[(2.0, 10.0), (14.0, 10.0)]);
        }
        MaskVis => {
            pen.rect(2.0, 2.0, 14.0, 14.0);
            pen.ellipse(8.0, 8.0, 3.8, 3.8);
        }
        Checker => {
            pen.rect(2.0, 2.0, 14.0, 14.0);
            pen.rect_fill(2.0, 2.0, 8.0, 8.0);
            pen.rect_fill(8.0, 8.0, 14.0, 14.0);
        }
        Region => {
            pen.rect(2.5, 2.5, 13.5, 13.5);
            pen.rect(5.5, 5.5, 10.5, 10.5);
        }
        ExtendedViewer => {
            pen.rect(5.0, 5.0, 11.0, 11.0);
            for (x, y, dx, dy) in [(2.0, 2.0, 1.0, 1.0), (14.0, 2.0, -1.0, 1.0), (2.0, 14.0, 1.0, -1.0), (14.0, 14.0, -1.0, -1.0)] {
                pen.line(&[(x + dx * 3.0, y), (x, y), (x, y + dy * 3.0)]);
            }
        }
        Plus => {
            pen.line(&[(8.0, 3.0), (8.0, 13.0)]);
            pen.line(&[(3.0, 8.0), (13.0, 8.0)]);
        }
        Minus => pen.line(&[(3.0, 8.0), (13.0, 8.0)]),
        Close => {
            pen.line(&[(4.0, 4.0), (12.0, 12.0)]);
            pen.line(&[(12.0, 4.0), (4.0, 12.0)]);
        }
        Link => {
            pen.arc(5.5, 8.0, 3.0, 90.0, 270.0);
            pen.arc(10.5, 8.0, 3.0, -90.0, 90.0);
            pen.line(&[(5.5, 5.0), (8.0, 5.0)]);
            pen.line(&[(5.5, 11.0), (8.0, 11.0)]);
            pen.line(&[(8.0, 5.0), (10.5, 5.0)]);
            pen.line(&[(8.0, 11.0), (10.5, 11.0)]);
        }
        MountainSmall => pen.fill(&[(4.0, 12.0), (8.0, 7.0), (12.0, 12.0)]),
        MountainLarge => {
            pen.fill(&[(1.0, 13.0), (7.0, 4.0), (13.0, 13.0)]);
            pen.fill(&[(8.0, 13.0), (11.5, 8.0), (15.0, 13.0)]);
        }
        Fullscreen => {
            pen.line(&[(2.0, 6.0), (2.0, 2.0), (6.0, 2.0)]);
            pen.line(&[(10.0, 2.0), (14.0, 2.0), (14.0, 6.0)]);
            pen.line(&[(14.0, 10.0), (14.0, 14.0), (10.0, 14.0)]);
            pen.line(&[(6.0, 14.0), (2.0, 14.0), (2.0, 10.0)]);
        }
        Channel => {
            // After Effects draws this one in colour: red, green and blue, as the channels it picks
            // between. The circles keep the geometry this icon already had; only their fill changes.
            pen.dot_colored(6.0, 6.5, 3.6, Color32::from_rgb(226, 58, 58));
            pen.dot_colored(10.0, 6.5, 3.6, Color32::from_rgb(58, 200, 88));
            pen.dot_colored(8.0, 10.0, 3.6, Color32::from_rgb(64, 120, 235));
        }
        Exposure => {
            pen.circle(8.0, 8.0, 5.5);
            pen.fill(&[(8.0, 2.5), (13.5, 8.0), (8.0, 13.5)]);
        }
        Chat => {
            pen.closed(&[(2.0, 3.0), (14.0, 3.0), (14.0, 11.0), (7.0, 11.0), (4.0, 14.0), (4.5, 11.0), (2.0, 11.0)]);
            pen.dot(5.3, 7.0, 0.95);
            pen.dot(8.0, 7.0, 0.95);
            pen.dot(10.7, 7.0, 0.95);
        }
        Globe => {
            pen.circle(8.0, 8.0, 6.0);
            pen.ellipse(8.0, 8.0, 2.6, 6.0);
            pen.line(&[(2.0, 8.0), (14.0, 8.0)]);
            pen.line(&[(3.2, 4.8), (12.8, 4.8)]);
            pen.line(&[(3.2, 11.2), (12.8, 11.2)]);
        }
        Code => {
            pen.line(&[(5.0, 4.0), (1.8, 8.0), (5.0, 12.0)]);
            pen.line(&[(11.0, 4.0), (14.2, 8.0), (11.0, 12.0)]);
            pen.line(&[(9.4, 2.8), (6.6, 13.2)]);
        }
        Sparkle => {
            SPARKLE.iter().for_each(|p| pen.fill(p));
        }
        Monitor => {
            pen.rrect(1.8, 2.5, 14.2, 11.0, 1.2);
            pen.line(&[(8.0, 11.0), (8.0, 13.8)]);
            pen.line(&[(5.0, 13.8), (11.0, 13.8)]);
        }
        Sun => {
            pen.circle(8.0, 8.0, 2.8);
            for i in 0..8 {
                let a = std::f32::consts::FRAC_PI_4 * i as f32;
                let (c, s) = (a.cos(), a.sin());
                pen.line(&[(8.0 + 4.6 * c, 8.0 + 4.6 * s), (8.0 + 6.4 * c, 8.0 + 6.4 * s)]);
            }
        }
        Moon => {
            // The outer edge of a disc, and the inner edge of the disc that eclipses it.
            pen.arc(8.0, 8.0, 5.8, -20.0, 250.0);
            pen.arc(10.6, 2.4, 4.6, 52.0, 178.0);
        }
        PenAdd | PenDelete => {
            // A small pen nib with a plus or minus beside it.
            pen.closed(&[(6.0, 2.5), (9.0, 8.0), (6.0, 12.0), (3.0, 8.0)]);
            pen.line(&[(6.0, 2.5), (6.0, 7.0)]);
            pen.line(&[(10.0, 11.5), (14.5, 11.5)]);
            if icon == PenAdd {
                pen.line(&[(12.25, 9.25), (12.25, 13.75)]);
            }
        }
        PenConvert => {
            // An open caret: a corner vertex with two straight sides.
            pen.line(&[(2.5, 13.0), (8.0, 3.5), (13.5, 13.0)]);
            pen.dot(8.0, 3.5, 1.4);
        }
        MaskFeather => {
            pen.closed(&[(6.0, 2.5), (9.0, 8.0), (6.0, 12.0), (3.0, 8.0)]);
            pen.line(&[(6.0, 2.5), (6.0, 7.0)]);
            pen.line(&[(10.5, 4.0), (12.5, 6.5), (13.5, 10.0), (12.5, 13.5)]);
        }
    }
}

/// Keyframe glyph by interpolation (diamond = linear, hourglass = bezier/ease, circle = auto,
/// square = hold), split into in/out halves.
pub fn keyframe(
    painter: &Painter,
    center: Pos2,
    size: f32,
    left: effectcraft_engine::keyframe::KeyHalf,
    right: effectcraft_engine::keyframe::KeyHalf,
    fill: Color32,
) {
    painter.extend(keyframe_shapes(center, size, left, right, fill));
}

/// The screen rect a [`keyframe`] glyph covers, read off the shapes it draws.
pub fn keyframe_bounds(center: Pos2, size: f32, left: effectcraft_engine::keyframe::KeyHalf, right: effectcraft_engine::keyframe::KeyHalf) -> Rect {
    // Any opaque fill: a transparent shape would report no bounds.
    egui::Shape::Vec(keyframe_shapes(center, size, left, right, Color32::WHITE)).visual_bounding_rect()
}

/// The halves of a [`keyframe`] glyph. Flat, without a dark outline (as in After Effects), so
/// keys that overlap read as one shape instead of a tangle of borders (#284); the hairline in
/// the fill colour closes the seam between the halves.
fn keyframe_shapes(
    center: Pos2,
    size: f32,
    left: effectcraft_engine::keyframe::KeyHalf,
    right: effectcraft_engine::keyframe::KeyHalf,
    fill: Color32,
) -> Vec<egui::Shape> {
    use effectcraft_engine::keyframe::KeyHalf as H;
    let r = size / 2.0;
    let mut out = Vec::with_capacity(2);
    let mut half = |pts: Vec<Pos2>| out.push(PathShape::convex_polygon(pts, fill, Stroke::new(0.5, fill)).into());
    let c = center;
    for (side, h) in [(-1.0f32, left), (1.0, right)] {
        match h {
            H::Linear => half(if side < 0.0 {
                vec![c + vec2(0.0, -r), c, c + vec2(0.0, r), c + vec2(-r, 0.0)]
            } else {
                vec![c + vec2(0.0, -r), c + vec2(r, 0.0), c + vec2(0.0, r), c]
            }),
            H::Hold => half(if side < 0.0 {
                vec![c + vec2(-r * 0.8, -r * 0.8), c + vec2(0.0, -r * 0.8), c + vec2(0.0, r * 0.8), c + vec2(-r * 0.8, r * 0.8)]
            } else {
                vec![c + vec2(0.0, -r * 0.8), c + vec2(r * 0.8, -r * 0.8), c + vec2(r * 0.8, r * 0.8), c + vec2(0.0, r * 0.8)]
            }),
            H::Bezier => half(if side < 0.0 {
                vec![c + vec2(-r * 0.75, -r), c + vec2(0.0, -r * 0.35), c + vec2(0.0, r * 0.35), c + vec2(-r * 0.75, r)]
            } else {
                vec![c + vec2(0.0, -r * 0.35), c + vec2(r * 0.75, -r), c + vec2(r * 0.75, r), c + vec2(0.0, r * 0.35)]
            }),
            H::Auto => {
                let n = 10;
                // Simple half-disc.
                let mut hp = vec![c + vec2(0.0, -r * 0.8), c + vec2(0.0, r * 0.8)];
                for i in 1..n {
                    let t = std::f32::consts::PI * i as f32 / n as f32;
                    hp.push(c + vec2(side * r * 0.8 * t.sin(), r * 0.8 * t.cos()));
                }
                half(hp);
            }
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use effectcraft_engine::keyframe::KeyHalf as H;

    /// The Selection arrow (and the Speaker and Sparkle) are concave; filled as one polygon they
    /// rendered as a broken fan. Each is drawn as convex pieces instead.
    #[test]
    fn concave_glyphs_are_filled_as_convex_pieces() {
        for piece in super::CONCAVE.iter().flat_map(|g| g.iter()) {
            let n = piece.len();
            let turns: Vec<f32> = (0..n)
                .map(|i| {
                    let (a, b, c) = (piece[i], piece[(i + 1) % n], piece[(i + 2) % n]);
                    (b.0 - a.0) * (c.1 - b.1) - (b.1 - a.1) * (c.0 - b.0)
                })
                .collect();
            assert!(turns.iter().all(|t| *t < 0.0) || turns.iter().all(|t| *t > 0.0), "{piece:?} is not convex: {turns:?}");
        }
    }

    /// Each half reaches as far as it is drawn, plus the hairline around it, and the halves are
    /// independent (a key can come in linear and go out on hold).
    #[test]
    fn keyframe_bounds_follow_each_half() {
        let c = egui::pos2(100.0, 50.0);
        let r = 5.5;
        // (half, how far it reaches sideways, how far up and down)
        let extents = [(H::Linear, r, r), (H::Hold, r * 0.8, r * 0.8), (H::Bezier, r * 0.75, r), (H::Auto, r * 0.8, r * 0.8)];
        for (l, lx, ly) in extents {
            for (rt, rx, ry) in extents {
                let b = super::keyframe_bounds(c, 11.0, l, rt);
                let want = egui::Rect::from_min_max(c - egui::vec2(lx, ly.max(ry)), c + egui::vec2(rx, ly.max(ry))).expand(0.25);
                assert!(b.min.distance(want.min) < 1e-3 && b.max.distance(want.max) < 1e-3, "{l:?}/{rt:?}: {b:?}, want {want:?}");
            }
        }
    }

    /// #284: keyframes have no dark border (overlapping keys looked messy): each half is filled
    /// and edged in the key's own colour.
    #[test]
    fn keyframes_are_drawn_without_a_dark_outline() {
        let fill = egui::Color32::from_gray(0xc0);
        for (l, r) in [(H::Linear, H::Linear), (H::Bezier, H::Hold), (H::Auto, H::Linear)] {
            let shapes = super::keyframe_shapes(egui::pos2(10.0, 10.0), 11.0, l, r, fill);
            assert_eq!(shapes.len(), 2);
            for s in shapes {
                let egui::Shape::Path(p) = s else { panic!("a path: {s:?}") };
                assert_eq!(p.fill, fill);
                assert!(p.stroke.width <= 0.5 && p.stroke.color == egui::epaint::ColorMode::Solid(fill), "{:?}", p.stroke);
            }
        }
    }
}