1
0
Fork 0

Add a ParamSlider display style highlighting step

This might work better for some types of discrete parameters.
This commit is contained in:
Robbert van der Helm 2022-03-19 19:29:19 +01:00
parent 4c5d61057e
commit 9e8b498676

View file

@ -44,6 +44,9 @@ pub enum ParamSliderStyle {
Centered, Centered,
/// Always fill the bar starting from the left. /// Always fill the bar starting from the left.
FromLeft, FromLeft,
/// Show the current step instead of filling a portion fothe bar, useful for discrete
/// parameters.
CurrentStep,
} }
enum ParamSliderEvent { enum ParamSliderEvent {
@ -171,21 +174,35 @@ impl ParamSlider {
.height(Stretch(1.0)) .height(Stretch(1.0))
.bind(normalized_param_value_lens, move |handle, value| { .bind(normalized_param_value_lens, move |handle, value| {
let current_value = *value.get(handle.cx); let current_value = *value.get(handle.cx);
let (start_t, mut delta) = if draw_fill_from_default { let (start_t, delta) = match style {
( ParamSliderStyle::Centered
default_value.min(current_value), if draw_fill_from_default =>
(default_value - current_value).abs(), {
) let delta = (default_value - current_value).abs();
} else { (
(0.0, current_value) default_value.min(current_value),
// Don't draw the filled portion at all if it could have been a
// rounding error since those slivers just look weird
if delta >= 1e-3 { delta } else { 0.0 },
)
}
ParamSliderStyle::Centered
| ParamSliderStyle::FromLeft => (0.0, current_value),
ParamSliderStyle::CurrentStep => {
let previous_step = unsafe {
param_ptr
.previous_normalized_step(current_value)
};
let next_step = unsafe {
param_ptr.next_normalized_step(current_value)
};
(
(previous_step + current_value) / 2.0,
(next_step + current_value) / 2.0,
)
}
}; };
// Don't draw the filled portion at all if it could have been a
// rounding error since those slivers just look weird
if delta < 1e-3 {
delta = 0.0;
}
handle handle
.left(Percentage(start_t * 100.0)) .left(Percentage(start_t * 100.0))
.width(Percentage(delta * 100.0)); .width(Percentage(delta * 100.0));