your-own-drum/options.lua

205 lines
4.4 KiB
Lua

local love = love
local draw = love.draw
local update = love.update
local mousepressed = love.mousepressed
local keypressed = love.keypressed
local mousemoved = love.mousemoved
local ExitMenu
options = options or {}
local options = options
local optionList = {}
local option
if not options.initialized then
options.initialized = true
local keyBindCallback = function(self, code)
self.value = code or self.value
end
options.optionValues = {
{ name = "options",
value = "",
callback = function(self)
end
},
{ name = "fullscreen",
value = false,
callback = function(self)
self.value = not(self.value)
love.window.setFullscreen( self.value )
love.event.push( "resize", love.graphics.getWidth(), love.graphics.getHeight() )
end
},
{ name = "high contrast",
value = false,
callback = function(self, code)
if code == "backspace" then return end
self.value = not( self.value )
return false
end
},
{ name = "volume",
value = 1.0,
callback = function(self, code)
local isIncreasing
if (code == "left" or code == "a") then isIncreasing = -1
elseif (code == "right" or code == "d") then isIncreasing = 1
else return false end
self.value = math.max( 0, math.min( 2,
self.value + (isIncreasing * 0.05 )))
love.audio.setVolume( self.value )
return true
end
},
{ name = "left",
value = "a",
callback = keyBindCallback
},
{ name = "right",
value = "d",
callback = keyBindCallback
},
{ name = "up",
value = "w",
callback = keyBindCallback
},
{ name = "down",
value = "s",
callback = keyBindCallback
}
}
do
for i, ov in ipairs( options.optionValues ) do
options[ ov.name ] = ov
end
end
end
local optionIdx = 1
local isAwaitingKey = false
local font = love.graphics.newFont( 32 )
local function Draw()
love.graphics.setColor( 1,1,1,1 )
if options["high contrast"].value then
love.graphics.setColor( 0, 0, 0, 1 )
end
for i, option in ipairs( options.optionValues ) do
love.graphics.printf( option.name, font, 100, i * 50, 1000, "left")
love.graphics.printf( tostring( option.value ), font, -100, i * 50, love.graphics.getWidth(), "right")
end
local w = love.graphics.getWidth()
love.graphics.setColor( 1,1,1,0.5 )
if isAwaitingKey then
love.graphics.rectangle( "fill", w - 200, optionIdx * 50, 100, 48, 10, 10 )
else
love.graphics.rectangle( "fill", 100, optionIdx * 50, 250, 48, 10, 10 )
end
end
local function SelectNextOption()
optionIdx = math.min( #options.optionValues, optionIdx + 1 )
option = options.optionValues[ optionIdx]
end
local function SelectPreviousOption()
optionIdx = math.max( 1, optionIdx - 1 )
option = options.optionValues[ optionIdx]
end
local function MouseMoved( x, y )
optionIdx =
math.min( #options.optionValues,
math.max( 1,
math.floor( y / 50 )
))
option = options.optionValues[ optionIdx]
if ( x > love.graphics.getWidth() - 200 ) then
isAwaitingKey = true
else
isAwaitingKey = false
end
end
local function KeyPress(key, code, isRepeat)
if not( isAwaitingKey ) then
if code == "backspace" then
return ExitMenu() end
if optionIdx and
code == "return" or
code == "right" then
isAwaitingKey = true
return
end
if code == "down" then return SelectNextOption() end
if code == "up" then return SelectPreviousOption() end
else
if code == "backspace" then isAwaitingKey = false end
if code == "escape" then return end
isAwaitingKey = (options.optionValues[optionIdx]):callback( code )
end
end
local function MousePressed( )
return KeyPress( )
end
local function EnterMenu()
draw = love.draw
update = love.update
mousepressed = love.mousepressed
keypressed = love.keypressed
mousemoved = love.mousemoved
love.mouse.setRelativeMode( false )
love.draw = Draw
love.keypressed = KeyPress
love.update = function() end
love.mousemoved = MouseMoved
love.mousepressed = MousePressed
end
function ExitMenu()
love.mouse.setRelativeMode( true )
love.mousemoved = mousemoved
love.keypressed = keypressed
love.mousepressed = mousepressed
love.update = update
love.draw = draw
end
options.EnterMenu = EnterMenu
options.ExitMenu = ExitMenu
return options