Menu Buttons

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.
Post Reply
totem
Prole
Posts: 6
Joined: Mon May 28, 2012 4:38 pm

Menu Buttons

Post 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.
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Menu Buttons

Post 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
totem
Prole
Posts: 6
Joined: Mon May 28, 2012 4:38 pm

Re: Menu Buttons

Post 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?
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Menu Buttons

Post 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
totem
Prole
Posts: 6
Joined: Mon May 28, 2012 4:38 pm

Re: Menu Buttons

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests