Slide bar going under my min value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void DrawSlideBar(float x, float y, float w, int min, int max, int &SliderValue)
    {
    	float sliderw = 5.f;
    	float sliderh = 15.f;
    
    	float sliderx = (SliderValue / ((max - min) / (w))) + x - sliderw / 2;
    
    	POINT MousePos = GetMousePos();
    
    	float clickx = sliderx - MousePos.x;
    	float moveto = sliderx - clickx - sliderw / 2;
    	if (MousePos.x >= x - sliderw / 2 && MousePos.y >= y - 2 && MousePos.x <= x + w + sliderw / 2 && MousePos.y <= y + sliderh + 2)
    	{
    		if (GetAsyncKeyState(0x1))
    		{
    			sliderx = moveto;
    		}
    	}
    	if (sliderx < x - sliderw / 2){ sliderx = x - sliderw / 2; }
    	if (sliderx > x + w - sliderw / 2){ sliderx = x + w - sliderw / 2; }
    
    	BoxFilled(x, y, w, sliderh, GRAY(170));
    	BoxFilled(x, y, w, sliderh, BLACK(170));
    	BoxFilled(sliderx, y, sliderw, sliderh, GREEN(170));
    
    	SliderValue = ((max - min) / (w))*(sliderx - x + sliderw / 2);
    	char chSliderValue[20];
    	sprintf_s(chSliderValue, "  [%1.0f]", (float)SliderValue);
    	DrawText(x + w + 120, y + 1, RED(255), chSliderValue, DT_CENTER);
    }


If I call the function like this, it all works like it should
 
DrawSlideBar(10, 10, 200, 0, 100, SliderValue);


The problem is if I set the min value bigger.
If I do it like this, the slide min value ends up 0 and the max ends up as 50.
 
DrawSlideBar(10, 10, 200, 50, 100, SliderValue);


I think its to do with how i set sliderx, but im not to sure on how to fix this.
Last edited on
What is min suppose to mean? An offset to x? -> x += min?
This is just a guess but try this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void DrawSlideBar(float x, float y, float w, int min, int max, int &SliderValue)
    {
    	float sliderw = 5.f;
    	float sliderh = 15.f;
    
    	float sliderx = (SliderValue / ((max - min) / (w))) + x - sliderw / 2 + min; //I changed this line
    
    	POINT MousePos = GetMousePos();
    
    	float clickx = sliderx - MousePos.x;
    	float moveto = sliderx - clickx - sliderw / 2;
    	if (MousePos.x >= x - sliderw / 2 && MousePos.y >= y - 2 && MousePos.x <= x + w + sliderw / 2 && MousePos.y <= y + sliderh + 2)
    	{
    		if (GetAsyncKeyState(0x1))
    		{
    			sliderx = moveto;
    		}
    	}
    	if (sliderx < x - sliderw / 2){ sliderx = x - sliderw / 2; }
    	if (sliderx > x + w - sliderw / 2){ sliderx = x + w - sliderw / 2; }
    
    	BoxFilled(x, y, w, sliderh, GRAY(170));
    	BoxFilled(x, y, w, sliderh, BLACK(170));
    	BoxFilled(sliderx, y, sliderw, sliderh, GREEN(170));
    
    	SliderValue = ((max - min) / (w))*(sliderx - x  - min + sliderw / 2); //Also changed this line
    	char chSliderValue[20];
    	sprintf_s(chSliderValue, "  [%1.0f]", (float)SliderValue);
    	DrawText(x + w + 120, y + 1, RED(255), chSliderValue, DT_CENTER);
    }
Last edited on
Min is ment to be the start of the slider

so that the slider starts at min, lets me slide it from min value to the max value.

Pindrought that makes the min of the sliders go in -values

x / y are the position it gets drawn on screen
w is the width of it.
min is the starting value.
Last edited on
so again: is min a percentage value?

if so, it would be slider_min_x = x + (w * min / 100.0) - (sliderw / 2)
Last edited on
no not really.

min is the value the slider starts from so if i call it like this
DrawSlideBar(10, 10, 200, 100, 255, SliderValue);

i should be able to slide the slider anywhere between 100 to 255

but at the moment if i call it like that the slider lets me slide to the values 0 to 155
Then another guess. min -> x; max -> x + w and SliderValue is something SliderValue >= min and SliderValue <= max

if so: float sliderx = ((SliderValue - min ) / (max - min) * w) + x - sliderw / 2;
Last edited on
I'm a little concerned why a function named 'DrawSlideBar' is changing the logical position of the slider. That is well outside the scope of what this function should be doing. It should just be drawing.... nothing else.


That said, your computations are overly complex and it's hard to follow the logic behind them. There's a lot of unexplained +2, -2, /2, and it's unclear what you're really trying to do. I can only assume the -2 are for padding.

But rather than try to decipher what you're trying to do, I just sort of just re-wrote this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

// note, I changed to pass SliderValue by value.  This function should not be modifying it
void DrawSlideBar(float x, float y, float w, int min, int max, int SliderValue)
{
    static const float sliderw = 5.f;
    static const float sliderh = 15.f;
    static const float padding = 1.f;       // pixel padding around all edges of drawn slider bar

    // drawable range is the range at which we can actually draw the slider.
    //  IE, it can be drawn at X=[0,drawRange]+x
    float drawRange = w - sliderw - 2*padding;      // 2*padding for left & right
    if(drawRange < 0)   drawRange = 0;

    // Now we have to scale the logical position of the slider to match the graphical
    //  position we want to draw it.
    float drawPos = SliderValue - min;      // first, make the position 0 based
    drawPos /= float(max-min);              // then make it [0,1]

    // then make sure it's within [0,1] bounds
    if(drawPos < 0)     drawPos = 0;
    if(drawPos > 1)     drawPos = 1;

    // Now scale it up to match the width of our slide bar
    drawPos *= drawRange;
    drawPos += x;


    // Lastly, draw it
    //   (I removed the Gray box because it just gets overdrawn by the Black box?)
    BoxFilled(x, y, w, sliderh, BLACK(170));
    BoxFilled(drawPos + padding, y + padding, sliderw, sliderh, GREEN(170));
}
Last edited on
ok so i got this to work how i want it to, to show you
but i want to be able to set the Sliderx inside the function from the slider value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void DrawSlider(float x, float y, float w, int min, int max, int &Sliderx, int &SliderValue)
{
	float sliderw = 5.f;
	float sliderh = 15.f;

	POINT MousePos = GetMousePos();

	float clickx = Sliderx - MousePos.x;
	float moveto = Sliderx - clickx - sliderw / 2;
	if (MousePos.x >= x - sliderw / 2 && MousePos.y >= y - 2 && MousePos.x <= x + w + sliderw / 2 && MousePos.y <= y + sliderh + 2)
	{
		if (GetAsyncKeyState(0x1))
		{
			Sliderx = moveto;
		}
	}
	if (Sliderx < x - sliderw / 2){ Sliderx = x - sliderw / 2; }
	if (Sliderx > x + w - sliderw / 2){ Sliderx = x + w - sliderw / 2; }

	BoxFilled(x, y, w, sliderh, GRAY(170));
	//this->BoxFilled(x, y, sliderx - x, sliderh, RED(170));
	BoxFilled(x, y, w, sliderh, BLACK(170));
	BoxFilled(Sliderx, y, sliderw, sliderh, GREEN(170));

	SliderValue = ((max - min) / (w))*(Sliderx - x + sliderw / 2) + min;
	char chSliderValue[20];
	sprintf_s(chSliderValue, "  [%1.0f]", (float)SliderValue);
	DrawText(x + w + 120, y + 1, RED(255), chSliderValue, DT_CENTER);
}

// My call
DrawSlider(10, 10, 200, 100, 255, Sliderx, SliderValue);
Topic archived. No new replies allowed.