programming GUI scroll bars with SFML problem

I am writing a GUI and I need help with scroll bars. I have this it works but sometimes if the member contentSize is a certain value it fails. I have tried to find the problem but have failed.
Here is the code

Decleration
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class VScrollBar : public Object
	{
		int mousepos[2];
		bool mousedown;
		public:
			int pos;
			float windowSize;
			float contentSize;
			float minGripSize;
			
			VScrollBar();
			float CalculateGripRatio();
			float CalculateGripSize();
			float CalculateScrollSize();
			float CalculateScrollRatio();
			float CalculateGripPosition();
			void ValidatePos();
			void Draw(sf::RenderWindow*);
		protected:
			void MouseScroll(int delta)
			{
				pos -= delta;
			}
			void MouseHover(float fx, float fy)
			{
				mousepos[1] = mousepos[0];
				mousepos[0] = fy;
				if ( mousedown )
				{
					float gripSize = CalculateGripSize();
					float gripPos = CalculateScrollSize() * CalculateScrollRatio();
					if ( fy >= gripPos && fy <= gripPos + gripSize )
						pos += mousepos[0]-mousepos[1];
				}
			}
			
			void MousePressed(int i)
			{
				if ( i == MOUSE_BUTTON_R )
				{
				/*	float mouseRatio = *mousepos/wY;
					pos = mouseRatio*contentSize;*/
					mousedown = true;
				}
			}
			
			void MouseReleased(int i)
			{
				if ( i == MOUSE_BUTTON_R )
					mousedown = false;
			}
			
	} ;


Definition
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
GUI::VScrollBar::VScrollBar()
{
     wY = 100;
     windowSize = 100;
     contentSize = 200;
     minGripSize = 5;
     pos = 0;
     *(mousepos) = 0;
     *(mousepos + 1) = 0;
     mousedown = false;
}

float GUI::VScrollBar::CalculateGripRatio()
{
     if ( contentSize == 0.f )
          return 0.f;
     return windowSize/contentSize;
}

float GUI::VScrollBar::CalculateGripSize()
{
     float tmp = wY * CalculateGripRatio();
     tmp = tmp < 3?3:tmp;
     return tmp<minGripSize?minGripSize:tmp;
}

float GUI::VScrollBar::CalculateScrollSize()
{
     return wY - CalculateGripSize();
}

float GUI::VScrollBar::CalculateScrollRatio()
{
     return pos/contentSize;
}

void GUI::VScrollBar::ValidatePos()
{
     if ( pos < 0 ) pos = 0;
     float gripPos = CalculateScrollSize() * CalculateScrollRatio();
     while ( gripPos > CalculateScrollSize() )
     {
          pos--;
          gripPos = CalculateScrollSize() * CalculateScrollRatio();
     }
}

void GUI::VScrollBar::Draw(sf::RenderWindow* RENW)
{
     ValidatePos();
     int gripSize = (int) CalculateGripSize();
     gripSize = gripSize < minGripSize?minGripSize:gripSize;
     float gripPos = CalculateScrollSize() * CalculateScrollRatio();
     sf::Shape grip = sf::Shape::Rectangle(pX+2, pY + gripPos, pX+wX-1,pY+gripPos+gripSize,sf::Color(70,70,70),true,sf::Color(200,200,200));
     sf::Shape track = sf::Shape::Rectangle(pX+1, pY, pX+wX,pY+wY,sf::Color(200,200,200),true,sf::Color(70,70,70));
     RENW->Draw(track);
     RENW->Draw(grip);
}
Last edited on
if the member contentSize is a certain value it fails


What value?

How does it fail?
Well I don't know which values; although one always fails and zero never fails.

It seems to pause the app for a few seconds before windows tells me the app has stopped responding. Similar to getting stuck in a loop.
Last edited on
Topic archived. No new replies allowed.