Dragging "slider thumb" value/position ONLY when mouse is clicked over the thumb?possible?


#1

Hi, 

 

I don't know if this has been already discussed but i haven't found anything related.

I am designing my own custom slider FADER Style using a custom Look&Feel.

I would like to change the thumb position&value ONlY when the mouse is clicked over the thumb bounds, not along the sliderbackground. Is it possible?? So far I have desactivated the setSliderSnapsToMousePosition feature.

 

Also, since my custom thumb is much bigger than the defult tiny thumb, Most of the bottom half of my thumb is not showed since I think it goes beyond the bottom limits of the slider height in itself. Has it happen to anyone??I am struggling changing the drawLinearSliderBackground(g, x, y, width, height-(OFFSET!!!), sliderPos, minSliderPos, maxSliderPos, style, slider); function int order to shorten the background length  to try to "bodge" it in somehow sad

 

Any ideas??

 

regards

 

Carlos


#2

You could add a hitTest to your slider that ignores the mouse when it's not over the thumb?


#3

Hi, 

 

How could I ignore the mouse if it's not over the thumb?

So far I have tried to get the relative mouseXY coordenates within the slider, and the physical?? position of the slider regarding the actual value in sliderDragStarted method, and then tried to compared them having into account my thumb bounds as offset.

And trying to use fader->setEnabled(false) to ignore the dragging activity.

And fader->setEnabled(true) to enable the slider again within  sliderDragDragEnd or sliderstoppedDragging, but I never get these callbacks called :(. EDIT: yes, i can enable the slider again by using: void MainContentComponent::sliderDragEnded(Slider* sliderThatWasMoved). I would like to know how to get the others called anyway ;D

Even using setenable(false) the textbox is disable visually, don't like that....

Is it a very rude way to performance this tasK...I am not a juce expert  surprise, i would appreciate any help with these, since i think it is a important feature for real time applications.


void MainContentComponent::sliderDragStarted(Slider* sliderThatWasMoved)
{

    unsigned int value = 0;
    float pos;

    unigned faderXoffset = 0; faderYoffset = 0; 

    if (sliderThatWasMoved == fader1)
    {
           Point<int> p(fader1->getMouseXYRelative());               //getXY coordenates

           offsetY= fader1->getHeight()/2;                                         //thumb height /2 as offset
           pos = fader1->getPositionOfValue(fader1->getValue());    //Relative position of the fader regarding the actual value


          if (p.getY()>(pos+Yoffset) || (p.getY<pos-Yoffset))             //if is mouse is not clicked within thumb bounds...
                   fader1->setEnabled(false);                                                 //disable fader

    }


void MainContentComponent::sliderDragDragEnd(Slider* sliderThatWasMoved)
{
    fader1->setEnabled(true);
}
void MainContentComponent::sliderstoppedDragging(Slider* sliderThatWasMoved)
{
    fader1->setEnabled(true);
}
 

Any idea also about the half vanished thumb in the bottom??

 

Regards

Carlos

 


#4

I have almost solved by tweaking the parameters checking. 

void MainContentComponent::sliderDragStarted(Slider* sliderThatWasMoved){


unsigned int value = 0;
    float pos;
    
    unsigned faderYoffset = 0;
    int x = 0, y = 0;
    int pos_max = 0, pos_min = 0;

faderYoffset = (fader1->getHeight()/4) / 2; //it is half height of my thumb that is relative to the fader height (in my lookandfeel custom objetc)

Point<int> p(fader1->getMouseXYRelative());
        y = p.getY();
        
        pos_min = fader1->getPositionOfValue(fader1->getMinimum());//not used but maybe useful if tweaking parameters further.
        pos_max = fader1->getPositionOfValue(fader1->getMaximum());
        pos = fader1->getPositionOfValue(fader1->getValue());// -100;

        if (    (y>(pos + faderYoffset)) || (y < (pos - faderYoffset)))
            fader1->setEnabled(false);
}

The only thing is if there is a more stylish way of ignoring the dragging activity besides of setting the slider enabled and disabled.

I am thinking I could do fix the vanished bottom part of the thumb by not allowing the thumb to get the upper and down limits with so kind of offset and compensating then the real value of the slider.

 

Hope, changing the background will be easier, didn't try yet :D

 

Anyway there would be great having this feature implemented in a more optimised way, since i think is a must. Most of faders I see in commercial products can't be dragged if you click out of the thumb.

 

Regards

Carlos


#5

Jules is recommending that you override hitTest (int x, int y), which is a method in Component, the superclass of Slider.  It determines if a click / drag / or any other mouse activity is applied on the component.

This is an example, it assumes a vertical slider with no textbox:

 

class MySlider : public Slider

{
public:

    bool hitTest (int x, int y) override
    {
        const int thumbHeight = 10; // Just an example

        const int thumbCentre = valueToProportionOfLength (getValue()) * getHeight();

        return std::abs(y - thumbCentre) < thumbHeight);
    }
};

#6

ohhh thanks! I thought he meant I should write my own "hitTest" function. Didn't know about that one

I assume that disabling&enabling the slider activity on sliderDragStarted&sliderDragEnded is the way to go ;

 

Thanks!

 

Carlos