Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include more things in prelude
Browse files Browse the repository at this point in the history
exa04 committed May 6, 2024
1 parent f62bde1 commit 683d4f8
Showing 1 changed file with 76 additions and 35 deletions.
111 changes: 76 additions & 35 deletions examples/peak_graph/src/editor.rs
Original file line number Diff line number Diff line change
@@ -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<Mutex<PeakBuffer>>,
range: (f32, f32),
}

impl Data {
pub(crate) fn new(peak_buffer: Arc<Mutex<PeakBuffer>>) -> 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> {
ViziaState::new(|| (800, 500))
@@ -28,44 +48,65 @@ pub(crate) fn create(editor_data: Data, editor_state: Arc<ViziaState>) -> 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));
})
}

0 comments on commit 683d4f8

Please sign in to comment.