Simple Buttons
Posted: Thu Apr 21, 2016 7:47 pm
While playing around with some code I figured that i needed to make buttons with ease and quickly. So i made a simple library and I am here to share it with you!
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
The code utilises an object oriented lib. called: "middleclass". Download here: https://github.com/kikito/middleclass
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.
Creating Buttons:
You create a button with one simple line:
Now of course there are some arguments. 9 to be exact.
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.
The other 3 arguments are self explanatory. text is the text, x is x position, y is y position
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:
As you might remember, I said there were 9 arguments. So let me explain the rest.
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:
If you have any questions then feel free to ask! I probably explained this horribly, so yeah
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)