1
0
Fork 0

Add an evenly distributed CurrentStep mode

This commit is contained in:
Robbert van der Helm 2022-03-22 19:44:40 +01:00
parent ee3f4e0b37
commit 5e94ec7f1d
2 changed files with 32 additions and 9 deletions

View file

@ -52,8 +52,12 @@ impl GenericUi {
.set_style(match unsafe { param_ptr.step_count() } { .set_style(match unsafe { param_ptr.step_count() } {
// This looks nice for boolean values, but it's too crowded for anything beyond // This looks nice for boolean values, but it's too crowded for anything beyond
// that without making the widget wider // that without making the widget wider
Some(step_count) if step_count <= 1 => ParamSliderStyle::CurrentStepLabeled, Some(step_count) if step_count <= 1 => {
Some(step_count) if step_count <= 64 => ParamSliderStyle::CurrentStep, ParamSliderStyle::CurrentStepLabeled { even: true }
}
Some(step_count) if step_count <= 64 => {
ParamSliderStyle::CurrentStep { even: true }
}
Some(_) => ParamSliderStyle::FromLeft, Some(_) => ParamSliderStyle::FromLeft,
// This is already the default, but continuous parameters should be drawn from // This is already the default, but continuous parameters should be drawn from
// the center if the default is also centered, or from the left if it is not // the center if the default is also centered, or from the left if it is not

View file

@ -44,12 +44,15 @@ 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 /// Show the current step instead of filling a portion of the bar, useful for discrete
/// parameters. /// parameters. Set `even` to `true` to distribute the ticks evenly instead of following the
CurrentStep, /// parameter's distribution. This can be desireable because discrete parameters have smaller
/// ranges near the edges (they'll span only half the range, which can make the display look
/// odd).
CurrentStep { even: bool },
/// The same as `CurrentStep`, but overlay the labels over the steps instead of showing the /// The same as `CurrentStep`, but overlay the labels over the steps instead of showing the
/// active value. Only useful for discrete parameters with two, maybe three possible values. /// active value. Only useful for discrete parameters with two, maybe three possible values.
CurrentStepLabeled, CurrentStepLabeled { even: bool },
} }
enum ParamSliderEvent { enum ParamSliderEvent {
@ -205,8 +208,21 @@ impl ParamSlider {
} }
ParamSliderStyle::Centered ParamSliderStyle::Centered
| ParamSliderStyle::FromLeft => (0.0, current_value), | ParamSliderStyle::FromLeft => (0.0, current_value),
ParamSliderStyle::CurrentStep ParamSliderStyle::CurrentStep { even: true }
| ParamSliderStyle::CurrentStepLabeled => { | ParamSliderStyle::CurrentStepLabeled { even: true }
if step_count.is_some() =>
{
// Assume the normalized value is distributed evenly
// across the range.
let step_count = step_count.unwrap() as f32;
let discrete_values = step_count + 1.0;
let previous_step = (current_value * step_count)
.floor()
/ discrete_values;
(previous_step, discrete_values.recip())
}
ParamSliderStyle::CurrentStep { .. }
| ParamSliderStyle::CurrentStepLabeled { .. } => {
let previous_step = unsafe { let previous_step = unsafe {
param_ptr param_ptr
.previous_normalized_step(current_value) .previous_normalized_step(current_value)
@ -237,7 +253,10 @@ impl ParamSlider {
// text overlapping the fill area slightly differently. We can // text overlapping the fill area slightly differently. We can
// set the cip region directly in vizia. // set the cip region directly in vizia.
match (style, step_count) { match (style, step_count) {
(ParamSliderStyle::CurrentStepLabeled, Some(step_count)) => { (
ParamSliderStyle::CurrentStepLabeled { .. },
Some(step_count),
) => {
HStack::new(cx, |cx| { HStack::new(cx, |cx| {
// There are step_count + 1 possible values for a // There are step_count + 1 possible values for a
// discrete parameter // discrete parameter