abey79 / vsvg

Fast and portable tools for plotter users
http://whisk.rs
MIT License
107 stars 12 forks source link

Add support for custom `enum` as sketch parameter #107

Closed abey79 closed 8 months ago

abey79 commented 8 months ago

This PR adds support for custom enum to be used as sketch parameter, via the same Widget derive macro and #[sketch_widget] helper attribute.

The generated code distinguishes two cases: 1) "simple", C-style enums (no variant contains data) 2) "complex" enums (one or more variant has named or unnamed fields)

The former are displayed as a simple label + combo box that align to the UI grid. The latter offer a hierarchical UI that displays UI for nested data.

The ui_demo.rs example is updated to showcase the change.

#[sketch_widget]
#[derive(Default)]
enum SimpleEnum {
    #[default]
    Poodle,
    Corgy,
    Dalmatian,
}

#[sketch_widget]
#[derive(Default)]
enum CustomEnum {
    Variant1 {
        #[param(slider, min = 0.0, max = 1.0)]
        some_float: f64,

        #[skip]
        incompatible: IncompatibleStruct,
    },
    Variant2(
        bool,
        #[param(slider, min = 0.0, max = 1.0)] f64,
        #[skip] IncompatibleStruct,
    ),
    #[default]
    Variant3,
}
image

Limitations