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.
Check MouseHover Function?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Check MouseHover Function?
If your image is rectangular, that is it doesn't have much padding then sure the math is pretty simple: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.
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
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
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
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
Re: Check MouseHover Function?
And this makes it where, when you hover the mouse over the image; it will change to a different image?
Just making sure.
Just making sure.
Re: Check MouseHover Function?
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 wrote:And this makes it where, when you hover the mouse over the image; it will change to a different image?
Check out my blog on gamedev
Re: Check MouseHover Function?
Thank you Ivan and Micha!
Who is online
Users browsing this forum: Bing [Bot], Semrush [Bot] and 4 guests