Pop.Box/lib/pop/init.lua

292 lines
8.5 KiB
Lua
Raw Normal View History

local pop = {
_VERSION = 'Pop.Box v0.0.0',
_DESCRIPTION = 'GUI library for LOVE, designed for ease of use',
_URL = 'http://github.com/Guard13007/Pop.Box',
_LICENSE = 'The MIT License (MIT)',
_AUTHOR = 'Paul Liverman III'
}
if not (love.getVersion) then
2016-04-17 06:58:17 +00:00
error("Pop.Box only supports LOVE versions >= 0.9.1")
end
local filesystem, graphics
do
local _obj_0 = love
filesystem, graphics = _obj_0.filesystem, _obj_0.graphics
end
local insert
insert = table.insert
local inheritsFromElement
inheritsFromElement = require(tostring(...) .. "/util").inheritsFromElement
local path = ...
2016-02-25 01:02:56 +00:00
pop.elements = { }
2016-03-29 01:00:17 +00:00
pop.skins = { }
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 01:00:17 +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 01:00:17 +00:00
local name = elements[i]:sub(1, -5)
pop.elements[name] = require(tostring(path) .. "/elements/" .. tostring(name))
2016-04-03 07:27:15 +00:00
if pop.elements[name].load then
pop.elements[name].load(pop)
end
2016-03-29 01:00:17 +00:00
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
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
end
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
end
end
pop.screen = pop.create("element", false):setSize(graphics.getWidth(), graphics.getHeight())
return print("created \"pop.screen\"")
end
2016-03-29 01:00:17 +00:00
pop.create = function(element, parent, ...)
2016-02-25 01:02:56 +00:00
if parent == nil then
2016-03-29 01:00:17 +00:00
parent = pop.screen
end
if inheritsFromElement(parent) then
element = pop.elements[element](parent, ...)
2016-05-01 08:55:50 +00:00
insert(parent.data.child, element)
elseif parent == false then
element = pop.elements[element](false, ...)
else
element = pop.elements[element](pop.screen, parent, ...)
2016-05-01 08:55:50 +00:00
insert(pop.screen.data.child, element)
2016-02-25 01:02:56 +00:00
end
2016-03-29 01:00:17 +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 01:00:17 +00:00
element = pop.screen
2016-02-25 01:02:56 +00:00
end
2016-05-01 08:55:50 +00:00
if element.data.update then
2016-02-25 01:02:56 +00:00
if element.update then
element:update(dt)
end
2016-05-01 08:55:50 +00:00
for i = 1, #element.data.child do
pop.update(dt, element.data.child[i])
2016-02-25 01:02:56 +00:00
end
end
end
pop.draw = function(element)
if element == nil then
2016-03-29 01:00:17 +00:00
element = pop.screen
2016-02-25 01:02:56 +00:00
end
2016-05-01 08:55:50 +00:00
if element.data.draw then
2016-02-25 01:02:56 +00:00
if element.draw then
2016-03-29 01:00:17 +00:00
element:draw()
2016-02-25 01:02:56 +00:00
end
2016-05-01 08:55:50 +00:00
for i = 1, #element.data.child do
pop.draw(element.data.child[i])
2016-02-25 01:02:56 +00:00
end
end
end
2016-04-17 08:27:06 +00:00
pop.mousemoved = function(x, y, dx, dy)
if pop.focused and pop.focused.mousemoved then
return pop.focused:mousemoved(x, y, dx, dy)
end
return false
end
2016-03-29 01:00:17 +00:00
pop.mousepressed = function(x, y, button, element)
if not (element) then
print("mousepressed", x, y, button)
2016-03-29 01:00:17 +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
2016-05-01 08:55:50 +00:00
if (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.h) then
for i = #element.data.child, 1, -1 do
do
2016-05-01 08:55:50 +00:00
handled = pop.mousepressed(x, y, button, element.data.child[i])
if handled then
return handled
end
end
2016-03-30 17:50:33 +00:00
end
if not (handled) then
2016-05-01 08:55:50 +00:00
if element.mousepressed and element.data.draw then
do
2016-05-01 08:55:50 +00:00
handled = element:mousepressed(x - element.data.x, y - element.data.y, button)
if handled then
pop.focused = element
end
2016-03-30 17:50:33 +00:00
end
end
end
end
return handled
2016-02-25 01:02:56 +00:00
end
pop.mousereleased = function(x, y, button, element)
local clickedHandled = false
local mousereleasedHandled = false
if element then
2016-05-01 08:55:50 +00:00
if (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.h) then
for i = #element.data.child, 1, -1 do
clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, element.data.child[i])
if clickedHandled or mousereleasedHandled then
return clickedHandled, mousereleasedHandled
end
end
if not (clickedHandled or mousereleasedHandled) then
2016-05-01 08:55:50 +00:00
if element.clicked and element.data.draw then
clickedHandled = element:clicked(x - element.data.x, y - element.data.y, button)
end
if element.mousereleased then
2016-05-01 08:55:50 +00:00
mousereleasedHandled = element:mousereleased(x - element.data.x, y - element.data.y, button)
end
if clickedHandled then
pop.focused = element
end
end
end
else
print("mousereleased", x, y, button)
pop.mousereleased(x, y, button, pop.screen)
2016-02-25 01:02:56 +00:00
end
return clickedHandled, mousereleasedHandled
2016-02-25 01:02:56 +00:00
end
pop.keypressed = function(key)
2016-03-29 01:00:17 +00:00
print("keypressed", key)
2016-04-17 07:29:27 +00:00
local element = pop.focused
2016-05-01 08:55:50 +00:00
if element and element.keypressed and element.data.draw then
2016-04-17 07:29:27 +00:00
return element.keypressed(key)
end
2016-03-29 01:00:17 +00:00
return false
2016-02-25 01:02:56 +00:00
end
pop.keyreleased = function(key)
2016-03-29 01:00:17 +00:00
print("keyreleased", key)
2016-04-17 07:29:27 +00:00
local element = pop.focused
if element and element.keyreleased then
return element.keyreleased(key)
end
2016-03-29 01:00:17 +00:00
return false
2016-02-25 01:02:56 +00:00
end
pop.textinput = function(text)
2016-03-29 01:00:17 +00:00
print("textinput", text)
2016-04-17 07:29:27 +00:00
local element = pop.focused
2016-05-01 08:55:50 +00:00
if element and element.textinput and element.data.draw then
2016-04-17 07:29:27 +00:00
return element.textinput(text)
end
2016-03-29 01:00:17 +00:00
return false
2016-02-25 01:02:56 +00:00
end
2016-03-29 01:00:17 +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-05-01 08:55:50 +00:00
if element.data.background and skin.background then
element.data.background = skin.background
2016-02-25 01:02:56 +00:00
end
2016-05-01 08:55:50 +00:00
if element.data.color and skin.color then
element.data.color = skin.color
2016-02-25 01:02:56 +00:00
end
2016-05-01 08:55:50 +00:00
if element.data.font and skin.font then
element.data.font = skin.font
2016-02-25 01:02:56 +00:00
end
2016-03-29 01:00:17 +00:00
if not (depth or (depth == 0)) then
if depth == tonumber(depth) then
2016-05-01 08:55:50 +00:00
for i = 1, #element.data.child do
pop.skin(element.data.child[i], skin, depth - 1)
2016-03-29 01:00:17 +00:00
end
else
2016-05-01 08:55:50 +00:00
for i = 1, #element.data.child do
pop.skin(element.data.child[i], skin, false)
2016-03-29 01:00:17 +00:00
end
end
2016-02-25 01:02:56 +00:00
end
end
pop.debugDraw = function(element)
if element == nil then
2016-03-29 01:00:17 +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 01:00:17 +00:00
graphics.setLineColor(0, 0, 0, 100)
2016-05-01 08:55:50 +00:00
graphics.rectangle("fill", element.data.x, element.data.y, element.data.w, element.data.h)
2016-02-25 01:02:56 +00:00
graphics.setColor(150, 150, 150, 150)
2016-05-01 08:55:50 +00:00
graphics.rectangle("line", element.data.x, element.data.y, element.data.w, element.data.h)
2016-02-25 01:02:56 +00:00
graphics.setColor(200, 200, 200, 255)
2016-05-01 08:55:50 +00:00
graphics.print(".", element.data.x, element.data.y)
2016-02-25 01:02:56 +00:00
end
2016-05-01 08:55:50 +00:00
for i = 1, #element.data.child do
pop.debugDraw(element.data.child[i])
2016-02-25 01:02:56 +00:00
end
end
pop.printElementTree = function(element, depth)
if element == nil then
element = pop.screen
end
if depth == nil then
depth = 0
end
local cls = element.__class.__name
if cls == "text" then
cls = cls .. " (\"" .. tostring(element:getText():gsub("\n", "\\n")) .. "\")"
elseif cls == "box" then
local bg = element:getBackground()
if type(bg) == "table" then
bg = tostring(bg[1]) .. ", " .. tostring(bg[2]) .. ", " .. tostring(bg[3]) .. ", " .. tostring(bg[4])
end
cls = cls .. " (" .. tostring(bg) .. ")"
end
print(string.rep("-", depth) .. " " .. tostring(cls))
2016-05-01 08:55:50 +00:00
for i = 1, #element.data.child do
pop.printElementStack(element.data.child[i], depth + 1)
end
end
2016-02-25 01:02:56 +00:00
pop.load()
return pop