cancel
Showing results for 
Search instead for 
Did you mean: 

Slider with inverted MIN, MAX values

JNova.0
Associate III

Hello,
I'm working with a slider and ran into this problem.
When I define

setValueRange(0, 255);

and then I set the value

setValue(25);

so everything works. But I would like to turn the values on the slider. That means I will enter

setValueRange(255, 0);

At this moment, the restrictive conditions are poorly applied.

In the code for the slider I found the problem here:

int16_t Slider::valueToPosition(int value) const
{
    value = MIN(valueRangeMax, value);
    value = MAX(value, valueRangeMin);
...

In order for the slider to work for the range "0, 255" as well as for "255, 0", this code needs to be modified.
I used the following code. The original is in the comment and there is also code using if. Uncommented, the functional code is using a conditional expression, which may not be as readable:

int16_t Slider::valueToPosition(int value) const {
/*
//    original
//    value = MIN(valueRangeMax, value);
//    value = MAX(value, valueRangeMin);
    
	if (valueRangeMax > valueRangeMin) {
		value = MIN(valueRangeMax, value);
		value = MAX(value, valueRangeMin);
	} else {
		value = MIN(valueRangeMin, value);
		value = MAX(value, valueRangeMax);
	}
*/
	value = (valueRangeMax > valueRangeMin) ?
	        (MIN(valueRangeMax, MAX(value, valueRangeMin))) :
	        (MIN(valueRangeMin, MAX(value, valueRangeMax)));
...

 

Hope this helps someone

JN

0 REPLIES 0