There is a buttons barebone library, it supports what you want it to support.
For example:
Code: Select all
-- main.lua
local myButtons = require ('myButtons')
function love.load()
local color = {1,1,1}
local function onButtonHovered (button)
button.hovered = true
button.lineWidth = 4
end
local function onButtonNotHovered (button)
button.hovered = false
button.lineWidth = 2
end
local function onToggle (button)
button.toggled = not button.toggled
if button.toggled then
button.color = {0,1,0}
else
button.color = {1,0,0}
end
myButtons.buttons[2].enabled = button.toggled
end
local function drawButton (button)
love.graphics.setColor (button.color)
love.graphics.setLineWidth (button.lineWidth)
love.graphics.rectangle ('line', button.x, button.y, button.w, button.h)
if button.textColor then
love.graphics.setColor (button.textColor)
end
if button.text then
love.graphics.print (button.text, button.x, button.y)
end
end
local function drawDisabledButton (button)
love.graphics.setColor ({0.5,0.5,0.5})
love.graphics.setLineWidth (button.lineWidth)
love.graphics.rectangle ('line', button.x, button.y, button.w, button.h)
if button.textColor then
love.graphics.setColor (button.textColor)
end
if button.text then
love.graphics.print (button.text, button.x, button.y)
end
end
local function drawButton2 (button)
love.graphics.setColor (1,1,1)
love.graphics.draw (button.image, button.x, button.y)
if button.hovered then
love.graphics.rectangle ('line', button.x, button.y, button.w, button.h)
end
if button.text then
love.graphics.print (button.text, button.x, button.y)
end
end
local image = love.graphics.newCanvas (100, 100)
love.graphics.setCanvas (image)
love.graphics.setLineStyle ('rough')
love.graphics.line (10,20, 90,20, 55,90)
love.graphics.setCanvas ()
myButtons:new {x=50, y=10, w=100, h=100, color=color,
drawButton=drawButton,
onButtonHovered=onButtonHovered,
onButtonNotHovered=onButtonNotHovered,
onToggle = onToggle,
text = "First",
textColor = {1,1,1},
}
myButtons:new {x=200, y=10, w=100, h=100, color=color,
drawButton=drawButton,
onButtonHovered=onButtonHovered,
onButtonNotHovered=onButtonNotHovered,
onToggle = onToggle,
text = "Second",
enabled = false,
drawDisabledButton = drawDisabledButton,
}
myButtons:new {
x=350, y=10, w=100, h=100,
drawButton=drawButton2,
image=image,
onButtonHovered = onButtonHovered,
onButtonNotHovered=onButtonNotHovered,
text = "Third"}
myButtons:new {
x=500, y=10, w=100, h=100,
color={0,1,1},
drawButton=drawButton,
onButtonHovered = onButtonHovered,
onButtonNotHovered=onButtonNotHovered,
onButtonPressed = love.event.quit,
text = "Exit",
textColor = {1,1,1},
}
end
function love.update(dt)
myButtons.update (dt)
end
function love.draw()
myButtons.draw ()
end
function love.keypressed(key, scancode, isrepeat)
if false then
elseif key == "escape" then
love.event.quit()
end
end
function love.mousepressed( x, y, button, istouch, presses )
myButtons.mousepressed(x, y, button)
end
function love.mousemoved( x, y, dx, dy, istouch )
myButtons.mousemoved (x, y, dx, dy)
end
function love.mousereleased( x, y, button, istouch, presses )
myButtons.mousereleased(x, y, button)
end
Code: Select all
-- myButtons.lua
local myButtons = {}
myButtons.buttons = {} -- default button set
myButtons.buttonSets = {myButtons.buttons}
function myButtons:newButtonsSet ()
local buttonSet = {}
myButtons.buttons = buttonSet
table.insert (myButtons.buttonSets, buttonSet)
return buttonSet
end
function myButtons:setButtonsSet (buttonSet)
myButtons.buttons = buttonSet
return buttonSet
end
function myButtons:new (data)
local button = {}
for i, v in pairs (data) do
button[i] = v
end
if not (button.enabled == false) then
button.enabled = true
end
if button.onButtonNotHovered then
button:onButtonNotHovered ()
end
table.insert(myButtons.buttons, button)
return button
end
function myButtons.update(dt)
for i, button in ipairs(myButtons.buttons) do
if button.enabled and button.updateButton then
button:updateButton(dt)
end
end
end
local function isOn (mx, my, x, y, w, h)
if (mx > x) and (mx < (x + w)) and (my > y) and (my < (y + h)) then
return true
end
end
function myButtons.mousepressed(mx, my, mbutton)
for i, button in ipairs(myButtons.buttons) do
local x, y, w, h = button.x, button.y, button.w, button.h
if button.enabled and isOn (mx, my, x, y, w, h) then
if button.onToggle then
button:onToggle(mx, my, mbutton, button.value)
elseif button.onButtonPressed then
button:onButtonPressed(mx, my, mbutton)
end
end
end
end
function myButtons.mousereleased(mx, my, mbutton)
for i, button in ipairs(myButtons.buttons) do
local x, y, w, h = button.x, button.y, button.w, button.h
if button.enabled and isOn (mx, my, x, y, w, h) then
if button.onButtonrReleased then
button:onButtonrReleased(mx, my, mbutton)
end
elseif button.enabled and button.onOtherButtonReleased then
button:onOtherButtonReleased (mx, my, mbutton)
end
end
end
function myButtons.mousemoved (mx, my, dx, dy)
for i, button in ipairs(myButtons.buttons) do
local x, y, w, h = button.x, button.y, button.w, button.h
if button.enabled and isOn (mx, my, x, y, w, h) then
if button.onButtonHovered then
button:onButtonHovered(mx, my, dx, dy)
end
elseif button.enabled and button.onButtonNotHovered then
button:onButtonNotHovered(mx, my, dx, dy)
end
end
end
function myButtons.draw ()
for i, button in ipairs(myButtons.buttons) do
if button.enabled and button.drawButton then
button:drawButton ()
elseif button.drawDisabledButton then
button:drawDisabledButton ()
end
end
end
return myButtons