SFML community forums

Help => Window => Topic started by: Crash47 on October 08, 2010, 08:24:39 am

Title: MouseOver
Post by: Crash47 on October 08, 2010, 08:24:39 am
Hi!

I'm having problem with MouseOver function.

I want to make:
Quote
If mouse is over object and I click left mouse button
// Do something


Code:
Code: [Select]
bool MouseOver(sf::Test  &Test)
{
if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left)) /*Do something*/();
}
Title: MouseOver
Post by: Hiura on October 08, 2010, 09:51:20 am
What is exactly your problem (how this mouse over function is called?...) ? What have you done so far ?
Title: MouseOver
Post by: Crash47 on October 08, 2010, 02:28:18 pm
Quote from: "Hiura"
What is exactly your problem (how this mouse over function is called?...) ? What have you done so far ?


Thanks for reply. I'm having problem with function MouseOver. I don't know how to use it.

Code: [Select]
bool MouseOver(sf::Test  &Test)
{
// Do something
}

Test is sprite, yes?
Title: MouseOver
Post by: Laurent on October 08, 2010, 02:42:06 pm
Where does this function come from? It's not part of SFML.
Title: Re: MouseOver
Post by: Luinechor on October 08, 2010, 02:48:41 pm
Quote from: "Crash47"

Code:
Code: [Select]
bool MouseOver(sf::Test  &Test)
{
if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left)) /*Do something*/();
}


Well, you need the position of the sprite and your mouse, too. It's a simple collision detection (google for 2D Collision Detection, you'll find a lot). It's more like this:

Code: [Select]

bool isPointOverSprite(const sf::Vector2f Position, const sf::Sprite &Sprite)
{
return (Position.x < Sprite.GetPosition().x + Sprite.GetSize().x) && (Sprite.GetPosition().x < Position.x) &&
(Position.y < Sprite.GetPosition().y + Sprite.GetSize().y) && (Sprite.GetPosition().y < Position.y);
}


In your case - Position.X would be
Code: [Select]
App.GetInput().GetMouseX()
Position.Y of course
Code: [Select]
App.GetInput().GetMouseY()

With the function above you could now use it like that:

Code: [Select]

sf::Vector2f mousePosition = new sf::Vector2f(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());
if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left)) && isPointOverSprite (mousePosition, yourSprite)