ring/scenes/settings.lua

122 lines
3.3 KiB
Lua

local love = assert( love )
local strings = require( "i18n" )
local settings = require( "settings" )
local t = {}
local currentSetting = false
local mouseOverSetting = false
local cachedSettingVal = nil
local menu = {}
for i, setting in ipairs( settings ) do
menu[i] = { x = 410, y = 10 + i * 25, w = love.graphics.getWidth() - 410 , h = 25 }
end
local function restoreCachedSetting()
if (not currentSetting) or (cachedSettingVal == nil) then return end
settings[currentSetting].val = cachedSettingVal
cachedSettingVal = nil
end
function t.keypressed( key, code, isRepeat )
if currentSetting then
if code == "escape" or code == "return" then
if code == "escape" then restoreCachedSetting() end
currentSetting = false
return
elseif settings[currentSetting].type == "keybind" then
settings[currentSetting].val = code
currentSetting = false
end
return
end
if code == "return" then
local main = require( "scenes.main" )
return main.play()
end
end
function t.wheelmoved(x, y)
if not currentSetting then return end
local setting = settings[currentSetting]
if setting.type == "number" then
if y > 0 then setting.val = setting.val + 1
elseif y < 0 then setting.val = setting.val - 1
end
end
end
function t.mousemoved( x, y )
for i, button in ipairs( menu ) do
if
x > button.x and
y > button.y and
y <= button.y + button.h then
mouseOverSetting = i
return
end
end
mouseOverSetting = false
end
function t.mousepressed( x, y )
for i, button in ipairs( menu ) do
if
x > button.x and
y > button.y and
y <= button.y + button.h then
restoreCachedSetting()
currentSetting = i
cachedSettingVal = settings[i].val
mouseOverSetting = false
end
end
end
function t.update()
end
function t.draw()
love.graphics.setScissor( 400, 0, love.graphics.getWidth() - 400, love.graphics.getHeight() )
love.graphics.setColor( 1, 0.1, 0.1, 0.2 )
if mouseOverSetting then
love.graphics.rectangle( "fill",
menu[mouseOverSetting].x,
menu[mouseOverSetting].y,
menu[mouseOverSetting].w,
menu[mouseOverSetting].h )
end
love.graphics.setColor( 1, 0.1, 0.1, 0.8 )
if currentSetting then
love.graphics.rectangle( "fill",
menu[currentSetting].x,
menu[currentSetting].y,
menu[currentSetting].w,
menu[currentSetting].h )
end
love.graphics.setColor( 1, 1, 1, 1 )
love.graphics.print( strings.settingsMenuTitle, 400 , 0 )
for i, setting in ipairs( settings ) do
local x, y = menu[i].x, menu[i].y
--love.graphics.rectangle( "line", x, y, love.graphics.getWidth() / 2, 25 )
love.graphics.print( setting.name, x + 10, y )
love.graphics.print( setting.val, x + 250, y )
end
if currentSetting then
love.graphics.print( strings.settingsMenuBackspace, 400, love.graphics.getHeight() - 2 * love.graphics.getFont():getHeight())
if settings[currentSetting].type == "number" then
love.graphics.print( strings.settingsMenuScroll, 400, love.graphics.getHeight() - 3 * love.graphics.getFont():getHeight())
end
else
love.graphics.print( strings.toContinue, 400, love.graphics.getHeight() - love.graphics.getFont():getHeight() )
end
end
return t