Merge pull request #13 from CentauriSoldier/master

Added State Change Callback Functions
This commit is contained in:
Caldas Lopes 2021-04-08 19:24:30 +01:00 committed by GitHub
commit 96dd0de7ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)
loveframes.state = name if (loveframes.state ~= name) then
loveframes.base.state = name
--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.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
--[[--------------------------------------------------------- --[[---------------------------------------------------------
@ -27,7 +83,7 @@ end
function loveframes.GetState() function loveframes.GetState()
return loveframes.state return loveframes.state
end end
--[[--------------------------------------------------------- --[[---------------------------------------------------------
@ -37,7 +93,7 @@ end
function loveframes.SetActiveSkin(name) function loveframes.SetActiveSkin(name)
local skin = name and loveframes.skins[name] local skin = name and loveframes.skins[name]
if not skin then print("SetActiveSkin: no such skin") return end if not skin then print("SetActiveSkin: no such skin") return end
loveframes.config["ACTIVESKIN"] = name loveframes.config["ACTIVESKIN"] = name
local object = loveframes.base local object = loveframes.base
object:SetSkin(name) object:SetSkin(name)
@ -78,7 +134,7 @@ function loveframes.GetCollisions(object, t)
local internals = object.internals local internals = object.internals
local objectstate = object.state local objectstate = object.state
local t = t or {} local t = t or {}
if objectstate == curstate and visible then if objectstate == curstate and visible then
local objectx = object.x local objectx = object.x
local objecty = object.y local objecty = object.y
@ -115,7 +171,7 @@ function loveframes.GetCollisions(object, t)
end end
end end
end end
return t return t
end end
@ -128,21 +184,21 @@ function loveframes.GetAllObjects(object, t)
local internals = object.internals local internals = object.internals
local children = object.children local children = object.children
local t = t or {} local t = t or {}
table.insert(t, object) table.insert(t, object)
if internals then if internals then
for k, v in ipairs(internals) do for k, v in ipairs(internals) do
loveframes.GetAllObjects(v, t) loveframes.GetAllObjects(v, t)
end end
end end
if children then if children then
for k, v in ipairs(children) do for k, v in ipairs(children) do
loveframes.GetAllObjects(v, t) loveframes.GetAllObjects(v, t)
end end
end end
return t return t
end end
@ -157,7 +213,7 @@ function loveframes.GetDirectoryContents(dir, t)
local t = t or {} local t = t or {}
local dirs = {} local dirs = {}
local files = love.filesystem.getDirectoryItems(dir) local files = love.filesystem.getDirectoryItems(dir)
for k, v in ipairs(files) do for k, v in ipairs(files) do
local isdir = love.filesystem.getInfo(dir.. "/" ..v) ~= nil and love.filesystem.getInfo(dir.. "/" ..v)["type"] == "directory" --love.filesystem.isDirectory(dir.. "/" ..v) local isdir = love.filesystem.getInfo(dir.. "/" ..v) ~= nil and love.filesystem.getInfo(dir.. "/" ..v)["type"] == "directory" --love.filesystem.isDirectory(dir.. "/" ..v)
if isdir == true then if isdir == true then
@ -170,19 +226,19 @@ function loveframes.GetDirectoryContents(dir, t)
end end
local name = table.concat(parts, ".") local name = table.concat(parts, ".")
table.insert(t, { table.insert(t, {
path = dir, path = dir,
fullpath = dir.. "/" ..v, fullpath = dir.. "/" ..v,
requirepath = loveframes.utf8.gsub(dir, "/", ".") .. "." ..name, requirepath = loveframes.utf8.gsub(dir, "/", ".") .. "." ..name,
name = name, name = name,
extension = extension extension = extension
}) })
end end
end end
for k, v in ipairs(dirs) do for k, v in ipairs(dirs) do
t = loveframes.GetDirectoryContents(v, t) t = loveframes.GetDirectoryContents(v, t)
end end
return t return t
end end
@ -194,11 +250,11 @@ end
--]]--------------------------------------------------------- --]]---------------------------------------------------------
function loveframes.Round(num, idp) function loveframes.Round(num, idp)
local mult = 10^(idp or 0) local mult = 10^(idp or 0)
if num >= 0 then if num >= 0 then
return math.floor(num * mult + 0.5) / mult return math.floor(num * mult + 0.5) / mult
else else
return math.ceil(num * mult - 0.5) / mult return math.ceil(num * mult - 0.5) / mult
end end
end end
@ -209,7 +265,7 @@ end
--]]--------------------------------------------------------- --]]---------------------------------------------------------
function loveframes.SplitString(str, pat) function loveframes.SplitString(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0 local t = {} -- NOTE: use {n = 0} in Lua-5.0
if pat == " " then if pat == " " then
local fpat = "(.-)" .. pat local fpat = "(.-)" .. pat
local last_end = 1 local last_end = 1
@ -244,7 +300,7 @@ function loveframes.SplitString(str, pat)
table.insert(t, cap) table.insert(t, cap)
end end
end end
return t return t
end end
@ -255,7 +311,7 @@ end
function loveframes.RemoveAll() function loveframes.RemoveAll()
loveframes.base.children = {} loveframes.base.children = {}
loveframes.base.internals = {} loveframes.base.internals = {}
loveframes.hoverobject = false loveframes.hoverobject = false
loveframes.downobject = false loveframes.downobject = false
loveframes.modalobject = false loveframes.modalobject = false
@ -273,7 +329,7 @@ function loveframes.TableHasValue(table, value)
return true return true
end end
end end
return false return false
end end
@ -283,7 +339,7 @@ end
--]]--------------------------------------------------------- --]]---------------------------------------------------------
function loveframes.TableHasKey(table, key) function loveframes.TableHasKey(table, key)
return table[key] ~= nil return table[key] ~= nil
end end
--[[--------------------------------------------------------- --[[---------------------------------------------------------
@ -348,9 +404,9 @@ end
- desc: returns loveframes.hoverobject - desc: returns loveframes.hoverobject
--]]--------------------------------------------------------- --]]---------------------------------------------------------
function loveframes.GetHoverObject() function loveframes.GetHoverObject()
return loveframes.hoverobject return loveframes.hoverobject
end end
--[[--------------------------------------------------------- --[[---------------------------------------------------------
@ -388,11 +444,11 @@ function loveframes.DebugDraw()
local fps = love.timer.getFPS() local fps = love.timer.getFPS()
local deltatime = love.timer.getDelta() local deltatime = love.timer.getDelta()
local font = loveframes.basicfontsmall local font = loveframes.basicfontsmall
if hoverobject then if hoverobject then
topcol = hoverobject topcol = hoverobject
end end
-- show frame docking zones -- show frame docking zones
if topcol.type == "frame" then if topcol.type == "frame" then
for k, v in pairs(topcol.dockzones) do for k, v in pairs(topcol.dockzones) do
@ -403,12 +459,12 @@ function loveframes.DebugDraw()
love.graphics.rectangle("line", v.x, v.y, v.width, v.height) love.graphics.rectangle("line", v.x, v.y, v.width, v.height)
end end
end end
-- outline the object that the mouse is hovering over -- outline the object that the mouse is hovering over
love.graphics.setColor(255/255, 204/255, 51/255, 255/255) love.graphics.setColor(255/255, 204/255, 51/255, 255/255)
love.graphics.setLineWidth(2) love.graphics.setLineWidth(2)
love.graphics.rectangle("line", topcol.x - 1, topcol.y - 1, topcol.width + 2, topcol.height + 2) love.graphics.rectangle("line", topcol.x - 1, topcol.y - 1, topcol.width + 2, topcol.height + 2)
-- draw main debug box -- draw main debug box
love.graphics.setFont(font) love.graphics.setFont(font)
love.graphics.setColor(0, 0, 0, 200/255) love.graphics.setColor(0, 0, 0, 200/255)
@ -420,7 +476,7 @@ function loveframes.DebugDraw()
love.graphics.print("FPS: " ..fps, infox + 10, infoy + 30) love.graphics.print("FPS: " ..fps, infox + 10, infoy + 30)
love.graphics.print("Delta Time: " ..deltatime, infox + 10, infoy + 40) love.graphics.print("Delta Time: " ..deltatime, infox + 10, infoy + 40)
love.graphics.print("Total Objects: " ..loveframes.objectcount, infox + 10, infoy + 50) love.graphics.print("Total Objects: " ..loveframes.objectcount, infox + 10, infoy + 50)
-- draw object information if needed -- draw object information if needed
if topcol.type ~= "base" then if topcol.type ~= "base" then
love.graphics.setColor(0, 0, 0, 200/255) love.graphics.setColor(0, 0, 0, 200/255)