Slider, initial dragging value problem

Hi, [I think it is solved - My solution at my first reply after this]

I am working in a custom slider. I have designed a big thumb (way bigger than the default one). When I click over it to drag it, the thumb always moves to that position since the newposition is calculated having into account the mouse position and the slider range, so I am always getting a small slider change whenever i click on the thumb since is impossible to click over the real XY origin (position).
I only want it to move when I start dragging, not when I click over it, (and snapped to the mouse position during draggin)

I have tried to modify the “handleAbsolouteDrag” method, in the newPos calculation but i can’t make it work flawless.

Any idea about that??


Hi there,

I don’t know if anyone cares about it but i think I have solved it…

I have added a variable “drag_offset” to the slider class so the void handleAbsoluteDrag (const MouseEvent& e) method can read it.
So in the hitTest I have written the following to get the click offset refereing the current centre position:
float i = 0;
i = getPositionOfValue(getValue());
set_offset(y - i);

“set_offset” is a method to write the drag_offset variable.
Then in handleAbsoluteDrag int the snapmousetothumb:

//the original one
//newPos = (mousePos - sliderRegionStart) / (double) sliderRegionSize;

//The modified one

if (mousePos == drag_offset)
newPos = (mousePos - sliderRegionStart) / (double)sliderRegionSize;
else if (mousePos > drag_offset) {
newPos = ((mousePos - drag_offset) - sliderRegionStart) / (double)sliderRegionSize;
newPos = ((mousePos + drag_offset) - sliderRegionStart) / (double)sliderRegionSize;

It seems that is working…any idea of a more optimized way to make it work like this??


I think the options you are looking for are the Slider::SliderStyle
It sounds to me that you are using Rotary, which means a position clicked around the centre corresponds to a certain value.
Maybe the value RotaryHorizontalVerticalDrag fit better what you need. It says a mouse movement in positive direction (x or y) will increment the value and vice versa…
To be set with void Slider::setSliderStyle (SliderStyle newStyle)

No, I am using a vertical slider in a fader style.

When you click over the fader vertical area, the thumb moves to the click position, i have overrided the Hitest method, so it only drags if you clicked within the thumb area.
The thing is that when you click over the thumb area, the new position is calculated regarding the mouse position, so is far impossible to click over the big thumb without moving it cos the new mouse positions doesn’t much the current slider position.

I hope I explain myself :sweat_smile:

Ah, I see…
you are right, one would probably not want to move the slider if he doesn’t click exactly in the middle of the knob, so the solution you proposed seems the right one to me.
You might want to add a test, if the knob was hit at all, so if one clicks on the slider “lane”, the value at that point can be selected, rather than moving relative from that position. But you might have already thought of that…

is that suggestion to make the thumb move to the clicked point in the lane??

I don’t know how to make it without a relative offset position as I did (so I didn’t think about it :sweat_smile:)

I had to check myself the juce demo, so I don’t tell something stupid…
It is normal behaviour, that if you click on the lane, the knob jumps to that position.
It is not, if you call s->setVelocityBasedMode (true); (obviously :wink: , see second slider on the demo).
And my favourite feature as pro tools user: alt-click moves to the deault value, if you use a SliderAttachment (loving it).
So if the knob was missed, I would simply set that relative to zero, so the normal behaviour in that case is preserved.

Is Slider::setSliderSnapsToMousePosition what you’re looking for?

1 Like