2010-09-19 13:37:55 +00:00
|
|
|
--[[
|
2013-02-20 15:54:57 +00:00
|
|
|
Copyright (c) 2010-2013 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
|
2013-01-27 10:43:39 +00:00
|
|
|
local current = {leave = __NULL__}
|
2010-08-13 12:48:24 +00:00
|
|
|
|
2011-11-19 19:41:43 +00:00
|
|
|
local GS = {}
|
2013-02-20 14:51:50 +00:00
|
|
|
function GS.new(t) return t or {} end -- constructor - deprecated!
|
2010-08-09 15:52:48 +00:00
|
|
|
|
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
|
|
|
local pre = current
|
2013-02-25 16:51:57 +00:00
|
|
|
;(current.leave or __NULL__)(self)
|
|
|
|
;(to.init or __NULL__)(to)
|
2013-01-26 11:56:59 +00:00
|
|
|
to.init = nil
|
2011-01-18 16:44:38 +00:00
|
|
|
current = to
|
2013-02-25 16:51:57 +00:00
|
|
|
return (current.enter or __NULL__)(current, 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](...)
|
2013-01-27 10:43:39 +00:00
|
|
|
return (current[func] or __NULL__)(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
|