2016-04-02 03:45:43 +00:00
|
|
|
if not (love.getVersion) then
|
|
|
|
error("Pop.Box only supports LÖVE versions >= 0.9.1")
|
|
|
|
end
|
2016-02-25 00:25:54 +00:00
|
|
|
local filesystem, graphics
|
|
|
|
do
|
|
|
|
local _obj_0 = love
|
|
|
|
filesystem, graphics = _obj_0.filesystem, _obj_0.graphics
|
|
|
|
end
|
|
|
|
local insert
|
|
|
|
insert = table.insert
|
2016-04-02 03:45:43 +00:00
|
|
|
local inheritsFromElement
|
|
|
|
inheritsFromElement = require(tostring(...) .. "/util").inheritsFromElement
|
2016-02-25 00:25:54 +00:00
|
|
|
local path = ...
|
2016-02-25 01:02:56 +00:00
|
|
|
local pop = { }
|
|
|
|
pop.elements = { }
|
2016-03-29 00:59:12 +00:00
|
|
|
pop.skins = { }
|
2016-04-02 03:45:43 +00:00
|
|
|
pop.events = { }
|
2016-03-29 00:59:12 +00:00
|
|
|
pop.screen = false
|
2016-03-30 17:50:33 +00:00
|
|
|
pop.focused = false
|
2016-02-25 01:02:56 +00:00
|
|
|
pop.load = function()
|
|
|
|
local elements = filesystem.getDirectoryItems(tostring(path) .. "/elements")
|
|
|
|
for i = 1, #elements do
|
2016-03-29 00:59:12 +00:00
|
|
|
local _continue_0 = false
|
|
|
|
repeat
|
|
|
|
if not (elements[i]:sub(-4) == ".lua") then
|
|
|
|
_continue_0 = true
|
|
|
|
break
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
2016-03-29 00:59:12 +00:00
|
|
|
local name = elements[i]:sub(1, -5)
|
|
|
|
pop.elements[name] = require(tostring(path) .. "/elements/" .. tostring(name))
|
|
|
|
print("element loaded: \"" .. tostring(name) .. "\"")
|
|
|
|
if not (pop[name]) then
|
|
|
|
if pop.elements[name].wrap then
|
|
|
|
pop[name] = pop.elements[name].wrap(pop)
|
|
|
|
else
|
|
|
|
pop[name] = function(...)
|
|
|
|
return pop.create(name, ...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
print("wrapper created: \"pop." .. tostring(name) .. "()\"")
|
|
|
|
end
|
|
|
|
_continue_0 = true
|
|
|
|
until true
|
|
|
|
if not _continue_0 then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local skins = filesystem.getDirectoryItems(tostring(path) .. "/skins")
|
|
|
|
for i = 1, #skins do
|
|
|
|
local _continue_0 = false
|
|
|
|
repeat
|
|
|
|
if not (skins[i]:sub(-4) == ".lua") then
|
|
|
|
_continue_0 = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
local name = skins[i]:sub(1, -5)
|
|
|
|
pop.skins[name] = require(tostring(path) .. "/skins/" .. tostring(name))
|
|
|
|
print("skin loaded: \"" .. tostring(name) .. "\"")
|
|
|
|
_continue_0 = true
|
|
|
|
until true
|
|
|
|
if not _continue_0 then
|
|
|
|
break
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
|
|
|
end
|
2016-04-02 03:45:43 +00:00
|
|
|
local extensions = filesystem.getDirectoryItems(tostring(path) .. "/extensions")
|
|
|
|
for i = 1, #extensions do
|
|
|
|
local _continue_0 = false
|
|
|
|
repeat
|
|
|
|
if not (extensions[i]:sub(-4) == ".lua") then
|
|
|
|
_continue_0 = true
|
|
|
|
break
|
2016-04-01 21:10:40 +00:00
|
|
|
end
|
2016-04-02 03:45:43 +00:00
|
|
|
local name = extensions[i]:sub(1, -5)
|
|
|
|
require(tostring(path) .. "/extensions/" .. tostring(name))
|
|
|
|
print("extension loaded: \"" .. tostring(name) .. "\"")
|
|
|
|
_continue_0 = true
|
|
|
|
until true
|
|
|
|
if not _continue_0 then
|
|
|
|
break
|
2016-04-01 21:10:40 +00:00
|
|
|
end
|
|
|
|
end
|
2016-04-02 03:45:43 +00:00
|
|
|
pop.screen = pop.create("element", false):setSize(graphics.getWidth(), graphics.getHeight())
|
|
|
|
return print("created \"pop.screen\"")
|
2016-04-01 21:10:40 +00:00
|
|
|
end
|
2016-03-29 00:59:12 +00:00
|
|
|
pop.create = function(element, parent, ...)
|
2016-02-25 01:02:56 +00:00
|
|
|
if parent == nil then
|
2016-03-29 00:59:12 +00:00
|
|
|
parent = pop.screen
|
|
|
|
end
|
2016-04-02 03:45:43 +00:00
|
|
|
if inheritsFromElement(parent) then
|
2016-04-03 06:33:18 +00:00
|
|
|
element = pop.elements[element](parent, ...)
|
2016-04-02 03:45:43 +00:00
|
|
|
insert(parent.child, element)
|
|
|
|
elseif parent == false then
|
2016-04-03 06:33:18 +00:00
|
|
|
element = pop.elements[element](false, ...)
|
2016-04-01 21:10:40 +00:00
|
|
|
else
|
2016-04-03 06:33:18 +00:00
|
|
|
element = pop.elements[element](pop.screen, parent, ...)
|
2016-04-02 03:45:43 +00:00
|
|
|
insert(pop.screen.child, element)
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
2016-03-29 00:59:12 +00:00
|
|
|
return element
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
|
|
|
pop.update = function(dt, element)
|
|
|
|
if element == nil then
|
2016-03-29 00:59:12 +00:00
|
|
|
element = pop.screen
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
2016-03-29 00:59:12 +00:00
|
|
|
if not (element.excludeUpdate) then
|
2016-02-25 01:02:56 +00:00
|
|
|
if element.update then
|
|
|
|
element:update(dt)
|
|
|
|
end
|
|
|
|
for i = 1, #element.child do
|
|
|
|
pop.update(dt, element.child[i])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
pop.draw = function(element)
|
|
|
|
if element == nil then
|
2016-03-29 00:59:12 +00:00
|
|
|
element = pop.screen
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
2016-03-29 00:59:12 +00:00
|
|
|
if not (element.excludeDraw) then
|
2016-02-25 01:02:56 +00:00
|
|
|
if element.draw then
|
2016-03-29 00:59:12 +00:00
|
|
|
element:draw()
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
|
|
|
for i = 1, #element.child do
|
2016-03-29 00:59:12 +00:00
|
|
|
pop.draw(element.child[i])
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-04-02 03:45:43 +00:00
|
|
|
pop.mousemoved = function(self, x, y, dx, dy)
|
|
|
|
if pop.focused and pop.focused.mousemoved then
|
|
|
|
return pop.focused:mousemoved(x, y, dx, dy)
|
|
|
|
end
|
2016-04-03 06:33:18 +00:00
|
|
|
print("mousemoved unhandled!")
|
2016-04-02 03:45:43 +00:00
|
|
|
return false
|
|
|
|
end
|
2016-03-29 00:59:12 +00:00
|
|
|
pop.mousepressed = function(x, y, button, element)
|
2016-04-02 03:45:43 +00:00
|
|
|
if not (element) then
|
|
|
|
print("mousepressed", x, y, button)
|
2016-03-29 00:59:12 +00:00
|
|
|
element = pop.screen
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
2016-03-30 17:50:33 +00:00
|
|
|
local handled = false
|
|
|
|
if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then
|
|
|
|
if element.mousepressed then
|
2016-04-03 06:33:18 +00:00
|
|
|
if element == pop.screen then
|
|
|
|
error("pop.screen somehow has a fucking mousepressed handler")
|
|
|
|
end
|
2016-03-30 17:50:33 +00:00
|
|
|
handled = element:mousepressed(x - element.x, y - element.y, button)
|
|
|
|
end
|
|
|
|
if handled then
|
2016-04-02 06:31:05 +00:00
|
|
|
print("pop.focused has been set!")
|
2016-03-30 17:50:33 +00:00
|
|
|
pop.focused = element
|
2016-04-03 06:33:18 +00:00
|
|
|
pop.events[button] = element
|
|
|
|
if element == pop.screen then
|
|
|
|
error("pop.screen fucking focused SOMEHOW")
|
|
|
|
end
|
2016-03-30 17:50:33 +00:00
|
|
|
else
|
|
|
|
for i = 1, #element.child do
|
|
|
|
handled = pop.mousepressed(x, y, button, element.child[i])
|
|
|
|
if handled then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return handled
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
2016-04-02 03:45:43 +00:00
|
|
|
pop.mousereleased = function(x, y, button)
|
|
|
|
print("mousereleased", x, y, button)
|
|
|
|
local clickedHandled = false
|
|
|
|
local mousereleasedHandled = false
|
|
|
|
do
|
|
|
|
local element = pop.events[button]
|
|
|
|
if element then
|
2016-04-03 06:33:18 +00:00
|
|
|
print("mousereleased should be functional")
|
2016-04-02 03:45:43 +00:00
|
|
|
if element.clicked and (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then
|
|
|
|
do
|
|
|
|
clickedHandled = element:clicked(x - element.x, y - element.y, button)
|
|
|
|
if clickedHandled then
|
|
|
|
pop.events[button] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-04-03 06:33:18 +00:00
|
|
|
for k, v in pairs(element) do
|
|
|
|
print(k, v)
|
|
|
|
end
|
2016-04-02 03:45:43 +00:00
|
|
|
if element.mousereleased then
|
2016-04-03 06:33:18 +00:00
|
|
|
print("mousereleased SHOULD be called")
|
2016-04-02 03:45:43 +00:00
|
|
|
do
|
|
|
|
mousereleasedHandled = element:mousereleased(x - element.x, y - element.y, button)
|
|
|
|
if mousereleasedHandled then
|
|
|
|
pop.events[button] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
2016-04-02 03:45:43 +00:00
|
|
|
return clickedHandled, mousereleasedHandled
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
|
|
|
pop.keypressed = function(key)
|
2016-03-29 00:59:12 +00:00
|
|
|
print("keypressed", key)
|
|
|
|
return false
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
|
|
|
pop.keyreleased = function(key)
|
2016-03-29 00:59:12 +00:00
|
|
|
print("keyreleased", key)
|
|
|
|
return false
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
|
|
|
pop.textinput = function(text)
|
2016-03-29 00:59:12 +00:00
|
|
|
print("textinput", text)
|
|
|
|
return false
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
2016-03-29 00:59:12 +00:00
|
|
|
pop.skin = function(element, skin, depth)
|
|
|
|
if element == nil then
|
|
|
|
element = pop.screen
|
|
|
|
end
|
|
|
|
if skin == nil then
|
|
|
|
skin = pop.skins.default
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
2016-03-29 00:59:12 +00:00
|
|
|
if element.background and skin.background then
|
2016-02-25 01:02:56 +00:00
|
|
|
element.background = skin.background
|
|
|
|
end
|
2016-03-29 00:59:12 +00:00
|
|
|
if element.color and skin.color then
|
2016-02-25 01:02:56 +00:00
|
|
|
element.color = skin.color
|
|
|
|
end
|
2016-03-29 00:59:12 +00:00
|
|
|
if element.font and skin.font then
|
2016-02-25 01:02:56 +00:00
|
|
|
element.font = skin.font
|
|
|
|
end
|
2016-03-29 00:59:12 +00:00
|
|
|
if not (depth or (depth == 0)) then
|
|
|
|
if depth == tonumber(depth) then
|
|
|
|
for i = 1, #element.child do
|
|
|
|
pop.skin(element.child[i], skin, depth - 1)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
for i = 1, #element.child do
|
|
|
|
pop.skin(element.child[i], skin, false)
|
|
|
|
end
|
2016-02-25 00:25:54 +00:00
|
|
|
end
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
pop.debugDraw = function(element)
|
|
|
|
if element == nil then
|
2016-03-29 00:59:12 +00:00
|
|
|
element = pop.screen
|
2016-02-25 01:02:56 +00:00
|
|
|
end
|
|
|
|
if element.debugDraw then
|
|
|
|
element:debugDraw()
|
|
|
|
else
|
|
|
|
graphics.setLineWidth(1)
|
2016-03-29 00:59:12 +00:00
|
|
|
graphics.setLineColor(0, 0, 0, 100)
|
2016-02-25 01:02:56 +00:00
|
|
|
graphics.rectangle("fill", element.x, element.y, element.w, element.h)
|
|
|
|
graphics.setColor(150, 150, 150, 150)
|
|
|
|
graphics.rectangle("line", element.x, element.y, element.w, element.h)
|
|
|
|
graphics.setColor(200, 200, 200, 255)
|
|
|
|
graphics.print(".", element.x, element.y)
|
|
|
|
end
|
|
|
|
for i = 1, #element.child do
|
|
|
|
pop.debugDraw(element.child[i])
|
|
|
|
end
|
2016-02-25 00:25:54 +00:00
|
|
|
end
|
2016-02-25 01:02:56 +00:00
|
|
|
pop.load()
|
|
|
|
return pop
|