How to create a faster/more accurate image alpha check mouse hover function
Posted: Thu Jan 12, 2017 7:12 am
Yeah, there goes Khranos with another convoluted title. To elaborate, I'm working with menus right now and I have buttons with kind of wander around a small area on the screen. When the mouse hovers over the initial parameters of an image (it's x/y in a table + its height/width), it checks up to 3 layers down in pixels checking for an alpha property greater than 0.
Before implementing the wandering feature of the buttons, everything was working perfectly. Post-wander, it's now kind of half-triggering. I assume it's because the x/y locations are changing while it's in the middle of checking the pixel it was on, causing it to become disoriented, but that could be incorrect.
Below is a .gif of the issue, if it clears up any confusion from above. As is seen, it's finding alpha particles where there should not be any (it isn't detecting the background, as it isn't in the table for detection). The mouse alpha particle detection function I'm using is below:
It's messy, but it has been getting the job done. I'm using flux to change a table containing the x/y of the images using a function which randomly selects a new point within its boundaries.
If it helps, I also have the selection of functions I'm using to trigger wandering, as well as the tables that they're pulling from located here: https://hastebin.com/zewedudaju.lua .
I'm thoroughly interested in both code examples and general directions to head towards -- tips are just as useful, if not more useful, than code snippets. Thank you in advance to anybody willing to help out!
Before implementing the wandering feature of the buttons, everything was working perfectly. Post-wander, it's now kind of half-triggering. I assume it's because the x/y locations are changing while it's in the middle of checking the pixel it was on, causing it to become disoriented, but that could be incorrect.
Below is a .gif of the issue, if it clears up any confusion from above. As is seen, it's finding alpha particles where there should not be any (it isn't detecting the background, as it isn't in the table for detection). The mouse alpha particle detection function I'm using is below:
Code: Select all
function cursorIsOverMenu(moX, moY, exception, exception2) -- handles menu button click detection.
for i, image in ipairs (imagesMenu) do
if i ~= exception and i ~= exception2 then --if the exception was reached (AKA it had to go 1 or 2 layers deeper on said pixel)
local x = moX
local y = moY
local imgData = image.img:getData()
if (x > image.x) and (y > image.y) and (x < (image.x + image.img:getWidth())) and (y < (image.y + image.img:getHeight())) then -- ensure the mouse is within the parameters.
if lume.sign(x) ~= -1 and lume.sign(y) ~= -1 then -- simply a debug that rarely gets used. Better to have it than not.
local xPix = moX - image.x
local yPix = moY - image.y
local r, g, b, a = imgData:getPixel(xPix, yPix)
if a ~= 0 then
print("on image "..i)
if image.visible then
return true, image.label
end
else
print("not on image "..i)
if not exception then
cursorIsOver(moX, moY, i)
elseif not exception2 then
cursorIsOver(moX, moY, exception, i)
end
end
end
end
end
end
end
If it helps, I also have the selection of functions I'm using to trigger wandering, as well as the tables that they're pulling from located here: https://hastebin.com/zewedudaju.lua .
I'm thoroughly interested in both code examples and general directions to head towards -- tips are just as useful, if not more useful, than code snippets. Thank you in advance to anybody willing to help out!