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

Author Topic: MouseOver  (Read 5764 times)

0 Members and 1 Guest are viewing this topic.

Crash47

  • Newbie
  • *
  • Posts: 6
    • View Profile
MouseOver
« 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*/();
}

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
MouseOver
« Reply #1 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 ?
SFML / OS X developer

Crash47

  • Newbie
  • *
  • Posts: 6
    • View Profile
MouseOver
« Reply #2 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
MouseOver
« Reply #3 on: October 08, 2010, 02:42:06 pm »
Where does this function come from? It's not part of SFML.
Laurent Gomila - SFML developer

Luinechor

  • Guest
Re: MouseOver
« Reply #4 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)