Pop.Box/lib/pop/elements/element.lua
2016-04-27 12:03:24 -07:00

280 lines
7.2 KiB
Lua

local graphics
graphics = love.graphics
local floor
floor = math.floor
local insert, remove
do
local _obj_0 = table
insert, remove = _obj_0.insert, _obj_0.remove
end
local tonumber = tonumber
local element
do
local _class_0
local _base_0 = {
debugDraw = function(self)
graphics.setLineWidth(0.5)
graphics.setColor(0, 0, 0, 100)
graphics.rectangle("fill", self.x, self.y, self.w, self.h)
graphics.setColor(0, 200, 0, 200)
graphics.rectangle("line", self.x, self.y, self.w, self.h)
graphics.setColor(200, 255, 200, 255)
graphics.print("e", self.x, self.y)
return self
end,
addChild = function(self, child)
if child.parent then
child.parent:removeChild(child)
end
insert(self.child, child)
child.parent = self
child:align()
return self
end,
removeChild = function(self, child)
if tonumber(child) == child then
self.child[child].parent = false
return remove(self.child, child)
else
for k, v in ipairs(self.child) do
if v == child then
return remove(self.child, k)
end
end
return "Element \"" .. tostring(child) .. "\" is not a child of element \"" .. tostring(self) .. "\". Cannot remove it."
end
end,
getChildren = function(self)
return self.child
end,
move = function(self, x, y)
if x then
self.x = self.x + x
end
if y then
self.y = self.y + y
end
for i = 1, #self.child do
if not (self.child[i].excludeMovement) then
self.child[i]:move(x, y)
end
end
return self
end,
setPosition = function(self, x, y)
local oldX = self.x
local oldY = self.y
if x then
local _exp_0 = self.horizontal
if "left" == _exp_0 then
self.x = x
elseif "center" == _exp_0 then
self.x = x - self.w / 2
elseif "right" == _exp_0 then
self.x = x - self.w
end
else
x = oldX
end
if y then
local _exp_0 = self.vertical
if "top" == _exp_0 then
self.y = y
elseif "center" == _exp_0 then
self.y = y - self.h / 2
elseif "bottom" == _exp_0 then
self.y = y - self.h
end
else
y = oldY
end
for i = 1, #self.child do
self.child[i]:move(x - oldX, y - oldY)
end
return self
end,
getPosition = function(self)
local resultX = self.x
local resultY = self.y
local _exp_0 = self.horizontal
if "center" == _exp_0 then
resultX = resultX + (self.w / 2)
elseif "right" == _exp_0 then
resultY = resultY + self.w
end
local _exp_1 = self.vertical
if "center" == _exp_1 then
resultY = resultY + (self.h / 2)
elseif "bottom" == _exp_1 then
resultY = resultY + self.h
end
return resultX, resultY
end,
setSize = function(self, w, h)
if w then
local _exp_0 = self.horizontal
if "center" == _exp_0 then
self.x = self.x - ((w - self.w) / 2)
elseif "right" == _exp_0 then
self.x = self.x - (w - self.w)
end
self.w = w
end
if h then
local _exp_0 = self.vertical
if "center" == _exp_0 then
self.y = self.y - ((h - self.h) / 2)
elseif "bottom" == _exp_0 then
self.y = self.y - (h - self.h)
end
self.h = h
end
return self
end,
getSize = function(self)
return self.w, self.h
end,
setWidth = function(self, w)
local _exp_0 = self.horizontal
if "center" == _exp_0 then
self.x = self.x - ((w - self.w) / 2)
elseif "right" == _exp_0 then
self.x = self.x - (w - self.w)
end
self.w = w
return self
end,
getWidth = function(self)
return self.w
end,
setHeight = function(self, h)
local _exp_0 = self.vertical
if "center" == _exp_0 then
self.y = self.y - ((h - self.h) / 2)
elseif "bottom" == _exp_0 then
self.y = self.y - (h - self.h)
end
self.h = h
return self
end,
getHeight = function(self)
return self.h
end,
adjustSize = function(self, w, h)
local W, H = self:getSize()
if w then
W = W + w
end
if h then
H = H + h
end
self:setSize(W, H)
return self
end,
align = function(self, horizontal, vertical, toPixel)
if toPixel == nil then
toPixel = true
end
self:setAlignment(horizontal, vertical)
self.x = self.parent.x
self.y = self.parent.y
local _exp_0 = self.horizontal
if "left" == _exp_0 then
self.x = self.x + self.spacing
elseif "center" == _exp_0 then
self.x = self.x + ((self.parent.w - self.w) / 2)
elseif "right" == _exp_0 then
self.x = self.x + (self.parent.w - self.w - self.spacing)
end
local _exp_1 = self.vertical
if "top" == _exp_1 then
self.y = self.y + self.spacing
elseif "center" == _exp_1 then
self.y = self.y + ((self.parent.h - self.h) / 2)
elseif "bottom" == _exp_1 then
self.y = self.y + (self.parent.h - self.h - self.spacing)
end
if toPixel then
self.x = floor(self.x)
self.y = floor(self.y)
end
return self
end,
alignTo = function(self, element, horizontal, vertical, toPixel)
if toPixel == nil then
toPixel = true
end
local parent = self.parent
self.parent = element
self:align(horizontal, vertical, toPixel)
self.parent = parent
return self
end,
setAlignment = function(self, horizontal, vertical)
if horizontal then
self.horizontal = horizontal
end
if vertical then
self.vertical = vertical
end
return self
end,
getAlignment = function(self)
return self.horizontal, self.vertical
end,
setMargin = function(self, spacing)
self.spacing = spacing
self:align()
return self
end,
getMargin = function(self)
return self.spacing
end,
fill = function(self)
self.x = self.parent.x + self.spacing
self.y = self.parent.y + self.spacing
self.w = self.parent.w - self.spacing * 2
self.h = self.parent.h - self.spacing * 2
end,
delete = function(self)
for k, v in ipairs(self.child) do
v:delete()
end
self.parent:removeChild(self)
self = nil
return nil
end
}
_base_0.__index = _base_0
_class_0 = setmetatable({
__init = function(self, parent)
self.parent = parent
self.child = { }
self.w = 0
self.h = 0
self.spacing = 0
if parent then
self.x = parent.x
self.y = parent.y
else
self.x = 0
self.y = 0
end
self.horizontal = "left"
self.vertical = "top"
end,
__base = _base_0,
__name = "element"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
element = _class_0
return _class_0
end