dcearth/ui/modal.lua

66 lines
1.2 KiB
Lua
Raw Normal View History

2024-04-29 01:21:32 +00:00
local love = assert( love )
local button = require( "ui.button" )
2024-04-29 01:21:32 +00:00
local t = {}
t.__index = t
local i = 0
function t.start( self )
2024-07-20 03:09:53 +00:00
print( "starting modal:", i + 1)
love.graphics.setScissor(
self.x or 0,
self.y or 0,
self.w or love.graphics.getWidth(),
2024-07-20 03:09:53 +00:00
self.h or love.graphics.getHeight())
2024-04-29 01:21:32 +00:00
i = i + 1
2024-07-20 03:09:53 +00:00
t[i] = { modal = self }
t.previous = t[i]
t.current = self
2024-04-29 01:21:32 +00:00
--store callbacks
for name in pairs( self ) do
if love[name] then
t[i][name] = love[name]
love[name] = self[name]
end
end
--store menus
local b = button.next
2024-07-20 03:09:53 +00:00
repeat
2024-04-29 01:21:32 +00:00
t[i][b] = b.visible
b = b.next
until b == button
end
function t.stop( self )
2024-07-20 03:09:53 +00:00
print( "stopping modal:", i )
2024-04-29 01:21:32 +00:00
--restore callbacks
for name in pairs( self ) do
if love[name] then
2024-07-20 03:09:53 +00:00
love[name] = t[i][name] or love[name]
2024-04-29 01:21:32 +00:00
end
end
--restore menus
local b = button
button.deselect()
2024-04-29 01:21:32 +00:00
repeat
b = b.next
2024-07-20 03:09:53 +00:00
b.visible = t[i][b] or false
2024-04-29 01:21:32 +00:00
until b == button
2024-07-20 03:09:53 +00:00
t.current = t[i].modal
2024-04-29 01:21:32 +00:00
t[i] = nil
i = i - 1
2024-07-20 03:09:53 +00:00
t.previous = t[i - 1]
2024-04-29 01:21:32 +00:00
2024-07-20 03:09:53 +00:00
love.graphics.setScissor(0, 0, love.graphics.getDimensions())
2024-04-29 01:21:32 +00:00
end
function t.new( modal )
return setmetatable( modal or {}, t )
end
return t