close #42 data-driven!

This commit is contained in:
Paul Liverman III 2016-05-01 01:55:50 -07:00
parent 61f126cd62
commit bd158e0099
18 changed files with 956 additions and 934 deletions

View File

@ -37,7 +37,7 @@ function love.load()
w2:move(-50, 80) w2:move(-50, 80)
w2:setHeight(500) w2:setHeight(500)
w2:move(0, -175) w2:move(0, -175)
w2.title:align("center") w2.data.title:align("center")
w2:position(0, 0) w2:position(0, 0)
w2:size(200, 120):position(90, 70) w2:size(200, 120):position(90, 70)
w2:setClose(false) w2:setClose(false)
@ -54,9 +54,9 @@ function love.load()
b = pop.box(G, test):align("right"):move(-25):setWidth(40) b = pop.box(G, test):align("right"):move(-25):setWidth(40)
c = pop.box(G, test):align("right"):move(0, 25):setHeight(40) c = pop.box(G, test):align("right"):move(0, 25):setHeight(40)
print(a.horizontal, a.vertical) print(a.data.horizontal, a.data.vertical)
print(b.horizontal, b.vertical) print(b.data.horizontal, b.data.vertical)
print(c.horizontal, c.vertical) print(c.data.horizontal, c.data.vertical)
local window = pop.window():align("center", "center"):setTitle("Welcome! This title is far too big!") local window = pop.window():align("center", "center"):setTitle("Welcome! This title is far too big!")
@ -114,9 +114,10 @@ function love.keypressed(key)
if (key == "w") and (not handled) then if (key == "w") and (not handled) then
local w = pop.window() local w = pop.window()
w.title:align("center") w.data.title:align("center")
end end
--[[
if (key == "e") and (not handled) then if (key == "e") and (not handled) then
print("EVENTS:") print("EVENTS:")
for k,v in pairs(pop.events) do for k,v in pairs(pop.events) do
@ -124,6 +125,7 @@ function love.keypressed(key)
end end
print("END") print("END")
end end
--]]
if (key == "p") and (not handled) then if (key == "p") and (not handled) then
pop.printElementTree() pop.printElementTree()

View File

@ -13,16 +13,16 @@ do
local _parent_0 = element local _parent_0 = element
local _base_0 = { local _base_0 = {
draw = function(self) draw = function(self)
if self.background then if self.data.background then
if type(self.background) == "table" then if type(self.data.background) == "table" then
graphics.setColor(self.background) graphics.setColor(self.data.background)
graphics.rectangle("fill", self.x, self.y, self.w, self.h) graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h)
else else
local w, h = self.background:getDimensions() local w, h = self.data.background:getDimensions()
w = self.w / w w = self.data.w / w
h = self.h / h h = self.data.h / h
graphics.setColor(255, 255, 255, 255) graphics.setColor(255, 255, 255, 255)
graphics.draw(self.background, self.x, self.y, 0, w, h) graphics.draw(self.data.background, self.data.x, self.data.y, 0, w, h)
end end
end end
return self return self
@ -30,28 +30,28 @@ do
debugDraw = function(self) debugDraw = function(self)
graphics.setLineWidth(0.5) graphics.setLineWidth(0.5)
graphics.setColor(0, 0, 0, 100) graphics.setColor(0, 0, 0, 100)
graphics.rectangle("fill", self.x, self.y, self.w, self.h) graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(0, 0, 200, 200) graphics.setColor(0, 0, 200, 200)
graphics.rectangle("line", self.x, self.y, self.w, self.h) graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(200, 200, 255, 255) graphics.setColor(200, 200, 255, 255)
graphics.print("b", self.x, self.y) graphics.print("b", self.data.x, self.data.y)
return self return self
end, end,
setBackground = function(self, background) setBackground = function(self, background)
self.background = background self.data.background = background
return self return self
end, end,
getBackground = function(self) getBackground = function(self)
return self.background return self.data.background
end, end,
setColor = function(self, r, g, b, a) setColor = function(self, r, g, b, a)
if a == nil then if a == nil then
a = 255 a = 255
end end
if type(r) == "table" then if type(r) == "table" then
self.background = r self.data.background = r
else else
self.background = { self.data.background = {
r, r,
g, g,
b, b,
@ -61,8 +61,8 @@ do
return self return self
end, end,
getColor = function(self) getColor = function(self)
if type(self.background) == "table" then if type(self.data.background) == "table" then
return unpack(self.background) return unpack(self.data.background)
else else
return error("Box \"" .. tostring(self) .. "\" doesn't have a color.") return error("Box \"" .. tostring(self) .. "\" doesn't have a color.")
end end
@ -76,9 +76,9 @@ do
background = false background = false
end end
_class_0.__parent.__init(self, parent) _class_0.__parent.__init(self, parent)
self.w = 20 self.data.w = 20
self.h = 20 self.data.h = 20
self.background = background self.data.background = background
end, end,
__base = _base_0, __base = _base_0,
__name = "box", __name = "box",

View File

@ -15,150 +15,150 @@ do
debugDraw = function(self) debugDraw = function(self)
graphics.setLineWidth(0.5) graphics.setLineWidth(0.5)
graphics.setColor(0, 0, 0, 100) graphics.setColor(0, 0, 0, 100)
graphics.rectangle("fill", self.x, self.y, self.w, self.h) graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(0, 200, 0, 200) graphics.setColor(0, 200, 0, 200)
graphics.rectangle("line", self.x, self.y, self.w, self.h) graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(200, 255, 200, 255) graphics.setColor(200, 255, 200, 255)
graphics.print("e", self.x, self.y) graphics.print("e", self.data.x, self.data.y)
return self return self
end, end,
addChild = function(self, child) addChild = function(self, child)
if child.parent then if child.data.parent then
child.parent:removeChild(child) child.data.parent:removeChild(child)
end end
insert(self.child, child) insert(self.data.child, child)
child.parent = self child.data.parent = self
child:align() child:align()
return self return self
end, end,
removeChild = function(self, child) removeChild = function(self, child)
if tonumber(child) == child then if tonumber(child) == child then
self.child[child].parent = false self.data.child[child].data.parent = false
return remove(self.child, child) return remove(self.data.child, child)
else else
for k, v in ipairs(self.child) do for k, v in ipairs(self.data.child) do
if v == child then if v == child then
return remove(self.child, k) return remove(self.data.child, k)
end end
end end
return "Element \"" .. tostring(child) .. "\" is not a child of element \"" .. tostring(self) .. "\". Cannot remove it." return "Element \"" .. tostring(child) .. "\" is not a child of element \"" .. tostring(self) .. "\". Cannot remove it."
end end
end, end,
getChildren = function(self) getChildren = function(self)
return self.child return self.data.child
end, end,
move = function(self, x, y) move = function(self, x, y)
if x then if x then
self.x = self.x + x self.data.x = self.data.x + x
end end
if y then if y then
self.y = self.y + y self.data.y = self.data.y + y
end end
for i = 1, #self.child do for i = 1, #self.data.child do
if not (self.child[i].excludeMovement) then if self.data.child[i].data.move then
self.child[i]:move(x, y) self.data.child[i]:move(x, y)
end end
end end
return self return self
end, end,
setPosition = function(self, x, y) setPosition = function(self, x, y)
local oldX = self.x local oldX = self.data.x
local oldY = self.y local oldY = self.data.y
if x then if x then
local _exp_0 = self.horizontal local _exp_0 = self.data.horizontal
if "left" == _exp_0 then if "left" == _exp_0 then
self.x = x self.data.x = x
elseif "center" == _exp_0 then elseif "center" == _exp_0 then
self.x = x - self.w / 2 self.data.x = x - self.data.w / 2
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
self.x = x - self.w self.data.x = x - self.data.w
end end
else else
x = oldX x = oldX
end end
if y then if y then
local _exp_0 = self.vertical local _exp_0 = self.data.vertical
if "top" == _exp_0 then if "top" == _exp_0 then
self.y = y self.data.y = y
elseif "center" == _exp_0 then elseif "center" == _exp_0 then
self.y = y - self.h / 2 self.data.y = y - self.data.h / 2
elseif "bottom" == _exp_0 then elseif "bottom" == _exp_0 then
self.y = y - self.h self.data.y = y - self.data.h
end end
else else
y = oldY y = oldY
end end
for i = 1, #self.child do for i = 1, #self.data.child do
self.child[i]:move(x - oldX, y - oldY) self.data.child[i]:move(x - oldX, y - oldY)
end end
return self return self
end, end,
getPosition = function(self) getPosition = function(self)
local resultX = self.x local resultX = self.data.x
local resultY = self.y local resultY = self.data.y
local _exp_0 = self.horizontal local _exp_0 = self.data.horizontal
if "center" == _exp_0 then if "center" == _exp_0 then
resultX = resultX + (self.w / 2) resultX = resultX + (self.data.w / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
resultY = resultY + self.w resultY = resultY + self.data.w
end end
local _exp_1 = self.vertical local _exp_1 = self.data.vertical
if "center" == _exp_1 then if "center" == _exp_1 then
resultY = resultY + (self.h / 2) resultY = resultY + (self.data.h / 2)
elseif "bottom" == _exp_1 then elseif "bottom" == _exp_1 then
resultY = resultY + self.h resultY = resultY + self.data.h
end end
return resultX, resultY return resultX, resultY
end, end,
setSize = function(self, w, h) setSize = function(self, w, h)
if w then if w then
local _exp_0 = self.horizontal local _exp_0 = self.data.horizontal
if "center" == _exp_0 then if "center" == _exp_0 then
self.x = self.x - ((w - self.w) / 2) self.data.x = self.data.x - ((w - self.data.w) / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
self.x = self.x - (w - self.w) self.data.x = self.data.x - (w - self.data.w)
end end
self.w = w self.data.w = w
end end
if h then if h then
local _exp_0 = self.vertical local _exp_0 = self.data.vertical
if "center" == _exp_0 then if "center" == _exp_0 then
self.y = self.y - ((h - self.h) / 2) self.data.y = self.data.y - ((h - self.data.h) / 2)
elseif "bottom" == _exp_0 then elseif "bottom" == _exp_0 then
self.y = self.y - (h - self.h) self.data.y = self.data.y - (h - self.data.h)
end end
self.h = h self.data.h = h
end end
return self return self
end, end,
getSize = function(self) getSize = function(self)
return self.w, self.h return self.data.w, self.data.h
end, end,
setWidth = function(self, w) setWidth = function(self, w)
local _exp_0 = self.horizontal local _exp_0 = self.data.horizontal
if "center" == _exp_0 then if "center" == _exp_0 then
self.x = self.x - ((w - self.w) / 2) self.data.x = self.data.x - ((w - self.data.w) / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
self.x = self.x - (w - self.w) self.data.x = self.data.x - (w - self.data.w)
end end
self.w = w self.data.w = w
return self return self
end, end,
getWidth = function(self) getWidth = function(self)
return self.w return self.data.w
end, end,
setHeight = function(self, h) setHeight = function(self, h)
local _exp_0 = self.vertical local _exp_0 = self.data.vertical
if "center" == _exp_0 then if "center" == _exp_0 then
self.y = self.y - ((h - self.h) / 2) self.data.y = self.data.y - ((h - self.data.h) / 2)
elseif "bottom" == _exp_0 then elseif "bottom" == _exp_0 then
self.y = self.y - (h - self.h) self.data.y = self.data.y - (h - self.data.h)
end end
self.h = h self.data.h = h
return self return self
end, end,
getHeight = function(self) getHeight = function(self)
return self.h return self.data.h
end, end,
adjustSize = function(self, w, h) adjustSize = function(self, w, h)
local W, H = self:getSize() local W, H = self:getSize()
@ -176,27 +176,27 @@ do
toPixel = true toPixel = true
end end
self:setAlignment(horizontal, vertical) self:setAlignment(horizontal, vertical)
self.x = self.parent.x self.data.x = self.data.parent.data.x
self.y = self.parent.y self.data.y = self.data.parent.data.y
local _exp_0 = self.horizontal local _exp_0 = self.data.horizontal
if "left" == _exp_0 then if "left" == _exp_0 then
self.x = self.x + self.spacing self.data.x = self.data.x + self.data.margin
elseif "center" == _exp_0 then elseif "center" == _exp_0 then
self.x = self.x + ((self.parent.w - self.w) / 2) self.data.x = self.data.x + ((self.data.parent.data.w - self.data.w) / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
self.x = self.x + (self.parent.w - self.w - self.spacing) self.data.x = self.data.x + (self.data.parent.data.w - self.data.w - self.data.margin)
end end
local _exp_1 = self.vertical local _exp_1 = self.data.vertical
if "top" == _exp_1 then if "top" == _exp_1 then
self.y = self.y + self.spacing self.data.y = self.data.y + self.data.margin
elseif "center" == _exp_1 then elseif "center" == _exp_1 then
self.y = self.y + ((self.parent.h - self.h) / 2) self.data.y = self.data.y + ((self.data.parent.data.h - self.data.h) / 2)
elseif "bottom" == _exp_1 then elseif "bottom" == _exp_1 then
self.y = self.y + (self.parent.h - self.h - self.spacing) self.data.y = self.data.y + (self.data.parent.data.h - self.data.h - self.data.margin)
end end
if toPixel then if toPixel then
self.x = floor(self.x) self.data.x = floor(self.data.x)
self.y = floor(self.y) self.data.y = floor(self.data.y)
end end
return self return self
end, end,
@ -204,81 +204,82 @@ do
if toPixel == nil then if toPixel == nil then
toPixel = true toPixel = true
end end
local parent = self.parent local parent = self.data.parent
self.parent = element self.data.parent = element
self:align(horizontal, vertical, toPixel) self:align(horizontal, vertical, toPixel)
self.parent = parent self.data.parent = parent
return self return self
end, end,
setAlignment = function(self, horizontal, vertical) setAlignment = function(self, horizontal, vertical)
if horizontal then if horizontal then
self.horizontal = horizontal self.data.horizontal = horizontal
end end
if vertical then if vertical then
self.vertical = vertical self.data.vertical = vertical
end end
return self return self
end, end,
getAlignment = function(self) getAlignment = function(self)
return self.horizontal, self.vertical return self.data.horizontal, self.data.vertical
end, end,
setMargin = function(self, spacing) setMargin = function(self, margin)
self.spacing = spacing self.data.margin = margin
self:align() self:align()
return self return self
end, end,
getMargin = function(self) getMargin = function(self)
return self.spacing return self.data.margin
end, end,
fill = function(self) fill = function(self)
self.x = self.parent.x + self.spacing self.data.x = self.data.parent.data.x + self.data.margin
self.y = self.parent.y + self.spacing self.data.y = self.data.parent.data.y + self.data.margin
self.w = self.parent.w - self.spacing * 2 self.data.w = self.data.parent.data.w - self.data.margin * 2
self.h = self.parent.h - self.spacing * 2 self.data.h = self.data.parent.data.h - self.data.margin * 2
end, end,
delete = function(self) delete = function(self)
for k, v in ipairs(self.child) do for k, v in ipairs(self.data.child) do
v:delete() v:delete()
end end
self.parent:removeChild(self) self.data.parent:removeChild(self)
self = nil self = nil
return nil return nil
end, end,
getVisibility = function(self) getVisibility = function(self)
return (not self.excludeDraw) return self.data.draw
end, end,
setVisibility = function(self, isVisible) setVisibility = function(self, isVisible)
self.excludeDraw = (not isVisible) self.data.draw = isVisible
return self return self
end, end,
getStatic = function(self) getStatic = function(self)
return self.excludeMovement return (not self.data.move)
end, end,
setStatic = function(self, isStatic) setStatic = function(self, isStatic)
self.excludeMovement = isStatic self.data.move = (not isStatic)
return self return self
end end
} }
_base_0.__index = _base_0 _base_0.__index = _base_0
_class_0 = setmetatable({ _class_0 = setmetatable({
__init = function(self, parent) __init = function(self, parent)
self.parent = parent self.data = {
self.child = { } parent = parent,
self.w = 0 child = { },
self.h = 0 w = 0,
self.spacing = 0 h = 0,
x = 0,
y = 0,
horizontal = "left",
vertical = "top",
margin = 0,
draw = true,
update = true,
move = true
}
if parent then if parent then
self.x = parent.x self.data.x = parent.data.x
self.y = parent.y self.data.y = parent.data.y
else
self.x = 0
self.y = 0
end end
self.horizontal = "left"
self.vertical = "top"
self.excludeDraw = false
self.excludeUpdate = false
self.excludeMovement = false
end, end,
__base = _base_0, __base = _base_0,
__name = "element" __name = "element"

View File

@ -13,38 +13,38 @@ do
local _parent_0 = element local _parent_0 = element
local _base_0 = { local _base_0 = {
draw = function(self) draw = function(self)
graphics.setColor(self.color) graphics.setColor(self.data.color)
graphics.setFont(self.font) graphics.setFont(self.data.font)
graphics.print(self.txt, self.x, self.y) graphics.print(self.data.text, self.data.x, self.data.y)
return self return self
end, end,
debugDraw = function(self) debugDraw = function(self)
graphics.setLineWidth(0.5) graphics.setLineWidth(0.5)
graphics.setColor(0, 0, 0, 100) graphics.setColor(0, 0, 0, 100)
graphics.rectangle("fill", self.x, self.y, self.w, self.h) graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(200, 0, 0, 200) graphics.setColor(200, 0, 0, 200)
graphics.rectangle("line", self.x, self.y, self.w, self.h) graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(255, 200, 200, 255) graphics.setColor(255, 200, 200, 255)
graphics.print("t", self.x, self.y) graphics.print("t", self.data.x, self.data.y)
return self return self
end, end,
setSize = function(self) setSize = function(self)
local w = self.font:getWidth(self.txt) local w = self.data.font:getWidth(self.data.text)
local h = self.font:getHeight() * (select(2, self.txt:gsub("\n", "\n")) + 1) local h = self.data.font:getHeight() * (select(2, self.data.text:gsub("\n", "\n")) + 1)
local _exp_0 = self.horizontal local _exp_0 = self.data.horizontal
if "center" == _exp_0 then if "center" == _exp_0 then
self.x = self.x - ((w - self.w) / 2) self.data.x = self.data.x - ((w - self.data.w) / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
self.x = self.x - (w - self.w - self.spacing) self.data.x = self.data.x - (w - self.data.w - self.data.margin)
end end
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.data.y = self.data.y - ((h - self.data.h) / 2)
elseif "bottom" == _exp_1 then elseif "bottom" == _exp_1 then
self.y = self.y - (h - self.h - self.spacing) self.data.y = self.data.y - (h - self.data.h - self.data.margin)
end end
self.w = w self.data.w = w
self.h = h self.data.h = h
return self return self
end, end,
setWidth = function(self) setWidth = function(self)
@ -59,29 +59,29 @@ do
if text == nil then if text == nil then
text = "" text = ""
end end
self.txt = text self.data.text = text
self:setSize() self:setSize()
return self return self
end, end,
getText = function(self) getText = function(self)
return self.txt return self.data.text
end, end,
setFont = function(self, font) setFont = function(self, font)
self.font = font self.data.font = font
self:setSize() self:setSize()
return self return self
end, end,
getFont = function(self) getFont = function(self)
return self.font return self.data.font
end, end,
setColor = function(self, r, g, b, a) setColor = function(self, r, g, b, a)
if a == nil then if a == nil then
a = 255 a = 255
end end
if type(r) == "table" then if type(r) == "table" then
self.color = r self.data.color = r
else else
self.color = { self.data.color = {
r, r,
g, g,
b, b,
@ -91,7 +91,7 @@ do
return self return self
end, end,
getColor = function(self) getColor = function(self)
return unpack(self.color) return unpack(self.data.color)
end end
} }
_base_0.__index = _base_0 _base_0.__index = _base_0
@ -110,9 +110,9 @@ do
} }
end end
_class_0.__parent.__init(self, parent) _class_0.__parent.__init(self, parent)
self.font = graphics.newFont(14) self.data.font = graphics.newFont(14)
self:setText(text) self:setText(text)
self.color = color self.data.color = color
end, end,
__base = _base_0, __base = _base_0,
__name = "text", __name = "text",

View File

@ -34,37 +34,33 @@ do
print(" assuming LOVE version > 0.10.1 (there may be bugs)") print(" assuming LOVE version > 0.10.1 (there may be bugs)")
end end
end end
local pop_ref = false
local window local window
do do
local _class_0 local _class_0
local _parent_0 = element local _parent_0 = element
local _base_0 = { local _base_0 = {
load = function(pop)
pop_ref = pop
end,
debugDraw = function(self) debugDraw = function(self)
graphics.setLineWidth(0.5) graphics.setLineWidth(0.5)
graphics.setColor(0, 0, 0, 100) graphics.setColor(0, 0, 0, 100)
graphics.rectangle("fill", self.x, self.y, self.w, self.h) graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(200, 0, 200, 200) graphics.setColor(200, 0, 200, 200)
graphics.rectangle("line", self.x, self.y, self.w, self.h) graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(255, 200, 255, 255) graphics.setColor(255, 200, 255, 255)
graphics.print("w", self.x, self.y) graphics.print("w", self.data.x, self.data.y)
return self return self
end, end,
addChild = function(self, child) addChild = function(self, child)
self.area:addChild(child) self.data.area:addChild(child)
return self return self
end, end,
removeChild = function(self, child) removeChild = function(self, child)
local result = self.area:removeChild(child) local result = self.data.area:removeChild(child)
if result == self.area then if result == self.data.area then
return self return self
elseif type(result) == "string" then elseif type(result) == "string" then
for k, v in ipairs(self.child) do for k, v in ipairs(self.data.child) do
if v == child then if v == child then
remove(self.child, k) remove(self.data.child, k)
return self return self
end end
end end
@ -74,142 +70,142 @@ do
end end
end, end,
getChildren = function(self) getChildren = function(self)
return self.area.child return self.data.area.child
end, end,
align = function(self, horizontal, vertical, toPixel) align = function(self, horizontal, vertical, toPixel)
_class_0.__parent.__base.align(self, horizontal, vertical, toPixel) _class_0.__parent.__base.align(self, horizontal, vertical, toPixel)
for i = 1, #self.child do for i = 1, #self.data.child do
self.child[i]:align() self.data.child[i]:align()
end end
self.area:move(nil, self.head:getHeight()) self.data.area:move(nil, self.data.head:getHeight())
return self return self
end, end,
setSize = function(self, w, h) setSize = function(self, w, h)
local x = 0 local x = 0
local y = 0 local y = 0
if w then if w then
local _exp_0 = self.horizontal local _exp_0 = self.data.horizontal
if "center" == _exp_0 then if "center" == _exp_0 then
x = x - ((w - self.w) / 2) x = x - ((w - self.data.w) / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
x = x - (w - self.w) x = x - (w - self.data.w)
end end
if self.close then if self.data.close then
self.head:setWidth(w - self.head:getHeight()) self.data.head:setWidth(w - self.data.head:getHeight())
else else
self.head:setWidth(w) self.data.head:setWidth(w)
end end
self.area:setWidth(w) self.data.area:setWidth(w)
self.w = w self.data.w = w
self.x = self.x + x self.data.x = self.data.x + x
self.title:align() self.data.title:align()
if self.close then if self.data.close then
self.close:align() self.data.close:align()
end end
end end
if h then if h then
h = h - self.head:getHeight() h = h - self.data.head:getHeight()
local _exp_0 = self.vertical local _exp_0 = self.data.vertical
if "center" == _exp_0 then if "center" == _exp_0 then
y = y - ((h - self.h) / 2) y = y - ((h - self.data.h) / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
y = y - (h - self.h) y = y - (h - self.data.h)
end end
self.area:setHeight(h) self.data.area:setHeight(h)
self.h = h + self.head:getHeight() self.data.h = h + self.data.head:getHeight()
self.y = self.y + y self.data.y = self.data.y + y
end end
self.head:move(x, y) self.data.head:move(x, y)
self.area:move(x, y) self.data.area:move(x, y)
return self return self
end, end,
setWidth = function(self, w) setWidth = function(self, w)
local x = 0 local x = 0
local _exp_0 = self.horizontal local _exp_0 = self.data.horizontal
if "center" == _exp_0 then if "center" == _exp_0 then
x = x - ((w - self.w) / 2) x = x - ((w - self.data.w) / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
x = x - (w - self.w) x = x - (w - self.data.w)
end end
if self.close then if self.data.close then
self.head:setWidth(w - self.head:getHeight()) self.data.head:setWidth(w - self.data.head:getHeight())
else else
self.head:setWidth(w) self.data.head:setWidth(w)
end end
self.area:setWidth(w) self.data.area:setWidth(w)
self.w = w self.data.w = w
self.x = self.x + x self.data.x = self.data.x + x
self.title:align() self.data.title:align()
if self.close then if self.data.close then
self.close:align() self.data.close:align()
end end
self.head:move(x) self.data.head:move(x)
self.area:move(x) self.data.area:move(x)
return self return self
end, end,
setHeight = function(self, h) setHeight = function(self, h)
local y = 0 local y = 0
h = h - self.head:getHeight() h = h - self.data.head:getHeight()
local _exp_0 = self.vertical local _exp_0 = self.data.vertical
if "center" == _exp_0 then if "center" == _exp_0 then
y = y - ((h - self.h) / 2) y = y - ((h - self.data.h) / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
y = y - (h - self.h) y = y - (h - self.data.h)
end end
self.area:setHeight(h) self.data.area:setHeight(h)
self.h = h + self.head:getHeight() self.data.h = h + self.data.head:getHeight()
self.y = self.y + y self.data.y = self.data.y + y
self.head:move(nil, y) self.data.head:move(nil, y)
self.title:move(nil, y) self.data.title:move(nil, y)
self.area:move(nil, y) self.data.area:move(nil, y)
return self return self
end, end,
setTitle = function(self, title) setTitle = function(self, title)
self.title:setText(title) self.data.title:setText(title)
if self.titleOverflow == "trunicate" then if self.data.overflow == "trunicate" then
while self.title:getWidth() > self.head:getWidth() do while self.data.title:getWidth() > self.data.head:getWidth() do
title = title:sub(1, -3) title = title:sub(1, -3)
self.title:setText(title .. "") self.data.title:setText(title .. "")
end end
elseif self.titleOverflow == "resize" then elseif self.data.overflow == "resize" then
if self.title:getWidth() > self.head:getWidth() then if self.data.title:getWidth() > self.data.head:getWidth() then
self:setWidth(self.title:getWidth()) self:setWidth(self.data.title:getWidth())
end end
end end
return self return self
end, end,
getTitle = function(self) getTitle = function(self)
return self.title:getText() return self.data.title:getText()
end, end,
setTitleOverflow = function(self, method) setTitleOverflow = function(self, method)
self.titleOverflow = method self.data.overflow = method
return self return self
end, end,
getTitleOverflow = function(self) getTitleOverflow = function(self)
return self.titleOverflow return self.data.overflow
end, end,
setClose = function(self, enabled) setClose = function(self, enabled)
if enabled then if enabled then
self.close = box(self, closeImage) self.data.close = box(self, closeImage)
self.close.clicked = function() self.data.close.clicked = function()
self:delete() self:delete()
return true return true
end end
local height = self.head:getHeight() local height = self.data.head:getHeight()
self.close:align("right"):setSize(height, height) self.data.close:align("right"):setSize(height, height)
self.head:setWidth(self.w - height) self.data.head:setWidth(self.data.w - height)
self.title:align() self.data.title:align()
insert(self.child, self.close) insert(self.data.child, self.data.close)
else else
self.close:delete() self.data.close:delete()
self.head:setWidth(self.w) self.data.head:setWidth(self.data.w)
self.title:align() self.data.title:align()
self.close = false self.data.close = false
end end
return self return self
end, end,
hasClose = function(self) hasClose = function(self)
if self.close then if self.data.close then
return true return true
else else
return false return false
@ -217,10 +213,10 @@ do
end, end,
delete = function(self) delete = function(self)
_class_0.__parent.__base.delete(self) _class_0.__parent.__base.delete(self)
self.head = nil self.data.head = nil
self.title = nil self.data.title = nil
self.area = nil self.data.area = nil
self.close = nil self.data.close = nil
end end
} }
_base_0.__index = _base_0 _base_0.__index = _base_0
@ -255,69 +251,68 @@ do
} }
end end
_class_0.__parent.__init(self, parent) _class_0.__parent.__init(self, parent)
self.head = box(self, tBackground) self.data.head = box(self, tBackground)
self.title = text(self.head, title, tColor) self.data.title = text(self.data.head, title, tColor)
self.area = box(self, wBackground) self.data.area = box(self, wBackground)
self.close = box(self, closeImage) self.data.close = box(self, closeImage)
local height = self.title:getHeight() local height = self.data.title:getHeight()
self.head:setSize(self.w - height, height) self.data.head:setSize(self.data.w - height, height)
self.area:move(nil, height) self.data.area:move(nil, height)
self.close:align("right"):setSize(height, height) self.data.close:align("right"):setSize(height, height)
self:setSize(100, 80) self:setSize(100, 80)
self.child = { self.data.child = {
self.head, self.data.head,
self.title, self.data.title,
self.area, self.data.area,
self.close self.data.close
} }
self.titleOverflow = "trunicate" self.data.overflow = "trunicate"
self.area.mousepressed = function() self.data.area.mousepressed = function()
return true return true
end end
self.area.clicked = function() self.data.area.clicked = function()
return true return true
end end
self.close.clicked = function() self.data.close.clicked = function()
self:delete() self:delete()
return true return true
end end
self.head.selected = false self.data.head.data.selected = false
if mousemoved_event then if mousemoved_event then
self.head.mousemoved = function(self, x, y, dx, dy) self.data.head.mousemoved = function(self, x, y, dx, dy)
if self.selected then if self.data.selected then
self.parent:move(dx, dy) self.data.parent:move(dx, dy)
return true return true
end end
return false return false
end end
self.head.mousepressed = function(self, x, y, button) self.data.head.mousepressed = function(self, x, y, button)
if button == left then if button == left then
self.selected = true self.data.selected = true
return true return true
end end
return false return false
end end
else else
self.head.mx = 0 self.data.head.data.mx = 0
self.head.my = 0 self.data.head.data.my = 0
self.head.update = function(self) self.data.head.update = function(self)
local x, y = mouse.getPosition() local x, y = mouse.getPosition()
return self:setPosition(x - mx, y - my) return self:setPosition(x - self.data.mx, y - self.data.my)
end end
self.head.mousepressed = function(self, x, y, button) self.data.head.mousepressed = function(self, x, y, button)
if button == left then if button == left then
self.selected = true self.data.selected = true
self.mx = x self.data.mx = x
self.my = y self.data.my = y
return true return true
end end
return false return false
end end
end end
self.head.mousereleased = function(self, x, y, button) self.data.head.mousereleased = function(self, x, y, button)
if button == left then if button == left then
self.selected = false self.data.selected = false
pop_ref.focused = false
return true return true
end end
return false return false

View File

@ -96,12 +96,12 @@ pop.create = function(element, parent, ...)
end end
if inheritsFromElement(parent) then if inheritsFromElement(parent) then
element = pop.elements[element](parent, ...) element = pop.elements[element](parent, ...)
insert(parent.child, element) insert(parent.data.child, element)
elseif parent == false then elseif parent == false then
element = pop.elements[element](false, ...) element = pop.elements[element](false, ...)
else else
element = pop.elements[element](pop.screen, parent, ...) element = pop.elements[element](pop.screen, parent, ...)
insert(pop.screen.child, element) insert(pop.screen.data.child, element)
end end
return element return element
end end
@ -109,12 +109,12 @@ pop.update = function(dt, element)
if element == nil then if element == nil then
element = pop.screen element = pop.screen
end end
if not (element.excludeUpdate) then if element.data.update then
if element.update then if element.update then
element:update(dt) element:update(dt)
end end
for i = 1, #element.child do for i = 1, #element.data.child do
pop.update(dt, element.child[i]) pop.update(dt, element.data.child[i])
end end
end end
end end
@ -122,12 +122,12 @@ pop.draw = function(element)
if element == nil then if element == nil then
element = pop.screen element = pop.screen
end end
if not (element.excludeDraw) then if element.data.draw then
if element.draw then if element.draw then
element:draw() element:draw()
end end
for i = 1, #element.child do for i = 1, #element.data.child do
pop.draw(element.child[i]) pop.draw(element.data.child[i])
end end
end end
end end
@ -143,19 +143,19 @@ pop.mousepressed = function(x, y, button, element)
element = pop.screen element = pop.screen
end end
local handled = false 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 (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 for i = #element.data.child, 1, -1 do
do do
handled = pop.mousepressed(x, y, button, element.child[i]) handled = pop.mousepressed(x, y, button, element.data.child[i])
if handled then if handled then
return handled return handled
end end
end end
end end
if not (handled) then if not (handled) then
if element.mousepressed and (not element.excludeDraw) then if element.mousepressed and element.data.draw then
do do
handled = element:mousepressed(x - element.x, y - element.y, button) handled = element:mousepressed(x - element.data.x, y - element.data.y, button)
if handled then if handled then
pop.focused = element pop.focused = element
end end
@ -169,19 +169,19 @@ pop.mousereleased = function(x, y, button, element)
local clickedHandled = false local clickedHandled = false
local mousereleasedHandled = false local mousereleasedHandled = false
if element then if element then
if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then 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.child, 1, -1 do for i = #element.data.child, 1, -1 do
clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, element.child[i]) clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, element.data.child[i])
if clickedHandled or mousereleasedHandled then if clickedHandled or mousereleasedHandled then
return clickedHandled, mousereleasedHandled return clickedHandled, mousereleasedHandled
end end
end end
if not (clickedHandled or mousereleasedHandled) then if not (clickedHandled or mousereleasedHandled) then
if element.clicked and (not element.excludeDraw) then if element.clicked and element.data.draw then
clickedHandled = element:clicked(x - element.x, y - element.y, button) clickedHandled = element:clicked(x - element.data.x, y - element.data.y, button)
end end
if element.mousereleased then if element.mousereleased then
mousereleasedHandled = element:mousereleased(x - element.x, y - element.y, button) mousereleasedHandled = element:mousereleased(x - element.data.x, y - element.data.y, button)
end end
if clickedHandled then if clickedHandled then
pop.focused = element pop.focused = element
@ -197,7 +197,7 @@ end
pop.keypressed = function(key) pop.keypressed = function(key)
print("keypressed", key) print("keypressed", key)
local element = pop.focused local element = pop.focused
if element and element.keypressed and (not element.excludeDraw) then if element and element.keypressed and element.data.draw then
return element.keypressed(key) return element.keypressed(key)
end end
return false return false
@ -213,7 +213,7 @@ end
pop.textinput = function(text) pop.textinput = function(text)
print("textinput", text) print("textinput", text)
local element = pop.focused local element = pop.focused
if element and element.textinput and (not element.excludeDraw) then if element and element.textinput and element.data.draw then
return element.textinput(text) return element.textinput(text)
end end
return false return false
@ -225,23 +225,23 @@ pop.skin = function(element, skin, depth)
if skin == nil then if skin == nil then
skin = pop.skins.default skin = pop.skins.default
end end
if element.background and skin.background then if element.data.background and skin.background then
element.background = skin.background element.data.background = skin.background
end end
if element.color and skin.color then if element.data.color and skin.color then
element.color = skin.color element.data.color = skin.color
end end
if element.font and skin.font then if element.data.font and skin.font then
element.font = skin.font element.data.font = skin.font
end end
if not (depth or (depth == 0)) then if not (depth or (depth == 0)) then
if depth == tonumber(depth) then if depth == tonumber(depth) then
for i = 1, #element.child do for i = 1, #element.data.child do
pop.skin(element.child[i], skin, depth - 1) pop.skin(element.data.child[i], skin, depth - 1)
end end
else else
for i = 1, #element.child do for i = 1, #element.data.child do
pop.skin(element.child[i], skin, false) pop.skin(element.data.child[i], skin, false)
end end
end end
end end
@ -255,14 +255,14 @@ pop.debugDraw = function(element)
else else
graphics.setLineWidth(1) graphics.setLineWidth(1)
graphics.setLineColor(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.data.x, element.data.y, element.data.w, element.data.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.data.x, element.data.y, element.data.w, element.data.h)
graphics.setColor(200, 200, 200, 255) graphics.setColor(200, 200, 200, 255)
graphics.print(".", element.x, element.y) graphics.print(".", element.data.x, element.data.y)
end end
for i = 1, #element.child do for i = 1, #element.data.child do
pop.debugDraw(element.child[i]) pop.debugDraw(element.data.child[i])
end end
end end
pop.printElementTree = function(element, depth) pop.printElementTree = function(element, depth)
@ -283,8 +283,8 @@ pop.printElementTree = function(element, depth)
cls = cls .. " (" .. tostring(bg) .. ")" cls = cls .. " (" .. tostring(bg) .. ")"
end end
print(string.rep("-", depth) .. " " .. tostring(cls)) print(string.rep("-", depth) .. " " .. tostring(cls))
for i = 1, #element.child do for i = 1, #element.data.child do
pop.printElementStack(element.child[i], depth + 1) pop.printElementStack(element.data.child[i], depth + 1)
end end
end end
pop.load() pop.load()

View File

@ -13,16 +13,16 @@ do
local _parent_0 = element local _parent_0 = element
local _base_0 = { local _base_0 = {
draw = function(self) draw = function(self)
if self.background then if self.data.background then
if type(self.background) == "table" then if type(self.data.background) == "table" then
graphics.setColor(self.background) graphics.setColor(self.data.background)
graphics.rectangle("fill", self.x, self.y, self.w, self.h) graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h)
else else
local w, h = self.background:getDimensions() local w, h = self.data.background:getDimensions()
w = self.w / w w = self.data.w / w
h = self.h / h h = self.data.h / h
graphics.setColor(255, 255, 255, 255) graphics.setColor(255, 255, 255, 255)
graphics.draw(self.background, self.x, self.y, 0, w, h) graphics.draw(self.data.background, self.data.x, self.data.y, 0, w, h)
end end
end end
return self return self
@ -30,28 +30,28 @@ do
debugDraw = function(self) debugDraw = function(self)
graphics.setLineWidth(0.5) graphics.setLineWidth(0.5)
graphics.setColor(0, 0, 0, 100) graphics.setColor(0, 0, 0, 100)
graphics.rectangle("fill", self.x, self.y, self.w, self.h) graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(0, 0, 200, 200) graphics.setColor(0, 0, 200, 200)
graphics.rectangle("line", self.x, self.y, self.w, self.h) graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(200, 200, 255, 255) graphics.setColor(200, 200, 255, 255)
graphics.print("b", self.x, self.y) graphics.print("b", self.data.x, self.data.y)
return self return self
end, end,
setBackground = function(self, background) setBackground = function(self, background)
self.background = background self.data.background = background
return self return self
end, end,
getBackground = function(self) getBackground = function(self)
return self.background return self.data.background
end, end,
setColor = function(self, r, g, b, a) setColor = function(self, r, g, b, a)
if a == nil then if a == nil then
a = 255 a = 255
end end
if type(r) == "table" then if type(r) == "table" then
self.background = r self.data.background = r
else else
self.background = { self.data.background = {
r, r,
g, g,
b, b,
@ -61,8 +61,8 @@ do
return self return self
end, end,
getColor = function(self) getColor = function(self)
if type(self.background) == "table" then if type(self.data.background) == "table" then
return unpack(self.background) return unpack(self.data.background)
else else
return error("Box \"" .. tostring(self) .. "\" doesn't have a color.") return error("Box \"" .. tostring(self) .. "\" doesn't have a color.")
end end
@ -76,9 +76,9 @@ do
background = false background = false
end end
_class_0.__parent.__init(self, parent) _class_0.__parent.__init(self, parent)
self.w = 20 self.data.w = 20
self.h = 20 self.data.h = 20
self.background = background self.data.background = background
end, end,
__base = _base_0, __base = _base_0,
__name = "box", __name = "box",

View File

@ -15,150 +15,150 @@ do
debugDraw = function(self) debugDraw = function(self)
graphics.setLineWidth(0.5) graphics.setLineWidth(0.5)
graphics.setColor(0, 0, 0, 100) graphics.setColor(0, 0, 0, 100)
graphics.rectangle("fill", self.x, self.y, self.w, self.h) graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(0, 200, 0, 200) graphics.setColor(0, 200, 0, 200)
graphics.rectangle("line", self.x, self.y, self.w, self.h) graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(200, 255, 200, 255) graphics.setColor(200, 255, 200, 255)
graphics.print("e", self.x, self.y) graphics.print("e", self.data.x, self.data.y)
return self return self
end, end,
addChild = function(self, child) addChild = function(self, child)
if child.parent then if child.data.parent then
child.parent:removeChild(child) child.data.parent:removeChild(child)
end end
insert(self.child, child) insert(self.data.child, child)
child.parent = self child.data.parent = self
child:align() child:align()
return self return self
end, end,
removeChild = function(self, child) removeChild = function(self, child)
if tonumber(child) == child then if tonumber(child) == child then
self.child[child].parent = false self.data.child[child].data.parent = false
return remove(self.child, child) return remove(self.data.child, child)
else else
for k, v in ipairs(self.child) do for k, v in ipairs(self.data.child) do
if v == child then if v == child then
return remove(self.child, k) return remove(self.data.child, k)
end end
end end
return "Element \"" .. tostring(child) .. "\" is not a child of element \"" .. tostring(self) .. "\". Cannot remove it." return "Element \"" .. tostring(child) .. "\" is not a child of element \"" .. tostring(self) .. "\". Cannot remove it."
end end
end, end,
getChildren = function(self) getChildren = function(self)
return self.child return self.data.child
end, end,
move = function(self, x, y) move = function(self, x, y)
if x then if x then
self.x = self.x + x self.data.x = self.data.x + x
end end
if y then if y then
self.y = self.y + y self.data.y = self.data.y + y
end end
for i = 1, #self.child do for i = 1, #self.data.child do
if not (self.child[i].excludeMovement) then if self.data.child[i].data.move then
self.child[i]:move(x, y) self.data.child[i]:move(x, y)
end end
end end
return self return self
end, end,
setPosition = function(self, x, y) setPosition = function(self, x, y)
local oldX = self.x local oldX = self.data.x
local oldY = self.y local oldY = self.data.y
if x then if x then
local _exp_0 = self.horizontal local _exp_0 = self.data.horizontal
if "left" == _exp_0 then if "left" == _exp_0 then
self.x = x self.data.x = x
elseif "center" == _exp_0 then elseif "center" == _exp_0 then
self.x = x - self.w / 2 self.data.x = x - self.data.w / 2
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
self.x = x - self.w self.data.x = x - self.data.w
end end
else else
x = oldX x = oldX
end end
if y then if y then
local _exp_0 = self.vertical local _exp_0 = self.data.vertical
if "top" == _exp_0 then if "top" == _exp_0 then
self.y = y self.data.y = y
elseif "center" == _exp_0 then elseif "center" == _exp_0 then
self.y = y - self.h / 2 self.data.y = y - self.data.h / 2
elseif "bottom" == _exp_0 then elseif "bottom" == _exp_0 then
self.y = y - self.h self.data.y = y - self.data.h
end end
else else
y = oldY y = oldY
end end
for i = 1, #self.child do for i = 1, #self.data.child do
self.child[i]:move(x - oldX, y - oldY) self.data.child[i]:move(x - oldX, y - oldY)
end end
return self return self
end, end,
getPosition = function(self) getPosition = function(self)
local resultX = self.x local resultX = self.data.x
local resultY = self.y local resultY = self.data.y
local _exp_0 = self.horizontal local _exp_0 = self.data.horizontal
if "center" == _exp_0 then if "center" == _exp_0 then
resultX = resultX + (self.w / 2) resultX = resultX + (self.data.w / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
resultY = resultY + self.w resultY = resultY + self.data.w
end end
local _exp_1 = self.vertical local _exp_1 = self.data.vertical
if "center" == _exp_1 then if "center" == _exp_1 then
resultY = resultY + (self.h / 2) resultY = resultY + (self.data.h / 2)
elseif "bottom" == _exp_1 then elseif "bottom" == _exp_1 then
resultY = resultY + self.h resultY = resultY + self.data.h
end end
return resultX, resultY return resultX, resultY
end, end,
setSize = function(self, w, h) setSize = function(self, w, h)
if w then if w then
local _exp_0 = self.horizontal local _exp_0 = self.data.horizontal
if "center" == _exp_0 then if "center" == _exp_0 then
self.x = self.x - ((w - self.w) / 2) self.data.x = self.data.x - ((w - self.data.w) / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
self.x = self.x - (w - self.w) self.data.x = self.data.x - (w - self.data.w)
end end
self.w = w self.data.w = w
end end
if h then if h then
local _exp_0 = self.vertical local _exp_0 = self.data.vertical
if "center" == _exp_0 then if "center" == _exp_0 then
self.y = self.y - ((h - self.h) / 2) self.data.y = self.data.y - ((h - self.data.h) / 2)
elseif "bottom" == _exp_0 then elseif "bottom" == _exp_0 then
self.y = self.y - (h - self.h) self.data.y = self.data.y - (h - self.data.h)
end end
self.h = h self.data.h = h
end end
return self return self
end, end,
getSize = function(self) getSize = function(self)
return self.w, self.h return self.data.w, self.data.h
end, end,
setWidth = function(self, w) setWidth = function(self, w)
local _exp_0 = self.horizontal local _exp_0 = self.data.horizontal
if "center" == _exp_0 then if "center" == _exp_0 then
self.x = self.x - ((w - self.w) / 2) self.data.x = self.data.x - ((w - self.data.w) / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
self.x = self.x - (w - self.w) self.data.x = self.data.x - (w - self.data.w)
end end
self.w = w self.data.w = w
return self return self
end, end,
getWidth = function(self) getWidth = function(self)
return self.w return self.data.w
end, end,
setHeight = function(self, h) setHeight = function(self, h)
local _exp_0 = self.vertical local _exp_0 = self.data.vertical
if "center" == _exp_0 then if "center" == _exp_0 then
self.y = self.y - ((h - self.h) / 2) self.data.y = self.data.y - ((h - self.data.h) / 2)
elseif "bottom" == _exp_0 then elseif "bottom" == _exp_0 then
self.y = self.y - (h - self.h) self.data.y = self.data.y - (h - self.data.h)
end end
self.h = h self.data.h = h
return self return self
end, end,
getHeight = function(self) getHeight = function(self)
return self.h return self.data.h
end, end,
adjustSize = function(self, w, h) adjustSize = function(self, w, h)
local W, H = self:getSize() local W, H = self:getSize()
@ -176,27 +176,27 @@ do
toPixel = true toPixel = true
end end
self:setAlignment(horizontal, vertical) self:setAlignment(horizontal, vertical)
self.x = self.parent.x self.data.x = self.data.parent.data.x
self.y = self.parent.y self.data.y = self.data.parent.data.y
local _exp_0 = self.horizontal local _exp_0 = self.data.horizontal
if "left" == _exp_0 then if "left" == _exp_0 then
self.x = self.x + self.spacing self.data.x = self.data.x + self.data.margin
elseif "center" == _exp_0 then elseif "center" == _exp_0 then
self.x = self.x + ((self.parent.w - self.w) / 2) self.data.x = self.data.x + ((self.data.parent.data.w - self.data.w) / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
self.x = self.x + (self.parent.w - self.w - self.spacing) self.data.x = self.data.x + (self.data.parent.data.w - self.data.w - self.data.margin)
end end
local _exp_1 = self.vertical local _exp_1 = self.data.vertical
if "top" == _exp_1 then if "top" == _exp_1 then
self.y = self.y + self.spacing self.data.y = self.data.y + self.data.margin
elseif "center" == _exp_1 then elseif "center" == _exp_1 then
self.y = self.y + ((self.parent.h - self.h) / 2) self.data.y = self.data.y + ((self.data.parent.data.h - self.data.h) / 2)
elseif "bottom" == _exp_1 then elseif "bottom" == _exp_1 then
self.y = self.y + (self.parent.h - self.h - self.spacing) self.data.y = self.data.y + (self.data.parent.data.h - self.data.h - self.data.margin)
end end
if toPixel then if toPixel then
self.x = floor(self.x) self.data.x = floor(self.data.x)
self.y = floor(self.y) self.data.y = floor(self.data.y)
end end
return self return self
end, end,
@ -204,81 +204,82 @@ do
if toPixel == nil then if toPixel == nil then
toPixel = true toPixel = true
end end
local parent = self.parent local parent = self.data.parent
self.parent = element self.data.parent = element
self:align(horizontal, vertical, toPixel) self:align(horizontal, vertical, toPixel)
self.parent = parent self.data.parent = parent
return self return self
end, end,
setAlignment = function(self, horizontal, vertical) setAlignment = function(self, horizontal, vertical)
if horizontal then if horizontal then
self.horizontal = horizontal self.data.horizontal = horizontal
end end
if vertical then if vertical then
self.vertical = vertical self.data.vertical = vertical
end end
return self return self
end, end,
getAlignment = function(self) getAlignment = function(self)
return self.horizontal, self.vertical return self.data.horizontal, self.data.vertical
end, end,
setMargin = function(self, spacing) setMargin = function(self, margin)
self.spacing = spacing self.data.margin = margin
self:align() self:align()
return self return self
end, end,
getMargin = function(self) getMargin = function(self)
return self.spacing return self.data.margin
end, end,
fill = function(self) fill = function(self)
self.x = self.parent.x + self.spacing self.data.x = self.data.parent.data.x + self.data.margin
self.y = self.parent.y + self.spacing self.data.y = self.data.parent.data.y + self.data.margin
self.w = self.parent.w - self.spacing * 2 self.data.w = self.data.parent.data.w - self.data.margin * 2
self.h = self.parent.h - self.spacing * 2 self.data.h = self.data.parent.data.h - self.data.margin * 2
end, end,
delete = function(self) delete = function(self)
for k, v in ipairs(self.child) do for k, v in ipairs(self.data.child) do
v:delete() v:delete()
end end
self.parent:removeChild(self) self.data.parent:removeChild(self)
self = nil self = nil
return nil return nil
end, end,
getVisibility = function(self) getVisibility = function(self)
return (not self.excludeDraw) return self.data.draw
end, end,
setVisibility = function(self, isVisible) setVisibility = function(self, isVisible)
self.excludeDraw = (not isVisible) self.data.draw = isVisible
return self return self
end, end,
getStatic = function(self) getStatic = function(self)
return self.excludeMovement return (not self.data.move)
end, end,
setStatic = function(self, isStatic) setStatic = function(self, isStatic)
self.excludeMovement = isStatic self.data.move = (not isStatic)
return self return self
end end
} }
_base_0.__index = _base_0 _base_0.__index = _base_0
_class_0 = setmetatable({ _class_0 = setmetatable({
__init = function(self, parent) __init = function(self, parent)
self.parent = parent self.data = {
self.child = { } parent = parent,
self.w = 0 child = { },
self.h = 0 w = 0,
self.spacing = 0 h = 0,
x = 0,
y = 0,
horizontal = "left",
vertical = "top",
margin = 0,
draw = true,
update = true,
move = true
}
if parent then if parent then
self.x = parent.x self.data.x = parent.data.x
self.y = parent.y self.data.y = parent.data.y
else
self.x = 0
self.y = 0
end end
self.horizontal = "left"
self.vertical = "top"
self.excludeDraw = false
self.excludeUpdate = false
self.excludeMovement = false
end, end,
__base = _base_0, __base = _base_0,
__name = "element" __name = "element"

View File

@ -13,38 +13,38 @@ do
local _parent_0 = element local _parent_0 = element
local _base_0 = { local _base_0 = {
draw = function(self) draw = function(self)
graphics.setColor(self.color) graphics.setColor(self.data.color)
graphics.setFont(self.font) graphics.setFont(self.data.font)
graphics.print(self.txt, self.x, self.y) graphics.print(self.data.text, self.data.x, self.data.y)
return self return self
end, end,
debugDraw = function(self) debugDraw = function(self)
graphics.setLineWidth(0.5) graphics.setLineWidth(0.5)
graphics.setColor(0, 0, 0, 100) graphics.setColor(0, 0, 0, 100)
graphics.rectangle("fill", self.x, self.y, self.w, self.h) graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(200, 0, 0, 200) graphics.setColor(200, 0, 0, 200)
graphics.rectangle("line", self.x, self.y, self.w, self.h) graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(255, 200, 200, 255) graphics.setColor(255, 200, 200, 255)
graphics.print("t", self.x, self.y) graphics.print("t", self.data.x, self.data.y)
return self return self
end, end,
setSize = function(self) setSize = function(self)
local w = self.font:getWidth(self.txt) local w = self.data.font:getWidth(self.data.text)
local h = self.font:getHeight() * (select(2, self.txt:gsub("\n", "\n")) + 1) local h = self.data.font:getHeight() * (select(2, self.data.text:gsub("\n", "\n")) + 1)
local _exp_0 = self.horizontal local _exp_0 = self.data.horizontal
if "center" == _exp_0 then if "center" == _exp_0 then
self.x = self.x - ((w - self.w) / 2) self.data.x = self.data.x - ((w - self.data.w) / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
self.x = self.x - (w - self.w - self.spacing) self.data.x = self.data.x - (w - self.data.w - self.data.margin)
end end
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.data.y = self.data.y - ((h - self.data.h) / 2)
elseif "bottom" == _exp_1 then elseif "bottom" == _exp_1 then
self.y = self.y - (h - self.h - self.spacing) self.data.y = self.data.y - (h - self.data.h - self.data.margin)
end end
self.w = w self.data.w = w
self.h = h self.data.h = h
return self return self
end, end,
setWidth = function(self) setWidth = function(self)
@ -59,29 +59,29 @@ do
if text == nil then if text == nil then
text = "" text = ""
end end
self.txt = text self.data.text = text
self:setSize() self:setSize()
return self return self
end, end,
getText = function(self) getText = function(self)
return self.txt return self.data.text
end, end,
setFont = function(self, font) setFont = function(self, font)
self.font = font self.data.font = font
self:setSize() self:setSize()
return self return self
end, end,
getFont = function(self) getFont = function(self)
return self.font return self.data.font
end, end,
setColor = function(self, r, g, b, a) setColor = function(self, r, g, b, a)
if a == nil then if a == nil then
a = 255 a = 255
end end
if type(r) == "table" then if type(r) == "table" then
self.color = r self.data.color = r
else else
self.color = { self.data.color = {
r, r,
g, g,
b, b,
@ -91,7 +91,7 @@ do
return self return self
end, end,
getColor = function(self) getColor = function(self)
return unpack(self.color) return unpack(self.data.color)
end end
} }
_base_0.__index = _base_0 _base_0.__index = _base_0
@ -110,9 +110,9 @@ do
} }
end end
_class_0.__parent.__init(self, parent) _class_0.__parent.__init(self, parent)
self.font = graphics.newFont(14) self.data.font = graphics.newFont(14)
self:setText(text) self:setText(text)
self.color = color self.data.color = color
end, end,
__base = _base_0, __base = _base_0,
__name = "text", __name = "text",

View File

@ -34,37 +34,33 @@ do
print(" assuming LOVE version > 0.10.1 (there may be bugs)") print(" assuming LOVE version > 0.10.1 (there may be bugs)")
end end
end end
local pop_ref = false
local window local window
do do
local _class_0 local _class_0
local _parent_0 = element local _parent_0 = element
local _base_0 = { local _base_0 = {
load = function(pop)
pop_ref = pop
end,
debugDraw = function(self) debugDraw = function(self)
graphics.setLineWidth(0.5) graphics.setLineWidth(0.5)
graphics.setColor(0, 0, 0, 100) graphics.setColor(0, 0, 0, 100)
graphics.rectangle("fill", self.x, self.y, self.w, self.h) graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(200, 0, 200, 200) graphics.setColor(200, 0, 200, 200)
graphics.rectangle("line", self.x, self.y, self.w, self.h) graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(255, 200, 255, 255) graphics.setColor(255, 200, 255, 255)
graphics.print("w", self.x, self.y) graphics.print("w", self.data.x, self.data.y)
return self return self
end, end,
addChild = function(self, child) addChild = function(self, child)
self.area:addChild(child) self.data.area:addChild(child)
return self return self
end, end,
removeChild = function(self, child) removeChild = function(self, child)
local result = self.area:removeChild(child) local result = self.data.area:removeChild(child)
if result == self.area then if result == self.data.area then
return self return self
elseif type(result) == "string" then elseif type(result) == "string" then
for k, v in ipairs(self.child) do for k, v in ipairs(self.data.child) do
if v == child then if v == child then
remove(self.child, k) remove(self.data.child, k)
return self return self
end end
end end
@ -74,142 +70,142 @@ do
end end
end, end,
getChildren = function(self) getChildren = function(self)
return self.area.child return self.data.area.child
end, end,
align = function(self, horizontal, vertical, toPixel) align = function(self, horizontal, vertical, toPixel)
_class_0.__parent.__base.align(self, horizontal, vertical, toPixel) _class_0.__parent.__base.align(self, horizontal, vertical, toPixel)
for i = 1, #self.child do for i = 1, #self.data.child do
self.child[i]:align() self.data.child[i]:align()
end end
self.area:move(nil, self.head:getHeight()) self.data.area:move(nil, self.data.head:getHeight())
return self return self
end, end,
setSize = function(self, w, h) setSize = function(self, w, h)
local x = 0 local x = 0
local y = 0 local y = 0
if w then if w then
local _exp_0 = self.horizontal local _exp_0 = self.data.horizontal
if "center" == _exp_0 then if "center" == _exp_0 then
x = x - ((w - self.w) / 2) x = x - ((w - self.data.w) / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
x = x - (w - self.w) x = x - (w - self.data.w)
end end
if self.close then if self.data.close then
self.head:setWidth(w - self.head:getHeight()) self.data.head:setWidth(w - self.data.head:getHeight())
else else
self.head:setWidth(w) self.data.head:setWidth(w)
end end
self.area:setWidth(w) self.data.area:setWidth(w)
self.w = w self.data.w = w
self.x = self.x + x self.data.x = self.data.x + x
self.title:align() self.data.title:align()
if self.close then if self.data.close then
self.close:align() self.data.close:align()
end end
end end
if h then if h then
h = h - self.head:getHeight() h = h - self.data.head:getHeight()
local _exp_0 = self.vertical local _exp_0 = self.data.vertical
if "center" == _exp_0 then if "center" == _exp_0 then
y = y - ((h - self.h) / 2) y = y - ((h - self.data.h) / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
y = y - (h - self.h) y = y - (h - self.data.h)
end end
self.area:setHeight(h) self.data.area:setHeight(h)
self.h = h + self.head:getHeight() self.data.h = h + self.data.head:getHeight()
self.y = self.y + y self.data.y = self.data.y + y
end end
self.head:move(x, y) self.data.head:move(x, y)
self.area:move(x, y) self.data.area:move(x, y)
return self return self
end, end,
setWidth = function(self, w) setWidth = function(self, w)
local x = 0 local x = 0
local _exp_0 = self.horizontal local _exp_0 = self.data.horizontal
if "center" == _exp_0 then if "center" == _exp_0 then
x = x - ((w - self.w) / 2) x = x - ((w - self.data.w) / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
x = x - (w - self.w) x = x - (w - self.data.w)
end end
if self.close then if self.data.close then
self.head:setWidth(w - self.head:getHeight()) self.data.head:setWidth(w - self.data.head:getHeight())
else else
self.head:setWidth(w) self.data.head:setWidth(w)
end end
self.area:setWidth(w) self.data.area:setWidth(w)
self.w = w self.data.w = w
self.x = self.x + x self.data.x = self.data.x + x
self.title:align() self.data.title:align()
if self.close then if self.data.close then
self.close:align() self.data.close:align()
end end
self.head:move(x) self.data.head:move(x)
self.area:move(x) self.data.area:move(x)
return self return self
end, end,
setHeight = function(self, h) setHeight = function(self, h)
local y = 0 local y = 0
h = h - self.head:getHeight() h = h - self.data.head:getHeight()
local _exp_0 = self.vertical local _exp_0 = self.data.vertical
if "center" == _exp_0 then if "center" == _exp_0 then
y = y - ((h - self.h) / 2) y = y - ((h - self.data.h) / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
y = y - (h - self.h) y = y - (h - self.data.h)
end end
self.area:setHeight(h) self.data.area:setHeight(h)
self.h = h + self.head:getHeight() self.data.h = h + self.data.head:getHeight()
self.y = self.y + y self.data.y = self.data.y + y
self.head:move(nil, y) self.data.head:move(nil, y)
self.title:move(nil, y) self.data.title:move(nil, y)
self.area:move(nil, y) self.data.area:move(nil, y)
return self return self
end, end,
setTitle = function(self, title) setTitle = function(self, title)
self.title:setText(title) self.data.title:setText(title)
if self.titleOverflow == "trunicate" then if self.data.overflow == "trunicate" then
while self.title:getWidth() > self.head:getWidth() do while self.data.title:getWidth() > self.data.head:getWidth() do
title = title:sub(1, -3) title = title:sub(1, -3)
self.title:setText(title .. "") self.data.title:setText(title .. "")
end end
elseif self.titleOverflow == "resize" then elseif self.data.overflow == "resize" then
if self.title:getWidth() > self.head:getWidth() then if self.data.title:getWidth() > self.data.head:getWidth() then
self:setWidth(self.title:getWidth()) self:setWidth(self.data.title:getWidth())
end end
end end
return self return self
end, end,
getTitle = function(self) getTitle = function(self)
return self.title:getText() return self.data.title:getText()
end, end,
setTitleOverflow = function(self, method) setTitleOverflow = function(self, method)
self.titleOverflow = method self.data.overflow = method
return self return self
end, end,
getTitleOverflow = function(self) getTitleOverflow = function(self)
return self.titleOverflow return self.data.overflow
end, end,
setClose = function(self, enabled) setClose = function(self, enabled)
if enabled then if enabled then
self.close = box(self, closeImage) self.data.close = box(self, closeImage)
self.close.clicked = function() self.data.close.clicked = function()
self:delete() self:delete()
return true return true
end end
local height = self.head:getHeight() local height = self.data.head:getHeight()
self.close:align("right"):setSize(height, height) self.data.close:align("right"):setSize(height, height)
self.head:setWidth(self.w - height) self.data.head:setWidth(self.data.w - height)
self.title:align() self.data.title:align()
insert(self.child, self.close) insert(self.data.child, self.data.close)
else else
self.close:delete() self.data.close:delete()
self.head:setWidth(self.w) self.data.head:setWidth(self.data.w)
self.title:align() self.data.title:align()
self.close = false self.data.close = false
end end
return self return self
end, end,
hasClose = function(self) hasClose = function(self)
if self.close then if self.data.close then
return true return true
else else
return false return false
@ -217,10 +213,10 @@ do
end, end,
delete = function(self) delete = function(self)
_class_0.__parent.__base.delete(self) _class_0.__parent.__base.delete(self)
self.head = nil self.data.head = nil
self.title = nil self.data.title = nil
self.area = nil self.data.area = nil
self.close = nil self.data.close = nil
end end
} }
_base_0.__index = _base_0 _base_0.__index = _base_0
@ -255,69 +251,68 @@ do
} }
end end
_class_0.__parent.__init(self, parent) _class_0.__parent.__init(self, parent)
self.head = box(self, tBackground) self.data.head = box(self, tBackground)
self.title = text(self.head, title, tColor) self.data.title = text(self.data.head, title, tColor)
self.area = box(self, wBackground) self.data.area = box(self, wBackground)
self.close = box(self, closeImage) self.data.close = box(self, closeImage)
local height = self.title:getHeight() local height = self.data.title:getHeight()
self.head:setSize(self.w - height, height) self.data.head:setSize(self.data.w - height, height)
self.area:move(nil, height) self.data.area:move(nil, height)
self.close:align("right"):setSize(height, height) self.data.close:align("right"):setSize(height, height)
self:setSize(100, 80) self:setSize(100, 80)
self.child = { self.data.child = {
self.head, self.data.head,
self.title, self.data.title,
self.area, self.data.area,
self.close self.data.close
} }
self.titleOverflow = "trunicate" self.data.overflow = "trunicate"
self.area.mousepressed = function() self.data.area.mousepressed = function()
return true return true
end end
self.area.clicked = function() self.data.area.clicked = function()
return true return true
end end
self.close.clicked = function() self.data.close.clicked = function()
self:delete() self:delete()
return true return true
end end
self.head.selected = false self.data.head.data.selected = false
if mousemoved_event then if mousemoved_event then
self.head.mousemoved = function(self, x, y, dx, dy) self.data.head.mousemoved = function(self, x, y, dx, dy)
if self.selected then if self.data.selected then
self.parent:move(dx, dy) self.data.parent:move(dx, dy)
return true return true
end end
return false return false
end end
self.head.mousepressed = function(self, x, y, button) self.data.head.mousepressed = function(self, x, y, button)
if button == left then if button == left then
self.selected = true self.data.selected = true
return true return true
end end
return false return false
end end
else else
self.head.mx = 0 self.data.head.data.mx = 0
self.head.my = 0 self.data.head.data.my = 0
self.head.update = function(self) self.data.head.update = function(self)
local x, y = mouse.getPosition() local x, y = mouse.getPosition()
return self:setPosition(x - mx, y - my) return self:setPosition(x - self.data.mx, y - self.data.my)
end end
self.head.mousepressed = function(self, x, y, button) self.data.head.mousepressed = function(self, x, y, button)
if button == left then if button == left then
self.selected = true self.data.selected = true
self.mx = x self.data.mx = x
self.my = y self.data.my = y
return true return true
end end
return false return false
end end
end end
self.head.mousereleased = function(self, x, y, button) self.data.head.mousereleased = function(self, x, y, button)
if button == left then if button == left then
self.selected = false self.data.selected = false
pop_ref.focused = false
return true return true
end end
return false return false

View File

@ -96,12 +96,12 @@ pop.create = function(element, parent, ...)
end end
if inheritsFromElement(parent) then if inheritsFromElement(parent) then
element = pop.elements[element](parent, ...) element = pop.elements[element](parent, ...)
insert(parent.child, element) insert(parent.data.child, element)
elseif parent == false then elseif parent == false then
element = pop.elements[element](false, ...) element = pop.elements[element](false, ...)
else else
element = pop.elements[element](pop.screen, parent, ...) element = pop.elements[element](pop.screen, parent, ...)
insert(pop.screen.child, element) insert(pop.screen.data.child, element)
end end
return element return element
end end
@ -109,12 +109,12 @@ pop.update = function(dt, element)
if element == nil then if element == nil then
element = pop.screen element = pop.screen
end end
if not (element.excludeUpdate) then if element.data.update then
if element.update then if element.update then
element:update(dt) element:update(dt)
end end
for i = 1, #element.child do for i = 1, #element.data.child do
pop.update(dt, element.child[i]) pop.update(dt, element.data.child[i])
end end
end end
end end
@ -122,12 +122,12 @@ pop.draw = function(element)
if element == nil then if element == nil then
element = pop.screen element = pop.screen
end end
if not (element.excludeDraw) then if element.data.draw then
if element.draw then if element.draw then
element:draw() element:draw()
end end
for i = 1, #element.child do for i = 1, #element.data.child do
pop.draw(element.child[i]) pop.draw(element.data.child[i])
end end
end end
end end
@ -143,19 +143,19 @@ pop.mousepressed = function(x, y, button, element)
element = pop.screen element = pop.screen
end end
local handled = false 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 (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 for i = #element.data.child, 1, -1 do
do do
handled = pop.mousepressed(x, y, button, element.child[i]) handled = pop.mousepressed(x, y, button, element.data.child[i])
if handled then if handled then
return handled return handled
end end
end end
end end
if not (handled) then if not (handled) then
if element.mousepressed and (not element.excludeDraw) then if element.mousepressed and element.data.draw then
do do
handled = element:mousepressed(x - element.x, y - element.y, button) handled = element:mousepressed(x - element.data.x, y - element.data.y, button)
if handled then if handled then
pop.focused = element pop.focused = element
end end
@ -169,19 +169,19 @@ pop.mousereleased = function(x, y, button, element)
local clickedHandled = false local clickedHandled = false
local mousereleasedHandled = false local mousereleasedHandled = false
if element then if element then
if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then 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.child, 1, -1 do for i = #element.data.child, 1, -1 do
clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, element.child[i]) clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, element.data.child[i])
if clickedHandled or mousereleasedHandled then if clickedHandled or mousereleasedHandled then
return clickedHandled, mousereleasedHandled return clickedHandled, mousereleasedHandled
end end
end end
if not (clickedHandled or mousereleasedHandled) then if not (clickedHandled or mousereleasedHandled) then
if element.clicked and (not element.excludeDraw) then if element.clicked and element.data.draw then
clickedHandled = element:clicked(x - element.x, y - element.y, button) clickedHandled = element:clicked(x - element.data.x, y - element.data.y, button)
end end
if element.mousereleased then if element.mousereleased then
mousereleasedHandled = element:mousereleased(x - element.x, y - element.y, button) mousereleasedHandled = element:mousereleased(x - element.data.x, y - element.data.y, button)
end end
if clickedHandled then if clickedHandled then
pop.focused = element pop.focused = element
@ -197,7 +197,7 @@ end
pop.keypressed = function(key) pop.keypressed = function(key)
print("keypressed", key) print("keypressed", key)
local element = pop.focused local element = pop.focused
if element and element.keypressed and (not element.excludeDraw) then if element and element.keypressed and element.data.draw then
return element.keypressed(key) return element.keypressed(key)
end end
return false return false
@ -213,7 +213,7 @@ end
pop.textinput = function(text) pop.textinput = function(text)
print("textinput", text) print("textinput", text)
local element = pop.focused local element = pop.focused
if element and element.textinput and (not element.excludeDraw) then if element and element.textinput and element.data.draw then
return element.textinput(text) return element.textinput(text)
end end
return false return false
@ -225,23 +225,23 @@ pop.skin = function(element, skin, depth)
if skin == nil then if skin == nil then
skin = pop.skins.default skin = pop.skins.default
end end
if element.background and skin.background then if element.data.background and skin.background then
element.background = skin.background element.data.background = skin.background
end end
if element.color and skin.color then if element.data.color and skin.color then
element.color = skin.color element.data.color = skin.color
end end
if element.font and skin.font then if element.data.font and skin.font then
element.font = skin.font element.data.font = skin.font
end end
if not (depth or (depth == 0)) then if not (depth or (depth == 0)) then
if depth == tonumber(depth) then if depth == tonumber(depth) then
for i = 1, #element.child do for i = 1, #element.data.child do
pop.skin(element.child[i], skin, depth - 1) pop.skin(element.data.child[i], skin, depth - 1)
end end
else else
for i = 1, #element.child do for i = 1, #element.data.child do
pop.skin(element.child[i], skin, false) pop.skin(element.data.child[i], skin, false)
end end
end end
end end
@ -255,14 +255,14 @@ pop.debugDraw = function(element)
else else
graphics.setLineWidth(1) graphics.setLineWidth(1)
graphics.setLineColor(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.data.x, element.data.y, element.data.w, element.data.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.data.x, element.data.y, element.data.w, element.data.h)
graphics.setColor(200, 200, 200, 255) graphics.setColor(200, 200, 200, 255)
graphics.print(".", element.x, element.y) graphics.print(".", element.data.x, element.data.y)
end end
for i = 1, #element.child do for i = 1, #element.data.child do
pop.debugDraw(element.child[i]) pop.debugDraw(element.data.child[i])
end end
end end
pop.printElementTree = function(element, depth) pop.printElementTree = function(element, depth)
@ -283,8 +283,8 @@ pop.printElementTree = function(element, depth)
cls = cls .. " (" .. tostring(bg) .. ")" cls = cls .. " (" .. tostring(bg) .. ")"
end end
print(string.rep("-", depth) .. " " .. tostring(cls)) print(string.rep("-", depth) .. " " .. tostring(cls))
for i = 1, #element.child do for i = 1, #element.data.child do
pop.printElementStack(element.child[i], depth + 1) pop.printElementStack(element.data.child[i], depth + 1)
end end
end end
pop.load() pop.load()

View File

@ -8,53 +8,53 @@ class box extends element
new: (parent, background=false) => new: (parent, background=false) =>
super parent super parent
@w = 20 @data.w = 20
@h = 20 @data.h = 20
@background = background @data.background = background
draw: => draw: =>
if @background if @data.background
if type(@background) == "table" if type(@data.background) == "table"
graphics.setColor @background graphics.setColor @data.background
graphics.rectangle "fill", @x, @y, @w, @h graphics.rectangle "fill", @data.x, @data.y, @data.w, @data.h
else else
w, h = @background\getDimensions! w, h = @data.background\getDimensions!
w = @w / w w = @data.w / w
h = @h / h h = @data.h / h
graphics.setColor 255, 255, 255, 255 graphics.setColor 255, 255, 255, 255
graphics.draw @background, @x, @y, 0, w, h graphics.draw @data.background, @data.x, @data.y, 0, w, h
return @ return @
debugDraw: => debugDraw: =>
graphics.setLineWidth 0.5 graphics.setLineWidth 0.5
graphics.setColor 0, 0, 0, 100 graphics.setColor 0, 0, 0, 100
graphics.rectangle "fill", @x, @y, @w, @h graphics.rectangle "fill", @data.x, @data.y, @data.w, @data.h
graphics.setColor 0, 0, 200, 200 graphics.setColor 0, 0, 200, 200
graphics.rectangle "line", @x, @y, @w, @h graphics.rectangle "line", @data.x, @data.y, @data.w, @data.h
graphics.setColor 200, 200, 255, 255 graphics.setColor 200, 200, 255, 255
graphics.print "b", @x, @y graphics.print "b", @data.x, @data.y
return @ return @
setBackground: (background) => setBackground: (background) =>
@background = background @data.background = background
return @ return @
getBackground: => getBackground: =>
return @background return @data.background
setColor: (r, g, b, a=255) => setColor: (r, g, b, a=255) =>
if type(r) == "table" if type(r) == "table"
@background = r @data.background = r
else else
@background = {r, g, b, a} @data.background = {r, g, b, a}
return @ return @
getColor: => getColor: =>
if type(@background) == "table" if type(@data.background) == "table"
return unpack @background return unpack @data.background
else else
error "Box \"#{@}\" doesn't have a color." --might be a bad idea error "Box \"#{@}\" doesn't have a color." --might be a bad idea

View File

@ -5,51 +5,50 @@ tonumber = tonumber
class element class element
new: (parent) => new: (parent) =>
@parent = parent
@child = {}
@w = 0 @data = {
@h = 0 parent: parent
child: {}
@spacing = 0 w: 0
h: 0
x: 0
y: 0
horizontal: "left"
vertical: "top"
margin: 0
draw: true
update: true
move: true
}
if parent if parent
@x = parent.x @data.x = parent.data.x
@y = parent.y @data.y = parent.data.y
--@horizontal = parent.horizontal --@data.horizontal = parent.data.horizontal
--@vertical = parent.vertical --@data.vertical = parent.data.vertical
--@align! --@align!
else
@x = 0
@y = 0
--@horizontal = "left"
--@vertical = "top"
@horizontal = "left"
@vertical = "top"
@excludeDraw = false
@excludeUpdate = false
@excludeMovement = false
debugDraw: => debugDraw: =>
graphics.setLineWidth 0.5 graphics.setLineWidth 0.5
graphics.setColor 0, 0, 0, 100 graphics.setColor 0, 0, 0, 100
graphics.rectangle "fill", @x, @y, @w, @h graphics.rectangle "fill", @data.x, @data.y, @data.w, @data.h
graphics.setColor 0, 200, 0, 200 graphics.setColor 0, 200, 0, 200
graphics.rectangle "line", @x, @y, @w, @h graphics.rectangle "line", @data.x, @data.y, @data.w, @data.h
graphics.setColor 200, 255, 200, 255 graphics.setColor 200, 255, 200, 255
graphics.print "e", @x, @y graphics.print "e", @data.x, @data.y
return @ return @
addChild: (child) => addChild: (child) =>
-- make sure we don't duplicate references -- make sure we don't duplicate references
if child.parent if child.data.parent
child.parent\removeChild child child.data.parent\removeChild child
insert @child, child insert @data.child, child
child.parent = @ child.data.parent = @
child\align! --NOTE not 100% sure if this is a good idea child\align! --NOTE not 100% sure if this is a good idea
@ -60,133 +59,135 @@ class element
removeChild: (child) => removeChild: (child) =>
if tonumber(child) == child if tonumber(child) == child
-- remove indexed child, return it -- remove indexed child, return it
@child[child].parent = false @data.child[child].data.parent = false
return remove @child, child return remove @data.child, child
else else
for k,v in ipairs @child for k,v in ipairs @data.child
if v == child if v == child
return remove @child, k return remove @data.child, k --NOTE might break due to modifying table while indexing it? shouldn't as it returns...
return "Element \"#{child}\" is not a child of element \"#{@}\". Cannot remove it." return "Element \"#{child}\" is not a child of element \"#{@}\". Cannot remove it."
-- returning an error string instead of erroring is kind of bad, but needed for window to function...
getChildren: => getChildren: =>
return @child return @data.child
--focusChild: (child) => --focusChild: (child) =>
-- insert @child, 1, @removeChild(child) -- insert @data.child, 1, @removeChild(child)
-- return @ -- return @
move: (x, y) => move: (x, y) =>
if x if x
@x = @x + x @data.x = @data.x + x
if y if y
@y = @y + y @data.y = @data.y + y
for i = 1, #@child for i = 1, #@data.child
unless @child[i].excludeMovement --unless @data.child[i].excludeMovement
@child[i]\move x, y if @data.child[i].data.move
@data.child[i]\move x, y
return @ return @
setPosition: (x, y) => setPosition: (x, y) =>
oldX = @x oldX = @data.x
oldY = @y oldY = @data.y
if x if x
switch @horizontal switch @data.horizontal
when "left" when "left"
@x = x @data.x = x
when "center" when "center"
@x = x - @w/2 @data.x = x - @data.w/2
when "right" when "right"
@x = x - @w @data.x = x - @data.w
else else
x = oldX x = oldX
if y if y
switch @vertical switch @data.vertical
when "top" when "top"
@y = y @data.y = y
when "center" when "center"
@y = y - @h/2 @data.y = y - @data.h/2
when "bottom" when "bottom"
@y = y - @h @data.y = y - @data.h
else else
y = oldY y = oldY
for i = 1, #@child for i = 1, #@data.child
@child[i]\move x - oldX, y - oldY @data.child[i]\move x - oldX, y - oldY
return @ return @
getPosition: => getPosition: =>
resultX = @x resultX = @data.x
resultY = @y resultY = @data.y
switch @horizontal switch @data.horizontal
when "center" when "center"
resultX += @w/2 resultX += @data.w/2
when "right" when "right"
resultY += @w resultY += @data.w
switch @vertical switch @data.vertical
when "center" when "center"
resultY += @h/2 resultY += @data.h/2
when "bottom" when "bottom"
resultY += @h resultY += @data.h
return resultX, resultY return resultX, resultY
setSize: (w, h) => setSize: (w, h) =>
if w if w
switch @horizontal switch @data.horizontal
when "center" when "center"
@x -= (w - @w)/2 @data.x -= (w - @data.w)/2
when "right" when "right"
@x -= w - @w @data.x -= w - @data.w
@w = w @data.w = w
if h if h
switch @vertical switch @data.vertical
when "center" when "center"
@y -= (h - @h)/2 @data.y -= (h - @data.h)/2
when "bottom" when "bottom"
@y -= h - @h @data.y -= h - @data.h
@h = h @data.h = h
return @ return @
getSize: => getSize: =>
return @w, @h return @data.w, @data.h
setWidth: (w) => setWidth: (w) =>
switch @horizontal switch @data.horizontal
when "center" when "center"
@x -= (w - @w)/2 @data.x -= (w - @data.w)/2
when "right" when "right"
@x -= w - @w @data.x -= w - @data.w
@w = w @data.w = w
return @ return @
getWidth: => getWidth: =>
return @w return @data.w
setHeight: (h) => setHeight: (h) =>
switch @vertical switch @data.vertical
when "center" when "center"
@y -= (h - @h)/2 @data.y -= (h - @data.h)/2
when "bottom" when "bottom"
@y -= h - @h @data.y -= h - @data.h
@h = h @data.h = h
return @ return @
getHeight: => getHeight: =>
return @h return @data.h
adjustSize: (w, h) => adjustSize: (w, h) =>
W, H = @getSize! W, H = @getSize!
@ -204,84 +205,88 @@ class element
align: (horizontal, vertical, toPixel=true) => align: (horizontal, vertical, toPixel=true) =>
@setAlignment horizontal, vertical @setAlignment horizontal, vertical
@x = @parent.x @data.x = @data.parent.data.x
@y = @parent.y @data.y = @data.parent.data.y
switch @horizontal switch @data.horizontal
when "left" when "left"
@x += @spacing @data.x += @data.margin
when "center" when "center"
@x += (@parent.w - @w)/2 @data.x += (@data.parent.data.w - @data.w)/2
when "right" when "right"
@x += @parent.w - @w - @spacing @data.x += @data.parent.data.w - @data.w - @data.margin
switch @vertical switch @data.vertical
when "top" when "top"
@y += @spacing @data.y += @data.margin
when "center" when "center"
@y += (@parent.h - @h)/2 @data.y += (@data.parent.data.h - @data.h)/2
when "bottom" when "bottom"
@y += @parent.h - @h - @spacing @data.y += @data.parent.data.h - @data.h - @data.margin
if toPixel if toPixel
@x = floor @x @data.x = floor @data.x
@y = floor @y @data.y = floor @data.y
--NOTE why does align not move or align children? maybe it's good that it doesn't, maybe it's bad
return @ return @
alignTo: (element, horizontal, vertical, toPixel=true) => alignTo: (element, horizontal, vertical, toPixel=true) =>
parent = @parent parent = @data.parent
@parent = element @data.parent = element
@align horizontal, vertical, toPixel @align horizontal, vertical, toPixel
@parent = parent @data.parent = parent
return @ return @
setAlignment: (horizontal, vertical) => setAlignment: (horizontal, vertical) =>
if horizontal if horizontal
@horizontal = horizontal @data.horizontal = horizontal
if vertical if vertical
@vertical = vertical @data.vertical = vertical
return @ return @
getAlignment: => getAlignment: =>
return @horizontal, @vertical return @data.horizontal, @data.vertical
setMargin: (spacing) => setMargin: (margin) =>
@spacing = spacing @data.margin = margin
@align! @align!
return @ return @
getMargin: => getMargin: =>
return @spacing return @data.margin
fill: => fill: =>
@x = @parent.x + @spacing @data.x = @data.parent.data.x + @data.margin
@y = @parent.y + @spacing @data.y = @data.parent.data.y + @data.margin
@w = @parent.w - @spacing*2 @data.w = @data.parent.data.w - @data.margin*2
@h = @parent.h - @spacing*2 @data.h = @data.parent.data.h - @data.margin*2
delete: => delete: =>
for k,v in ipairs @child for k,v in ipairs @data.child
v\delete! v\delete!
@parent\removeChild @ @data.parent\removeChild @
--@data.child = nil --not 100% sure if this is needed
--@data = nil -- pretty sure this .. actually has no effect for why I added it
@ = nil @ = nil
return nil return nil
getVisibility: => getVisibility: =>
return (not @excludeDraw) return @data.draw
setVisibility: (isVisible) => setVisibility: (isVisible) =>
@excludeDraw = (not isVisible) @data.draw = isVisible
return @ return @
getStatic: => getStatic: =>
return @excludeMovement return (not @data.move)
setStatic: (isStatic) => setStatic: (isStatic) =>
@excludeMovement = isStatic @data.move = (not isStatic)
return @ return @

View File

@ -16,47 +16,47 @@ class text extends element
new: (parent, text="", color={255,255,255,255}) => new: (parent, text="", color={255,255,255,255}) =>
super parent super parent
@font = graphics.newFont 14 @data.font = graphics.newFont 14
@setText text @setText text
@color = color @data.color = color
draw: => draw: =>
graphics.setColor @color graphics.setColor @data.color
graphics.setFont @font graphics.setFont @data.font
graphics.print @txt, @x, @y graphics.print @data.text, @data.x, @data.y
return @ return @
debugDraw: => debugDraw: =>
graphics.setLineWidth 0.5 graphics.setLineWidth 0.5
graphics.setColor 0, 0, 0, 100 graphics.setColor 0, 0, 0, 100
graphics.rectangle "fill", @x, @y, @w, @h graphics.rectangle "fill", @data.x, @data.y, @data.w, @data.h
graphics.setColor 200, 0, 0, 200 graphics.setColor 200, 0, 0, 200
graphics.rectangle "line", @x, @y, @w, @h graphics.rectangle "line", @data.x, @data.y, @data.w, @data.h
graphics.setColor 255, 200, 200, 255 graphics.setColor 255, 200, 200, 255
graphics.print "t", @x, @y graphics.print "t", @data.x, @data.y
return @ return @
-- unlike most elements, you cannot set a size for text elements -- unlike most elements, you cannot set a size for text elements
setSize: => setSize: =>
w = @font\getWidth @txt w = @data.font\getWidth @data.text
h = @font\getHeight! * (select(2, @txt\gsub("\n", "\n")) + 1) --hack to get height of multiple lines h = @data.font\getHeight! * (select(2, @data.text\gsub("\n", "\n")) + 1) --hack to get height of multiple lines
switch @horizontal switch @data.horizontal
when "center" when "center"
@x -= (w - @w)/2 @data.x -= (w - @data.w)/2
when "right" when "right"
@x -= w - @w - @spacing @data.x -= w - @data.w - @data.margin
switch @vertical switch @vertical
when "center" when "center"
@y -= (h - @h)/2 @data.y -= (h - @data.h)/2
when "bottom" when "bottom"
@y -= h - @h - @spacing @data.y -= h - @data.h - @data.margin
@w = w @data.w = w
@h = h @data.h = h
return @ return @
@ -71,28 +71,28 @@ class text extends element
return @ return @
setText: (text="") => setText: (text="") =>
@txt = text @data.text = text
@setSize! @setSize!
return @ return @
getText: => getText: =>
return @txt return @data.text
setFont: (font) => setFont: (font) =>
@font = font @data.font = font
@setSize! @setSize!
return @ return @
getFont: => getFont: =>
return @font return @data.font
setColor: (r, g, b, a=255) => setColor: (r, g, b, a=255) =>
if type(r) == "table" if type(r) == "table"
@color = r @data.color = r
else else
@color = {r, g, b, a} @data.color = {r, g, b, a}
return @ return @
getColor: => getColor: =>
return unpack @color return unpack @data.color

View File

@ -25,134 +25,139 @@ do
print "elements/window: unrecognized LOVE version: #{major}.#{minor}.#{revision}" print "elements/window: unrecognized LOVE version: #{major}.#{minor}.#{revision}"
print " assuming LOVE version > 0.10.1 (there may be bugs)" print " assuming LOVE version > 0.10.1 (there may be bugs)"
pop_ref = false -- reference to pop, loaded by pop.load! --pop_ref = false -- reference to pop, loaded by pop.load!
-- turns out we don't need it...?
class window extends element class window extends element
load: (pop) -> --TODO IMPLEMENT THIS COMMENTED OUT CHANGE. IT SHOULD WORK THE SAME EXCEPT BE BETTER.
pop_ref = pop --@load = (pop) ->
--load: (pop) ->
-- pop_ref = pop
new: (parent, title="window", tBackground={25, 180, 230, 255}, tColor={255, 255, 255, 255}, wBackground={200, 200, 210, 255}) => new: (parent, title="window", tBackground={25, 180, 230, 255}, tColor={255, 255, 255, 255}, wBackground={200, 200, 210, 255}) =>
super parent super parent
-- NOTE @title having @head as its parent might break things horribly -- NOTE @data.title having @data.head as its parent might break things horribly
@head = box @, tBackground -- title box at top @data.head = box @, tBackground -- title box at top
@title = text @head, title, tColor -- text at top @data.title = text @data.head, title, tColor -- text at top
@area = box @, wBackground -- main window area @data.area = box @, wBackground -- main window area
@close = box @, closeImage -- close button @data.close = box @, closeImage -- close button
-- correct placement / sizes of elements -- correct placement / sizes of elements
height = @title\getHeight! height = @data.title\getHeight!
@head\setSize @w - height, height @data.head\setSize @data.w - height, height
@area\move nil, height @data.area\move nil, height
@close\align("right")\setSize height, height @data.close\align("right")\setSize height, height
@setSize 100, 80 @setSize 100, 80
-- our child elements are still child elements -- our child elements are still child elements
--TODO change title to be a child of head ? --NOTE this is needed anywhere an element creates its own children (aka is NOT called through pop.create)
@child = { -- TODO put this note in the dev docs!
@head, @title, @area, @close --TODO change title to be a child of head ? title already thinks that head is its parent (look above!)
@data.child = {
@data.head, @data.title, @data.area, @data.close
} }
@titleOverflow = "trunicate" -- defaults to trunicating title to fit in window @data.overflow = "trunicate" -- defaults to trunicating title to fit in window
-- window area steals mouse events to keep them from propagating under it -- window area steals mouse events to keep them from propagating under it
@area.mousepressed = -> @data.area.mousepressed = ->
return true return true
@area.clicked = -> @data.area.clicked = ->
return true return true
@close.clicked = -> @data.close.clicked = ->
@delete! @delete!
return true return true
@head.selected = false -- whether or not the window title (and thus, the window) has been selected @data.head.data.selected = false -- whether or not the window title (and thus, the window) has been selected
if mousemoved_event if mousemoved_event
@head.mousemoved = (x, y, dx, dy) => @data.head.mousemoved = (x, y, dx, dy) =>
if @selected if @data.selected
@parent\move dx, dy @data.parent\move dx, dy
return true return true
return false return false
@head.mousepressed = (x, y, button) => @data.head.mousepressed = (x, y, button) =>
if button == left if button == left
@selected = true @data.selected = true
return true return true
return false return false
else else
@head.mx = 0 -- local mouse coordinates when selected @data.head.data.mx = 0 -- local mouse coordinates when selected
@head.my = 0 @data.head.data.my = 0
@head.update = => @data.head.update = =>
x, y = mouse.getPosition! x, y = mouse.getPosition!
@setPosition x - mx, y - my @setPosition x - @data.mx, y - @data.my
--return false -- why?
@head.mousepressed = (x, y, button) => @data.head.mousepressed = (x, y, button) =>
if button == left if button == left
@selected = true @data.selected = true
@mx = x @data.mx = x
@my = y @data.my = y
return true return true
return false return false
@head.mousereleased = (x, y, button) => @data.head.mousereleased = (x, y, button) =>
if button == left if button == left
@selected = false @data.selected = false
pop_ref.focused = false -- clear our focus --pop_ref.focused = false -- clear our focus (should this be done? I think not)
return true return true
return false return false
--@head.focusChild = => --@data.head.focusChild = =>
-- @parent\focusChild @ -- nope -- @data.parent\focusChild @ -- nope
-- return @ -- return @
debugDraw: => debugDraw: =>
graphics.setLineWidth 0.5 graphics.setLineWidth 0.5
graphics.setColor 0, 0, 0, 100 graphics.setColor 0, 0, 0, 100
graphics.rectangle "fill", @x, @y, @w, @h graphics.rectangle "fill", @data.x, @data.y, @data.w, @data.h
graphics.setColor 200, 0, 200, 200 graphics.setColor 200, 0, 200, 200
graphics.rectangle "line", @x, @y, @w, @h graphics.rectangle "line", @data.x, @data.y, @data.w, @data.h
graphics.setColor 255, 200, 255, 255 graphics.setColor 255, 200, 255, 255
graphics.print "w", @x, @y graphics.print "w", @data.x, @data.y
return @ return @
addChild: (child) => addChild: (child) =>
@area\addChild child @data.area\addChild child
return @ return @
-- pass through to window, but return us if window returns itself -- pass through to window, but return us if @data.area returns itself
--TODO check if that's even possible, with the API change to removeChild, it shouldn't be happening EVER
removeChild: (child) => removeChild: (child) =>
result = @area\removeChild child result = @data.area\removeChild child
if result == @area if result == @data.area
return @ return @
elseif type(result) == "string" elseif type(result) == "string"
for k,v in ipairs @child for k,v in ipairs @data.child
if v == child if v == child
remove @child, k remove @data.child, k
return @ return @
return "Element \"#{child}\" is not a child of window \"#{@}\". Cannot remove it." return "Element \"#{child}\" is not a child of window \"#{@}\". Cannot remove it."
else else
return result return result
getChildren: => getChildren: =>
return @area.child return @data.area.child
--focusChild: => --focusChild: =>
-- @parent\focusChild @ -- @data.parent\focusChild @
-- --NOTE might need to also actually focus the sub-element -- --NOTE might need to also actually focus the sub-element
-- return @ -- return @
align: (horizontal, vertical, toPixel) => align: (horizontal, vertical, toPixel) =>
super horizontal, vertical, toPixel super horizontal, vertical, toPixel
for i = 1, #@child for i = 1, #@data.child
@child[i]\align! @data.child[i]\align!
@area\move nil, @head\getHeight! @data.area\move nil, @data.head\getHeight!
return @ return @
@ -161,147 +166,150 @@ class window extends element
y = 0 y = 0
if w if w
switch @horizontal switch @data.horizontal
when "center" when "center"
x -= (w - @w)/2 x -= (w - @data.w)/2
when "right" when "right"
x -= w - @w x -= w - @data.w
if @close if @data.close
@head\setWidth w - @head\getHeight! @data.head\setWidth w - @data.head\getHeight!
else else
@head\setWidth w @data.head\setWidth w
@area\setWidth w @data.area\setWidth w
@w = w @data.w = w
@x += x @data.x += x
@title\align! @data.title\align!
if @close if @data.close
@close\align! @data.close\align!
if h if h
h = h - @head\getHeight! h = h - @data.head\getHeight!
switch @vertical switch @data.vertical
when "center" when "center"
y -= (h - @h)/2 y -= (h - @data.h)/2
when "right" when "right"
y -= h - @h y -= h - @data.h
@area\setHeight h @data.area\setHeight h
@h = h + @head\getHeight! @data.h = h + @data.head\getHeight!
@y += y @data.y += y
@head\move x, y @data.head\move x, y
--@title\move x, y --@data.title\move x, y
@area\move x, y @data.area\move x, y
return @ return @
setWidth: (w) => setWidth: (w) =>
x = 0 x = 0
switch @horizontal switch @data.horizontal
when "center" when "center"
x -= (w - @w)/2 x -= (w - @data.w)/2
when "right" when "right"
x -= w - @w x -= w - @data.w
if @close if @data.close
@head\setWidth w - @head\getHeight! @data.head\setWidth w - @data.head\getHeight!
else else
@head\setWidth w @data.head\setWidth w
@area\setWidth w @data.area\setWidth w
@w = w @data.w = w
@x += x @data.x += x
@title\align! @data.title\align!
if @close if @data.close
@close\align! @data.close\align!
@head\move x @data.head\move x
--@title\move x --@data.title\move x
@area\move x @data.area\move x
return @ return @
setHeight: (h) => setHeight: (h) =>
y = 0 y = 0
h = h - @head\getHeight! h = h - @data.head\getHeight!
switch @vertical switch @data.vertical
when "center" when "center"
y -= (h - @h)/2 y -= (h - @data.h)/2
when "right" when "right"
y -= h - @h y -= h - @data.h
@area\setHeight h @data.area\setHeight h
@h = h + @head\getHeight! @data.h = h + @data.head\getHeight!
@y += y @data.y += y
@head\move nil, y @data.head\move nil, y
@title\move nil, y @data.title\move nil, y
@area\move nil, y @data.area\move nil, y
return @ return @
setTitle: (title) => setTitle: (title) =>
@title\setText title @data.title\setText title
if @titleOverflow == "trunicate" --NOTE when the entire window is resized, these checks do not get performed..this is probably a bad thing
while @title\getWidth! > @head\getWidth! --TODO make sure they always get checked, and that a trunicated title is saved so it can be un-trunicated when able
if @data.overflow == "trunicate"
while @data.title\getWidth! > @data.head\getWidth!
title = title\sub 1, -3 title = title\sub 1, -3
@title\setText title .. "…" @data.title\setText title .. "…"
elseif @titleOverflow == "resize" elseif @data.overflow == "resize"
if @title\getWidth! > @head\getWidth! if @data.title\getWidth! > @data.head\getWidth!
@setWidth @title\getWidth! @setWidth @data.title\getWidth!
return @ return @
getTitle: => getTitle: =>
return @title\getText! return @data.title\getText!
setTitleOverflow: (method) => setTitleOverflow: (method) =>
@titleOverflow = method @data.overflow = method
return @ return @
getTitleOverflow: => getTitleOverflow: =>
return @titleOverflow return @data.overflow
--TODO make the constructor call this instead of having these methods defined twice...
setClose: (enabled) => setClose: (enabled) =>
if enabled if enabled
@close = box @, closeImage @data.close = box @, closeImage
@close.clicked = -> @data.close.clicked = ->
@delete! @delete!
return true return true
height = @head\getHeight! height = @data.head\getHeight!
@close\align("right")\setSize height, height @data.close\align("right")\setSize height, height
@head\setWidth @w - height @data.head\setWidth @data.w - height
@title\align! @data.title\align!
insert @child, @close insert @data.child, @data.close
else else
@close\delete! @data.close\delete!
@head\setWidth @w @data.head\setWidth @data.w
@title\align! @data.title\align!
@close = false @data.close = false
return @ return @
hasClose: => hasClose: =>
if @close if @data.close
return true return true
else else
return false return false
delete: => delete: =>
super! super!
@head = nil @data.head = nil
@title = nil @data.title = nil
@area = nil @data.area = nil
@close = nil @data.close = nil
return return

View File

@ -65,14 +65,19 @@ element.__base.static = (s) =>
else else
return @getStatic! return @getStatic!
--TODO implement this, as it now can be done!
-- box.__base.background -- can't be done! -- box.__base.background -- can't be done!
--TODO implement anything else / more than could/should be implemented on element class!
box.__base.color = (r, g, b, a) => box.__base.color = (r, g, b, a) =>
if r or g or b or a if r or g or b or a
return @setColor r, g, b, a return @setColor r, g, b, a
else else
return @getColor! return @getColor!
--TODO see if anything else should be done for boxes!
text.__base.text = (text) => text.__base.text = (text) =>
if text if text
return @setText text return @setText text
@ -90,3 +95,5 @@ text.__base.color = (r, g, b, a) =>
return @setColor r, g, b, a return @setColor r, g, b, a
else else
return @getColor! return @getColor!
--TODO see if anything else should be done for boxes!

View File

@ -56,6 +56,8 @@ pop.load = ->
unless skins[i]\sub(-4) == ".lua" unless skins[i]\sub(-4) == ".lua"
continue continue
--NOTE why not allow skins to have a load function that is passed a reference to pop?
name = skins[i]\sub 1, -5 name = skins[i]\sub 1, -5
pop.skins[name] = require "#{path}/skins/#{name}" pop.skins[name] = require "#{path}/skins/#{name}"
@ -70,6 +72,7 @@ pop.load = ->
name = extensions[i]\sub 1, -5 name = extensions[i]\sub 1, -5
require "#{path}/extensions/#{name}" require "#{path}/extensions/#{name}"
--TODO find out if there is a use case for actually having a reference to one of these when loaded
print "extension loaded: \"#{name}\"" print "extension loaded: \"#{name}\""
@ -82,30 +85,32 @@ pop.create = (element, parent=pop.screen, ...) ->
-- if valid parent element -- if valid parent element
if inheritsFromElement parent if inheritsFromElement parent
element = pop.elements[element](parent, ...) element = pop.elements[element](parent, ...)
insert parent.child, element insert parent.data.child, element
-- if explicitly no parent -- if explicitly no parent
elseif parent == false elseif parent == false
element = pop.elements[element](false, ...) element = pop.elements[element](false, ...)
-- else use pop.screen, and "parent" is actually first argument -- else use pop.screen, and "parent" is actually first argument
else else
element = pop.elements[element](pop.screen, parent, ...) element = pop.elements[element](pop.screen, parent, ...)
insert pop.screen.child, element insert pop.screen.data.child, element
return element return element
pop.update = (dt, element=pop.screen) -> pop.update = (dt, element=pop.screen) ->
unless element.excludeUpdate if element.data.update
if element.update if element.update
element\update dt element\update dt
for i = 1, #element.child for i = 1, #element.data.child
pop.update dt, element.child[i] pop.update dt, element.data.child[i]
--for child in *element\getChildren!
-- pop.update dt, child
pop.draw = (element=pop.screen) -> pop.draw = (element=pop.screen) ->
unless element.excludeDraw if element.data.draw
if element.draw if element.draw
element\draw! element\draw!
for i = 1, #element.child for i = 1, #element.data.child
pop.draw element.child[i] pop.draw element.data.child[i]
--TODO implement a way for an element to attach itself to mousemoved events --TODO implement a way for an element to attach itself to mousemoved events
pop.mousemoved = (x, y, dx, dy) -> pop.mousemoved = (x, y, dx, dy) ->
@ -124,17 +129,17 @@ pop.mousepressed = (x, y, button, element) ->
handled = false handled = false
-- if it was inside the current element.. -- if it was inside the current element..
if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) 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)
-- check its child elements in reverse order, returning if something handles it -- check its child elements in reverse order, returning if something handles it
for i = #element.child, 1, -1 for i = #element.data.child, 1, -1
if handled = pop.mousepressed x, y, button, element.child[i] if handled = pop.mousepressed x, y, button, element.data.child[i]
return handled return handled
-- if a child hasn't handled it yet -- if a child hasn't handled it yet
unless handled unless handled
-- if we can handle it and are visible, try to handle it, and set pop.focused -- if we can handle it and are visible, try to handle it, and set pop.focused
if element.mousepressed and (not element.excludeDraw) if element.mousepressed and element.data.draw
if handled = element\mousepressed x - element.x, y - element.y, button if handled = element\mousepressed x - element.data.x, y - element.data.y, button
pop.focused = element pop.focused = element
-- return whether or not we have handled the event -- return whether or not we have handled the event
@ -147,28 +152,28 @@ pop.mousereleased = (x, y, button, element) ->
-- if we have an element, and are within its bounds -- if we have an element, and are within its bounds
if element if element
if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) 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)
-- check its children in reverse for handling a clicked or mousereleased event -- check its children in reverse for handling a clicked or mousereleased event
for i = #element.child, 1, -1 for i = #element.data.child, 1, -1
clickedHandled, mousereleasedHandled = pop.mousereleased x, y, button, element.child[i] clickedHandled, mousereleasedHandled = pop.mousereleased x, y, button, element.data.child[i]
if clickedHandled or mousereleasedHandled if clickedHandled or mousereleasedHandled
return clickedHandled, mousereleasedHandled return clickedHandled, mousereleasedHandled
-- if that doesn't work, we try to handle it ourselves -- if that doesn't work, we try to handle it ourselves
unless clickedHandled or mousereleasedHandled unless clickedHandled or mousereleasedHandled
-- clicked only happens on visible elements, mousereleased happens either way -- clicked only happens on visible elements, mousereleased happens either way
if element.clicked and (not element.excludeDraw) if element.clicked and element.data.draw
clickedHandled = element\clicked x - element.x, y - element.y, button clickedHandled = element\clicked x - element.data.x, y - element.data.y, button
if element.mousereleased if element.mousereleased
mousereleasedHandled = element\mousereleased x - element.x, y - element.y, button mousereleasedHandled = element\mousereleased x - element.data.x, y - element.data.y, button
-- if we clicked, we're focused! -- if we clicked, we're focused!
if clickedHandled if clickedHandled
pop.focused = element pop.focused = element
--NOTE this might cause an error in the above for loop! --NOTE this might cause an error in the above for loop!
-- basically, move focused element to front of its parent's child -- basically, move focused element to front of its parent's child
--element.parent\focusChild element --element.data.parent\focusChild element
--table.insert element.parent, element.parent\removeChild(element), --table.insert element.data.parent, element.parent\removeChild(element),
-- else, default to pop.screen to begin! (and print that we received an event) -- else, default to pop.screen to begin! (and print that we received an event)
else else
@ -182,7 +187,7 @@ pop.keypressed = (key) ->
-- keypressed events must be on visible elements -- keypressed events must be on visible elements
element = pop.focused element = pop.focused
if element and element.keypressed and (not element.excludeDraw) if element and element.keypressed and element.data.draw
return element.keypressed key return element.keypressed key
return false return false
@ -202,7 +207,7 @@ pop.textinput = (text) ->
-- textinput events must be on visible elements -- textinput events must be on visible elements
element = pop.focused element = pop.focused
if element and element.textinput and (not element.excludeDraw) if element and element.textinput and element.data.draw
return element.textinput text return element.textinput text
return false return false
@ -213,20 +218,20 @@ pop.textinput = (text) ->
-- depth can be an integer for how many levels to go down when skinning -- depth can be an integer for how many levels to go down when skinning
-- defaults to pop.screen and the default skin -- defaults to pop.screen and the default skin
pop.skin = (element=pop.screen, skin=pop.skins.default, depth) -> pop.skin = (element=pop.screen, skin=pop.skins.default, depth) ->
if element.background and skin.background if element.data.background and skin.background
element.background = skin.background element.data.background = skin.background
if element.color and skin.color if element.data.color and skin.color
element.color = skin.color element.data.color = skin.color
if element.font and skin.font if element.data.font and skin.font
element.font = skin.font element.data.font = skin.font
unless depth or (depth == 0) unless depth or (depth == 0)
if depth == tonumber depth if depth == tonumber depth
for i = 1, #element.child for i = 1, #element.data.child
pop.skin element.child[i], skin, depth - 1 pop.skin element.data.child[i], skin, depth - 1
else else
for i = 1, #element.child for i = 1, #element.data.child
pop.skin element.child[i], skin, false pop.skin element.data.child[i], skin, false
pop.debugDraw = (element=pop.screen) -> pop.debugDraw = (element=pop.screen) ->
if element.debugDraw if element.debugDraw
@ -234,14 +239,14 @@ pop.debugDraw = (element=pop.screen) ->
else else
graphics.setLineWidth 1 graphics.setLineWidth 1
graphics.setLineColor 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.data.x, element.data.y, element.data.w, element.data.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.data.x, element.data.y, element.data.w, element.data.h
graphics.setColor 200, 200, 200, 255 graphics.setColor 200, 200, 200, 255
graphics.print ".", element.x, element.y graphics.print ".", element.data.x, element.data.y
for i = 1, #element.child for i = 1, #element.data.child
pop.debugDraw element.child[i] pop.debugDraw element.data.child[i]
pop.printElementTree = (element=pop.screen, depth=0) -> pop.printElementTree = (element=pop.screen, depth=0) ->
cls = element.__class.__name cls = element.__class.__name
@ -258,8 +263,8 @@ pop.printElementTree = (element=pop.screen, depth=0) ->
print string.rep("-", depth) .. " #{cls}" print string.rep("-", depth) .. " #{cls}"
for i = 1, #element.child for i = 1, #element.data.child
pop.printElementStack element.child[i], depth + 1 pop.printElementStack element.data.child[i], depth + 1
pop.load! pop.load!

View File

@ -1,6 +1,9 @@
-- Note that the "default" name is a bit of a misnomer, as this does not -- Note that the "default" name is a bit of a misnomer, as this does not
-- specify the defaults used in Pop.Box elements (they define their own) -- specify the defaults used in Pop.Box elements (they define their own)
--TODO make this actually specify defaults and be used (along with redoing the
-- skinning system entirely)
import graphics from love import graphics from love
return { return {