Check MouseHover Function?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
kerperlo
Prole
Posts: 9
Joined: Wed Dec 24, 2014 9:46 pm

Check MouseHover Function?

Post by kerperlo »

Hello,

I am wondering if you can check when the mouse hovers over an image. Once over it, the image will change. When the mouse leaves and isn't hovering over the image, it changes back again.

Is it possible? If so, how? I would love to have recommendations or steer me into the right direction.

Thanks.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Check MouseHover Function?

Post by ivan »

kerperlo wrote:Hello,

I am wondering if you can check when the mouse hovers over an image. Once over it, the image will change. When the mouse leaves and isn't hovering over the image, it changes back again.

Is it possible? If so, how? I would love to have recommendations or steer me into the right direction.

Thanks.
If your image is rectangular, that is it doesn't have much padding then sure the math is pretty simple:

Code: Select all

-- "l","t" being the left-top corner of the rectangle
-- "w","h" being the width and height
function rect_vs_pt(l,t, w,h, px,py)
  return not (px < l or py < t or px > l + w or py > t + h)
end
If you know the "center" of the rectangle it's even simpler:

Code: Select all

-- "rx","ry" being the center of the rectangle
-- "hw","hh" being the half-width and height extents
function rect_vs_pt2(rx,ry, hw,hh, px,py)
  local dx,dy = rx - px, ry - py
  return not(dx*dx > hw*hw or dy*dy > hh*hh)
end
If your rectangle is rotated then, just rotate the point around the center of the rect:

Code: Select all

 -- "rx","ry" being the center of the rectangle
-- "hw","hh being the half-width and height extents of the rectangle
-- "a" being the angle in radians
function rotated_rect_vs_pt2(rx,ry, hw,hh, a, px,py)
  -- translate the point
  local dx, dy = rx - px, ry - py
  -- rotate the point
  local c, s = math.cos(a), math.sin(a)
  local lpx, lpy = c*dx - s*dy, s*dx + c*dy
  -- now the point in is in rect coords
  return not (lpx*lpx > hw*hw or lpy*lpy > hh*hh)
end
If your rectangle is rotated around the left-top corner then:

Code: Select all

-- "l", "t" being the top-left corner of the rectangle
-- "w", "h" being the width and height
-- "a" being the angle in radians
function rotated_rect_vs_pt(l,t, w,h, a, px,py)
  local hw, hh = w/2, h/2
  -- find the center of the rect
  local rx, ry = math.cos(a)*hw + l, math.sin(a)*hh + t
  return rotated_rect_vs_pt2(rx,ry, hw,hh, a, px,py)
end
The code is untested but should work assuming all angles are in radians (see math.rad or math.deg)
kerperlo
Prole
Posts: 9
Joined: Wed Dec 24, 2014 9:46 pm

Re: Check MouseHover Function?

Post by kerperlo »

And this makes it where, when you hover the mouse over the image; it will change to a different image?
Just making sure.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Check MouseHover Function?

Post by micha »

kerperlo wrote:And this makes it where, when you hover the mouse over the image; it will change to a different image?
No, not yet. What ivan posted is the code that checks whether a point is inside a rectangle or not. To change the image you have to (1) get the mouse position (2) check with one of ivan's functions if it is inside the button (3) if yes, draw the hover-image, if not, draw the standard-image.
kerperlo
Prole
Posts: 9
Joined: Wed Dec 24, 2014 9:46 pm

Re: Check MouseHover Function?

Post by kerperlo »

Thank you Ivan and Micha!
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests