Pop.Box/pop/init.lua

188 lines
4.6 KiB
Lua
Raw Permalink 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 = {}
2016-01-21 23:01:15 +00:00
pop.window = {child = {}} --top level element, defined in pop.load()
2016-01-26 00:13:05 +00:00
pop.focused = pop.window --defaults to top level
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-21 22:18:28 +00:00
for i=1, #elementList do
2016-01-20 22:34:25 +00:00
local name = elementList[i]:sub(1, -5)
pop.elementClasses[name] = require(path .. "/elements/" .. name)
2016-01-21 23:01:15 +00:00
print("loaded \"" .. name .. "\" element")
2016-01-20 22:34:25 +00:00
-- wrapper to be able to call pop.element() to create elements
if not pop[name] then
pop[name] = function(...) return pop.create(name, ...) end
2016-01-21 23:37:33 +00:00
print("wrapper: pop." .. name .. "() created")
2016-01-20 22:34:25 +00:00
end
2015-11-18 03:27:06 +00:00
end
2016-01-20 22:34:25 +00:00
-- set top element
2016-01-21 22:18:28 +00:00
pop.window = pop.create("element"):setSize(lg.getWidth(), lg.getHeight())
2016-01-21 23:01:15 +00:00
print("created pop.window")
2016-01-20 22:34:25 +00:00
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, ...)
2016-01-21 23:01:15 +00:00
table.insert(parent.child, newElement) --NOTE pop.window is its own parent?
2016-01-20 22:34:25 +00:00
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
2016-01-25 21:46:21 +00:00
if not element.excludeUpdating then
if element.update then
element:update(dt)
end
2016-01-20 22:34:25 +00:00
2016-01-25 21:46:21 +00:00
for i=1,#element.child do
pop.update(dt, element.child[i])
end
2016-01-20 22:34:25 +00:00
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-21 22:18:28 +00:00
if not element.excludeRendering then
2016-01-21 23:37:33 +00:00
if element.draw then
2016-01-21 22:18:28 +00:00
element:draw()
end
2015-11-20 11:30:40 +00:00
2016-01-21 22:18:28 +00:00
for i=1,#element.child do
pop.draw(element.child[i])
end
2015-11-20 11:30:40 +00:00
end
2015-11-18 03:27:06 +00:00
end
2016-01-26 00:13:05 +00:00
function pop.mousepressed(button, x, y, element)
if not element then
element = pop.window
if (x < element.x) or (y < element.y) or
(x > (element.x + element.w)) or (y > (element.y + element.h)) then
return
end
end
local handled = false
for i=1,#element.child do
if (x >= element.x) and (y >= element.y) and
(x <= (element.x + element.w)) and (y <= (element.y + element.h)) then
handled = pop.mousepressed(button, x, y, element.child[i])
end
end
if (not handled) and element.mousepressed then
element:mousepressed(button, x - element.x, y - element.y)
pop.focused = element
end
return handled
2015-11-18 03:27:06 +00:00
end
2016-01-26 00:13:05 +00:00
function pop.mousereleased(button, x, y, element)
if not element then
element = pop.window
if (x < element.x) or (y < element.y) or
(x > (element.x + element.w)) or (y > (element.y + element.h)) then
return
end
end
local handled = false
for i=1,#element.child do
if (x >= element.x) and (y >= element.y) and
(x <= (element.x + element.w)) and (y <= (element.y + element.h)) then
handled = pop.mousereleased(button, x, y, element.child[i])
end
end
if not handled then
if element.mousereleased then
element:mousereleased(button, x - element.x, y - element.y)
handled = true
end
if element.clicked then
element:clicked(button, x - element.x, y - element.y)
handled = true
end
end
return handled
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-25 21:46:21 +00:00
function pop.textinput(text)
--TODO something useful will happen here
end
function pop.skin(element, skin, stop)
if element.background then
element.background = skin.background
end
if element.color then
element.color = skin.color
end
if element.font then
element.font = skin.font
end
if not stop then
for i=1,#element.child do
pop.skin(element.child[i], skin)
end
end
end
2016-01-25 21:46:21 +00:00
function pop.debugDraw(element)
if not element then
element = pop.window
end
if element.debugDraw then
element:debugDraw()
else
lg.setLineWidth(1)
lg.setColor(0, 0, 0, 100)
lg.rectangle("fill", self.x, self.y, self.w, self.h)
lg.setColor(150, 150, 150, 150)
lg.rectangle("line", self.x, self.y, self.w, self.h)
lg.setColor(200, 200, 200, 255)
lg.print(".", self.x, self.y)
end
for i=1,#element.child do
pop.debugDraw(element.child[i])
end
end
2016-01-20 22:34:25 +00:00
pop.load()
2015-11-18 03:27:06 +00:00
return pop