From 683d4f8473fee2f9a168cb5428d4ac75521d5234 Mon Sep 17 00:00:00 2001 From: luna <223230@proton.me> Date: Mon, 6 May 2024 22:25:49 +0200 Subject: [PATCH] Include more things in prelude --- examples/peak_graph/src/editor.rs | 111 ++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 35 deletions(-) diff --git a/examples/peak_graph/src/editor.rs b/examples/peak_graph/src/editor.rs index 4e108d8..f171997 100644 --- a/examples/peak_graph/src/editor.rs +++ b/examples/peak_graph/src/editor.rs @@ -1,24 +1,44 @@ +use crate::editor::EditorEvents::SetMinAmplitude; use cyma::prelude::*; +use cyma::visualizers::Meter; use cyma::{ utils::PeakBuffer, visualizers::{Graph, Grid, UnitRuler}, }; use nih_plug::editor::Editor; +use nih_plug_vizia::vizia::style::Length::Value; use nih_plug_vizia::{assets, create_vizia_editor, vizia::prelude::*, ViziaState, ViziaTheming}; +use std::ops::Range; use std::sync::{Arc, Mutex}; #[derive(Lens, Clone)] pub(crate) struct Data { peak_buffer: Arc>, + range: (f32, f32), } impl Data { pub(crate) fn new(peak_buffer: Arc>) -> Self { - Self { peak_buffer } + Self { + peak_buffer, + range: (-32., 8.), + } } } -impl Model for Data {} +impl Model for Data { + fn event(&mut self, cx: &mut EventContext, event: &mut Event) { + event.map(|event, _| match event { + EditorEvents::SetMinAmplitude(min) => { + self.range.0 = *min; + } + }) + } +} + +enum EditorEvents { + SetMinAmplitude(f32), +} pub(crate) fn default_state() -> Arc { ViziaState::new(|| (800, 500)) @@ -28,44 +48,65 @@ pub(crate) fn create(editor_data: Data, editor_state: Arc) -> Option create_vizia_editor(editor_state, ViziaTheming::default(), move |cx, _| { assets::register_noto_sans_light(cx); editor_data.clone().build(cx); + VStack::new(cx, |cx| { + HStack::new(cx, |cx| { + ZStack::new(cx, |cx| { + Grid::new( + cx, + ValueScaling::Linear, + Data::range, + vec![6.0, 0.0, -6.0, -12.0, -18.0, -24.0, -30.0], + Orientation::Horizontal, + ) + .color(Color::rgb(60, 60, 60)); + Graph::new(cx, Data::peak_buffer, Data::range, ValueScaling::Decibels) + .color(Color::rgba(255, 255, 255, 160)) + .background_color(Color::rgba(255, 255, 255, 60)); + }) + .background_color(Color::rgb(16, 16, 16)); - HStack::new(cx, |cx| { - ZStack::new(cx, |cx| { - Grid::new( + Binding::new(cx, Data::range, |cx, range| { + UnitRuler::new( + cx, + range.get(cx), + ValueScaling::Linear, + vec![ + (6.0, "6db"), + (0.0, "0db"), + (-6.0, "-6db"), + (-12.0, "-12db"), + (-18.0, "-18db"), + (-24.0, "-24db"), + (-30.0, "-30db"), + ], + Orientation::Vertical, + ) + .font_size(12.) + .color(Color::rgb(160, 160, 160)) + .width(Pixels(48.)); + }); + Meter::new( cx, - ValueScaling::Linear, - (-32., 8.), - vec![6.0, 0.0, -6.0, -12.0, -18.0, -24.0, -30.0], - Orientation::Horizontal, + Data::peak_buffer, + Data::range, + ValueScaling::Decibels, + Orientation::Vertical, ) - .color(Color::rgb(60, 60, 60)); - - Graph::new(cx, Data::peak_buffer, (-32.0, 8.0), ValueScaling::Decibels) - .color(Color::rgba(255, 255, 255, 160)) - .background_color(Color::rgba(255, 255, 255, 60)); + .width(Pixels(24.0)) + .background_color(Color::rgb(60, 60, 60)) + .fill_from_max(); }) - .background_color(Color::rgb(16, 16, 16)); - - UnitRuler::new( - cx, - (-32.0, 8.0), - ValueScaling::Linear, - vec![ - (6.0, "6db"), - (0.0, "0db"), - (-6.0, "-6db"), - (-12.0, "-12db"), - (-18.0, "-18db"), - (-24.0, "-24db"), - (-30.0, "-30db"), - ], - Orientation::Vertical, - ) - .font_size(12.) - .color(Color::rgb(160, 160, 160)) - .width(Pixels(48.)); + .col_between(Pixels(8.)); + Slider::new(cx, Data::range.map(|r| r.0)) + .range(Range { + start: -72.0, + end: -6.0, + }) + .on_changing(|cx, val| { + cx.emit(SetMinAmplitude(val)); + }) + .space(Pixels(16.)); }) - .col_between(Pixels(8.)) .background_color(Color::rgb(0, 0, 0)); }) }