1
0
Fork 0

Apply even ParamSlider Step distribution to input

This remaps the input range to be the same as the displayed values since
the actual normalized number range has the numbers at the edge of the
range taking up half as much of the range as the other numbers.
This commit is contained in:
Robbert van der Helm 2022-03-22 23:28:15 +01:00
parent 31a84d51dd
commit d97826e6a3

View file

@ -216,9 +216,8 @@ impl ParamSlider {
// across the range. // across the range.
let step_count = step_count.unwrap() as f32; let step_count = step_count.unwrap() as f32;
let discrete_values = step_count + 1.0; let discrete_values = step_count + 1.0;
let previous_step = (current_value * step_count) let previous_step =
.floor() (current_value * step_count) / discrete_values;
/ discrete_values;
(previous_step, discrete_values.recip()) (previous_step, discrete_values.recip())
} }
ParamSliderStyle::CurrentStep { .. } ParamSliderStyle::CurrentStep { .. }
@ -317,6 +316,33 @@ impl ParamSlider {
)); ));
} }
} }
/// `set_normalized_value()`, but resulting from a mouse drag. When using the 'even' stepped
/// slider styles from [`ParamSliderStyle`] this will remap the normalized range to match up
/// with the fill value display.
fn set_normalized_value_drag(&self, cx: &mut Context, normalized_value: f32) {
let normalized_value = match (cx.data(), unsafe { self.param_ptr.step_count() }) {
(
Some(ParamSliderInternal {
style:
ParamSliderStyle::CurrentStep { even: true }
| ParamSliderStyle::CurrentStepLabeled { even: true },
..
}),
Some(step_count),
) => {
// We'll remap the value range to be the same as the displayed range, e.g. with each
// value occupying an equal area on the slider instead of the centers of those
// ranges being distributed over the entire `[0, 1]` range.
let discrete_values = step_count as f32 + 1.0;
let rounded_value = ((normalized_value * discrete_values) - 0.5).round();
rounded_value / step_count as f32
}
_ => normalized_value,
};
self.set_normalized_value(cx, normalized_value);
}
} }
impl View for ParamSlider { impl View for ParamSlider {
@ -377,7 +403,7 @@ impl View for ParamSlider {
})); }));
} else { } else {
self.granular_drag_start_x_value = None; self.granular_drag_start_x_value = None;
self.set_normalized_value( self.set_normalized_value_drag(
cx, cx,
util::remap_current_entity_x_coordinate(cx, cx.mouse.cursorx), util::remap_current_entity_x_coordinate(cx, cx.mouse.cursorx),
); );
@ -414,7 +440,7 @@ impl View for ParamSlider {
}) })
}); });
self.set_normalized_value( self.set_normalized_value_drag(
cx, cx,
util::remap_current_entity_x_coordinate( util::remap_current_entity_x_coordinate(
cx, cx,
@ -426,7 +452,7 @@ impl View for ParamSlider {
} else { } else {
self.granular_drag_start_x_value = None; self.granular_drag_start_x_value = None;
self.set_normalized_value( self.set_normalized_value_drag(
cx, cx,
util::remap_current_entity_x_coordinate(cx, *x), util::remap_current_entity_x_coordinate(cx, *x),
); );