2022-03-19 01:26:19 +11:00
|
|
|
//! 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.
|
|
|
|
|
2022-03-19 01:52:22 +11:00
|
|
|
use nih_plug::param::internals::ParamPtr;
|
|
|
|
use nih_plug::prelude::{GuiContext, Param};
|
2022-03-19 01:26:19 +11:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-03-19 05:12:38 +11:00
|
|
|
use vizia::{Context, Model};
|
|
|
|
|
2022-03-23 03:06:54 +11:00
|
|
|
mod generic_ui;
|
2022-03-19 11:17:13 +11:00
|
|
|
mod param_slider;
|
2022-03-22 08:28:54 +11:00
|
|
|
mod peak_meter;
|
2022-03-19 10:38:26 +11:00
|
|
|
pub mod util;
|
|
|
|
|
2022-03-23 04:50:50 +11:00
|
|
|
pub use generic_ui::GenericUi;
|
2022-03-20 04:49:49 +11:00
|
|
|
pub use param_slider::{ParamSlider, ParamSliderExt, ParamSliderStyle};
|
2022-03-22 08:28:54 +11:00
|
|
|
pub use peak_meter::PeakMeter;
|
2022-03-19 11:17:13 +11:00
|
|
|
|
2022-03-19 05:12:38 +11:00
|
|
|
/// Register the default theme for the widgets exported by this module. This is automatically called
|
|
|
|
/// for you when using [`create_vizia_editor()`][super::create_vizia_editor()].
|
|
|
|
pub fn register_theme(cx: &mut Context) {
|
2022-03-23 03:33:59 +11:00
|
|
|
cx.add_theme(include_str!("../assets/widgets.css"));
|
2022-03-19 05:12:38 +11:00
|
|
|
}
|
2022-03-19 01:26:19 +11:00
|
|
|
|
|
|
|
/// 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`.
|
2022-03-19 01:52:22 +11:00
|
|
|
///
|
|
|
|
/// Call the [`upcast()`][Self::upcast()] method to be able to emit this event through a
|
|
|
|
/// [`Context`][vizia::Context].
|
2022-03-19 01:26:19 +11:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2022-03-19 01:52:22 +11:00
|
|
|
pub enum ParamEvent<'a, P: Param> {
|
|
|
|
/// Begin an automation gesture for a parameter.
|
|
|
|
BeginSetParameter(&'a P),
|
|
|
|
/// Set a parameter to a new normalized value. This needs to be surrounded by a matching
|
|
|
|
/// `BeginSetParameter` and `EndSetParameter`.
|
|
|
|
SetParameter(&'a P, P::Plain),
|
|
|
|
/// Set a parameter to a new normalized value. This needs to be surrounded by a matching
|
|
|
|
/// `BeginSetParameter` and `EndSetParameter`.
|
|
|
|
SetParameterNormalized(&'a P, f32),
|
|
|
|
/// End an automation gesture for a parameter.
|
|
|
|
EndSetParameter(&'a P),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The same as [`ParamEvent`], but type erased.
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
2022-03-19 03:45:27 +11:00
|
|
|
pub enum RawParamEvent {
|
2022-03-19 01:26:19 +11:00
|
|
|
/// 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),
|
|
|
|
/// 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<dyn GuiContext>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Model for ParamModel {
|
|
|
|
fn event(&mut self, _cx: &mut vizia::Context, event: &mut vizia::Event) {
|
|
|
|
if let Some(param_event) = event.message.downcast() {
|
2022-03-19 01:52:22 +11:00
|
|
|
// `ParamEvent` gets downcast into `NormalizedParamEvent` by the `Message`
|
|
|
|
// implementation below
|
2022-03-19 01:26:19 +11:00
|
|
|
match *param_event {
|
2022-03-19 03:45:27 +11:00
|
|
|
RawParamEvent::BeginSetParameter(p) => unsafe {
|
2022-03-19 01:26:19 +11:00
|
|
|
self.context.raw_begin_set_parameter(p)
|
|
|
|
},
|
2022-03-19 03:45:27 +11:00
|
|
|
RawParamEvent::SetParameterNormalized(p, v) => unsafe {
|
2022-03-19 01:26:19 +11:00
|
|
|
self.context.raw_set_parameter_normalized(p, v)
|
|
|
|
},
|
2022-03-19 03:45:27 +11:00
|
|
|
RawParamEvent::EndSetParameter(p) => unsafe {
|
2022-03-19 01:52:22 +11:00
|
|
|
self.context.raw_end_set_parameter(p)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 03:45:27 +11:00
|
|
|
impl<P: Param> From<ParamEvent<'_, P>> for RawParamEvent {
|
2022-03-19 01:52:22 +11:00
|
|
|
fn from(event: ParamEvent<'_, P>) -> Self {
|
|
|
|
match event {
|
2022-03-19 03:45:27 +11:00
|
|
|
ParamEvent::BeginSetParameter(p) => RawParamEvent::BeginSetParameter(p.as_ptr()),
|
2022-03-19 01:52:22 +11:00
|
|
|
ParamEvent::SetParameter(p, v) => {
|
2022-03-19 03:45:27 +11:00
|
|
|
RawParamEvent::SetParameterNormalized(p.as_ptr(), p.preview_normalized(v))
|
2022-03-19 01:52:22 +11:00
|
|
|
}
|
|
|
|
ParamEvent::SetParameterNormalized(p, v) => {
|
2022-03-19 03:45:27 +11:00
|
|
|
RawParamEvent::SetParameterNormalized(p.as_ptr(), v)
|
2022-03-19 01:26:19 +11:00
|
|
|
}
|
2022-03-19 03:45:27 +11:00
|
|
|
ParamEvent::EndSetParameter(p) => RawParamEvent::EndSetParameter(p.as_ptr()),
|
2022-03-19 01:26:19 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-19 01:52:22 +11:00
|
|
|
|
|
|
|
impl<P: Param> ParamEvent<'_, P> {
|
|
|
|
/// Convert this event into a type erased version of itself that can be emitted through
|
|
|
|
/// [`Context::emit()`][vizia::Context::emit()].
|
|
|
|
///
|
|
|
|
/// TODO: Think of a better, clearer term for this
|
2022-03-19 03:45:27 +11:00
|
|
|
pub fn upcast(self) -> RawParamEvent {
|
2022-03-19 01:52:22 +11:00
|
|
|
self.into()
|
|
|
|
}
|
|
|
|
}
|