Page 1 of 1

Check MouseHover Function?

Posted: Tue Jan 27, 2015 12:05 pm
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.

Re: Check MouseHover Function?

Posted: Tue Jan 27, 2015 12:26 pm
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)

Re: Check MouseHover Function?

Posted: Tue Jan 27, 2015 3:34 pm
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.

Re: Check MouseHover Function?

Posted: Tue Jan 27, 2015 4:05 pm
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.

Re: Check MouseHover Function?

Posted: Tue Jan 27, 2015 5:09 pm
by kerperlo
Thank you Ivan and Micha!