From 74389c01ef403782ef56bfb78d5b05f484de2880 Mon Sep 17 00:00:00 2001 From: Bart Brouns Date: Wed, 24 Apr 2024 13:18:38 +0200 Subject: [PATCH] make left and right switchable on graph --- src/editor.rs | 131 +++++++++++++++++++++++++++++--------------------- src/lib.rs | 20 +++----- src/params.rs | 14 +----- 3 files changed, 85 insertions(+), 80 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index cfd66bc..a427899 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -24,6 +24,8 @@ struct LambData { level_buffer_r: Arc>, gr_buffer_l: Arc>, gr_buffer_r: Arc>, + show_left: bool, + show_right: bool, } impl LambData { @@ -33,6 +35,8 @@ impl LambData { level_buffer_r: Arc>, gr_buffer_l: Arc>, gr_buffer_r: Arc>, + show_left: bool, + show_right: bool, ) -> Self { Self { params, @@ -40,11 +44,23 @@ impl LambData { level_buffer_r, gr_buffer_l, gr_buffer_r, + show_left, + show_right, } } } - -impl Model for LambData {} +pub enum AppEvent { + ShowLeft, + ShowRight, +} +impl Model for LambData { + fn event(&mut self, cx: &mut EventContext, event: &mut Event) { + event.map(|app_event, meta| match app_event { + AppEvent::ShowLeft => self.show_left = self.params.show_left.value(), + AppEvent::ShowRight => self.show_right = self.params.show_right.value(), + }); + } +} // Makes sense to also define this here, makes it a bit easier to keep track of pub(crate) fn default_state() -> Arc { @@ -59,6 +75,8 @@ pub(crate) fn create( level_buffer_r: Arc>, gr_buffer_l: Arc>, gr_buffer_r: Arc>, + show_left: bool, + show_right: bool, editor_state: Arc, ) -> Option> { create_vizia_editor(editor_state, ViziaTheming::Custom, move |cx, _| { @@ -75,6 +93,8 @@ pub(crate) fn create( level_buffer_r: level_buffer_r.clone(), gr_buffer_l: gr_buffer_l.clone(), gr_buffer_r: gr_buffer_r.clone(), + show_left: true, + show_right: true, } .build(cx); @@ -203,17 +223,21 @@ pub(crate) fn create( .bottom(Pixels(6.0)); HStack::new(cx, |cx| { ParamButton::new(cx, LambData::params, |params| ¶ms.show_left) + .on_press(|ex| ex.emit(AppEvent::ShowLeft)) + .disable_scroll_wheel() + // .on_scroll(|ex| ex.emit(AppEvent::ShowLeft)) .with_label("left") .width(Stretch(1.0)) .bottom(Pixels(6.0)); ParamButton::new(cx, LambData::params, |params| ¶ms.show_right) + .on_press(|ex| ex.emit(AppEvent::ShowRight)) + .disable_scroll_wheel() + // .on_scroll(|ex| ex.emit(AppEvent::ShowRight)) .with_label("right") .width(Stretch(1.0)) .bottom(Pixels(6.0)); }) - .width(Stretch(1.0)) - // .width(Percentage(100.0)) - ; + .width(Stretch(1.0)); }) .width(Percentage(100.0)) .col_between(Percentage(5.0)); @@ -225,13 +249,13 @@ pub(crate) fn create( .col_between(Percentage(2.5)) .height(Auto) .width(Percentage(100.0)); - peak_graph(cx, params.show_left.value(), params.show_right.value()); + peak_graph(cx); }) // everything - .width(Percentage(95.0)) - .height(Auto) - .left(Percentage(2.5)) - .right(Percentage(2.5)) - .class("center"); + .width(Percentage(95.0)) + .height(Auto) + .left(Percentage(2.5)) + .right(Percentage(2.5)) + .class("center"); ResizeHandle::new(cx); }) } @@ -398,7 +422,7 @@ impl>> View } /// Draws a peak graph with a grid backdrop, unit ruler, and a peak meter to side. -fn peak_graph(cx: &mut Context, show_left: bool, show_right: bool) { +fn peak_graph(cx: &mut Context) { HStack::new(cx, |cx| { ZStack::new(cx, |cx| { Grid::new( @@ -410,51 +434,50 @@ fn peak_graph(cx: &mut Context, show_left: bool, show_right: bool) { ) .color(Color::rgba(160, 160, 160, 60)); - if show_left { - println!("left"); - // level - Graph::new( - cx, - LambData::level_buffer_l, - (METER_MIN, METER_MAX), - ValueScaling::Decibels, - ) - .color(Color::rgba(0, 0, 255, 30)) - .background_color(Color::rgba(0, 0, 0, 40)); + // level + Graph::new( + cx, + LambData::level_buffer_l, + (METER_MIN, METER_MAX), + ValueScaling::Decibels, + ) + .visibility(LambData::show_left) + .color(Color::rgba(0, 0, 255, 30)) + .background_color(Color::rgba(0, 0, 0, 40)); - // gain reduction - Graph::new( - cx, - LambData::gr_buffer_l, - (METER_MIN, METER_MAX), - ValueScaling::Decibels, - ) - .color(Color::rgba(0, 0, 255, 255)) - .background_color(Color::rgba(250, 250, 250, 40)) - .fill_from(0.0); - }; - if show_right { - println!("right"); - // level - Graph::new( - cx, - LambData::level_buffer_r, - (METER_MIN, METER_MAX), - ValueScaling::Decibels, - ) + // level + Graph::new( + cx, + LambData::level_buffer_r, + (METER_MIN, METER_MAX), + ValueScaling::Decibels, + ) + .visibility(LambData::show_right) .color(Color::rgba(255, 0, 0, 30)) .background_color(Color::rgba(0, 0, 0, 40)); - // gain reduction - Graph::new( - cx, - LambData::gr_buffer_r, - (METER_MIN, METER_MAX), - ValueScaling::Decibels, - ) - .color(Color::rgba(255, 0, 0, 255)) - .background_color(Color::rgba(250, 250, 250, 40)) - .fill_from(0.0); - }; + // gain reduction + Graph::new( + cx, + LambData::gr_buffer_l, + (METER_MIN, METER_MAX), + ValueScaling::Decibels, + ) + .visibility(LambData::show_left) + .color(Color::rgba(0, 0, 255, 255)) + .background_color(Color::rgba(250, 250, 250, 40)) + .fill_from(0.0); + // gain reduction + Graph::new( + cx, + LambData::gr_buffer_r, + (METER_MIN, METER_MAX), + ValueScaling::Decibels, + ) + .visibility(LambData::show_right) + .color(Color::rgba(255, 0, 0, 255)) + .background_color(Color::rgba(250, 250, 250, 40)) + .fill_from(0.0); + // }; }); ZStack::new(cx, |cx| { diff --git a/src/lib.rs b/src/lib.rs index 2948dfd..1760686 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,23 +32,17 @@ pub struct Lamb { level_buffer_r: Arc>, gr_buffer_l: Arc>, gr_buffer_r: Arc>, + show_left: bool, + show_right: bool, /// If this is set at the start of the processing cycle, then the graph duration should be updated. should_update_time_scale: Arc, - should_update_show_left: Arc, - should_update_show_right: Arc, } impl Default for Lamb { fn default() -> Self { let should_update_time_scale = Arc::new(AtomicBool::new(false)); - let should_update_show_left = Arc::new(AtomicBool::new(false)); - let should_update_show_right = Arc::new(AtomicBool::new(false)); Self { - params: Arc::new(LambParams::new( - should_update_time_scale.clone(), - should_update_show_left.clone(), - should_update_show_right.clone(), - )), + params: Arc::new(LambParams::new(should_update_time_scale.clone())), // params: Arc::new(LambParams::default()), dsp: dsp::LambRs::default_boxed(), @@ -63,9 +57,9 @@ impl Default for Lamb { level_buffer_r: Arc::new(Mutex::new(PeakBuffer::new(1114, 7.0, 0.0))), gr_buffer_l: Arc::new(Mutex::new(MinimaBuffer::new(1114, 7.0, 0.0))), gr_buffer_r: Arc::new(Mutex::new(MinimaBuffer::new(1114, 7.0, 0.0))), + show_left: true, + show_right: true, should_update_time_scale, - should_update_show_left, - should_update_show_right, } } } @@ -161,8 +155,6 @@ impl Plugin for Lamb { // Reset buffers and envelopes here. This can be called from the audio thread and may not // allocate. You can remove this function if you do not need it. self.should_update_time_scale.store(true, Ordering::Release); - self.should_update_show_left.store(true, Ordering::Release); - self.should_update_show_right.store(true, Ordering::Release); } fn editor(&mut self, _async_executor: AsyncExecutor) -> Option> { @@ -172,6 +164,8 @@ impl Plugin for Lamb { self.level_buffer_r.clone(), self.gr_buffer_l.clone(), self.gr_buffer_r.clone(), + self.show_left.clone(), + self.show_right.clone(), self.params.editor_state.clone(), ) } diff --git a/src/params.rs b/src/params.rs index 8d6e021..f4a585f 100644 --- a/src/params.rs +++ b/src/params.rs @@ -201,11 +201,7 @@ pub fn s2v_bool_in_out() -> Arc Option + Send + Sync> { } impl LambParams { - pub fn new( - should_update_time_scale: Arc, - should_update_show_left: Arc, - should_update_show_right: Arc, - ) -> Self { + pub fn new(should_update_time_scale: Arc) -> Self { Self { editor_state: editor::default_state(), @@ -357,17 +353,9 @@ impl LambParams { .hide() .hide_in_generic_ui(), show_left: BoolParam::new("show_left", true) - .with_callback({ - let should_update_show_left = should_update_show_left.clone(); - Arc::new(move |_| should_update_show_left.store(true, Ordering::Release)) - }) .hide() .hide_in_generic_ui(), show_right: BoolParam::new("show_right", true) - .with_callback({ - let should_update_show_right = should_update_show_right.clone(); - Arc::new(move |_| should_update_show_right.store(true, Ordering::Release)) - }) .hide() .hide_in_generic_ui(), }