From 7acebd229956de0fb7c374f05d452a3be9ac7b35 Mon Sep 17 00:00:00 2001 From: luna <223230@proton.me> Date: Mon, 6 May 2024 22:07:59 +0200 Subject: [PATCH] Add scaling to `RangeModifiers` to allow binding the scaling --- src/visualizers/graph.rs | 29 ++++++++++++++++++----------- src/visualizers/grid.rs | 18 +++++++++++++----- src/visualizers/meter.rs | 12 ++++++++++++ src/visualizers/mod.rs | 8 ++++++++ src/visualizers/oscilloscope.rs | 14 +++++++++++++- 5 files changed, 64 insertions(+), 17 deletions(-) diff --git a/src/visualizers/graph.rs b/src/visualizers/graph.rs index 7b337c9..4ac2d32 100644 --- a/src/visualizers/graph.rs +++ b/src/visualizers/graph.rs @@ -35,6 +35,7 @@ where enum GraphEvents { UpdateRange((f32, f32)), + UpdateScaling(ValueScaling), } impl Graph @@ -48,15 +49,15 @@ where range: impl Res<(f32, f32)> + Clone, scaling: impl Res + Clone, ) -> Handle { - let r = range.get_val(cx); Self { buffer, - range: r, + range: range.get_val(cx), scaling: scaling.get_val(cx), fill_from: FillFrom::Bottom, } .build(cx, |_| {}) .range(range) + .scaling(scaling) } } @@ -68,6 +69,12 @@ where fn element(&self) -> Option<&'static str> { Some("graph") } + fn event(&mut self, _cx: &mut EventContext, event: &mut Event) { + event.map(|e, _| match e { + GraphEvents::UpdateRange(v) => self.range = *v, + GraphEvents::UpdateScaling(s) => self.scaling = *s, + }); + } fn draw(&self, cx: &mut DrawContext, canvas: &mut Canvas) { let bounds = cx.bounds(); @@ -121,15 +128,6 @@ where &vg::Paint::color(cx.font_color().into()).with_line_width(line_width), ); } - fn event( - &mut self, - _cx: &mut nih_plug_vizia::vizia::context::EventContext, - event: &mut nih_plug_vizia::vizia::events::Event, - ) { - event.map(|e, _| match e { - GraphEvents::UpdateRange(v) => self.range = *v, - }); - } } impl<'a, L, I> FillModifiers for Handle<'a, Graph> @@ -193,6 +191,15 @@ where (*cx).emit_to(e, GraphEvents::UpdateRange(r.clone())); }); + self + } + fn scaling(mut self, scaling: impl Res) -> Self { + let e = self.entity(); + + scaling.set_or_bind(self.context(), e, move |cx, s| { + (*cx).emit_to(e, GraphEvents::UpdateScaling(s.clone())) + }); + self } } diff --git a/src/visualizers/grid.rs b/src/visualizers/grid.rs index c9b4340..3b27cf7 100644 --- a/src/visualizers/grid.rs +++ b/src/visualizers/grid.rs @@ -41,6 +41,7 @@ pub struct Grid { enum GridEvents { UpdateRange((f32, f32)), + UpdateScaling(ValueScaling), } impl Grid { @@ -59,6 +60,7 @@ impl Grid { } .build(cx, |_| {}) .range(range) + .scaling(scaling) } } @@ -116,13 +118,10 @@ impl View for Grid { &vg::Paint::color(cx.font_color().into()).with_line_width(line_width), ); } - fn event( - &mut self, - _cx: &mut nih_plug_vizia::vizia::context::EventContext, - event: &mut nih_plug_vizia::vizia::events::Event, - ) { + fn event(&mut self, _cx: &mut EventContext, event: &mut Event) { event.map(|e, _| match e { GridEvents::UpdateRange(v) => self.range = *v, + GridEvents::UpdateScaling(v) => self.scaling = *v, }); } } @@ -135,6 +134,15 @@ impl<'a> RangeModifiers for Handle<'a, Grid> { (*cx).emit_to(e, GridEvents::UpdateRange(r.clone())); }); + self + } + fn scaling(mut self, scaling: impl Res) -> Self { + let e = self.entity(); + + scaling.set_or_bind(self.context(), e, move |cx, s| { + (*cx).emit_to(e, GridEvents::UpdateScaling(s)); + }); + self } } diff --git a/src/visualizers/meter.rs b/src/visualizers/meter.rs index ed2d1fb..1f27636 100644 --- a/src/visualizers/meter.rs +++ b/src/visualizers/meter.rs @@ -57,11 +57,13 @@ where } .build(cx, |_| {}) .range(range) + .scaling(scaling) } } enum MeterEvents { UpdateRange((f32, f32)), + UpdateScaling(ValueScaling), } impl View for Meter @@ -144,6 +146,7 @@ where fn event(&mut self, _cx: &mut EventContext, event: &mut Event) { event.map(|e, _| match e { MeterEvents::UpdateRange(v) => self.range = *v, + MeterEvents::UpdateScaling(v) => self.scaling = *v, }); } } @@ -209,6 +212,15 @@ where (*cx).emit_to(e, MeterEvents::UpdateRange(r)); }); + self + } + fn scaling(mut self, scaling: impl Res) -> Self { + let e = self.entity(); + + scaling.set_or_bind(self.context(), e, move |cx, s| { + (*cx).emit_to(e, MeterEvents::UpdateScaling(s)); + }); + self } } diff --git a/src/visualizers/mod.rs b/src/visualizers/mod.rs index 872525a..58af505 100644 --- a/src/visualizers/mod.rs +++ b/src/visualizers/mod.rs @@ -18,10 +18,18 @@ pub use spectrum_analyzer::*; pub use unit_ruler::*; pub use waveform::*; +use super::utils::ValueScaling; use nih_plug_vizia::vizia::binding::Res; pub trait RangeModifiers { + /// Sets the minimum and maximum values that can be displayed by the view + /// + /// The values are relative to the scaling - e.g. for peak volume information, + /// `(-48., 6.)` would be -48 to +6 dB when the scaling is set to + /// [`ValueScaling::Decibels`] fn range(self, range: impl Res<(f32, f32)>) -> Self; + /// Specifies what scaling the view should use + fn scaling(self, scaling: impl Res) -> Self; } pub(crate) enum FillFrom { diff --git a/src/visualizers/oscilloscope.rs b/src/visualizers/oscilloscope.rs index 1a2f477..1462962 100644 --- a/src/visualizers/oscilloscope.rs +++ b/src/visualizers/oscilloscope.rs @@ -36,6 +36,7 @@ where enum OscilloscopeEvents { UpdateRange((f32, f32)), + UpdateScaling(ValueScaling), } impl Oscilloscope @@ -61,6 +62,7 @@ where } .build(cx, |_| {}) .range(range) + .scaling(scaling) } } @@ -130,6 +132,7 @@ where fn event(&mut self, cx: &mut EventContext, event: &mut Event) { event.map(|e, _| match e { OscilloscopeEvents::UpdateRange(v) => self.range = *v, + OscilloscopeEvents::UpdateScaling(v) => self.scaling = *v, }); } } @@ -142,7 +145,16 @@ where let e = self.entity(); range.set_or_bind(self.context(), e, move |cx, r| { - (*cx).emit_to(e, OscilloscopeEvents::UpdateRange(r.clone())); + (*cx).emit_to(e, OscilloscopeEvents::UpdateRange(r)); + }); + + self + } + fn scaling(mut self, scaling: impl Res) -> Self { + let e = self.entity(); + + scaling.set_or_bind(self.context(), e, move |cx, s| { + (*cx).emit_to(e, OscilloscopeEvents::UpdateScaling(s)); }); self