2022-02-09 11:32:19 +01:00
|
|
|
use egui::{vec2, Response, Sense, Stroke, TextStyle, Ui, Widget};
|
2022-02-09 01:07:57 +01:00
|
|
|
|
2022-02-09 11:32:19 +01:00
|
|
|
use super::util;
|
2022-02-09 11:13:51 +01:00
|
|
|
use nih_plug::{Param, ParamSetter};
|
2022-02-09 01:07:57 +01:00
|
|
|
|
|
|
|
/// A slider widget similar to [egui::widgets::Slider] that knows about NIH-plug parameters ranges
|
|
|
|
/// and can get values for it.
|
|
|
|
///
|
|
|
|
/// TODO: Vertical orientation
|
|
|
|
/// TODO: (before I forget) mouse scrolling, ctrl+click and double click to reset
|
2022-02-09 11:13:51 +01:00
|
|
|
pub struct ParamSlider<'a, P: Param> {
|
|
|
|
param: &'a P,
|
2022-02-09 01:07:57 +01:00
|
|
|
setter: &'a ParamSetter<'a>,
|
|
|
|
}
|
|
|
|
|
2022-02-09 11:13:51 +01:00
|
|
|
impl<'a, P: Param> ParamSlider<'a, P> {
|
2022-02-09 01:07:57 +01:00
|
|
|
/// Create a new slider for a parameter. Use the other methods to modify the slider before
|
|
|
|
/// passing it to [Ui::add()].
|
2022-02-09 11:13:51 +01:00
|
|
|
pub fn for_param(param: &'a P, setter: &'a ParamSetter<'a>) -> Self {
|
2022-02-09 01:07:57 +01:00
|
|
|
Self { param, setter }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn normalized_value(&self) -> f32 {
|
|
|
|
self.param.normalized_value()
|
|
|
|
}
|
|
|
|
|
2022-02-09 11:34:58 +01:00
|
|
|
fn begin_drag(&self) {
|
2022-02-09 01:07:57 +01:00
|
|
|
self.setter.begin_set_parameter(self.param);
|
2022-02-09 11:34:58 +01:00
|
|
|
}
|
2022-02-09 11:18:38 +01:00
|
|
|
|
2022-02-09 11:34:58 +01:00
|
|
|
fn set_normalized_value(&self, normalized: f32) {
|
2022-02-09 11:48:05 +01:00
|
|
|
if normalized != self.normalized_value() {
|
|
|
|
// This snaps to the nearest plain value if the parameter is stepped in some wayA
|
|
|
|
// TODO: As an optimization, we could add a `const CONTINUOUS: bool` to the parameter to
|
|
|
|
// avoid this normalized->plain->normalized conversion for parameters that don't need
|
|
|
|
// it
|
|
|
|
let value = self.param.preview_plain(normalized);
|
|
|
|
self.setter.set_parameter(self.param, value);
|
|
|
|
}
|
2022-02-09 11:34:58 +01:00
|
|
|
}
|
2022-02-09 11:18:38 +01:00
|
|
|
|
2022-02-09 11:42:47 +01:00
|
|
|
// This still needs to be part of a drag gestur
|
|
|
|
fn reset_param(&self) {
|
|
|
|
let normalized_default = self.setter.default_normalized_param_value(self.param);
|
|
|
|
self.setter
|
|
|
|
.set_parameter_normalized(self.param, normalized_default);
|
|
|
|
}
|
|
|
|
|
2022-02-09 11:34:58 +01:00
|
|
|
fn end_drag(&self) {
|
2022-02-09 01:07:57 +01:00
|
|
|
self.setter.end_set_parameter(self.param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 11:13:51 +01:00
|
|
|
impl<P: Param> Widget for ParamSlider<'_, P> {
|
2022-02-09 01:07:57 +01:00
|
|
|
fn ui(self, ui: &mut Ui) -> Response {
|
|
|
|
// Allocate space, but add some padding on the top and bottom to make it look a bit slimmer.
|
|
|
|
let height = ui
|
|
|
|
.fonts()
|
|
|
|
.row_height(TextStyle::Body)
|
|
|
|
.max(ui.spacing().interact_size.y);
|
|
|
|
let slider_height = ui.painter().round_to_pixel(height * 0.65);
|
|
|
|
let response = ui
|
|
|
|
.vertical(|ui| {
|
|
|
|
ui.allocate_space(vec2(
|
|
|
|
ui.spacing().slider_width,
|
|
|
|
(height - slider_height) / 2.0,
|
|
|
|
));
|
|
|
|
let response = ui.allocate_response(
|
|
|
|
vec2(ui.spacing().slider_width, slider_height),
|
|
|
|
Sense::click_and_drag(),
|
|
|
|
);
|
|
|
|
ui.allocate_space(vec2(
|
|
|
|
ui.spacing().slider_width,
|
|
|
|
(height - slider_height) / 2.0,
|
|
|
|
));
|
|
|
|
response
|
|
|
|
})
|
|
|
|
.inner;
|
|
|
|
|
|
|
|
// Handle user input
|
2022-02-09 11:34:58 +01:00
|
|
|
// TODO: As mentioned above, handle double click and ctrl+click, maybe also value entry
|
2022-02-09 11:42:47 +01:00
|
|
|
// TODO: Handle shift+drag being more granular
|
2022-02-09 11:34:58 +01:00
|
|
|
if response.drag_started() {
|
|
|
|
self.begin_drag();
|
|
|
|
}
|
2022-02-09 01:07:57 +01:00
|
|
|
if let Some(click_pos) = response.interact_pointer_pos() {
|
2022-02-09 11:36:10 +01:00
|
|
|
let proportion =
|
|
|
|
egui::emath::remap_clamp(click_pos.x, response.rect.x_range(), 0.0..=1.0) as f64;
|
2022-02-09 01:07:57 +01:00
|
|
|
self.set_normalized_value(proportion as f32);
|
|
|
|
}
|
2022-02-09 11:42:47 +01:00
|
|
|
// TODO: Also handle ctrl+click
|
|
|
|
if response.double_clicked() {
|
|
|
|
self.reset_param();
|
|
|
|
}
|
2022-02-09 11:34:58 +01:00
|
|
|
if response.drag_released() {
|
|
|
|
self.end_drag();
|
|
|
|
}
|
2022-02-09 01:07:57 +01:00
|
|
|
|
|
|
|
// And finally draw the thing
|
|
|
|
if ui.is_rect_visible(response.rect) {
|
|
|
|
// We'll do a flat widget with background -> filled foreground -> slight border
|
|
|
|
ui.painter()
|
|
|
|
.rect_filled(response.rect, 0.0, ui.visuals().widgets.inactive.bg_fill);
|
|
|
|
|
|
|
|
let filled_proportion = self.normalized_value();
|
2022-02-09 11:19:49 +01:00
|
|
|
if filled_proportion > 0.0 {
|
|
|
|
let mut filled_rect = response.rect;
|
|
|
|
filled_rect.set_width(response.rect.width() * filled_proportion);
|
|
|
|
let filled_bg = if response.dragged() {
|
2022-02-09 11:32:19 +01:00
|
|
|
util::add_hsv(ui.visuals().selection.bg_fill, 0.0, -0.1, 0.1)
|
2022-02-09 11:19:49 +01:00
|
|
|
} else {
|
|
|
|
ui.visuals().selection.bg_fill
|
|
|
|
};
|
|
|
|
ui.painter().rect_filled(filled_rect, 0.0, filled_bg);
|
|
|
|
}
|
2022-02-09 01:07:57 +01:00
|
|
|
|
|
|
|
ui.painter().rect_stroke(
|
|
|
|
response.rect,
|
|
|
|
0.0,
|
|
|
|
Stroke::new(1.0, ui.visuals().widgets.active.bg_fill),
|
|
|
|
);
|
|
|
|
|
|
|
|
// TODO: Render the text
|
|
|
|
}
|
|
|
|
|
|
|
|
response
|
|
|
|
}
|
|
|
|
}
|