1
0
Fork 0

Allow resetting parameters by double clicking

This commit is contained in:
Robbert van der Helm 2022-02-09 11:42:47 +01:00
parent 882ba8f61d
commit cd683187e0
2 changed files with 22 additions and 6 deletions

View file

@ -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<P: Param> 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<P: Param> 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();
}

View file

@ -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<P: Param>(&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<P: Param>(&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<P: Param>(&self, param: &P) -> P::Plain {
param.preview_plain(self.default_normalized_param_value(param))
}
}