I made it in about an hour, and I would be glad if it even just saved one person that hour.
Installation is simple: Just type this in, in the top of your main.lua
Code: Select all
require("button")
Note: that by default it loads Middleclass from the main folder in your game. Just go ahead and change this if you already have it another place.
Lastly you will have to write these two functions in love.update and love.draw.
Code: Select all
function love.update(dt)
updateButtons()
end
function love.draw()
drawButtons()
end
You create a button with one simple line:
Code: Select all
button:new()
The first 4 are obligatory. These are: Code, text, x and y.
"Code" is the main star here. This is here you write all the code that is gonna be executed when the button is clicked.
You will have to start with "function()" then your code, and then but an "end" at the end.
Code: Select all
--Ex:
button:new(function()
local cakeIsGood = true
end
)
Note: The way the width and height of the button is determined by the text. This can be changed with ease.
Here is an example of a simple button:
Code: Select all
button:new(function()
local theCakeIsALie = true
end, "Click Me", 100, 100
)
--button:new(code, text, x, y)
Note: You will not get an error for NOT including these when you call "button:new()"
There are: rx, ry, textColor, font and color
Lets go through them:
rx and ry are how much you want to "round off" the buttons rectangle. Number values.
"textColor" is just the color of the text.
"font" is the font of the text. https://love2d.org/wiki/Tutorial:Fonts_and_Text
"color" is just the color of the button.
Note: both "textColor" and "color" are tables!
Here is an example of a button using all arguments:
Code: Select all
button:new(function()
referenceGotten = false
end, "Click Me", 20, 20, 10, 10, {0,0,255}, love.graphics.newFont(30), {255,0,0}
)
--button:new(code, text, x, y, textColor, font, color)