Edit: I added a scrolling version that works like a Dance Dance Revolution song select screen. It's good for lots of menu items.
Download: Notes:
- This works really well with a gamestate library like hump.gamestate. If you're not already using one, you really should.
- One of the first things you should do when using this library is open it up and hack the draw function. The whole system is designed to be concise and easily hackable, but also easy to just drop in place.
- There isn't mouse support because the game I'm working on doesn't even use the mouse, I might add that later.
(You can see the library in action in the .love attached.)
Code: Select all
Menu = require 'menu.lua'
fullscreen = false
function love.load()
testmenu = Menu.new()
testmenu:addItem{
name = 'Start Game',
action = function()
-- do something
end
}
testmenu:addItem{
name = 'Options',
action = function()
-- do something
end
}
testmenu:addItem{
name = 'Quit',
action = function()
love.event.push('q')
end
}
end
function love.update(dt)
testmenu:update(dt)
end
function love.draw()
testmenu:draw(10, 10)
end
function love.keypressed(key)
testmenu:keypressed(key)
end
Don't forget to add all three of the callbacks!
You can even make items toggle themselves using self:
Code: Select all
testmenu:addItem{
name = 'Enable fullscreen',
action = function(self)
if not fullscreen then
if love.graphics.setMode(0, 0, true) then
fullscreen = true
self.name = 'Disable fullscreen'
end
else
if love.graphics.setMode(800, 600, false) then
fullscreen = false
self.name = 'Enable fullscreen'
end
end
end
}