Pop.Box/pop/init.lua

101 lines
2.3 KiB
Lua
Raw Normal View History

2015-11-20 11:30:40 +00:00
local lf = love.filesystem
local lg = love.graphics
2016-01-20 22:34:25 +00:00
local path = ...
2015-11-20 11:30:40 +00:00
2015-11-18 03:27:06 +00:00
local pop = {}
2016-01-20 22:34:25 +00:00
pop.elementClasses = {}
--pop.elements = {}
pop.window = false --top level element, defined in pop.load()
2015-11-20 11:30:40 +00:00
pop.skins = {}
2016-01-20 22:34:25 +00:00
pop.currentSkin = "clear"
2015-11-18 03:27:06 +00:00
2016-01-20 22:34:25 +00:00
function pop.load()
-- load element classes
local elementList = lf.getDirectoryItems(path .. "/elements")
2015-11-18 03:27:06 +00:00
2016-01-20 22:34:25 +00:00
for i=0, #elementList do
local name = elementList[i]:sub(1, -5)
pop.elementClasses[name] = require(path .. "/elements/" .. name)
-- wrapper to be able to call pop.element() to create elements
if not pop[name] then
pop[name] = function(...) return pop.create(name, ...) end
end
2015-11-18 03:27:06 +00:00
end
2016-01-20 22:34:25 +00:00
-- load skins
local skinList = lf.getDirectoryItems(path .. "/skins")
2015-11-18 03:27:06 +00:00
2016-01-20 22:34:25 +00:00
for i=0, #skinList do
local name = skinList[i]:sub(1, -5)
pop.skins[name] = require(path .. "/skins/" .. name)
pop.skins[name].name = name
end
-- set top element
pop.window = pop.create("box"):setSize(lg.getWidth(), lg.getHeight())
end
function pop.create(elementType, parent, ...)
if not parent then
parent = pop.window
2015-11-18 03:27:06 +00:00
end
2016-01-20 22:34:25 +00:00
local newElement = pop.elementClasses[elementType](pop, parent, ...)
table.insert(parent.child, newElement) --NOTE pop.window is its own parent!
2015-11-18 03:27:06 +00:00
return newElement
end
2016-01-20 22:34:25 +00:00
function pop.update(dt, element)
if not element then
element = pop.window
end
if element.update then
element:update(dt)
end
--TODO redo this loop
for _, childElement in pairs(element.child) do
pop.update(dt, childElement)
end
end
2015-11-18 03:27:06 +00:00
function pop.draw(element)
if not element then
2016-01-20 22:34:25 +00:00
element = pop.window
2015-11-18 03:27:06 +00:00
end
2016-01-20 22:34:25 +00:00
if element.skin.draw and element.skin.draw(element) then
-- do nothing...
elseif element.draw then
element:draw()
end
2015-11-20 11:30:40 +00:00
2016-01-20 22:34:25 +00:00
--TODO redo this loop
2015-11-20 11:30:40 +00:00
for _, childElement in pairs(element.child) do
pop.draw(childElement)
end
2015-11-18 03:27:06 +00:00
end
function pop.mousepressed(button, x, y)
2016-01-20 22:34:25 +00:00
--TODO find element, if it has a callback, call with button and LOCAL x/y
2015-11-18 03:27:06 +00:00
end
function pop.mousereleased(button, x, y)
2016-01-20 22:34:25 +00:00
--TODO find element, if it has a callback, call with button and LOCAL x/y
2015-11-18 03:27:06 +00:00
end
2016-01-20 22:34:25 +00:00
function pop.keypressed(key)
--TODO no idea what to do with this
2015-11-21 00:41:11 +00:00
end
function pop.keyreleased(key)
2016-01-20 22:34:25 +00:00
--TODO no idea what to do with this
2015-11-21 00:41:11 +00:00
end
2016-01-20 22:34:25 +00:00
pop.load()
2015-11-18 03:27:06 +00:00
return pop