2010-09-19 13:37:55 +00:00
|
|
|
--[[
|
2012-04-10 15:00:02 +00:00
|
|
|
Copyright (c) 2010-2012 Matthias Richter
|
2010-09-19 13:37:55 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
Except as contained in this notice, the name(s) of the above copyright holders
|
|
|
|
shall not be used in advertising or otherwise to promote the sale, use or
|
|
|
|
other dealings in this Software without prior written authorization.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
]]--
|
|
|
|
|
2010-08-13 13:36:12 +00:00
|
|
|
local function __NULL__() end
|
2011-11-19 19:41:43 +00:00
|
|
|
|
2010-08-13 12:48:24 +00:00
|
|
|
-- default gamestate produces error on every callback
|
|
|
|
local function __ERROR__() error("Gamestate not initialized. Use Gamestate.switch()") end
|
2011-11-19 19:41:43 +00:00
|
|
|
local current = setmetatable({leave = __NULL__}, {__index = __ERROR__})
|
2010-08-13 12:48:24 +00:00
|
|
|
|
2011-11-19 19:41:43 +00:00
|
|
|
local GS = {}
|
|
|
|
function GS.new()
|
2013-01-26 11:56:59 +00:00
|
|
|
return setmetatable({}, {__index = function() return __NULL__ end})
|
2010-08-09 15:52:48 +00:00
|
|
|
end
|
|
|
|
|
2011-11-19 19:41:43 +00:00
|
|
|
function GS.switch(to, ...)
|
2010-11-16 15:26:33 +00:00
|
|
|
assert(to, "Missing argument: Gamestate to switch to")
|
2011-01-18 16:44:38 +00:00
|
|
|
current:leave()
|
|
|
|
local pre = current
|
2011-03-15 16:34:28 +00:00
|
|
|
to:init()
|
2013-01-26 11:56:59 +00:00
|
|
|
to.init = nil
|
2011-01-18 16:44:38 +00:00
|
|
|
current = to
|
2011-01-19 15:41:59 +00:00
|
|
|
return current:enter(pre, ...)
|
2010-08-09 15:52:48 +00:00
|
|
|
end
|
|
|
|
|
2011-11-19 19:41:43 +00:00
|
|
|
-- holds all defined love callbacks after GS.registerEvents is called
|
|
|
|
-- returns empty function on undefined callback
|
|
|
|
local registry = setmetatable({}, {__index = function() return __NULL__ end})
|
2010-10-31 12:04:08 +00:00
|
|
|
|
2011-11-19 19:41:43 +00:00
|
|
|
local all_callbacks = {
|
|
|
|
'update', 'draw', 'focus', 'keypressed', 'keyreleased',
|
|
|
|
'mousepressed', 'mousereleased', 'joystickpressed',
|
|
|
|
'joystickreleased', 'quit'
|
|
|
|
}
|
2010-10-31 12:04:08 +00:00
|
|
|
|
2011-11-19 19:41:43 +00:00
|
|
|
function GS.registerEvents(callbacks)
|
|
|
|
callbacks = callbacks or all_callbacks
|
|
|
|
for _, f in ipairs(callbacks) do
|
|
|
|
registry[f] = love[f]
|
2012-10-06 07:46:36 +00:00
|
|
|
love[f] = function(...) return GS[f](...) end
|
2011-11-19 19:41:43 +00:00
|
|
|
end
|
2010-08-09 15:52:48 +00:00
|
|
|
end
|
|
|
|
|
2011-11-19 19:41:43 +00:00
|
|
|
-- forward any undefined functions
|
|
|
|
setmetatable(GS, {__index = function(_, func)
|
|
|
|
return function(...)
|
|
|
|
registry[func](...)
|
2012-10-06 07:46:36 +00:00
|
|
|
return current[func](current, ...)
|
2011-11-19 19:41:43 +00:00
|
|
|
end
|
|
|
|
end})
|
2011-01-22 10:53:31 +00:00
|
|
|
|
2011-11-19 19:41:43 +00:00
|
|
|
return GS
|