mirror of
https://github.com/TangentFoxy/Pop.Box.git
synced 2024-12-15 12:44:20 +00:00
complete rewrite mostly from scratch...mostly the same
This commit is contained in:
parent
a5e3714af8
commit
6571320310
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
lua51.dll
|
||||||
|
moon*.exe
|
||||||
|
moonscript.dll
|
@ -1,4 +1,5 @@
|
|||||||
@ECHO OFF
|
@ECHO OFF
|
||||||
|
REM Assumes you have Windows binaries in the project's root directory!
|
||||||
cd src
|
cd src
|
||||||
"%cd%\..\moonc.exe" -t ../lib .
|
"%cd%\..\moonc.exe" -t ../lib .
|
||||||
cd ..
|
cd ..
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
function love.conf(t)
|
function love.conf(t)
|
||||||
t.title = "Pop.Box Demo / Tests"
|
t.title = "Pop.Box demo/tests"
|
||||||
t.console = true
|
t.console = true
|
||||||
|
|
||||||
t.window.width = 960
|
t.window.width = 960
|
||||||
|
@ -1,17 +1,22 @@
|
|||||||
local pop = require "pop"
|
|
||||||
|
|
||||||
local lg = love.graphics
|
local lg = love.graphics
|
||||||
|
local pop
|
||||||
local visualTestsShown = false
|
|
||||||
local testsRun = false
|
|
||||||
local debugDrawEnabled = false
|
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
pop.text(nil, "Press \"s\" to show objects for visual testing/demo.\nPress \"t\" to run tests.\nPress \"d\" to toggle debug draw."):move(2, 2)
|
pop = require "pop"
|
||||||
--TODO correct the fact that the size is wrong here! (height doesn't take into account \n)
|
local c = pop.box():align("center", "center"):setSize(300, 300)
|
||||||
--NOTE width? Is width calculated correctly when \n's exist? TEST THIS (also test tabs)
|
pop.box(c, {255, 0, 0, 255}):setSize(100, 50)
|
||||||
pop.text(nil, "This is a test\ncollection of strings to see how width is determined.\nLooks like it takes width of widest line!"):move(30, 120)
|
pop.box(c, {0, 255, 0, 255}):align("center"):setSize(100, 100)
|
||||||
pop.element():align("right", "bottom"):setSize(25, 25):move(-5, -5)
|
pop.box(c, {0, 0, 255, 255}):align("right"):setSize(50, 100)
|
||||||
|
pop.box(c, {100, 100, 100, 255}):align("center", "center"):setSize(500, 100)
|
||||||
|
pop.box(c):align("center"):setSize(50, 500):move(0, -100)
|
||||||
|
pop.box(c, {255, 255, 0, 255}):align(false, "bottom"):setSize(100, 100)
|
||||||
|
pop.box(c, {255, 150, 0, 255}):align("center", "bottom"):setSize(100, 50)
|
||||||
|
pop.box(c, {0, 255, 255}):align("right", "bottom"):setSize(50, 100):move(-50)
|
||||||
|
pop.text(nil, "Here's some test text\n(with newlines)\nin the top left corner!")
|
||||||
|
pop.text(nil, "Here's some test text in the bottom right corner!"):align("right", "bottom")
|
||||||
|
pop.skin(pop.text("Here's easier-to-code test text in the center!"):align("center", "center", true)) -- 'true' means align to pixel!
|
||||||
|
w = pop.box(nil, {255, 255, 255, 255}):align(false, "bottom"):setSize(150, 150)
|
||||||
|
b = pop.box(w, {0, 0, 0, 255}):setMargin(5):setSize(100, 100)
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
@ -20,55 +25,29 @@ end
|
|||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
pop.draw()
|
pop.draw()
|
||||||
|
--pop.debugDraw()
|
||||||
if debugDrawEnabled then
|
|
||||||
pop.debugDraw()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.textinput(text)
|
function love.mousepressed(x, y, button)
|
||||||
pop.textinput(text)
|
pop.mousepressed(x, y, button)
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.mousepressed(button, x, y)
|
function love.mousereleased(x, y, button)
|
||||||
pop.mousepressed(button, x, y)
|
pop.mousereleased(x, y, button)
|
||||||
end
|
|
||||||
|
|
||||||
function love.mousereleased(button, x, y)
|
|
||||||
pop.mousereleased(button, x, y)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.keypressed(key)
|
function love.keypressed(key)
|
||||||
if key == "escape" then
|
local handled = pop.keypressed(key)
|
||||||
|
|
||||||
|
if (key == "escape") and (not handled) then
|
||||||
love.event.quit()
|
love.event.quit()
|
||||||
else
|
|
||||||
if (key == "s") and (not visualTestsShown) then
|
|
||||||
-- old visual tests
|
|
||||||
local align = pop.box():align("center", "center"):setSize(200, 200)
|
|
||||||
pop.box(align):align("left", "top"):setSize(75, 10):setColor(255, 0, 255, 255)
|
|
||||||
pop.box(align):align("center", "top"):setColor(100, 100, 100)
|
|
||||||
pop.box(align, {0, 255, 0, 255}):setSize(20, 5):align("right", "top")
|
|
||||||
pop.box(align):align("left", "center"):setColor(0, 0, 255)
|
|
||||||
pop.box(align):align("center", "center"):setSize(90, 90):setColor(255, 255, 255)
|
|
||||||
pop.box(align):align("right", "center"):setColor(255, 0, 0)
|
|
||||||
pop.box(align):align("left", "bottom"):setColor(0, 255, 0):setSize(nil, 40)
|
|
||||||
pop.box(align):align("center", "bottom"):setColor(255, 255, 0)
|
|
||||||
pop.box(align):align("right", "bottom"):setColor(0, 255, 255):setSize(40, 40)
|
|
||||||
--pop.box(nil, {255, 0, 0, 255}):align("left", "top"):setSize(50, 50) --TODO adjust z-height of elements
|
|
||||||
pop.text(nil, "Hello World!"):align("center"):setText("Hey, I've been modified!")--:move(0, 18)
|
|
||||||
pop.text(nil, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()-=_+[]{}\\|:;\"',./<>?`~"):align("center", "bottom")
|
|
||||||
|
|
||||||
visualTestsShown = true
|
|
||||||
elseif (key == "t") and (not testsRun) then
|
|
||||||
require "test"
|
|
||||||
elseif key == "d" then
|
|
||||||
debugDrawEnabled = not debugDrawEnabled
|
|
||||||
end
|
|
||||||
|
|
||||||
pop.keypressed(key)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.keyreleased(key)
|
function love.keyreleased(key)
|
||||||
pop.keyreleased(key)
|
pop.keyreleased(key)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function love.textinput(text)
|
||||||
|
pop.textinput(text)
|
||||||
|
end
|
||||||
|
@ -16,8 +16,8 @@ do
|
|||||||
if self.background then
|
if self.background then
|
||||||
if type(self.background) == "table" then
|
if type(self.background) == "table" then
|
||||||
graphics.setColor(self.background)
|
graphics.setColor(self.background)
|
||||||
|
graphics.rectangle("fill", self.x, self.y, self.w, self.h)
|
||||||
else
|
else
|
||||||
graphics.setColor(self.background)
|
|
||||||
local w, h = self.background:getDimensions()
|
local w, h = self.background:getDimensions()
|
||||||
w = self.w / w
|
w = self.w / w
|
||||||
h = self.h / h
|
h = self.h / h
|
||||||
@ -47,12 +47,16 @@ do
|
|||||||
if a == nil then
|
if a == nil then
|
||||||
a = 255
|
a = 255
|
||||||
end
|
end
|
||||||
self.background = {
|
if type(r) == "table" then
|
||||||
r,
|
self.background = r
|
||||||
g,
|
else
|
||||||
b,
|
self.background = {
|
||||||
a
|
r,
|
||||||
}
|
g,
|
||||||
|
b,
|
||||||
|
a
|
||||||
|
}
|
||||||
|
end
|
||||||
return self
|
return self
|
||||||
end,
|
end,
|
||||||
getColor = function(self)
|
getColor = function(self)
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
local graphics
|
local graphics
|
||||||
graphics = love.graphics
|
graphics = love.graphics
|
||||||
|
local floor
|
||||||
|
floor = math.floor
|
||||||
local element
|
local element
|
||||||
do
|
do
|
||||||
local _class_0
|
local _class_0
|
||||||
@ -15,10 +17,14 @@ do
|
|||||||
return self
|
return self
|
||||||
end,
|
end,
|
||||||
move = function(self, x, y)
|
move = function(self, x, y)
|
||||||
self.x = self.x + x
|
if x then
|
||||||
self.y = self.y + y
|
self.x = self.x + x
|
||||||
|
end
|
||||||
|
if y then
|
||||||
|
self.y = self.y + y
|
||||||
|
end
|
||||||
for i = 1, #self.child do
|
for i = 1, #self.child do
|
||||||
if not self.child[i].excludeMovement then
|
if not (self.child[i].excludeMovement) then
|
||||||
self.child[i]:move(x, y)
|
self.child[i]:move(x, y)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -27,24 +33,32 @@ do
|
|||||||
setPosition = function(self, x, y)
|
setPosition = function(self, x, y)
|
||||||
local oldX = self.x
|
local oldX = self.x
|
||||||
local oldY = self.y
|
local oldY = self.y
|
||||||
local _exp_0 = self.horizontal
|
if x then
|
||||||
if "left" == _exp_0 then
|
local _exp_0 = self.horizontal
|
||||||
self.x = x
|
if "left" == _exp_0 then
|
||||||
elseif "center" == _exp_0 then
|
self.x = x
|
||||||
self.x = x - self.w / 2
|
elseif "center" == _exp_0 then
|
||||||
elseif "right" == _exp_0 then
|
self.x = x - self.w / 2
|
||||||
self.x = x - self.w
|
elseif "right" == _exp_0 then
|
||||||
|
self.x = x - self.w
|
||||||
|
end
|
||||||
|
else
|
||||||
|
x = oldX
|
||||||
end
|
end
|
||||||
local _exp_1 = self.vertical
|
if y then
|
||||||
if "top" == _exp_1 then
|
local _exp_0 = self.vertical
|
||||||
self.y = y
|
if "top" == _exp_0 then
|
||||||
elseif "center" == _exp_1 then
|
self.y = y
|
||||||
self.y = y - self.h / 2
|
elseif "center" == _exp_0 then
|
||||||
elseif "bottom" == _exp_1 then
|
self.y = y - self.h / 2
|
||||||
self.y = y - self.h
|
elseif "bottom" == _exp_0 then
|
||||||
|
self.y = y - self.h
|
||||||
|
end
|
||||||
|
else
|
||||||
|
y = oldY
|
||||||
end
|
end
|
||||||
for i = 1, #self.child do
|
for i = 1, #self.child do
|
||||||
if not self.child[i].excludeMovement then
|
if not (self.child[i].excludeMovement) then
|
||||||
self.child[i]:move(x - oldX, y - oldY)
|
self.child[i]:move(x - oldX, y - oldY)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -57,7 +71,7 @@ do
|
|||||||
if "center" == _exp_0 then
|
if "center" == _exp_0 then
|
||||||
resultX = resultX + (self.w / 2)
|
resultX = resultX + (self.w / 2)
|
||||||
elseif "right" == _exp_0 then
|
elseif "right" == _exp_0 then
|
||||||
resultX = resultX + self.w
|
resultY = resultY + self.w
|
||||||
end
|
end
|
||||||
local _exp_1 = self.vertical
|
local _exp_1 = self.vertical
|
||||||
if "center" == _exp_1 then
|
if "center" == _exp_1 then
|
||||||
@ -102,7 +116,7 @@ do
|
|||||||
self:setSize(W, H)
|
self:setSize(W, H)
|
||||||
return self
|
return self
|
||||||
end,
|
end,
|
||||||
align = function(self, horizontal, vertical)
|
align = function(self, horizontal, vertical, toPixel)
|
||||||
self:setAlignment(horizontal, vertical)
|
self:setAlignment(horizontal, vertical)
|
||||||
self.x = self.parent.x
|
self.x = self.parent.x
|
||||||
self.y = self.parent.y
|
self.y = self.parent.y
|
||||||
@ -122,13 +136,17 @@ do
|
|||||||
elseif "bottom" == _exp_1 then
|
elseif "bottom" == _exp_1 then
|
||||||
self.y = self.y + (self.parent.h - self.h - self.margin)
|
self.y = self.y + (self.parent.h - self.h - self.margin)
|
||||||
end
|
end
|
||||||
|
if toPixel then
|
||||||
|
self.x = floor(self.x)
|
||||||
|
self.y = floor(self.y)
|
||||||
|
end
|
||||||
return self
|
return self
|
||||||
end,
|
end,
|
||||||
alignTo = function(self, element, horizontal, vertical)
|
alignTo = function(self, element, horizontal, vertical)
|
||||||
local realParent = self.parent
|
local parent = self.parent
|
||||||
self.parent = element
|
self.parent = element
|
||||||
self:align(horizontal, vertical)
|
self:align(horizontal, vertical)
|
||||||
self.parent = realParent
|
self.parent = parent
|
||||||
return self
|
return self
|
||||||
end,
|
end,
|
||||||
setAlignment = function(self, horizontal, vertical)
|
setAlignment = function(self, horizontal, vertical)
|
||||||
@ -139,6 +157,14 @@ do
|
|||||||
self.vertical = vertical
|
self.vertical = vertical
|
||||||
end
|
end
|
||||||
return self
|
return self
|
||||||
|
end,
|
||||||
|
setMargin = function(self, margin)
|
||||||
|
self.margin = margin
|
||||||
|
self:align()
|
||||||
|
return self
|
||||||
|
end,
|
||||||
|
getMargin = function(self)
|
||||||
|
return self.margin
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
_base_0.__index = _base_0
|
_base_0.__index = _base_0
|
||||||
@ -146,8 +172,13 @@ do
|
|||||||
__init = function(self, pop, parent)
|
__init = function(self, pop, parent)
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
self.child = { }
|
self.child = { }
|
||||||
self.x = parent.x or 0
|
if parent then
|
||||||
self.y = parent.y or 0
|
self.x = parent.x or 0
|
||||||
|
self.y = parent.y or 0
|
||||||
|
else
|
||||||
|
self.x = 0
|
||||||
|
self.y = 0
|
||||||
|
end
|
||||||
self.w = 10
|
self.w = 10
|
||||||
self.h = 10
|
self.h = 10
|
||||||
self.horizontal = "left"
|
self.horizontal = "left"
|
||||||
|
@ -5,13 +5,22 @@ do
|
|||||||
local _obj_0 = string
|
local _obj_0 = string
|
||||||
sub, len = _obj_0.sub, _obj_0.len
|
sub, len = _obj_0.sub, _obj_0.len
|
||||||
end
|
end
|
||||||
local path = sub(..., 1, len(...) - len("/text"))
|
local path = sub(..., 1, len(...) - len("/box"))
|
||||||
local element = require(tostring(path) .. "/element")
|
local element = require(tostring(path) .. "/element")
|
||||||
local text
|
local text
|
||||||
do
|
do
|
||||||
local _class_0
|
local _class_0
|
||||||
local _parent_0 = element
|
local _parent_0 = element
|
||||||
local _base_0 = {
|
local _base_0 = {
|
||||||
|
wrap = function(pop)
|
||||||
|
return function(parent, ...)
|
||||||
|
if type(parent) == "string" then
|
||||||
|
return pop.create("text", nil, parent, ...)
|
||||||
|
else
|
||||||
|
return pop.create("text", parent, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end,
|
||||||
draw = function(self)
|
draw = function(self)
|
||||||
graphics.setColor(self.color)
|
graphics.setColor(self.color)
|
||||||
graphics.setFont(self.font)
|
graphics.setFont(self.font)
|
||||||
@ -40,7 +49,7 @@ do
|
|||||||
local _exp_1 = self.vertical
|
local _exp_1 = self.vertical
|
||||||
if "center" == _exp_1 then
|
if "center" == _exp_1 then
|
||||||
self.y = self.y - ((h - self.h) / 2)
|
self.y = self.y - ((h - self.h) / 2)
|
||||||
elseif "right" == _exp_1 then
|
elseif "bottom" == _exp_1 then
|
||||||
self.y = self.y - (h - self.h - self.margin)
|
self.y = self.y - (h - self.h - self.margin)
|
||||||
end
|
end
|
||||||
self.w = w
|
self.w = w
|
||||||
@ -70,12 +79,16 @@ do
|
|||||||
if a == nil then
|
if a == nil then
|
||||||
a = 255
|
a = 255
|
||||||
end
|
end
|
||||||
self.color = {
|
if type(r) == "table" then
|
||||||
r,
|
self.color = r
|
||||||
g,
|
else
|
||||||
b,
|
self.color = {
|
||||||
a
|
r,
|
||||||
}
|
g,
|
||||||
|
b,
|
||||||
|
a
|
||||||
|
}
|
||||||
|
end
|
||||||
return self
|
return self
|
||||||
end,
|
end,
|
||||||
getColor = function(self)
|
getColor = function(self)
|
||||||
|
@ -8,38 +8,71 @@ insert = table.insert
|
|||||||
local path = ...
|
local path = ...
|
||||||
local pop = { }
|
local pop = { }
|
||||||
pop.elements = { }
|
pop.elements = { }
|
||||||
pop.window = {
|
pop.skins = { }
|
||||||
child = { }
|
pop.screen = false
|
||||||
}
|
|
||||||
pop.load = function()
|
pop.load = function()
|
||||||
local elements = filesystem.getDirectoryItems(tostring(path) .. "/elements")
|
local elements = filesystem.getDirectoryItems(tostring(path) .. "/elements")
|
||||||
for i = 1, #elements do
|
for i = 1, #elements do
|
||||||
local name = elements[i]:sub(1, -5)
|
local _continue_0 = false
|
||||||
pop.elements[name] = require(tostring(path) .. "/elements/" .. tostring(name))
|
repeat
|
||||||
print("loaded element: " .. tostring(name))
|
if not (elements[i]:sub(-4) == ".lua") then
|
||||||
if not pop[name] then
|
_continue_0 = true
|
||||||
pop[name] = function(...)
|
break
|
||||||
return pop.create(name, ...)
|
|
||||||
end
|
end
|
||||||
print("wrapper created: " .. tostring(name) .. "()")
|
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
|
||||||
end
|
end
|
||||||
pop.window = pop.create("element"):setSize(graphics.getWidth(), graphics.getHeight())
|
local skins = filesystem.getDirectoryItems(tostring(path) .. "/skins")
|
||||||
return print("created window")
|
for i = 1, #skins do
|
||||||
end
|
local _continue_0 = false
|
||||||
pop.create = function(elementType, parent, ...)
|
repeat
|
||||||
if parent == nil then
|
if not (skins[i]:sub(-4) == ".lua") then
|
||||||
parent = pop.window
|
_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
|
||||||
|
end
|
||||||
end
|
end
|
||||||
local newElement = pop.elements[elementType](parent, ...)
|
pop.screen = pop.create("element", false):setSize(graphics.getWidth(), graphics.getHeight())
|
||||||
insert(parent.child, newElement)
|
return print("created \"pop.screen\"")
|
||||||
return newElement
|
end
|
||||||
|
pop.create = function(element, parent, ...)
|
||||||
|
if parent == nil then
|
||||||
|
parent = pop.screen
|
||||||
|
end
|
||||||
|
element = pop.elements[element](pop, parent, ...)
|
||||||
|
if parent then
|
||||||
|
insert(parent.child, element)
|
||||||
|
end
|
||||||
|
return element
|
||||||
end
|
end
|
||||||
pop.update = function(dt, element)
|
pop.update = function(dt, element)
|
||||||
if element == nil then
|
if element == nil then
|
||||||
element = pop.window
|
element = pop.screen
|
||||||
end
|
end
|
||||||
if not element.excludeUpdating then
|
if not (element.excludeUpdate) then
|
||||||
if element.update then
|
if element.update then
|
||||||
element:update(dt)
|
element:update(dt)
|
||||||
end
|
end
|
||||||
@ -50,86 +83,80 @@ pop.update = function(dt, element)
|
|||||||
end
|
end
|
||||||
pop.draw = function(element)
|
pop.draw = function(element)
|
||||||
if element == nil then
|
if element == nil then
|
||||||
element = pop.window
|
element = pop.screen
|
||||||
end
|
end
|
||||||
if not element.excludeRendering then
|
if not (element.excludeDraw) then
|
||||||
if element.draw then
|
if element.draw then
|
||||||
local _
|
element:draw()
|
||||||
do
|
|
||||||
local _base_0 = element
|
|
||||||
local _fn_0 = _base_0.draw
|
|
||||||
_ = function(...)
|
|
||||||
return _fn_0(_base_0, ...)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
for i = 1, #element.child do
|
for i = 1, #element.child do
|
||||||
pop.draw(element.child)
|
pop.draw(element.child[i])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
pop.mousepressed = function(button, x, y, element)
|
pop.mousepressed = function(x, y, button, element)
|
||||||
if element == nil then
|
if element == nil then
|
||||||
element = pop.window
|
element = pop.screen
|
||||||
end
|
|
||||||
if (x >= element.x) and (x <= (element.x + element.w)) then
|
|
||||||
if (y >= element.y) and (y <= (element.y + element.h)) then
|
|
||||||
for i = 1, #element.child do
|
|
||||||
if pop.mousepressed(button, x, y, element.child[i]) then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if element.mousepressed then
|
|
||||||
return element:mousepressed(button, x - element.x, y - element.y)
|
|
||||||
else
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
print("mousepressed", x, y, button, element)
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
pop.mousereleased = function(button, x, y, element)
|
pop.mousereleased = function(x, y, button, element)
|
||||||
if element == nil then
|
if element == nil then
|
||||||
element = pop.window
|
element = pop.screen
|
||||||
end
|
end
|
||||||
|
print("mousereleased", x, y, button, element)
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
pop.keypressed = function(key)
|
pop.keypressed = function(key)
|
||||||
return print("pop.keypressed() is unimplemented.")
|
print("keypressed", key)
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
pop.keyreleased = function(key)
|
pop.keyreleased = function(key)
|
||||||
return print("pop.keyreleased() is unimplemented.")
|
print("keyreleased", key)
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
pop.textinput = function(text)
|
pop.textinput = function(text)
|
||||||
return print("pop.textinput() is unimplemented.")
|
print("textinput", text)
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
pop.skin = function(element, skin, apply_to_children)
|
pop.skin = function(element, skin, depth)
|
||||||
if apply_to_children == nil then
|
if element == nil then
|
||||||
apply_to_children = true
|
element = pop.screen
|
||||||
end
|
end
|
||||||
element.margin = skin.margin
|
if skin == nil then
|
||||||
if element.background then
|
skin = pop.skins.default
|
||||||
|
end
|
||||||
|
if element.background and skin.background then
|
||||||
element.background = skin.background
|
element.background = skin.background
|
||||||
end
|
end
|
||||||
if element.color then
|
if element.color and skin.color then
|
||||||
element.color = skin.color
|
element.color = skin.color
|
||||||
end
|
end
|
||||||
if element.font then
|
if element.font and skin.font then
|
||||||
element.font = skin.font
|
element.font = skin.font
|
||||||
end
|
end
|
||||||
if apply_to_children then
|
if not (depth or (depth == 0)) then
|
||||||
for i = 1, #element.child do
|
if depth == tonumber(depth) then
|
||||||
pop.skin(element.child[i], skin)
|
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
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
pop.debugDraw = function(element)
|
pop.debugDraw = function(element)
|
||||||
if element == nil then
|
if element == nil then
|
||||||
element = pop.window
|
element = pop.screen
|
||||||
end
|
end
|
||||||
if element.debugDraw then
|
if element.debugDraw then
|
||||||
element:debugDraw()
|
element:debugDraw()
|
||||||
else
|
else
|
||||||
graphics.setLineWidth(1)
|
graphics.setLineWidth(1)
|
||||||
graphics.setColor(0, 0, 0, 100)
|
graphics.setLineColor(0, 0, 0, 100)
|
||||||
graphics.rectangle("fill", element.x, element.y, element.w, element.h)
|
graphics.rectangle("fill", element.x, element.y, element.w, element.h)
|
||||||
graphics.setColor(150, 150, 150, 150)
|
graphics.setColor(150, 150, 150, 150)
|
||||||
graphics.rectangle("line", element.x, element.y, element.w, element.h)
|
graphics.rectangle("line", element.x, element.y, element.w, element.h)
|
||||||
|
@ -1,13 +0,0 @@
|
|||||||
local graphics
|
|
||||||
graphics = love.graphics
|
|
||||||
return {
|
|
||||||
color = {
|
|
||||||
255,
|
|
||||||
255,
|
|
||||||
255,
|
|
||||||
255
|
|
||||||
},
|
|
||||||
background = false,
|
|
||||||
font = graphics.newFont(13),
|
|
||||||
margin = 2
|
|
||||||
}
|
|
16
demo/pop/skins/default.lua
Normal file
16
demo/pop/skins/default.lua
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
local lg = love.graphics
|
||||||
|
return {
|
||||||
|
background = {
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
220
|
||||||
|
},
|
||||||
|
color = {
|
||||||
|
255,
|
||||||
|
255,
|
||||||
|
255,
|
||||||
|
250
|
||||||
|
},
|
||||||
|
font = lg.newFont(14)
|
||||||
|
}
|
@ -1,13 +0,0 @@
|
|||||||
local graphics
|
|
||||||
graphics = love.graphics
|
|
||||||
return {
|
|
||||||
color = {
|
|
||||||
255,
|
|
||||||
255,
|
|
||||||
255,
|
|
||||||
255
|
|
||||||
},
|
|
||||||
background = false,
|
|
||||||
font = graphics.newFont(13),
|
|
||||||
margin = 2
|
|
||||||
}
|
|
@ -10,15 +10,15 @@ class box extends element
|
|||||||
|
|
||||||
@background = background
|
@background = background
|
||||||
|
|
||||||
draw: => --NOTE these ifs might be wrong? test this!
|
draw: =>
|
||||||
if @background
|
if @background
|
||||||
if type(@background) == "table"
|
if type(@background) == "table"
|
||||||
graphics.setColor @background
|
graphics.setColor @background
|
||||||
|
graphics.rectangle "fill", @x, @y, @w, @h
|
||||||
else
|
else
|
||||||
graphics.setColor @background
|
|
||||||
w, h = @background\getDimensions!
|
w, h = @background\getDimensions!
|
||||||
w = @w/w
|
w = @w / w
|
||||||
h = @h/h
|
h = @h / h
|
||||||
graphics.draw @background, @x, @y, 0, w, h
|
graphics.draw @background, @x, @y, 0, w, h
|
||||||
|
|
||||||
return @
|
return @
|
||||||
@ -42,11 +42,15 @@ class box extends element
|
|||||||
return @background
|
return @background
|
||||||
|
|
||||||
setColor: (r, g, b, a=255) =>
|
setColor: (r, g, b, a=255) =>
|
||||||
@background = {r, g, b, a}
|
if type(r) == "table"
|
||||||
|
@background = r
|
||||||
|
else
|
||||||
|
@background = {r, g, b, a}
|
||||||
|
|
||||||
return @
|
return @
|
||||||
|
|
||||||
getColor: =>
|
getColor: =>
|
||||||
if type(@background) == "table"
|
if type(@background) == "table"
|
||||||
return unpack @background
|
return unpack @background
|
||||||
else
|
else
|
||||||
error "This box doesn't have a color." --might be bad design...
|
error "This box doesn't have a color." --might be a bad idea
|
||||||
|
@ -1,12 +1,18 @@
|
|||||||
import graphics from love
|
import graphics from love
|
||||||
|
import floor from math
|
||||||
|
|
||||||
class element
|
class element
|
||||||
new: (pop, parent) =>
|
new: (pop, parent) =>
|
||||||
@parent = parent
|
@parent = parent
|
||||||
@child = {}
|
@child = {}
|
||||||
|
|
||||||
@x = parent.x or 0
|
if parent
|
||||||
@y = parent.y or 0
|
@x = parent.x or 0
|
||||||
|
@y = parent.y or 0
|
||||||
|
else
|
||||||
|
@x = 0
|
||||||
|
@y = 0
|
||||||
|
|
||||||
@w = 10
|
@w = 10
|
||||||
@h = 10
|
@h = 10
|
||||||
|
|
||||||
@ -26,11 +32,13 @@ class element
|
|||||||
return @
|
return @
|
||||||
|
|
||||||
move: (x, y) =>
|
move: (x, y) =>
|
||||||
@x += x
|
if x
|
||||||
@y += y
|
@x = @x + x
|
||||||
|
if y
|
||||||
|
@y = @y + y
|
||||||
|
|
||||||
for i = 1, #@child
|
for i = 1, #@child
|
||||||
if not @child[i].excludeMovement
|
unless @child[i].excludeMovement
|
||||||
@child[i]\move x, y
|
@child[i]\move x, y
|
||||||
|
|
||||||
return @
|
return @
|
||||||
@ -39,24 +47,30 @@ class element
|
|||||||
oldX = @x
|
oldX = @x
|
||||||
oldY = @y
|
oldY = @y
|
||||||
|
|
||||||
switch @horizontal
|
if x
|
||||||
when "left"
|
switch @horizontal
|
||||||
@x = x
|
when "left"
|
||||||
when "center"
|
@x = x
|
||||||
@x = x - @w/2
|
when "center"
|
||||||
when "right"
|
@x = x - @w/2
|
||||||
@x = x - @w
|
when "right"
|
||||||
|
@x = x - @w
|
||||||
|
else
|
||||||
|
x = oldX
|
||||||
|
|
||||||
switch @vertical
|
if y
|
||||||
when "top"
|
switch @vertical
|
||||||
@y = y
|
when "top"
|
||||||
when "center"
|
@y = y
|
||||||
@y = y - @h/2
|
when "center"
|
||||||
when "bottom"
|
@y = y - @h/2
|
||||||
@y = y - @h
|
when "bottom"
|
||||||
|
@y = y - @h
|
||||||
|
else
|
||||||
|
y = oldY
|
||||||
|
|
||||||
for i = 1, #@child
|
for i = 1, #@child
|
||||||
if not @child[i].excludeMovement
|
unless @child[i].excludeMovement
|
||||||
@child[i]\move x - oldX, y - oldY
|
@child[i]\move x - oldX, y - oldY
|
||||||
|
|
||||||
return @
|
return @
|
||||||
@ -69,7 +83,7 @@ class element
|
|||||||
when "center"
|
when "center"
|
||||||
resultX += @w/2
|
resultX += @w/2
|
||||||
when "right"
|
when "right"
|
||||||
resultX += @w
|
resultY += @w
|
||||||
|
|
||||||
switch @vertical
|
switch @vertical
|
||||||
when "center"
|
when "center"
|
||||||
@ -115,7 +129,8 @@ class element
|
|||||||
|
|
||||||
return @
|
return @
|
||||||
|
|
||||||
align: (horizontal, vertical) =>
|
--TODO note that align requires a parent!
|
||||||
|
align: (horizontal, vertical, toPixel) =>
|
||||||
@setAlignment horizontal, vertical
|
@setAlignment horizontal, vertical
|
||||||
|
|
||||||
@x = @parent.x
|
@x = @parent.x
|
||||||
@ -137,15 +152,19 @@ class element
|
|||||||
when "bottom"
|
when "bottom"
|
||||||
@y += @parent.h - @h - @margin
|
@y += @parent.h - @h - @margin
|
||||||
|
|
||||||
|
if toPixel
|
||||||
|
@x = floor @x
|
||||||
|
@y = floor @y
|
||||||
|
|
||||||
return @
|
return @
|
||||||
|
|
||||||
alignTo: (element, horizontal, vertical) =>
|
alignTo: (element, horizontal, vertical) =>
|
||||||
realParent = @parent
|
parent = @parent
|
||||||
@parent = element
|
@parent = element
|
||||||
|
|
||||||
@align horizontal, vertical
|
@align horizontal, vertical
|
||||||
|
|
||||||
@parent = realParent
|
@parent = parent
|
||||||
|
|
||||||
return @
|
return @
|
||||||
|
|
||||||
@ -156,3 +175,11 @@ class element
|
|||||||
@vertical = vertical
|
@vertical = vertical
|
||||||
|
|
||||||
return @
|
return @
|
||||||
|
|
||||||
|
setMargin: (margin) =>
|
||||||
|
@margin = margin
|
||||||
|
@align!
|
||||||
|
return @
|
||||||
|
|
||||||
|
getMargin: =>
|
||||||
|
return @margin
|
||||||
|
@ -1,10 +1,17 @@
|
|||||||
import graphics from love
|
import graphics from love
|
||||||
import sub, len from string
|
import sub, len from string
|
||||||
|
|
||||||
path = sub ..., 1, len(...) - len "/text"
|
path = sub ..., 1, len(...) - len "/box"
|
||||||
element = require "#{path}/element"
|
element = require "#{path}/element"
|
||||||
|
|
||||||
class text extends element
|
class text extends element
|
||||||
|
wrap: (pop) ->
|
||||||
|
return (parent, ...) ->
|
||||||
|
if type(parent) == "string"
|
||||||
|
return pop.create("text", nil, parent, ...)
|
||||||
|
else
|
||||||
|
return pop.create("text", parent, ...)
|
||||||
|
|
||||||
new: (pop, parent, text="", color={255,255,255,255}) =>
|
new: (pop, parent, text="", color={255,255,255,255}) =>
|
||||||
super pop, parent
|
super pop, parent
|
||||||
|
|
||||||
@ -30,9 +37,10 @@ class text extends element
|
|||||||
|
|
||||||
return @
|
return @
|
||||||
|
|
||||||
|
-- unlike most elements, you cannot set a size for text elements
|
||||||
setSize: =>
|
setSize: =>
|
||||||
w = @font\getWidth @text
|
w = @font\getWidth @text
|
||||||
h = @font\getHeight! * (select(2, @text\gsub("\n", "\n")) + 1) --hack to get height of multiple lines of text
|
h = @font\getHeight! * (select(2, @text\gsub("\n", "\n")) + 1) --hack to get height of multiple lines
|
||||||
|
|
||||||
switch @horizontal
|
switch @horizontal
|
||||||
when "center"
|
when "center"
|
||||||
@ -43,7 +51,7 @@ class text extends element
|
|||||||
switch @vertical
|
switch @vertical
|
||||||
when "center"
|
when "center"
|
||||||
@y -= (h - @h)/2
|
@y -= (h - @h)/2
|
||||||
when "right"
|
when "bottom"
|
||||||
@y -= h - @h - @margin
|
@y -= h - @h - @margin
|
||||||
|
|
||||||
@w = w
|
@w = w
|
||||||
@ -68,7 +76,11 @@ class text extends element
|
|||||||
return @font
|
return @font
|
||||||
|
|
||||||
setColor: (r, g, b, a=255) =>
|
setColor: (r, g, b, a=255) =>
|
||||||
@color = {r, g, b, a}
|
if type(r) == "table"
|
||||||
|
@color = r
|
||||||
|
else
|
||||||
|
@color = {r, g, b, a}
|
||||||
|
|
||||||
return @
|
return @
|
||||||
|
|
||||||
getColor: =>
|
getColor: =>
|
||||||
|
@ -6,94 +6,115 @@ path = ...
|
|||||||
pop = {}
|
pop = {}
|
||||||
|
|
||||||
pop.elements = {}
|
pop.elements = {}
|
||||||
pop.window = {child: {}} --placeholder to allow window creation without specialized code
|
pop.skins = {}
|
||||||
--pop.focused = false
|
|
||||||
|
|
||||||
|
pop.screen = false -- initialized in pop.load()
|
||||||
|
--pop.focused ?
|
||||||
|
|
||||||
|
-- loads elements and skins, creates pop.screen (intended to only be called once at the beginning)
|
||||||
pop.load = ->
|
pop.load = ->
|
||||||
elements = filesystem.getDirectoryItems "#{path}/elements"
|
elements = filesystem.getDirectoryItems "#{path}/elements"
|
||||||
for i = 1, #elements
|
for i = 1, #elements
|
||||||
|
-- only attempt to load lua files
|
||||||
|
unless elements[i]\sub(-4) == ".lua"
|
||||||
|
continue
|
||||||
|
|
||||||
|
-- load into pop.elements table
|
||||||
name = elements[i]\sub 1, -5
|
name = elements[i]\sub 1, -5
|
||||||
pop.elements[name] = require "#{path}/elements/#{name}"
|
pop.elements[name] = require "#{path}/elements/#{name}"
|
||||||
print "loaded element: #{name}"
|
print "element loaded: \"#{name}\""
|
||||||
|
|
||||||
if not pop[name]
|
-- create pop.element() wrapper if possible
|
||||||
pop[name] = (...) -> return pop.create(name, ...)
|
unless pop[name]
|
||||||
print "wrapper created: #{name}()"
|
if pop.elements[name].wrap
|
||||||
|
pop[name] = pop.elements[name].wrap pop
|
||||||
|
else
|
||||||
|
pop[name] = (...) ->
|
||||||
|
return pop.create(name, ...)
|
||||||
|
|
||||||
pop.window = pop.create("element")\setSize(graphics.getWidth!, graphics.getHeight!)
|
print "wrapper created: \"pop.#{name}()\""
|
||||||
--pop.window.parent = pop.window --may be dangerous? infinite loop looking for the window?
|
|
||||||
--pop.window.parent = false --may be dangerous? attempting to index a boolean?
|
|
||||||
print "created window"
|
|
||||||
|
|
||||||
pop.create = (elementType, parent=pop.window, ...) ->
|
-- works just like above, except no wrappers
|
||||||
newElement = pop.elements[elementType](parent, ...)
|
skins = filesystem.getDirectoryItems "#{path}/skins"
|
||||||
insert parent.child, newElement
|
for i = 1, #skins
|
||||||
return newElement
|
unless skins[i]\sub(-4) == ".lua"
|
||||||
|
continue
|
||||||
|
name = skins[i]\sub 1, -5
|
||||||
|
pop.skins[name] = require "#{path}/skins/#{name}"
|
||||||
|
print "skin loaded: \"#{name}\""
|
||||||
|
|
||||||
pop.update = (dt, element=pop.window) ->
|
-- main window (called screen because there will be a window element class)
|
||||||
if not element.excludeUpdating
|
pop.screen = pop.create("element", false)\setSize(graphics.getWidth!, graphics.getHeight!)
|
||||||
|
print "created \"pop.screen\""
|
||||||
|
|
||||||
|
-- creates an element with specified parent (parent can be false)
|
||||||
|
pop.create = (element, parent=pop.screen, ...) ->
|
||||||
|
element = pop.elements[element](pop, parent, ...)
|
||||||
|
|
||||||
|
if parent
|
||||||
|
insert parent.child, element
|
||||||
|
|
||||||
|
return element
|
||||||
|
|
||||||
|
pop.update = (dt, element=pop.screen) ->
|
||||||
|
unless element.excludeUpdate
|
||||||
if element.update
|
if element.update
|
||||||
element\update dt
|
element\update dt
|
||||||
|
|
||||||
for i = 1, #element.child
|
for i = 1, #element.child
|
||||||
pop.update dt, element.child[i]
|
pop.update dt, element.child[i]
|
||||||
|
|
||||||
pop.draw = (element=pop.window) ->
|
pop.draw = (element=pop.screen) ->
|
||||||
if not element.excludeRendering
|
unless element.excludeDraw
|
||||||
if element.draw
|
if element.draw
|
||||||
element\draw
|
element\draw!
|
||||||
|
|
||||||
for i = 1, #element.child
|
for i = 1, #element.child
|
||||||
pop.draw element.child
|
pop.draw element.child[i]
|
||||||
|
|
||||||
pop.mousepressed = (button, x, y, element=pop.window) ->
|
pop.mousepressed = (x, y, button, element=pop.screen) ->
|
||||||
-- if within bounds, check children
|
print "mousepressed", x, y, button, element
|
||||||
-- if not handled, check if we can handle it
|
return false --TODO event handlers return if they have handled the event!
|
||||||
-- abort with success if handled
|
|
||||||
if (x >= element.x) and (x <= (element.x + element.w))
|
|
||||||
if (y >= element.y) and (y <= (element.y + element.h))
|
|
||||||
for i = 1, #element.child
|
|
||||||
if pop.mousepressed button, x, y, element.child[i]
|
|
||||||
return true
|
|
||||||
|
|
||||||
if element.mousepressed
|
pop.mousereleased = (x, y, button, element=pop.screen) ->
|
||||||
return element\mousepressed button, x - element.x, y - element.y
|
print "mousereleased", x, y, button, element
|
||||||
else
|
return false --TODO event handlers return if they have handled the event!
|
||||||
return false
|
|
||||||
|
|
||||||
--TODO multiple return values, mousereleased first, click second
|
|
||||||
pop.mousereleased = (button, x, y, element=pop.window) ->
|
|
||||||
-- same as mousepressed, except a click can be fired as well
|
|
||||||
|
|
||||||
pop.keypressed = (key) ->
|
pop.keypressed = (key) ->
|
||||||
print "pop.keypressed() is unimplemented."
|
print "keypressed", key
|
||||||
|
return false --TODO event handlers return if they have handled the event!
|
||||||
|
|
||||||
pop.keyreleased = (key) ->
|
pop.keyreleased = (key) ->
|
||||||
print "pop.keyreleased() is unimplemented."
|
print "keyreleased", key
|
||||||
|
return false --TODO event handlers return if they have handled the event!
|
||||||
|
|
||||||
pop.textinput = (text) ->
|
pop.textinput = (text) ->
|
||||||
print "pop.textinput() is unimplemented."
|
print "textinput", text
|
||||||
|
return false --TODO event handlers return if they have handled the event!
|
||||||
|
|
||||||
pop.skin = (element, skin, apply_to_children=true) ->
|
-- skins an element (and its children unless depth == true or 0)
|
||||||
element.margin = skin.margin
|
-- depth can be an integer for how many levels to go down when skinning
|
||||||
|
-- defaults to pop.screen and the default skin
|
||||||
if element.background
|
pop.skin = (element=pop.screen, skin=pop.skins.default, depth) ->
|
||||||
|
if element.background and skin.background
|
||||||
element.background = skin.background
|
element.background = skin.background
|
||||||
if element.color
|
if element.color and skin.color
|
||||||
element.color = skin.color
|
element.color = skin.color
|
||||||
if element.font
|
if element.font and skin.font
|
||||||
element.font = skin.font
|
element.font = skin.font
|
||||||
|
|
||||||
if apply_to_children
|
unless depth or (depth == 0)
|
||||||
for i = 1, #element.child
|
if depth == tonumber depth
|
||||||
pop.skin element.child[i], skin
|
for i = 1, #element.child
|
||||||
|
pop.skin element.child[i], skin, depth - 1
|
||||||
|
else
|
||||||
|
for i = 1, #element.child
|
||||||
|
pop.skin element.child[i], skin, false
|
||||||
|
|
||||||
pop.debugDraw = (element=pop.window) ->
|
pop.debugDraw = (element=pop.screen) ->
|
||||||
if element.debugDraw
|
if element.debugDraw
|
||||||
element\debugDraw!
|
element\debugDraw!
|
||||||
else
|
else
|
||||||
graphics.setLineWidth 1
|
graphics.setLineWidth 1
|
||||||
graphics.setColor 0, 0, 0, 100
|
graphics.setLineColor 0, 0, 0, 100
|
||||||
graphics.rectangle "fill", element.x, element.y, element.w, element.h
|
graphics.rectangle "fill", element.x, element.y, element.w, element.h
|
||||||
graphics.setColor 150, 150, 150, 150
|
graphics.setColor 150, 150, 150, 150
|
||||||
graphics.rectangle "line", element.x, element.y, element.w, element.h
|
graphics.rectangle "line", element.x, element.y, element.w, element.h
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
import graphics from love
|
|
||||||
|
|
||||||
return {
|
|
||||||
color: {255, 255, 255, 255}
|
|
||||||
background: false
|
|
||||||
font: graphics.newFont 13
|
|
||||||
margin: 2
|
|
||||||
}
|
|
7
src/pop/skins/default.moon
Normal file
7
src/pop/skins/default.moon
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import graphics from love
|
||||||
|
|
||||||
|
return {
|
||||||
|
background: {0, 0, 0, 220}
|
||||||
|
color: {255, 255, 255, 250}
|
||||||
|
font: graphics.newFont 14
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user