2022-03-16 03:06:47 +11:00
|
|
|
use atomic_float::AtomicF32;
|
|
|
|
use nih_plug::prelude::{util, Editor, GuiContext};
|
2022-03-15 00:16:47 +11:00
|
|
|
use nih_plug_iced::widgets as nih_widgets;
|
2022-03-14 07:59:13 +11:00
|
|
|
use nih_plug_iced::*;
|
|
|
|
use std::sync::Arc;
|
2022-03-16 03:06:47 +11:00
|
|
|
use std::time::Duration;
|
2022-03-14 07:59:13 +11:00
|
|
|
|
|
|
|
use crate::GainParams;
|
|
|
|
|
|
|
|
// Makes sense to also define this here, makes it a bit easier to keep track of
|
|
|
|
pub(crate) fn default_state() -> Arc<IcedState> {
|
|
|
|
IcedState::from_size(200, 150)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn create(
|
2022-04-07 23:31:46 +10:00
|
|
|
params: Arc<GainParams>,
|
2022-03-16 03:06:47 +11:00
|
|
|
peak_meter: Arc<AtomicF32>,
|
2022-03-14 07:59:13 +11:00
|
|
|
editor_state: Arc<IcedState>,
|
|
|
|
) -> Option<Box<dyn Editor>> {
|
2022-03-16 03:06:47 +11:00
|
|
|
create_iced_editor::<GainEditor>(editor_state, (params, peak_meter))
|
2022-03-14 07:59:13 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
struct GainEditor {
|
2022-04-07 23:31:46 +10:00
|
|
|
params: Arc<GainParams>,
|
2022-03-14 07:59:13 +11:00
|
|
|
context: Arc<dyn GuiContext>,
|
|
|
|
|
2022-03-16 03:06:47 +11:00
|
|
|
peak_meter: Arc<AtomicF32>,
|
|
|
|
|
2022-03-15 00:16:47 +11:00
|
|
|
gain_slider_state: nih_widgets::param_slider::State,
|
2022-03-16 03:06:47 +11:00
|
|
|
peak_meter_state: nih_widgets::peak_meter::State,
|
2022-03-14 07:59:13 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
enum Message {
|
|
|
|
/// Update a parameter's value.
|
2022-03-15 00:16:47 +11:00
|
|
|
ParamUpdate(nih_widgets::ParamMessage),
|
2022-03-14 07:59:13 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IcedEditor for GainEditor {
|
|
|
|
type Executor = executor::Default;
|
|
|
|
type Message = Message;
|
2022-04-07 23:31:46 +10:00
|
|
|
type InitializationFlags = (Arc<GainParams>, Arc<AtomicF32>);
|
2022-03-14 07:59:13 +11:00
|
|
|
|
|
|
|
fn new(
|
2022-03-16 03:06:47 +11:00
|
|
|
(params, peak_meter): Self::InitializationFlags,
|
2022-03-14 07:59:13 +11:00
|
|
|
context: Arc<dyn GuiContext>,
|
|
|
|
) -> (Self, Command<Self::Message>) {
|
|
|
|
let editor = GainEditor {
|
|
|
|
params,
|
|
|
|
context,
|
2022-03-15 00:16:47 +11:00
|
|
|
|
2022-03-16 03:06:47 +11:00
|
|
|
peak_meter,
|
|
|
|
|
2022-03-15 00:16:47 +11:00
|
|
|
gain_slider_state: Default::default(),
|
2022-03-16 03:06:47 +11:00
|
|
|
peak_meter_state: Default::default(),
|
2022-03-14 07:59:13 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
(editor, Command::none())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn context(&self) -> &dyn GuiContext {
|
|
|
|
self.context.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(
|
|
|
|
&mut self,
|
|
|
|
_window: &mut WindowQueue,
|
|
|
|
message: Self::Message,
|
|
|
|
) -> Command<Self::Message> {
|
|
|
|
match message {
|
|
|
|
Message::ParamUpdate(message) => self.handle_param_message(message),
|
|
|
|
}
|
|
|
|
|
|
|
|
Command::none()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&mut self) -> Element<'_, Self::Message> {
|
|
|
|
Column::new()
|
|
|
|
.align_items(Alignment::Center)
|
|
|
|
.push(
|
|
|
|
Text::new("Gain GUI")
|
2022-03-15 02:05:39 +11:00
|
|
|
.font(assets::NOTO_SANS_LIGHT)
|
2022-03-14 07:59:13 +11:00
|
|
|
.size(40)
|
2022-03-14 10:53:22 +11:00
|
|
|
.height(50.into())
|
2022-03-14 07:59:13 +11:00
|
|
|
.width(Length::Fill)
|
|
|
|
.horizontal_alignment(alignment::Horizontal::Center)
|
|
|
|
.vertical_alignment(alignment::Vertical::Bottom),
|
|
|
|
)
|
|
|
|
.push(
|
|
|
|
Text::new("Gain")
|
2022-03-14 10:53:22 +11:00
|
|
|
.height(20.into())
|
2022-03-14 07:59:13 +11:00
|
|
|
.width(Length::Fill)
|
|
|
|
.horizontal_alignment(alignment::Horizontal::Center)
|
|
|
|
.vertical_alignment(alignment::Vertical::Center),
|
|
|
|
)
|
|
|
|
.push(
|
2022-03-21 23:09:51 +11:00
|
|
|
nih_widgets::ParamSlider::new(&mut self.gain_slider_state, &self.params.gain)
|
|
|
|
.map(Message::ParamUpdate),
|
2022-03-14 07:59:13 +11:00
|
|
|
)
|
|
|
|
.push(Space::with_height(10.into()))
|
|
|
|
.push(
|
2022-03-16 03:06:47 +11:00
|
|
|
nih_widgets::PeakMeter::new(
|
|
|
|
&mut self.peak_meter_state,
|
|
|
|
util::gain_to_db(self.peak_meter.load(std::sync::atomic::Ordering::Relaxed)),
|
|
|
|
)
|
|
|
|
.hold_time(Duration::from_millis(600)),
|
2022-03-14 07:59:13 +11:00
|
|
|
)
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn background_color(&self) -> nih_plug_iced::Color {
|
|
|
|
nih_plug_iced::Color {
|
|
|
|
r: 0.98,
|
|
|
|
g: 0.98,
|
|
|
|
b: 0.98,
|
|
|
|
a: 1.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|