td-rs icon indicating copy to clipboard operation
td-rs copied to clipboard

Question/request for Example

Open CD10h opened this issue 1 year ago • 1 comments

Could you provide a plugin example with dynamic creation/updates of parameters without using derive(Params)? Let's say, for example, I get possible options from a file or another lib and would like to create/remove based on its contents or, as another example, enabling/disabling certain UI options or menus based on the state of another.

note: this is a great project, thank you for providing this. btw I have a branch on my fork with ntsc-rs as a quick and dirty plugin.

CD10h avatar May 01 '24 09:05 CD10h

@CD10h Thanks for using the project!

If you want to see what code is generated by #[derive(Params)], you might try running cargo expand on an example plugin. Basically, we implement a trait for a variety of types that know how to register themselves with the ParameterManager and fetch themselves from op inputs. If you are having issues with using the derive proc macro, please let me know, it may be the case that we can improve this for your types.

But, if you want to implement it manually, you basically just need to implement OperatorParams for your params struct. The register method will be called once on app initialization, in the setupParameters lifecycle method on the C++ side. The update method is called by the framework right before calling your execute method. ParameterManager's API basically matches the C++ version.

struct MyParams {
  my_float: f64,
}


impl OperatorParams for MyParams {
    fn register(&mut self, parameter_manager: &mut ParameterManager) {
        parameter_manager.append_float(NumericParameter {
            name: "Myfloat".to_string(),
            label: "My Float".to_string(),
            page: "Custom".to_string(),
            default_values: [0.0; 4],
            min_values: [0.0; 4],
            max_values: [1.0; 4],
            clamp_mins: [true; 4],
            clamp_maxes: [true; 4],
            min_sliders: [0.0; 4],
            max_sliders: [5.0; 4],
        });
    }

    fn update(&mut self, inputs: &ParamInputs) {
        self.my_float = inputs.get_float("Myfloat", 0);
    }
}

These should be roughly comparable to the C++ examples, and I'd look there for further documentation about how to use the parameter manager and read inputs back.

Then, if you wanted to initialize your param struct through something like reading a config, etc, just do that in impl OpNew constructor.

For enabling and disabling params, see the example here: https://github.com/tychedelia/td-rs/blob/4fddc52be2f07f6d523982e46b8a927b626042db/plugins/chop/generator/src/lib.rs#L62-L63. It would definitely be cool to support doing this automatically in the proc macro, but right now it should be handled imperatively in your execute impl.

Please let me know if you have any other questions!

tychedelia avatar May 01 '24 19:05 tychedelia