Customizing sliders



I’m trying to customize a Slider in order not to make it “jump” to a new value when I click the mouse on it.
I’d like just to start a relative move whatever is the location where I started dragging the slider cursor.

How’s best to proceed?



Well using Slider::setVelocityBasedMode might be a good way to start! Some of the sliders in the juce demo demonstrate this…


This is the current formula for velocity based slider motion:

speed = 0.2 * (1.0 + sin (double_Pi * (1.5 + jmax (0.0, speed - 1.0) / maxSpeed)));

I think it would be useful to developers if it were possible to fine-tune those parameters that are now hard-coded, i.e. provide get and set methods for the following parameters:

double velocityBasedModeSensitivity; // defaults to the value of 0.2 currently multipling the whole formula 0.2
double velocityBasedModeThreshold;   // defaults to the value of 0.1 currently subtracted from "speed"
double velocityBasedModeOffset;      // defaults to 0.0 and it's a value to be added to the starting 1.5 for trimming the starting slope

(names are of your choice, obviously, if you decide to implement them. These are only for explaining what I guess their meaning is)

the formula would then become:

speed = velocityBasedModeSensitivity * (1.0 + sin (double_Pi * (1.5 + velocityBasedModeOffset + jmax (0.0, speed - velocityBasedModeThreshold) / maxSpeed)));

And the methods for setting and getting (thought to live in the juce_Slider.h):

double getVelocityBasedModeSensitivity() { return velocityBasedModeSensitivity; }

void setVelocityBasedModeSensitivity (double newValue)
    if (newValue > 0.0)
        velocityBasedModeSensitivity = newValue; 

double getVelocityBasedModeThreshold() { return velocityBasedModeThreshold; }

void setVelocityBasedModeThreshold (double newValue)
    if (newValue >= 0.0)	// Allow movements without threshold
        velocityBasedModeThreshold = newValue; 

double getVelocityBasedModeOffset() { return velocityBasedModeOffset; }

void setVelocityBasedModeOffset (double newValue)
    if (newValue >= 0.0)
        velocityBasedModeOffset = newValue; 

Constructor should also be modified for initialization of the default values, adding the following lines:

    velocityBasedModeSensitivity (0.2),
    velocityBasedModeThreshold (0.1),
    velocityBasedModeOffset (0.0)

What do you think about such customization?


That’s a pretty good idea, actually! I’ll add something along those lines, thanks!


Not sure about the offset though - surely changing that value would make it move at different speeds depending on the direction, and could even go backwards - do you actually need that ability?


the offset parameters should serve as a mean for setting the starting velocity, or a lower bound for slider’s speed: mathematically speaking, supposing the “speed” variablle having the 0 value (or very small ones) before formula evaluation, gives a calculated speed very close to 0, since

1.0 + sin(Pi*1.5) = 0

If we had an initial offset of, say, 0.01, this would increase the minimum speed we’d have for ideal 0 speed: evaluating the formula would give

1.0 + sin(Pi*1.51) > 0

This is a “limit” calculation: of course, we don’t really want the slider to move if the mouse didn’t move (i.e. speed = 0 before calculating the formula), but this should already be true given the fact this happens in a function that’s called only when the mouse actually moves.

I agree with you that offset value shouldn’t be allowed to be set negative, this would have no useful effects on the interface.

I’m sorry if the explaination isn’t so clear, it’d require a small drawing of the first period of a sine function to be properly shown…


ah yes, sorry, I didn’t notice that it was always dealing with a positive value there.