Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Need help programming GUI  (Read 1688 times)

0 Members and 1 Guest are viewing this topic.

Dark Byte

  • Newbie
  • *
  • Posts: 30
    • View Profile
Need help programming GUI
« on: January 02, 2011, 06:53:36 am »
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 (it just freezes the program). I have tried to find the problem but have failed.
Here is the code

Declaration
Code: [Select]

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
Code: [Select]

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);
}