Pop.Box/lib/pop/elements/element.lua

308 lines
8.6 KiB
Lua
Raw Permalink Normal View History

local graphics
graphics = love.graphics
2016-03-29 01:00:17 +00:00
local floor
floor = math.floor
2016-04-17 06:58:17 +00:00
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)
2016-05-01 08:55:50 +00:00
graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(0, 200, 0, 200)
2016-05-01 08:55:50 +00:00
graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(200, 255, 200, 255)
2016-05-01 08:55:50 +00:00
graphics.print("e", self.data.x, self.data.y)
return self
end,
2016-04-01 01:59:16 +00:00
addChild = function(self, child)
2016-05-01 08:55:50 +00:00
if child.data.parent then
child.data.parent:removeChild(child)
2016-04-17 06:58:17 +00:00
end
2016-05-01 08:55:50 +00:00
insert(self.data.child, child)
child.data.parent = self
child:align()
2016-04-01 01:59:16 +00:00
return self
end,
2016-04-17 06:58:17 +00:00
removeChild = function(self, child)
if tonumber(child) == child then
2016-05-01 08:55:50 +00:00
self.data.child[child].data.parent = false
return remove(self.data.child, child)
2016-04-17 06:58:17 +00:00
else
2016-05-01 08:55:50 +00:00
for k, v in ipairs(self.data.child) do
2016-04-17 06:58:17 +00:00
if v == child then
2016-05-01 08:55:50 +00:00
return remove(self.data.child, k)
2016-04-17 06:58:17 +00:00
end
end
return "Element \"" .. tostring(child) .. "\" is not a child of element \"" .. tostring(self) .. "\". Cannot remove it."
2016-04-17 06:58:17 +00:00
end
end,
getChildren = function(self)
2016-05-01 08:55:50 +00:00
return self.data.child
end,
move = function(self, x, y)
2016-03-29 01:00:17 +00:00
if x then
2016-05-01 08:55:50 +00:00
self.data.x = self.data.x + x
2016-03-29 01:00:17 +00:00
end
if y then
2016-05-01 08:55:50 +00:00
self.data.y = self.data.y + y
2016-03-29 01:00:17 +00:00
end
2016-05-01 08:55:50 +00:00
for i = 1, #self.data.child do
if self.data.child[i].data.move then
self.data.child[i]:move(x, y)
end
end
return self
end,
setPosition = function(self, x, y)
2016-05-01 08:55:50 +00:00
local oldX = self.data.x
local oldY = self.data.y
2016-03-29 01:00:17 +00:00
if x then
2016-05-01 08:55:50 +00:00
local _exp_0 = self.data.horizontal
2016-03-29 01:00:17 +00:00
if "left" == _exp_0 then
2016-05-01 08:55:50 +00:00
self.data.x = x
2016-03-29 01:00:17 +00:00
elseif "center" == _exp_0 then
2016-05-01 08:55:50 +00:00
self.data.x = x - self.data.w / 2
2016-03-29 01:00:17 +00:00
elseif "right" == _exp_0 then
2016-05-01 08:55:50 +00:00
self.data.x = x - self.data.w
2016-03-29 01:00:17 +00:00
end
else
x = oldX
end
2016-03-29 01:00:17 +00:00
if y then
2016-05-01 08:55:50 +00:00
local _exp_0 = self.data.vertical
2016-03-29 01:00:17 +00:00
if "top" == _exp_0 then
2016-05-01 08:55:50 +00:00
self.data.y = y
2016-03-29 01:00:17 +00:00
elseif "center" == _exp_0 then
2016-05-01 08:55:50 +00:00
self.data.y = y - self.data.h / 2
2016-03-29 01:00:17 +00:00
elseif "bottom" == _exp_0 then
2016-05-01 08:55:50 +00:00
self.data.y = y - self.data.h
2016-03-29 01:00:17 +00:00
end
else
y = oldY
end
2016-05-01 08:55:50 +00:00
for i = 1, #self.data.child do
self.data.child[i]:move(x - oldX, y - oldY)
end
return self
end,
getPosition = function(self)
2016-05-01 08:55:50 +00:00
local resultX = self.data.x
local resultY = self.data.y
local _exp_0 = self.data.horizontal
if "center" == _exp_0 then
2016-05-01 08:55:50 +00:00
resultX = resultX + (self.data.w / 2)
elseif "right" == _exp_0 then
2016-05-01 08:55:50 +00:00
resultY = resultY + self.data.w
end
2016-05-01 08:55:50 +00:00
local _exp_1 = self.data.vertical
if "center" == _exp_1 then
2016-05-01 08:55:50 +00:00
resultY = resultY + (self.data.h / 2)
elseif "bottom" == _exp_1 then
2016-05-01 08:55:50 +00:00
resultY = resultY + self.data.h
end
return resultX, resultY
end,
setSize = function(self, w, h)
if w then
2016-05-01 08:55:50 +00:00
local _exp_0 = self.data.horizontal
if "center" == _exp_0 then
2016-05-01 08:55:50 +00:00
self.data.x = self.data.x - ((w - self.data.w) / 2)
elseif "right" == _exp_0 then
2016-05-01 08:55:50 +00:00
self.data.x = self.data.x - (w - self.data.w)
end
2016-05-01 08:55:50 +00:00
self.data.w = w
end
if h then
2016-05-01 08:55:50 +00:00
local _exp_0 = self.data.vertical
if "center" == _exp_0 then
2016-05-01 08:55:50 +00:00
self.data.y = self.data.y - ((h - self.data.h) / 2)
elseif "bottom" == _exp_0 then
2016-05-01 08:55:50 +00:00
self.data.y = self.data.y - (h - self.data.h)
end
2016-05-01 08:55:50 +00:00
self.data.h = h
end
return self
end,
getSize = function(self)
2016-05-01 08:55:50 +00:00
return self.data.w, self.data.h
end,
setWidth = function(self, w)
2016-05-01 08:55:50 +00:00
local _exp_0 = self.data.horizontal
if "center" == _exp_0 then
2016-05-01 08:55:50 +00:00
self.data.x = self.data.x - ((w - self.data.w) / 2)
elseif "right" == _exp_0 then
2016-05-01 08:55:50 +00:00
self.data.x = self.data.x - (w - self.data.w)
end
2016-05-01 08:55:50 +00:00
self.data.w = w
return self
end,
getWidth = function(self)
2016-05-01 08:55:50 +00:00
return self.data.w
end,
setHeight = function(self, h)
2016-05-01 08:55:50 +00:00
local _exp_0 = self.data.vertical
if "center" == _exp_0 then
2016-05-01 08:55:50 +00:00
self.data.y = self.data.y - ((h - self.data.h) / 2)
elseif "bottom" == _exp_0 then
2016-05-01 08:55:50 +00:00
self.data.y = self.data.y - (h - self.data.h)
end
2016-05-01 08:55:50 +00:00
self.data.h = h
return self
end,
getHeight = function(self)
2016-05-01 08:55:50 +00:00
return self.data.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,
2016-03-29 01:00:17 +00:00
align = function(self, horizontal, vertical, toPixel)
2016-04-17 06:58:17 +00:00
if toPixel == nil then
toPixel = true
end
self:setAlignment(horizontal, vertical)
2016-05-01 08:55:50 +00:00
self.data.x = self.data.parent.data.x
self.data.y = self.data.parent.data.y
local _exp_0 = self.data.horizontal
if "left" == _exp_0 then
2016-05-01 08:55:50 +00:00
self.data.x = self.data.x + self.data.margin
elseif "center" == _exp_0 then
2016-05-01 08:55:50 +00:00
self.data.x = self.data.x + ((self.data.parent.data.w - self.data.w) / 2)
elseif "right" == _exp_0 then
2016-05-01 08:55:50 +00:00
self.data.x = self.data.x + (self.data.parent.data.w - self.data.w - self.data.margin)
end
2016-05-01 08:55:50 +00:00
local _exp_1 = self.data.vertical
if "top" == _exp_1 then
2016-05-01 08:55:50 +00:00
self.data.y = self.data.y + self.data.margin
elseif "center" == _exp_1 then
2016-05-01 08:55:50 +00:00
self.data.y = self.data.y + ((self.data.parent.data.h - self.data.h) / 2)
elseif "bottom" == _exp_1 then
2016-05-01 08:55:50 +00:00
self.data.y = self.data.y + (self.data.parent.data.h - self.data.h - self.data.margin)
end
2016-04-17 06:58:17 +00:00
if toPixel then
2016-05-01 08:55:50 +00:00
self.data.x = floor(self.data.x)
self.data.y = floor(self.data.y)
2016-03-29 01:00:17 +00:00
end
return self
end,
2016-04-17 06:58:17 +00:00
alignTo = function(self, element, horizontal, vertical, toPixel)
if toPixel == nil then
toPixel = true
end
2016-05-01 08:55:50 +00:00
local parent = self.data.parent
self.data.parent = element
2016-04-17 06:58:17 +00:00
self:align(horizontal, vertical, toPixel)
2016-05-01 08:55:50 +00:00
self.data.parent = parent
return self
end,
setAlignment = function(self, horizontal, vertical)
if horizontal then
2016-05-01 08:55:50 +00:00
self.data.horizontal = horizontal
end
if vertical then
2016-05-01 08:55:50 +00:00
self.data.vertical = vertical
end
return self
2016-03-29 01:00:17 +00:00
end,
2016-04-03 07:47:07 +00:00
getAlignment = function(self)
2016-05-01 08:55:50 +00:00
return self.data.horizontal, self.data.vertical
2016-04-03 07:47:07 +00:00
end,
2016-05-01 08:55:50 +00:00
setMargin = function(self, margin)
self.data.margin = margin
2016-03-29 01:00:17 +00:00
self:align()
return self
end,
getMargin = function(self)
2016-05-01 08:55:50 +00:00
return self.data.margin
end,
fill = function(self)
2016-05-01 08:55:50 +00:00
self.data.x = self.data.parent.data.x + self.data.margin
self.data.y = self.data.parent.data.y + self.data.margin
self.data.w = self.data.parent.data.w - self.data.margin * 2
self.data.h = self.data.parent.data.h - self.data.margin * 2
2016-04-17 07:40:48 +00:00
end,
delete = function(self)
2016-05-01 08:55:50 +00:00
for k, v in ipairs(self.data.child) do
2016-04-17 07:40:48 +00:00
v:delete()
end
2016-05-01 08:55:50 +00:00
self.data.parent:removeChild(self)
self = nil
2016-04-20 23:12:38 +00:00
return nil
end,
getVisibility = function(self)
2016-05-01 08:55:50 +00:00
return self.data.draw
end,
setVisibility = function(self, isVisible)
2016-05-01 08:55:50 +00:00
self.data.draw = isVisible
return self
end,
getStatic = function(self)
2016-05-01 08:55:50 +00:00
return (not self.data.move)
end,
setStatic = function(self, isStatic)
2016-05-01 08:55:50 +00:00
self.data.move = (not isStatic)
return self
end
}
_base_0.__index = _base_0
_class_0 = setmetatable({
2016-06-09 04:44:27 +00:00
__init = function(self, parent, data)
local default = {
2016-05-01 08:55:50 +00:00
parent = parent,
child = { },
w = 0,
h = 0,
x = 0,
y = 0,
horizontal = "left",
vertical = "top",
margin = 0,
draw = true,
update = true,
move = true
}
2016-06-09 04:44:27 +00:00
if type(data) == "table" then
self.data = data
for k, v in pairs(default) do
if type(self.data[k]) == "nil" then
self.data[k] = v
end
end
else
self.data = default
end
if self.data.parent then
self.data.x = self.data.parent.data.x
self.data.y = self.data.parent.data.y
2016-03-29 01:00:17 +00:00
end
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