Skip to content

Refactor type system of items #154

@AzurIce

Description

@AzurIce

Note

Latest thinking: #154 (comment)

Problem

The items we used in programmatic animation engine often involve subtypes and supertypes.

For example, an Sphere is actually expressed with a Surface, while a Surface is actually a MeshItem.

In OOP languages, this is often described with inheriting, but Rust is not OOP.
So, instead, in ranim-items, this is expressed by using From/Into conversions:

graph LR
    Square --> Rectangle
    Square --> RegularPolygon
    Square --> Parallelogram
    Rectangle --> Parallelogram
    Rectangle --> Polygon
    Parallelogram --> Polygon
    RegularPolygon --> Polygon
    
    Circle --> Ellipse
    Circle --> Arc
    Circle --> EllipticArc
    Ellipse --> EllipticArc
    Arc --> EllipticArc
    ArcBetweenPoints --> Arc
    
    Polygon --> VItem
    Parallelogram --> VItem
    EllipticArc --> VItem
    Ellipse --> VItem
    Arc --> VItem
    Circle --> VItem
    Line --> VItem
    Square --> VItem
    Rectangle --> VItem
    
    VItem --> CoreVItem
    
    Sphere --> Surface
    Surface --> MeshItem
    MeshItem --> CoreMeshItem
    
    TextItem --> VecVItem
    TypstText --> VecVItem
    SvgItem --> VecVItem -.- VItem
Loading

All item types are individual, flattened structs with From/Into impls to convert from subtypes to supertypes semantically.

1. Massive boilerplate

VItems like Arc, Circle, Ellipse all share the same fields (basis, fill_rgba, stroke_rgba, stroke_width) and the same trait impls (FillColor, StrokeColor, Opacity, ShiftTransform, etc.). Each type must redeclare these fields and re-derive/re-impl the same traits, plus all the From/Into and Extract logic.

2. Missing interpolation levels

All items can only interpolate with themselves at the low-level point data (VPointVec). There is no parameter-level interpolation (e.g., interpolating radius or angle directly).

3. Tedious type conversions

Subtype → supertype requires manually implementing a large number of From/Into conversions.

4. Atomic vs. Compound items at the same level

Some items convert 1:1 into a CoreItem (e.g., VItem), while others produce multiple CoreItems via Extract (e.g., TextItem, TypstText, SvgItem). These are conceptually different levels of abstraction but are treated at the same level in the current design.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions