Change slider's value only when the slider knob was clicked


#1

(this is for a vertical slider)

What I want is to only move the slider knob when the slider knob was clicked on the first place and ignore any clicks on the rail (like many plugins behave). Is there an easy way to do this?

setSliderSnapsToMousePosition is almost what I want, but whenever I click outside the knob and inside the slider rail, and then drag, the slider moves.

If I have to create a custom slider class, what’s the best way to go?

Thanks.


#2

There’s no easy way to make the existing slider do that. I guess you’d have to sub-class it and override the hitTest or mouseDown method somehow.


#3

I was able to solve this by doing what Jules recommended. This is what I did:

[list]

  1. I subclassed Slider, overrode hitTest, and made it return true when x and y hit the knob
  2. setSliderSnapsToMousePosition(false);
  3. setMouseDragSensitivity (railHeight - knobHeight); //if it’s horizontal knobWidth & railWidth
    [/list]

My fader now

[list]

  1. Doesn’t do any skipping when I click anywhere on the rail.
  2. Doesn’t do any skipping when I click the knob itself.
  3. When I drag, the knob matches the movement of the mouse.
    [/list]

#4

I would like to do a similar task, but I only want to disable snapsToMouse when clicking on the knob, while still allowing it in the rail. I tried calling setSliderSnapsToMousePosition() in hitTest, depending on where the click is, but this (as one might expect) causes really strange behaviour. lol… Any ideas?

-cpr


#5

Got it working… One only wants to change the snapsToMouse setting on the first time thru hitTest. I track ‘first time’ by tracking mouseDown, ie. if coming thru hitTest, and mouse is not down, then change the setting, once the mouse is down, don’t change it.


#6

Hey jucers,

I found this thread helpful as I'm looking for my sliders to only respond when the 'thumb' is clicked. 

 

Additionally, I'd like to pass clicks that are not on the thumb to the component behind it. 

What would be the most straight-forward way to accomplish this?