vision/src/client/menu.lua

166 lines
3.7 KiB
Lua

local lg = assert( love.graphics )
local love = assert( love )
local scene = assert( require 'client.scene' )
local strings = assert( require 'client.assets.strings' )
local button = assert( require 'client.button' )
local menu = {}
local t = 0
local wWidth = 800
local wHeight = 600
local selectedButtonIdx = nil
local canvas = lg.newCanvas()
local buttons =
{
button{
x = 15, w = 400, h = 50,
y = 115,
text = strings.newgame_button,
color = { 0.3, 0.2, 0.9, 0.5 },
callback = function() return scene.game() end },
button{
x = 15, w = 400, h = 50,
y = 115 + 55,
text = strings.join_button,
color = { 0.3, 0.2, 0.8, 0.5 },
callback = function() return scene.browser() end },
button{
x = 15, w = 400, h = 50,
y = 115 + 55 * 2,
text = strings.option_button,
color = { 0.3, 0.2, 0.9, 0.5 },
callback = function() return scene.options() end },
button{
x = 15, w = 400, h = 50,
y = 115 + 55 * 3,
text = strings.quit_button,
color = { 0.3, 0.2, 1.0, 0.5 },
callback = love.event.quit },
}
local gradientQuad = lg.newMesh{
{
-- top-left corner (red-tinted)
0, 0, -- position of the vertex
0, 0, -- texture coordinate at the vertex position
0.4, 0.1, 0.4, 0.0 -- color of the vertex
},
{
-- top-right corner (green-tinted)
1, 0,
1, 0, -- texture coordinates are in the range of [0, 1]
0.8, 0.3, 0.1, 1.0
},
{
-- bottom-right corner (blue-tinted)
1, 1,
1, 1,
0.7, 0.4, 0.1, 1.0
},
{
-- bottom-left corner (yellow-tinted)
0, 1,
0, 1,
0.4, 0.15, 0.3, 0.0
},
}
function menu.draw()
lg.setCanvas()
lg.setColor( 1,1,1,1 )
lg.draw( canvas )
end
function menu.paint()
lg.setCanvas( canvas )
--bg
lg.setColor( 0.8, 0.6, 0.3, 1 )
lg.rectangle( "fill", 0, 0, wWidth, wHeight )
--buttons
for id, button in pairs( buttons ) do button:draw( ) end
--gradient
lg.setColor( 1, 1, 1, 1 )
lg.draw( gradientQuad, 0, 0, 0, wWidth, wHeight )
lg.setCanvas()
end
function menu.onLoad()
lg.setNewFont( "client/assets/SitkaB.ttc", 28 )
return menu.resize( lg.getDimensions() )
end
function menu.update( dt )
t = t + dt
end
function menu.resize( w, h )
wWidth, wHeight = w, h
canvas = lg.newCanvas()
for id, uiButton in pairs( buttons ) do
uiButton.w = wWidth
end
menu.paint()
end
function menu.mousemoved( x, y, dx, dy, istouch )
if selectedButtonIdx then
buttons[selectedButtonIdx].selected = false
selectedButtonIdx = nil
end
for id, menuButton in ipairs( buttons ) do
if menuButton:contains( x, y ) then
menuButton.selected = true
selectedButtonIdx = id
break
end
end
return menu.paint()
end
function menu.mousepressed( x, y, button, istouch, presses )
if not selectedButtonIdx then return end
local uiButton = buttons[selectedButtonIdx]
if uiButton:contains( x, y ) then return uiButton() end
return menu.paint()
end
function menu.keypressed( key, code, isrepeat )
if code == "escape" then
return love.event.quit()
end
if code == "return" and selectedButtonIdx then
return buttons[selectedButtonIdx]()
end
if code == "down" or code == "tab" or code == "up" then
local sbi = (selectedButtonIdx or 1)
buttons[sbi].selected = false
--Increment / decrement
sbi = sbi + ((code == "up") and -1 or 1)
if sbi > #buttons then sbi = 1 end
if sbi < 1 then sbi = #buttons end
--Assign
print( "selecting button: ", sbi )
selectedButtonIdx = sbi
buttons[selectedButtonIdx].selected = true
end
return menu.paint()
end
scene.mainmenu = menu
return menu