Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add FromMidPoint slider style #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions examples/gain_gui/src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use atomic_float::AtomicF32;
use nih_plug::prelude::{util, Editor};
use vizia_plug::vizia::prelude::*;
use vizia_plug::widgets::*;
use vizia_plug::{create_vizia_editor, ViziaState, ViziaTheming};
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Duration;
use vizia_plug::vizia::prelude::*;
use vizia_plug::widgets::*;
use vizia_plug::{create_vizia_editor, ViziaState, ViziaTheming};

use crate::GainParams;

Expand Down Expand Up @@ -63,6 +63,5 @@ pub(crate) fn create(
.row_between(Pixels(0.0))
.child_left(Stretch(1.0))
.child_right(Stretch(1.0));

})
}
2 changes: 1 addition & 1 deletion examples/gain_gui/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use atomic_float::AtomicF32;
use nih_plug::prelude::*;
use vizia_plug::ViziaState;
use std::sync::Arc;
use vizia_plug::ViziaState;

mod editor;

Expand Down
19 changes: 9 additions & 10 deletions src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! The [`Editor`] trait implementation for Vizia editors.


use crossbeam::atomic::AtomicCell;
use nih_plug::debug::*;
use nih_plug::prelude::{Editor, GuiContext, ParentWindowHandle};
Expand Down Expand Up @@ -49,15 +48,15 @@ impl Editor for ViziaEditor {
let mut application = Application::new(move |cx| {
// Set some default styles to match the iced integration
//if theming >= ViziaTheming::Custom {
cx.set_default_font(&[NOTO_SANS]);
if let Err(err) = cx.add_stylesheet(include_style!("src/assets/theme.css")) {
nih_error!("Failed to load stylesheet: {err:?}");
panic!();
}

// There doesn't seem to be any way to bundle styles with a widget, so we'll always
// include the style sheet for our custom widgets at context creation
widgets::register_theme(cx);
cx.set_default_font(&[NOTO_SANS]);
if let Err(err) = cx.add_stylesheet(include_style!("src/assets/theme.css")) {
nih_error!("Failed to load stylesheet: {err:?}");
panic!();
}

// There doesn't seem to be any way to bundle styles with a widget, so we'll always
// include the style sheet for our custom widgets at context creation
widgets::register_theme(cx);
//}

// Any widget can change the parameters by emitting `ParamEvent` events. This model will
Expand Down
15 changes: 7 additions & 8 deletions src/widgets/param_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,13 @@ impl ParamWidgetBase {
// avoid this normalized->plain->normalized conversion for parameters that don't need
// it
let plain_value = unsafe { self.param_ptr.preview_plain(normalized_value) };

// For the aforementioned snapping
let normalized_plain_value = unsafe { self.param_ptr.preview_normalized(plain_value) };
cx.emit(RawParamEvent::SetParameterNormalized(
self.param_ptr,
normalized_plain_value,
));


// For the aforementioned snapping
let normalized_plain_value = unsafe { self.param_ptr.preview_normalized(plain_value) };
cx.emit(RawParamEvent::SetParameterNormalized(
self.param_ptr,
normalized_plain_value,
));
}

/// End an automation gesture. This must be called at the end of a gesture, after zero or more
Expand Down
24 changes: 16 additions & 8 deletions src/widgets/param_slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ pub enum ParamSliderStyle {
Centered,
/// Always fill the bar starting from the left.
FromLeft,
/// Fill the bar from the mid point, regardless of where the default value lies
FromMidPoint,
/// Show the current step instead of filling a portion of the bar, useful for discrete
/// parameters. Set `even` to `true` to distribute the ticks evenly instead of following the
/// parameter's distribution. This can be desireable because discrete parameters have smaller
/// parameter's distribution. This can be desirable because discrete parameters have smaller
/// ranges near the edges (they'll span only half the range, which can make the display look
/// odd).
CurrentStep { even: bool },
Expand Down Expand Up @@ -331,6 +333,16 @@ impl ParamSlider {
if delta >= 1e-3 { delta } else { 0.0 },
)
}
ParamSliderStyle::FromMidPoint => {
let delta = (0.5 - current_value).abs();

// Don't draw the filled portion at all if it could have been a
// rounding error since those slivers just look weird
(
0.5_f32.min(current_value),
if delta >= 1e-3 { delta } else { 0.0 },
)
}
ParamSliderStyle::Centered | ParamSliderStyle::FromLeft => (0.0, current_value),
ParamSliderStyle::CurrentStep { even: true }
| ParamSliderStyle::CurrentStepLabeled { even: true }
Expand Down Expand Up @@ -367,7 +379,9 @@ impl ParamSlider {
ParamSliderStyle::CurrentStep { .. } | ParamSliderStyle::CurrentStepLabeled { .. } => {
(0.0, 0.0)
}
ParamSliderStyle::Centered | ParamSliderStyle::FromLeft => {
ParamSliderStyle::Centered
| ParamSliderStyle::FromMidPoint
| ParamSliderStyle::FromLeft => {
let modulation_start = param.unmodulated_normalized_value();

(
Expand All @@ -383,7 +397,6 @@ impl ParamSlider {
/// to match up with the fill value display. This still needs to be wrapped in a parameter
/// automation gesture.
fn set_normalized_value_drag(&self, cx: &mut EventContext, normalized_value: f32) {

let normalized_value = match (self.style, self.param_base.step_count()) {
(
ParamSliderStyle::CurrentStep { even: true }
Expand All @@ -400,8 +413,6 @@ impl ParamSlider {
_ => normalized_value,
};



self.param_base.set_normalized_value(cx, normalized_value);
}
}
Expand Down Expand Up @@ -504,7 +515,6 @@ impl View for ParamSlider {
}
}
WindowEvent::MouseMove(x, _y) => {

// if meta.target == cx.current() {
// println!("{}", *x);
// }
Expand Down Expand Up @@ -535,8 +545,6 @@ impl View for ParamSlider {
} else {
self.granular_drag_status = None;



self.set_normalized_value_drag(
cx,
util::remap_current_entity_x_coordinate(cx, *x),
Expand Down
12 changes: 5 additions & 7 deletions src/widgets/peak_meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl PeakMeter {
.class("ticks__label")
.class("ticks__label--inf")
} else if last_tick {
// This is only inclued in the array to make positioning this easier
// This is only included in the array to make positioning this easier
Label::new(cx, "dBFS")
.class("ticks__label")
.class("ticks__label--dbfs")
Expand Down Expand Up @@ -206,12 +206,10 @@ where

let grayscale_color = 0.3 + ((1.0 - tick_fraction) * 0.5);
let mut paint = vg::Paint::default();
paint.set_color4f(vg::Color4f::new(
grayscale_color,
grayscale_color,
grayscale_color,
1.0
), None);
paint.set_color4f(
vg::Color4f::new(grayscale_color, grayscale_color, grayscale_color, 1.0),
None,
);
paint.set_stroke_width(TICK_WIDTH * dpi_scale);
paint.set_style(vg::PaintStyle::Stroke);
canvas.draw_path(&path, &paint);
Expand Down