Skip to content

Commit

Permalink
Add scaling to RangeModifiers to allow binding the scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
exa04 committed May 6, 2024
1 parent d2676a2 commit 7acebd2
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 17 deletions.
29 changes: 18 additions & 11 deletions src/visualizers/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ where

enum GraphEvents {
UpdateRange((f32, f32)),
UpdateScaling(ValueScaling),
}

impl<L, I> Graph<L, I>
Expand All @@ -48,15 +49,15 @@ where
range: impl Res<(f32, f32)> + Clone,
scaling: impl Res<ValueScaling> + Clone,
) -> Handle<Self> {
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)
}
}

Expand All @@ -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();

Expand Down Expand Up @@ -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<L, I>>
Expand Down Expand Up @@ -193,6 +191,15 @@ where
(*cx).emit_to(e, GraphEvents::UpdateRange(r.clone()));
});

self
}
fn scaling(mut self, scaling: impl Res<ValueScaling>) -> Self {
let e = self.entity();

scaling.set_or_bind(self.context(), e, move |cx, s| {
(*cx).emit_to(e, GraphEvents::UpdateScaling(s.clone()))
});

self
}
}
18 changes: 13 additions & 5 deletions src/visualizers/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct Grid {

enum GridEvents {
UpdateRange((f32, f32)),
UpdateScaling(ValueScaling),
}

impl Grid {
Expand All @@ -59,6 +60,7 @@ impl Grid {
}
.build(cx, |_| {})
.range(range)
.scaling(scaling)
}
}

Expand Down Expand Up @@ -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,
});
}
}
Expand All @@ -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<ValueScaling>) -> Self {
let e = self.entity();

scaling.set_or_bind(self.context(), e, move |cx, s| {
(*cx).emit_to(e, GridEvents::UpdateScaling(s));
});

self
}
}
12 changes: 12 additions & 0 deletions src/visualizers/meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ where
}
.build(cx, |_| {})
.range(range)
.scaling(scaling)
}
}

enum MeterEvents {
UpdateRange((f32, f32)),
UpdateScaling(ValueScaling),
}

impl<L, I> View for Meter<L, I>
Expand Down Expand Up @@ -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,
});
}
}
Expand Down Expand Up @@ -209,6 +212,15 @@ where
(*cx).emit_to(e, MeterEvents::UpdateRange(r));
});

self
}
fn scaling(mut self, scaling: impl Res<ValueScaling>) -> Self {
let e = self.entity();

scaling.set_or_bind(self.context(), e, move |cx, s| {
(*cx).emit_to(e, MeterEvents::UpdateScaling(s));
});

self
}
}
8 changes: 8 additions & 0 deletions src/visualizers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValueScaling>) -> Self;
}

pub(crate) enum FillFrom {
Expand Down
14 changes: 13 additions & 1 deletion src/visualizers/oscilloscope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ where

enum OscilloscopeEvents {
UpdateRange((f32, f32)),
UpdateScaling(ValueScaling),
}

impl<B> Oscilloscope<B>
Expand All @@ -61,6 +62,7 @@ where
}
.build(cx, |_| {})
.range(range)
.scaling(scaling)
}
}

Expand Down Expand Up @@ -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,
});
}
}
Expand All @@ -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<ValueScaling>) -> Self {
let e = self.entity();

scaling.set_or_bind(self.context(), e, move |cx, s| {
(*cx).emit_to(e, OscilloscopeEvents::UpdateScaling(s));
});

self
Expand Down

0 comments on commit 7acebd2

Please sign in to comment.