Pop.Box/pop/init.lua

100 lines
3.3 KiB
Lua
Raw Normal View History

2015-11-20 11:30:40 +00:00
local lf = love.filesystem
local lg = love.graphics
2015-11-18 03:27:06 +00:00
local pop = {}
2015-11-20 11:30:40 +00:00
local path = ... --NOTE Pop.Box must be required as its directory name (w SLASHES)!
2015-11-18 03:27:06 +00:00
-- elements are local
local box = require(path .. ".elements.box")
local text = require(path .. ".elements.text")
2015-11-21 00:41:11 +00:00
--TODO require these how skins are required
pop.elements = {}
local elements = lf.getDirectoryItems(path .. "/elements") --NOTE Pop.Box must be required with SLASHES!
for _, v in ipairs(elements) do
local name = v:sub(1,-5)
pop.elements[name] = require(path .. "/elements/" .. name) --TODO test this actually works right
end
2015-11-18 03:27:06 +00:00
2015-11-20 11:30:40 +00:00
-- skins define how elements are drawn
pop.skins = {}
local skins = lf.getDirectoryItems(path .. "/skins") --NOTE Pop.Box must be required with SLASHES!
for _, v in ipairs(skins) do
local name = v:sub(1,-5)
pop.skins[name] = require(path .. "/skins/" .. name)
pop.skins[name].name = name
end
pop.currentSkin = "clearspace" --default skin
2015-11-18 03:27:06 +00:00
-- everything has one parent element (initialized at the end)
pop.parentElement = false
2015-11-21 00:41:11 +00:00
-- this function actually creates elements based on what type of element is requested
2015-11-18 03:27:06 +00:00
function pop.create(elementType, parent, ...)
if not parent then
parent = pop.parentElement
end
local newElement
2015-11-21 00:41:11 +00:00
--TODO replace these with calls to pop.elements.ELEMENTNAME
2015-11-18 03:27:06 +00:00
if elementType == "box" then
newElement = box(pop, parent, ...)
elseif elementType == "text" then
newElement = text(pop, parent, ...)
else
error("Invalid element type: " .. elementType)
end
return newElement
end
-- pretty wrappers to call pop.element() instead of pop.create("element")
2015-11-20 11:30:40 +00:00
pop.box = function(...) return pop.create("box", ...) end
pop.text = function(...) return pop.create("text", ...) end
2015-11-18 03:27:06 +00:00
2015-11-21 00:41:11 +00:00
-- called every frame to draw elements
2015-11-18 03:27:06 +00:00
function pop.draw(element)
if not element then
element = pop.parentElement
end
element:draw()
2015-11-20 11:30:40 +00:00
for _, childElement in pairs(element.child) do
--pop.draw(childElement, element.x, element.y)
pop.draw(childElement)
end
2015-11-18 03:27:06 +00:00
end
-- TODO decide if we should track mouse movement
2015-11-21 00:41:11 +00:00
-- these are functions to be overwritten by user if they want a global event handler
2015-11-20 11:30:40 +00:00
function pop.onMousePress(button, x, y) end
function pop.onMouseRelease(button, x, y) end
2015-11-18 03:27:06 +00:00
function pop.mousepressed(button, x, y)
2015-11-20 11:30:40 +00:00
--TODO find which element it belongs to and if that element has a callback set,
-- if it does, use that, else, use the global callback (defined above..as nil)
2015-11-18 03:27:06 +00:00
end
function pop.mousereleased(button, x, y)
2015-11-20 11:30:40 +00:00
--TODO find which element it belongs to and if that element has a callback set,
-- if it does, use that, else, use the global callback (defined above..as nil)
2015-11-18 03:27:06 +00:00
end
2015-11-21 00:41:11 +00:00
function pop.keypressed(key, unicode)
--TODO handle this in some manner when needed
end
function pop.keyreleased(key)
--TODO handle this when needed
end
2015-11-18 03:27:06 +00:00
-- initialize the top element
2015-11-20 11:30:40 +00:00
pop.parentElement = box(pop, {child={}}) -- dummy object because it has no parent
--pop.parentElement:setSizeControl("specified") -- make it not try to update itself (TODO or based on outro, and custom code to check top parent against screen size)
--pop.parentElement:setSize(lg.getWidth(), lg.getHeight()) -- fill window size
2015-11-18 03:27:06 +00:00
--pop.parentElement:setVisible(false) -- uneeded since its clear...
return pop