Page 1 of 1
Menu Buttons
Posted: Wed Jul 04, 2012 6:54 pm
by totem
I don't have any code yet but I was just wondering if anyone had any elegant techniques for clickable buttons.
I was thinking about having the corners of each button saved in a table, and then calling mouse.getPosition each frame and checking to see if the coord is within the boundaries of the button. I'd have to do 8 equivalence tests for each button, and I would imagine it to be pretty slow, especially with more than one button.
Any ideas?
All contributions welcome and wanted.
Re: Menu Buttons
Posted: Wed Jul 04, 2012 7:01 pm
by dreadkillz
Just do a simple point in bounding box test (assuming that your box is a rectangle and not some funky quadrilateral). It's fast and short. A variation of this:
https://love2d.org/wiki/BoundingBox.lua
Code: Select all
-- where x,y,w,h are the coordinates and dimensions of the button box and mx,my are the coordinates of your mouse
local function regionhit(mx,my,x,y,w,h)
return mx > x and mx < x+w and my > y and my < y+h
end
Re: Menu Buttons
Posted: Wed Jul 04, 2012 7:12 pm
by totem
dreadkillz wrote:Just do a simple point in bounding box test (assuming that your box is a rectangle and not some funky quadrilateral). It's fast and short. A variation of this:
https://love2d.org/wiki/BoundingBox.lua
Code: Select all
-- where x,y,w,h are the coordinates and dimensions of the button box and mx,my are the coordinates of your mouse
local function regionhit(mx,my,x,y,w,h)
return mx > x and mx < x+w and my > y and my < y+h
end
Fantastic. Thanks so much.
+1
EDIT: Do you have any experience working with this? I'm probably looking at about 20 buttons on screen at one time. Will I need to set up some coroutines?
Re: Menu Buttons
Posted: Wed Jul 04, 2012 7:31 pm
by dreadkillz
I'm still learning myself, but Lua is flexible enough so that you can think of your own way of implementing things. Personally, I would create a new table for each button, and store relevant data about the button such as callbacks when clicked, dimensions, coordinates, colors, etc.
So:
Code: Select all
button1 = {color = ..., x = ..., y = ..., w = ..., h = .., callback = ..., etc.}
-- and then you could do something like an if statement to check if the button is clicked:
if button1WasClicked then
button1.callback()
button1WasClicked = nil -- clear the clicked status
end
EDIT: Also you can check out the excellent LOVE Frames library for more advance gui stuff:
https://love2d.org/wiki/Category:Libraries
Re: Menu Buttons
Posted: Wed Jul 04, 2012 7:49 pm
by totem
dreadkillz wrote:I'm still learning myself, but Lua is flexible enough so that you can think of your own way of implementing things. Personally, I would create a new table for each button, and store relevant data about the button such as callbacks when clicked, dimensions, coordinates, colors, etc.
So:
Code: Select all
button1 = {color = ..., x = ..., y = ..., w = ..., h = .., callback = ..., etc.}
-- and then you could do something like an if statement to check if the button is clicked:
if button1WasClicked then
button1.callback()
button1WasClicked = nil -- clear the clicked status
end
EDIT: Also you can check out the excellent LOVE Frames library for more advance gui stuff:
https://love2d.org/wiki/Category:Libraries
I've seen Frames, and I actually considered using it for a different project, but I'm trying to avoid any and all complexity in this endeavor. I also want to code as much of the project as possible myself. Ha.
Well, thanks for the advice, I'll get it figured out one way or another.