1
General discussions / Need help with scaling Box2D coords
« on: July 02, 2011, 07:34:12 pm »
Nvm it is fixed
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
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;
}
} ;
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);
}
sf::Vector2f Project (sf::Vector2f& v1, sf::Vector2f& v2)
{
sf::Vector2f vec;
float x_ = v1.x * v2.x;
float y_ = v1.y * v2.y;
float d = pow(v2.x, 2) + pow(v2.y, 2);
x_ = (x_ + y_)/d;
vec.x = x_ * v2.x;
vec.y = x_ * v2.y;
return vec;
}
float Dot (sf::Vector2f& v1, sf::Vector2f& v2)
{
return v1.x * v2.x + v1.y * v2.y;
}
sf::Rect<sf::Vector2f> GetRectanglePoints (sf::Shape& Obj)
{
if ( Obj.GetNbPoints() == 4 )
{
sf::Rect<sf::Vector2f> ret;
ret.Top = Obj.GetPointPosition(0); // top-left corner
ret.Right = Obj.GetPointPosition(1); // top-right corner
ret.Bottom = Obj.GetPointPosition(2); // bottom-right corner
ret.Left = Obj.GetPointPosition(3); // bottom-left corner
return ret;
}
}
bool RectsCollide (sf::Rect<sf::Vector2f>& rect1, sf::Rect<sf::Vector2f>& rect2)
{
// Find the axis
sf::Vector2f Axis[4];
Axis[0].x = rect1.Right.x - rect1.Top.x;
Axis[0].y = rect1.Right.y - rect1.Top.y;
Axis[1].x = rect1.Right.x - rect1.Bottom.x;
Axis[1].y = rect1.Right.y - rect1.Bottom.y;
Axis[2].x = rect2.Top.x - rect2.Left.x;
Axis[2].x = rect2.Top.y - rect2.Left.y;
Axis[3].x = rect2.Top.x - rect2.Right.x;
Axis[3].y = rect2.Top.y - rect2.Right.y;
// Project the vectors onto the axis
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
int main()
{
sf::RenderWindow App(sf::VideoMode(800,600,32), "Test app");
sf::Image Back;
if ( !Back.LoadFromFile("background.png") )
App.Close();
sf::Sprite BackSPR;
BackSPR.SetImage(Back);
while ( App.IsOpened() )
{
App.Clear();
sf::Event Event;
while ( App.GetEvent(Event) )
{
if ( Event.Type == sf::Event::Closed )
App.Close();
}
App.Draw(BackSPR);
App.Display();
}
return 0;
}
Sprite.SetImage( IM.Data["gfx/player.bmp"] );