Pop.Box/init.lua

414 lines
12 KiB
Lua
Raw Normal View History

2016-06-21 19:32:58 +00:00
local pop = {
2016-08-02 23:30:59 +00:00
_VERSION = '0.0.0',
2016-06-21 19:32:58 +00:00
_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'
}
local log
log = function(...)
return print("[Pop.Box]", ...)
end
2016-06-21 19:32:58 +00:00
if not (love.getVersion) then
error("Pop.Box only supports LOVE versions >= 0.9.1")
end
local path = (...):gsub("%.", "/")
2016-08-02 23:30:59 +00:00
if (...):sub(-4) == "init" then
2016-08-21 05:29:16 +00:00
path = (...):sub(1, -5)
if not (path) then
path = "."
end
2016-08-02 23:30:59 +00:00
end
log("Require path detected: \"" .. tostring(path) .. "\"")
2016-06-21 19:32:58 +00:00
local filesystem, graphics
do
local _obj_0 = love
filesystem, graphics = _obj_0.filesystem, _obj_0.graphics
end
local insert
insert = table.insert
local inheritsFromElement
2016-08-02 23:30:59 +00:00
inheritsFromElement = require(tostring(path) .. "/util").inheritsFromElement
local dumps, loads
do
local _obj_0 = require(tostring(path) .. "/lib/bitser/bitser")
dumps, loads = _obj_0.dumps, _obj_0.loads
end
local major, minor, revision = love.getVersion()
if major == 0 and minor == 9 then
pop.constants = {
left_mouse = "l",
middle_mouse = "m",
right_mouse = "r",
button_4 = "x1",
button_5 = "x2",
mouse_wheel_down = "wd",
mouse_wheel_up = "wu"
}
elseif major == 0 and minor == 10 then
pop.constants = {
left_mouse = 1,
2017-04-11 17:42:31 +00:00
middle_mouse = 3,
right_mouse = 2,
button_4 = 4,
button_5 = 5
}
else
pop.constants = { }
end
2016-06-21 19:32:58 +00:00
pop.elements = { }
pop.skins = { }
2016-08-21 05:29:16 +00:00
pop.extensions = { }
2016-06-21 19:32:58 +00:00
pop.screen = false
pop.focused = false
2017-04-09 00:10:44 +00:00
pop.hovered = false
pop.log = log
pop.load = function(load_path)
if load_path == nil then
load_path = path
end
log("Loading elements from \"" .. tostring(load_path) .. "/elements\"")
local elements = filesystem.getDirectoryItems(tostring(load_path) .. "/elements")
2016-06-21 19:32:58 +00:00
for i = 1, #elements do
local _continue_0 = false
repeat
if not (elements[i]:sub(-4) == ".lua") then
log("Ignored non-Lua file \"" .. tostring(load_path) .. "/elements/" .. tostring(elements[i]) .. "\"")
2016-06-21 19:32:58 +00:00
_continue_0 = true
break
end
local name = elements[i]:sub(1, -5)
log("Requiring \"" .. tostring(name) .. "\" from \"" .. tostring(load_path) .. "/elements/" .. tostring(name) .. "\"")
pop.elements[name] = require(tostring(load_path) .. "/elements/" .. tostring(name))
2016-06-21 19:32:58 +00:00
if pop.elements[name].load then
pop.elements[name].load(pop)
end
log("Element loaded: \"" .. tostring(name) .. "\"")
2016-06-21 19:32:58 +00:00
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
log("Wrapper created: \"pop." .. tostring(name) .. "()\"")
2016-06-21 19:32:58 +00:00
end
_continue_0 = true
until true
if not _continue_0 then
break
end
end
local skins = filesystem.getDirectoryItems(tostring(load_path) .. "/skins")
2016-06-21 19:32:58 +00:00
for i = 1, #skins do
local _continue_0 = false
repeat
if not (skins[i]:sub(-4) == ".lua") then
log("Ignored non-Lua file \"" .. tostring(load_path) .. "/skins/" .. tostring(skins[i]) .. "\"")
2016-06-21 19:32:58 +00:00
_continue_0 = true
break
end
local name = skins[i]:sub(1, -5)
log("Requiring \"" .. tostring(name) .. "\" from \"" .. tostring(load_path) .. "/skins/" .. tostring(name) .. "\"")
pop.skins[name] = require(tostring(load_path) .. "/skins/" .. tostring(name))
2016-06-21 19:32:58 +00:00
if pop.skins[name].load then
pop.skins[name].load(pop)
end
log("Skin loaded: \"" .. tostring(name) .. "\"")
2016-06-21 19:32:58 +00:00
_continue_0 = true
until true
if not _continue_0 then
break
end
end
local extensions = filesystem.getDirectoryItems(tostring(load_path) .. "/extensions")
2016-06-21 19:32:58 +00:00
for i = 1, #extensions do
local _continue_0 = false
repeat
if not (extensions[i]:sub(-4) == ".lua") then
log("Ignored non-Lua file \"" .. tostring(load_path) .. "/extensions/" .. tostring(extensions[i]) .. "\"")
2016-06-21 19:32:58 +00:00
_continue_0 = true
break
end
local name = extensions[i]:sub(1, -5)
log("Requiring \"" .. tostring(name) .. "\" from \"" .. tostring(load_path) .. "/extensions/" .. tostring(name) .. "\"")
pop.extensions[name] = require(tostring(load_path) .. "/extensions/" .. tostring(name))
2017-04-30 23:40:29 +00:00
if type(pop.extensions[name]) == "table" and pop.extensions[name].load then
2016-08-21 05:29:16 +00:00
pop.extensions[name].load(pop)
end
log("Extension loaded: \"" .. tostring(name) .. "\"")
2016-06-21 19:32:58 +00:00
_continue_0 = true
until true
if not _continue_0 then
break
end
end
if not (pop.screen) then
pop.screen = pop.create("element", false):setSize(graphics.getWidth(), graphics.getHeight())
2017-04-09 00:10:44 +00:00
pop.screen.data.update = true
return log("Created \"pop.screen\"")
end
2016-06-21 19:32:58 +00:00
end
pop.create = function(element, parent, data, ...)
2016-06-21 19:32:58 +00:00
if parent == nil then
parent = pop.screen
end
if inheritsFromElement(parent) then
if type(data) == "table" then
element = pop.elements[element](parent, data, ...)
else
element = pop.elements[element](parent, { }, data, ...)
end
2016-06-21 19:32:58 +00:00
insert(parent.child, element)
insert(parent.data.child, element.data)
element.parent = parent
2016-08-21 07:49:16 +00:00
element.data.parent = parent.data
2016-06-21 19:32:58 +00:00
elseif parent == false then
if type(data) == "table" then
element = pop.elements[element](false, data, ...)
else
element = pop.elements[element](false, { }, data, ...)
end
2016-08-21 07:49:16 +00:00
element.parent = false
element.data.parent = false
2016-06-21 19:32:58 +00:00
else
if type(parent) == "table" then
element = pop.elements[element](pop.screen, parent, data, ...)
else
element = pop.elements[element](pop.screen, { }, parent, data, ...)
end
2016-06-21 19:32:58 +00:00
insert(pop.screen.child, element)
insert(pop.screen.data.child, element.data)
element.parent = pop.screen
2016-08-21 07:49:16 +00:00
element.data.parent = pop.screen.data
if element.parent and element.parent.childAdded then
print("working?")
element.parent:childAdded(element)
end
2016-06-21 19:32:58 +00:00
end
return element
end
pop.update = function(dt, element)
if element == nil then
element = pop.screen
end
if element.data.update then
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
element = pop.screen
end
if element.data.draw then
if element.draw then
element:draw()
end
for i = 1, #element.child do
pop.draw(element.child[i])
end
end
end
2017-04-09 00:10:44 +00:00
pop.mousemoved = function(x, y, dx, dy, element)
if element == nil then
element = pop.screen
end
2017-04-12 10:37:29 +00:00
if element.data.draw and element.data.hoverable and (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
2017-04-09 00:10:44 +00:00
pop.hovered = element
2017-04-12 10:37:29 +00:00
for i = 1, #element.child do
2017-04-09 00:10:44 +00:00
pop.mousemoved(x, y, dx, dy, element.child[i])
end
end
if pop.focused and pop.focused.mousemoved and element == pop.screen then
2016-09-08 04:42:19 +00:00
return pop.focused:mousemoved(x - pop.focused.data.x, y - pop.focused.data.y, dx, dy)
2016-06-21 19:32:58 +00:00
end
return false
end
pop.mousepressed = function(x, y, button, element)
if not (element) then
2017-04-30 23:40:29 +00:00
if button == pop.constants.mouse_wheel_down then
2017-04-12 10:37:29 +00:00
pop.wheelmoved(0, -1)
return true
2017-04-30 23:40:29 +00:00
elseif button == pop.constants.mouse_wheel_up then
2017-04-12 10:37:29 +00:00
pop.wheelmoved(0, 1)
return true
end
log("mousepressed", x, y, button)
2016-06-21 19:32:58 +00:00
element = pop.screen
end
local handled = false
2017-04-11 08:23:10 +00:00
if element.data.draw and (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.child, 1, -1 do
handled = pop.mousepressed(x, y, button, element.child[i])
if handled ~= false then
return handled
2016-06-21 19:32:58 +00:00
end
end
if element.mousepressed then
handled = element:mousepressed(x - element.data.x, y - element.data.y, button)
if handled ~= false then
pop.focused = element
2016-06-21 19:32:58 +00:00
end
end
end
return handled
end
pop.mousereleased = function(x, y, button, element)
local clickedHandled = false
local mousereleasedHandled = false
if element then
2017-04-11 03:32:31 +00:00
if element.data.draw and (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.child, 1, -1 do
2016-06-21 19:32:58 +00:00
clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, element.child[i])
if clickedHandled ~= false or mousereleasedHandled ~= false then
2016-06-21 19:32:58 +00:00
return clickedHandled, mousereleasedHandled
end
end
if element.clicked then
clickedHandled = element:clicked(x - element.data.x, y - element.data.y, button)
end
if element.mousereleased then
mousereleasedHandled = element:mousereleased(x - element.data.x, y - element.data.y, button)
end
if clickedHandled ~= false then
pop.focused = element
2016-06-21 19:32:58 +00:00
end
end
else
log("mousereleased", x, y, button)
2017-04-30 23:40:29 +00:00
do
element = pop.focused
if element then
if element.data.draw and (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
if element.clicked then
clickedHandled = element:clicked(x - element.data.x, y - element.data.y, button)
end
end
if element.mousereleased then
mousereleasedHandled = element:mousereleased(x - element.data.x, y - element.data.y, button)
end
if clickedHandled ~= false or mousereleasedHandled ~= false then
return clickedHandled, mousereleasedHandled
end
end
end
2016-06-21 19:32:58 +00:00
pop.mousereleased(x, y, button, pop.screen)
end
return clickedHandled, mousereleasedHandled
end
2017-04-12 10:37:29 +00:00
pop.wheelmoved = function(x, y)
log("wheelmoved", x, y)
if pop.hovered and pop.hovered.wheelmoved then
return pop.hovered:wheelmoved(x, y)
end
return false
end
2016-06-21 19:32:58 +00:00
pop.keypressed = function(key)
log("keypressed", key)
2016-06-21 19:32:58 +00:00
local element = pop.focused
if element and element.keypressed and element.data.draw then
return element.keypressed(key)
end
return false
end
pop.keyreleased = function(key)
log("keyreleased", key)
2016-06-21 19:32:58 +00:00
local element = pop.focused
if element and element.keyreleased then
return element.keyreleased(key)
end
return false
end
pop.textinput = function(text)
log("textinput", text)
2016-06-21 19:32:58 +00:00
local element = pop.focused
if element and element.textinput and element.data.draw then
return element.textinput(text)
end
return false
end
pop.import = function(data, parent)
if parent == nil then
parent = pop.screen
end
local element
if type(data) == "string" then
data = loads(data)
element = pop.create(data.type, parent, data)
else
element = pop.elements[data.type](parent, data)
insert(parent.child, element)
end
for i = 1, #data.child do
pop.import(data.child[i], element)
end
end
pop.export = function(element)
if element == nil then
element = pop.screen
end
return dumps(element.data)
end
2016-06-21 19:32:58 +00:00
pop.debugDraw = function(element)
if element == nil then
element = pop.screen
end
2017-04-11 03:32:31 +00:00
graphics.setLineWidth(1)
graphics.setColor(0, 0, 0, 100)
graphics.rectangle("fill", element.data.x, element.data.y, element.data.w, element.data.h)
graphics.setColor(150, 150, 150, 150)
graphics.rectangle("line", element.data.x, element.data.y, element.data.w, element.data.h)
graphics.setColor(200, 200, 200, 255)
if element.debugInfo then
graphics.print(tostring(element.__class.__name:sub(1, 1)) .. " (" .. tostring(element:debugInfo()) .. ")", element.data.x, element.data.y)
2016-06-21 19:32:58 +00:00
else
2017-04-11 03:32:31 +00:00
graphics.print(tostring(element.__class.__name:sub(1, 1)), element.data.x, element.data.y)
2016-06-21 19:32:58 +00:00
end
for i = 1, #element.child do
pop.debugDraw(element.child[i])
end
end
pop.printElementTree = function(element, fn, depth)
2016-06-21 19:32:58 +00:00
if element == nil then
element = pop.screen
end
if depth == nil then
depth = 0
end
if "table" ~= type(element) then
depth = fn
fn = element
element = pop.screen
end
if "number" == type(fn) then
depth = fn
fn = nil
end
if not (depth) then
depth = 0
end
2016-06-21 19:32:58 +00:00
local cls = element.__class.__name
2017-04-11 03:32:31 +00:00
if element.debugInfo then
cls = cls .. " (" .. tostring(element:debugInfo()) .. ")"
end
if fn then
cls = cls .. " " .. tostring(fn(element))
2016-06-21 19:32:58 +00:00
end
2017-04-09 00:10:44 +00:00
if depth > 0 then
log(string.rep("-", depth) .. " " .. tostring(cls))
else
log(cls)
end
2016-06-21 19:32:58 +00:00
for i = 1, #element.child do
pop.printElementTree(element.child[i], fn, depth + 1)
2016-06-21 19:32:58 +00:00
end
end
pop.load()
return pop