//! Widgets and utilities for making widgets to integrate VIZIA with NIH-plug. //! //! # Note //! //! None of these widgets are finalized, and their sizes or looks can change at any point. Feel free //! to copy the widgets and modify them to your personal taste. use nih_plug::{context::GuiContext, param::internals::ParamPtr}; use std::sync::Arc; use vizia::Model; /// An event that updates a parameter's value. Since NIH-plug manages the parameters, interacting /// with parameter values with VIZIA works a little different from updating any other state. These /// events are automatically handled by `nih_plug_vizia`. #[derive(Debug, Clone, Copy)] pub enum ParamEvent { /// Begin an automation gesture for a parameter. BeginSetParameter(ParamPtr), /// Set a parameter to a new normalized value. This needs to be surrounded by a matching /// `BeginSetParameter` and `EndSetParameter`. SetParameterNormalized(ParamPtr, f32), /// Reset a parameter to its default value. This needs to be surrounded by a matching /// `BeginSetParameter` and `EndSetParameter`. ResetParameter(ParamPtr), /// End an automation gesture for a parameter. EndSetParameter(ParamPtr), } /// Handles parameter updates for VIZIA GUIs. Registered in /// [`ViziaEditor::spawn()`][super::ViziaEditor::spawn()]. pub(crate) struct ParamModel { pub context: Arc, } impl Model for ParamModel { fn event(&mut self, _cx: &mut vizia::Context, event: &mut vizia::Event) { if let Some(param_event) = event.message.downcast() { match *param_event { ParamEvent::BeginSetParameter(p) => unsafe { self.context.raw_begin_set_parameter(p) }, ParamEvent::SetParameterNormalized(p, v) => unsafe { self.context.raw_set_parameter_normalized(p, v) }, ParamEvent::ResetParameter(p) => unsafe { let default_value = self.context.raw_default_normalized_param_value(p); self.context.raw_set_parameter_normalized(p, default_value); }, ParamEvent::EndSetParameter(p) => unsafe { self.context.raw_end_set_parameter(p) }, } } } }