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

Author Topic: how to detect when mouseover an object  (Read 6253 times)

0 Members and 1 Guest are viewing this topic.

langisser

  • Newbie
  • *
  • Posts: 9
    • View Profile
how to detect when mouseover an object
« on: January 08, 2010, 08:04:18 am »
how to detect when mouseover/click an object

Example:
I want to create my event when mouseover/click a string or image. How to?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
how to detect when mouseover an object
« Reply #1 on: January 08, 2010, 10:22:30 am »
Get the surrounding rectangle of your object and check, whether it contains the mouse coordinate (delivered by a MouseButtonPressed event).

sf::String (sf::Text in SFML2) has a method GetRect(), at sf::Sprite you have to calculate the bounding rect with position, size and origin.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

langisser

  • Newbie
  • *
  • Posts: 9
    • View Profile
how to detect when mouseover an object
« Reply #2 on: January 08, 2010, 11:29:32 am »
That's cool.
This's my code.
Code: [Select]

int MouseOver(int MouseX,int MouseY,sf::String Hello)
{

//return 0 = mouse over
//retunr 1 = mouse not over
if ( MouseX > Hello.GetRect().Left && MouseX < Hello.GetRect().Right)
{
if ( MouseY > Hello.GetRect().Top && MouseY < Hello.GetRect().Bottom)
return 0;
}
return 1;
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
how to detect when mouseover an object
« Reply #3 on: January 08, 2010, 06:34:42 pm »
Why don't you use bool? :shock:
And why are there two ifs? One is enough.

Besides, I would pass Hello by const reference and store the rectangle given by Hello.GetRect() (you can't be sure SFML doesn't calculate it every time, and even if, this is an implementation detail and might change in the future).

P.S. I found a mistake in my upper post, of course I meant sf::Sprite and not sf::String a second time.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

langisser

  • Newbie
  • *
  • Posts: 9
    • View Profile
how to detect when mouseover an object
« Reply #4 on: January 11, 2010, 06:15:49 am »
Thank you for recommend,Now i use bool.

core4mac

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
how to detect when mouseover an object
« Reply #5 on: January 19, 2010, 08:52:09 am »
You can use the Contains method of the rect.
Code: [Select]

bool Match (float x, float y)
{
   // m_rect is of type sf::FloatRect
   return m_rect.Contains (x,y);
}

 

anything