From cd683187e07841006959414de4c25fcb30a9b7b4 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Wed, 9 Feb 2022 11:42:47 +0100 Subject: [PATCH] Allow resetting parameters by double clicking --- nih_plug_egui/src/widgets/param_slider.rs | 12 ++++++++++++ src/context.rs | 16 ++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/nih_plug_egui/src/widgets/param_slider.rs b/nih_plug_egui/src/widgets/param_slider.rs index 570c6f77..9bbbdbd1 100644 --- a/nih_plug_egui/src/widgets/param_slider.rs +++ b/nih_plug_egui/src/widgets/param_slider.rs @@ -37,6 +37,13 @@ impl<'a, P: Param> ParamSlider<'a, P> { self.setter.set_parameter(self.param, value); } + // 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); + } + fn end_drag(&self) { self.setter.end_set_parameter(self.param); } @@ -70,6 +77,7 @@ impl Widget for ParamSlider<'_, P> { // Handle user input // TODO: As mentioned above, handle double click and ctrl+click, maybe also value entry + // TODO: Handle shift+drag being more granular if response.drag_started() { self.begin_drag(); } @@ -78,6 +86,10 @@ impl Widget for ParamSlider<'_, P> { egui::emath::remap_clamp(click_pos.x, response.rect.x_range(), 0.0..=1.0) as f64; self.set_normalized_value(proportion as f32); } + // TODO: Also handle ctrl+click + if response.double_clicked() { + self.reset_param(); + } if response.drag_released() { self.end_drag(); } diff --git a/src/context.rs b/src/context.rs index cb8a6d80..2ce65fad 100644 --- a/src/context.rs +++ b/src/context.rs @@ -157,14 +157,18 @@ impl<'a> ParamSetter<'a> { unsafe { self.context.raw_end_set_parameter(param.as_ptr()) }; } - /// Retrieve the default value for a parameter, in case you forgot. This is useful when - /// implementing GUIs, and it does not perform a callback. - fn raw_default_normalized_param_value(&self, param: &P) -> P::Plain { - let normalized = unsafe { + /// Retrieve the default value for a parameter, in case you forgot. The value is already + /// normalized to `[0, 1]`. This is useful when implementing GUIs, and it does not perform a callback. + pub fn default_normalized_param_value(&self, param: &P) -> f32 { + unsafe { self.context .raw_default_normalized_param_value(param.as_ptr()) - }; - param.preview_plain(normalized) + } + } + + /// The same as [Self::default_normalized_param_value], but without the normalization. + pub fn default_param_value(&self, param: &P) -> P::Plain { + param.preview_plain(self.default_normalized_param_value(param)) } }