Efficient Mouse Interaction and Event Handling for Objects

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.
Athrikesha
Prole
Posts: 7
Joined: Mon Sep 18, 2023 6:46 pm
Contact:

Efficient Mouse Interaction and Event Handling for Objects

Post by Athrikesha »

_______new to lua & love2d ___________

Is there a simple way/library to detect hovering, keypress over an object(such as an image, or a shape) ?

All methods i see online and in forums, refer to the idea where we trace back to see if the mouse-co ordinates'range is with the image position co-ordinates. But i guess, there should be a simple logical implementation, shouldnt it ?

Like place n number of images on screen, and when mouse hovers over them detect it, even if need be even the dynamically position updated images too can be detected. Or am i missing something. ?

The simplest code for image that i could think of and generate is

Code: Select all

local image1, image2 -- Define variables to hold the images

function love.load()
    -- Load images
    image1 = love.graphics.newImage("tbc.png")
    image2 = love.graphics.newImage("sas.jpg")

    -- Set the positions where you want to display the images
    image1X, image1Y = 100, 100
    image2X, image2Y = 300, 100
end

function love.draw()
    -- Draw images at specified positions
    love.graphics.draw(image1, image1X, image1Y)
    love.graphics.draw(image2, image2X, image2Y)
end

function love.update(dt)
    -- Get the mouse coordinates
    local mouseX, mouseY = love.mouse.getPosition()

    -- Check if mouse is hovering over image1
    if mouseX >= image1X and mouseX <= image1X + image1:getWidth() and
       mouseY >= image1Y and mouseY <= image1Y + image1:getHeight() then
        -- Mouse is hovering over image1
        print("Mouse is over image1")
    end

    -- Check if mouse is hovering over image2
    if mouseX >= image2X and mouseX <= image2X + image2:getWidth() and
       mouseY >= image2Y and mouseY <= image2Y + image2:getHeight() then
        -- Mouse is hovering over image2
        print("Mouse is over image2")
    end
end
Kindly help
User avatar
Pinko
Prole
Posts: 45
Joined: Sat Jul 29, 2023 3:22 pm
Contact:

Re: Efficient Mouse Interaction and Event Handling for Objects

Post by Pinko »

I am also a beginner in Lua and my opinion is, that if you want this feature, you have to calculate the positions of all picutures constantly, and this can be very much depending on how many pictures need to be tracked.It could be possible for one or two pictures, but not for a hundred pictures.
Author of the vape knigge. I have thought of you! is much better than "i have waited for you". :cool:
User avatar
darkfrei
Party member
Posts: 1184
Joined: Sat Feb 08, 2020 11:09 pm

Re: Efficient Mouse Interaction and Event Handling for Objects

Post by darkfrei »

Here you can move rectangles with the mouse:
viewtopic.php?p=254122#p254122
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
BrotSagtMist
Party member
Posts: 625
Joined: Fri Aug 06, 2021 10:30 pm

Re: Efficient Mouse Interaction and Event Handling for Objects

Post by BrotSagtMist »

There are a few other methods, but they are all highly situation dependent.
This if its in range check is the best option to use usually.
So whats wrong with it? Where is your complain?
This is easy to write and fast to execute and there is nothing to improve.
Unless you have to deal with say a few thousand images at once where instead you can map the screen to a lookup table.
A lookup table has almost zero check time but a high creation cost. So there is that.
obey
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Efficient Mouse Interaction and Event Handling for Objects

Post by milon »

Welcome to the forum, Athrikesha!

I don't know if this is the "best" way or not, but check out my Wordle clone Wordie. Within each game state, I define a table 'gui' and put all my gui objects in it. When I'm drawing that state, I iterate through each gui object. If it has a 'draw' function, I call that draw function. Then I check for mouse hovering: the object must belong to the current game state, it must have a defined 'hover' method, and the mouse must be in its hover region. Here's what that function looks like:

Code: Select all

function game.draw()
    local x, y = love.mouse.getPosition()
    for i = 1, #gui do
        if gui[i].draw then gui[i].draw() end
        if state.current == state.game and gui[i].hover and isHovering(gui[i], x, y) then gui[i].hover(x, y) end
    end
end
Note that the hover() function is unique to each gui object. It may simply add a circular glow effect to a circular object, or it may recolor a rectangular region, etc.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
dusoft
Party member
Posts: 514
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Efficient Mouse Interaction and Event Handling for Objects

Post by dusoft »

milon wrote: Tue Sep 19, 2023 2:02 pm Note that the hover() function is unique to each gui object. It may simply add a circular glow effect to a circular object, or it may recolor a rectangular region, etc.
Ah, I like that concept.
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Efficient Mouse Interaction and Event Handling for Objects

Post by milon »

dusoft wrote: Tue Sep 19, 2023 3:34 pm
milon wrote: Tue Sep 19, 2023 2:02 pm Note that the hover() function is unique to each gui object. It may simply add a circular glow effect to a circular object, or it may recolor a rectangular region, etc.
Ah, I like that concept.
It quickly becomes a lot of table lookups, so localize where possible! ;)
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
knorke
Party member
Posts: 250
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

Re: Efficient Mouse Interaction and Event Handling for Objects

Post by knorke »

Welcome,
Maybe I misunderstand the question but do you know about tables?
Instead of copy-pasting the same code for image1,image2,image3,... you only write it once. Then you use a loop to iterate through all images.
http://lua-users.org/wiki/TablesTutorial

Also, if you have lots of clickable images but they have the same dimensions (for example like the tiles in Minesweeper games) then you can simply divide the mouse X or Y position by the size of an image to get the clicked image.
I did that in this game: viewtopic.php?p=71782#p71782
See getMouseTileXY() and function p2t in main.lua

Code: Select all

--convert pixel-position to tile-number
function p2t (px,py)
	return math.floor((px/tileSize)+0.5), math.floor((py/tileSize)+0.5)
end
Athrikesha
Prole
Posts: 7
Joined: Mon Sep 18, 2023 6:46 pm
Contact:

Re: Efficient Mouse Interaction and Event Handling for Objects

Post by Athrikesha »

darkfrei wrote: Tue Sep 19, 2023 9:36 am Here you can move rectangles with the mouse:
viewtopic.php?p=254122#p254122
No this is still quite buggy, unless i am mistaken. Moreover i think that it is slower, than one might expect. One improvement would be to lock the active rectangle and release the lock only when mouse is released, but i dont know. Slower in the sense, the speed when we drag a rectangle is dubious.
Athrikesha
Prole
Posts: 7
Joined: Mon Sep 18, 2023 6:46 pm
Contact:

Re: Efficient Mouse Interaction and Event Handling for Objects

Post by Athrikesha »

Pinko wrote: Tue Sep 19, 2023 8:42 am I am also a beginner in Lua and my opinion is, that if you want this feature, you have to calculate the positions of all picutures constantly, and this can be very much depending on how many pictures need to be tracked.It could be possible for one or two pictures, but not for a hundred pictures.
yeah, apart from the obvious way, can't we have a library that can keep track of the images on canvas more effectively. And dont know!, make it quite easier to update position, while upholding the transition efficiency. Its moonshot, but i expected a something like rete algorithm to calculate in intervals rather.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 5 guests