Localize function registry to Gamestate.switch().

This commit is contained in:
Matthias Richter 2013-08-19 14:10:00 +02:00
parent b3c329a1e8
commit 87b82750ff

View File

@ -60,10 +60,6 @@ function GS.current()
return stack[#stack] return stack[#stack]
end end
-- holds all defined love callbacks after GS.registerEvents is called
-- returns empty function on undefined callback
local registry = setmetatable({}, {__index = function() return __NULL__ end})
local all_callbacks = { local all_callbacks = {
'update', 'draw', 'focus', 'keypressed', 'keyreleased', 'update', 'draw', 'focus', 'keypressed', 'keyreleased',
'mousepressed', 'mousereleased', 'joystickpressed', 'mousepressed', 'mousereleased', 'joystickpressed',
@ -71,17 +67,20 @@ local all_callbacks = {
} }
function GS.registerEvents(callbacks) function GS.registerEvents(callbacks)
local registry = {}
callbacks = callbacks or all_callbacks callbacks = callbacks or all_callbacks
for _, f in ipairs(callbacks) do for _, f in ipairs(callbacks) do
registry[f] = love[f] registry[f] = love[f] or __NULL__
love[f] = function(...) return GS[f](...) end love[f] = function(...)
registry[f](...)
return GS[f](...)
end
end end
end end
-- forward any undefined functions -- forward any undefined functions
setmetatable(GS, {__index = function(_, func) setmetatable(GS, {__index = function(_, func)
return function(...) return function(...)
registry[func](...)
return (stack[#stack][func] or __NULL__)(stack[#stack], ...) return (stack[#stack][func] or __NULL__)(stack[#stack], ...)
end end
end}) end})