mirror of
https://github.com/linux-man/LoveFrames.git
synced 2024-11-18 16:04:22 +00:00
Merge pull request #13 from CentauriSoldier/master
Added State Change Callback Functions
This commit is contained in:
commit
96dd0de7ba
@ -5,19 +5,75 @@
|
|||||||
|
|
||||||
return function(loveframes)
|
return function(loveframes)
|
||||||
---------- module start ----------
|
---------- module start ----------
|
||||||
|
|
||||||
-- util library
|
-- util library
|
||||||
--local util = {}
|
--local util = {}
|
||||||
|
|
||||||
|
local statecallbacks = {};
|
||||||
|
|
||||||
--[[---------------------------------------------------------
|
--[[---------------------------------------------------------
|
||||||
- func: SetState(name)
|
- func: SetState(name)
|
||||||
- desc: sets the current state
|
- desc: sets the current state
|
||||||
--]]---------------------------------------------------------
|
--]]---------------------------------------------------------
|
||||||
function loveframes.SetState(name)
|
function loveframes.SetState(name)
|
||||||
|
|
||||||
|
if (loveframes.state ~= name) then
|
||||||
|
|
||||||
|
--fire the closing function of the soon-to-be-closed state
|
||||||
|
if (type(statecallbacks[loveframes.state]) == "table" and
|
||||||
|
type(statecallbacks[loveframes.state].onclose) == "function") then
|
||||||
|
--callback must accept the name of the current state and soon-to-be-opened state as args
|
||||||
|
statecallbacks[loveframes.state].onclose(loveframes.state, name);
|
||||||
|
end
|
||||||
|
|
||||||
|
local oldstate = loveframes.state;
|
||||||
loveframes.state = name
|
loveframes.state = name
|
||||||
loveframes.base.state = name
|
loveframes.base.state = name
|
||||||
|
|
||||||
|
--fire the opening function of the newly-opened state
|
||||||
|
if (type(statecallbacks[loveframes.state]) == "table" and
|
||||||
|
type(statecallbacks[loveframes.state].onopen) == "function") then
|
||||||
|
--callback must accept the name of the current state and previous state as args
|
||||||
|
statecallbacks[loveframes.state].onopen(name, oldstate);
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
--[[---------------------------------------------------------
|
||||||
|
- func: SetStateOnOpenCallback(name, func)
|
||||||
|
- desc: sets a state's opening callback function
|
||||||
|
--]]---------------------------------------------------------
|
||||||
|
function loveframes.SetStateOnOpenCallback(name, func)
|
||||||
|
|
||||||
|
if (type(name) == "string" and name:gsub("%s", '') ~= '' and
|
||||||
|
type(func) == "function") then
|
||||||
|
|
||||||
|
if (not (statecallbacks[name])) then
|
||||||
|
statecallbacks[name] = {};
|
||||||
|
end
|
||||||
|
|
||||||
|
statecallbacks[name].onopen = func;
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
--[[---------------------------------------------------------
|
||||||
|
- func: SetStateOnOpenCallback(name, func)
|
||||||
|
- desc: sets a state's closing callback function
|
||||||
|
--]]---------------------------------------------------------
|
||||||
|
function loveframes.SetStateOnCloseCallback(name, func)
|
||||||
|
|
||||||
|
if (type(name) == "string" and name:gsub("%s", '') ~= '' and
|
||||||
|
type(func) == "function") then
|
||||||
|
|
||||||
|
if (not (statecallbacks[name])) then
|
||||||
|
statecallbacks[name] = {};
|
||||||
|
end
|
||||||
|
|
||||||
|
statecallbacks[name].onclose = func;
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--[[---------------------------------------------------------
|
--[[---------------------------------------------------------
|
||||||
|
Loading…
Reference in New Issue
Block a user