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.
Menu Buttons
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- dreadkillz
- Party member
- Posts: 223
- Joined: Sun Mar 04, 2012 2:04 pm
- Location: USA
Re: Menu Buttons
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
Fantastic. Thanks so much.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
+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?
- dreadkillz
- Party member
- Posts: 223
- Joined: Sun Mar 04, 2012 2:04 pm
- Location: USA
Re: Menu Buttons
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:
EDIT: Also you can check out the excellent LOVE Frames library for more advance gui stuff:
https://love2d.org/wiki/Category:Libraries
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
https://love2d.org/wiki/Category:Libraries
Re: Menu Buttons
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.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:EDIT: Also you can check out the excellent LOVE Frames library for more advance gui stuff: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
https://love2d.org/wiki/Category:Libraries
Well, thanks for the advice, I'll get it figured out one way or another.
Who is online
Users browsing this forum: No registered users and 3 guests