Pop.Box/elements/element.lua

366 lines
10 KiB
Lua
Raw Normal View History

2017-08-14 01:50:54 +00:00
local pop
local graphics
graphics = love.graphics
2017-05-01 01:01:19 +00:00
local floor, max
do
local _obj_0 = math
floor, max = _obj_0.floor, _obj_0.max
end
local inheritsFromElement
2017-08-13 10:52:45 +00:00
inheritsFromElement = require(tostring((...):sub(1, -19)) .. "/util").inheritsFromElement
2016-08-21 05:29:16 +00:00
local element
do
local _class_0
local _base_0 = {
2017-08-14 01:50:54 +00:00
load = function(pop_lib)
pop = pop_lib
end,
2016-11-25 03:37:53 +00:00
align = function(self, horizontal, vertical, toPixel)
if toPixel == nil then
toPixel = true
end
if not (self.data.align) then
2017-04-09 00:10:44 +00:00
return self
2016-11-25 03:37:53 +00:00
end
if horizontal then
self.data.horizontalAlign = horizontal
2016-11-25 03:37:53 +00:00
end
if vertical then
self.data.verticalAlign = vertical
2016-11-25 03:37:53 +00:00
end
self.data.x = self.parent.data.x
self.data.y = self.parent.data.y
local _exp_0 = self.data.horizontalAlign
2017-04-30 21:13:56 +00:00
if "left" == _exp_0 then
2017-05-01 01:01:19 +00:00
self.data.x = self.data.x + max(self.parent.data.padding + self.parent.data.horizontalPadding, self.data.margin + self.data.horizontalMargin)
2017-04-30 21:13:56 +00:00
elseif "center" == _exp_0 then
2016-11-25 03:37:53 +00:00
self.data.x = self.data.x + ((self.parent.data.w - self.data.w) / 2)
elseif "right" == _exp_0 then
2017-05-01 01:01:19 +00:00
self.data.x = self.data.x + (self.parent.data.w - self.data.w - max(self.parent.data.padding + self.parent.data.horizontalPadding, self.data.margin + self.data.horizontalMargin))
else
self.data.x = self.data.x + (self.parent.data.w * self.data.horizontalAlign)
if self.data.horizontalAlign < 0 then
self.data.x = self.data.x + self.parent.data.w
end
2016-11-25 03:37:53 +00:00
end
local _exp_1 = self.data.verticalAlign
2017-04-30 21:13:56 +00:00
if "top" == _exp_1 then
2017-05-01 01:01:19 +00:00
self.data.y = self.data.y + (self.parent.data.padding + self.data.margin + self.data.verticalMargin)
2017-04-30 21:13:56 +00:00
elseif "center" == _exp_1 then
2016-11-25 03:37:53 +00:00
self.data.y = self.data.y + ((self.parent.data.h - self.data.h) / 2)
2016-11-25 05:32:04 +00:00
elseif "bottom" == _exp_1 then
2017-05-01 01:01:19 +00:00
self.data.y = self.data.y + (self.parent.data.h - self.data.h - max(self.parent.data.padding + self.parent.data.verticalPadding, self.data.margin + self.data.verticalMargin))
else
self.data.y = self.data.y + (self.parent.data.h * self.data.verticalAlign)
if self.data.verticalAlign < 0 then
self.data.y = self.data.y + self.parent.data.h
end
2016-11-25 03:37:53 +00:00
end
if toPixel then
self.data.x = floor(self.data.x)
self.data.y = floor(self.data.y)
end
return self
end,
2017-04-09 09:02:14 +00:00
setPosition = function(self, x, y, toPixel)
if toPixel == nil then
toPixel = true
end
2017-04-11 03:32:31 +00:00
local dx, dy = self.data.x, self.data.y
2017-04-09 09:02:14 +00:00
if x then
self.data.x = x
local _exp_0 = self.data.horizontalAlign
2017-04-30 21:13:56 +00:00
if "center" == _exp_0 then
self.data.x = self.data.x - (self.data.w / 2)
elseif "right" == _exp_0 then
self.data.x = self.data.x - self.data.w
end
2017-04-09 09:02:14 +00:00
end
if y then
self.data.y = y
local _exp_0 = self.data.verticalAlign
2017-04-30 21:13:56 +00:00
if "center" == _exp_0 then
self.data.y = self.data.y - (self.data.h / 2)
elseif "bottom" == _exp_0 then
self.data.y = self.data.y - self.data.h
end
2017-04-09 09:02:14 +00:00
end
if toPixel then
self.data.x = floor(self.data.x)
self.data.y = floor(self.data.y)
end
2017-04-11 03:32:31 +00:00
dx = self.data.x - dx
dy = self.data.y - dy
local _list_0 = self.child
for _index_0 = 1, #_list_0 do
local child = _list_0[_index_0]
child:move(dx, dy)
end
2017-04-09 09:02:14 +00:00
return self
end,
2017-04-13 21:28:52 +00:00
getPosition = function(self)
2017-04-30 21:13:56 +00:00
local x, y = self.data.x, self.data.y
local _exp_0 = self.data.horizontalAlign
2017-04-30 21:13:56 +00:00
if "center" == _exp_0 then
x = x + (self.data.w / 2)
elseif "right" == _exp_0 then
y = y + self.data.w
end
local _exp_1 = self.data.verticalAlign
2017-04-30 21:13:56 +00:00
if "center" == _exp_1 then
y = y + (self.data.h / 2)
elseif "bottom" == _exp_1 then
y = y + self.data.h
end
return x, y
2017-04-13 21:28:52 +00:00
end,
2016-09-08 04:42:19 +00:00
setSize = function(self, w, h)
if w then
self.data.w = w
end
if h then
self.data.h = h
end
self:align()
2016-09-08 04:42:19 +00:00
return self
end,
getSize = function(self)
return self.data.w, self.data.h
end,
setWidth = function(self, w)
self.data.w = w
2017-04-30 21:13:56 +00:00
self:align()
2016-09-08 04:42:19 +00:00
return self
end,
getWidth = function(self)
return self.data.w
end,
setHeight = function(self, h)
self.data.h = h
2017-04-30 21:13:56 +00:00
self:align()
2016-09-08 04:42:19 +00:00
return self
end,
getHeight = function(self)
return self.data.h
end,
2017-04-30 21:13:56 +00:00
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,
move = function(self, x, y)
if x == nil then
x = 0
end
if y == nil then
y = 0
end
2017-04-30 21:13:56 +00:00
self.data.x = self.data.x + x
self.data.y = self.data.y + y
2017-04-11 03:32:31 +00:00
local _list_0 = self.child
for _index_0 = 1, #_list_0 do
local child = _list_0[_index_0]
child:move(x, y)
end
return self
2017-04-09 09:02:14 +00:00
end,
2017-04-30 21:13:56 +00:00
setPadding = function(self, padding)
self.data.padding = padding
self:align()
return self
end,
getPadding = function(self)
return self.data.padding
end,
2017-08-28 08:17:54 +00:00
setMargin = function(self, margin)
self.data.margin = margin
self:align()
return self
end,
getMargin = function(self)
return self.data.margin
end,
2017-04-30 23:40:29 +00:00
indexOf = function(self, element)
for i = 1, #self.child do
if self.child[i] == element then
return i
end
end
end,
dataIndexOf = function(self, data)
for i = 1, #self.data.child do
if self.data.child[i] == data then
return i
end
end
end,
add = function(self, element)
if not (inheritsFromElement(element)) then
for _index_0 = 1, #element do
local e = element[_index_0]
self:add(e)
return self
end
end
element.parent:remove(element)
table.insert(self.child, element)
table.insert(self.data.child, element.data)
return self
end,
remove = function(self, element)
if not (inheritsFromElement(element)) then
for _index_0 = 1, #element do
local e = element[_index_0]
self:remove(e)
return self
end
end
local index = self:indexOf(element)
local dataIndex = self:dataIndexOf(element.data)
table.remove(self.child, index)
table.remove(self.data.child, dataIndex)
return self
end,
2017-04-09 09:02:14 +00:00
delete = function(self)
2017-08-14 01:50:54 +00:00
if self == pop.hovered then
pop.hovered = false
pop.log(tostring(self) .. " (" .. tostring(self.type) .. ") unfocused (deleted)")
end
2017-04-10 01:25:11 +00:00
for i = #self.child, 1, -1 do
self.child[i]:delete()
end
2017-04-13 15:11:45 +00:00
if self.parent then
for i = 1, #self.parent.child do
if self.parent.child[i] == self then
table.remove(self.parent.child, i)
break
end
2017-04-09 09:02:14 +00:00
end
end
2017-04-13 15:11:45 +00:00
if self.parent then
for i = 1, #self.parent.data.child do
if self.parent.data.child[i] == self.data then
table.remove(self.parent.data.child, i)
break
end
2017-04-09 09:02:14 +00:00
end
end
2017-04-10 01:25:11 +00:00
self.parent = nil
self.data.parent = nil
2017-04-13 15:11:45 +00:00
self = nil
2016-09-08 04:42:19 +00:00
end
2016-08-21 05:29:16 +00:00
}
_base_0.__index = _base_0
_class_0 = setmetatable({
2016-08-23 01:19:58 +00:00
__init = function(self, parent, data)
if data == nil then
data = { }
end
self.parent, self.data = parent, data
2016-11-25 03:37:53 +00:00
if type(self.data) ~= "table" then
2016-09-08 03:53:22 +00:00
self.data = { }
end
2016-09-08 04:42:19 +00:00
if not (self.data.parent) then
self.data.parent = false
end
if not (self.data.child) then
self.data.child = { }
end
if not (self.data.type) then
self.data.type = "element"
end
2017-08-28 08:17:54 +00:00
if self.data.update == nil then
self.data.update = true
end
if self.data.draw == nil then
self.data.draw = true
end
if self.data.hoverable == nil then
self.data.hoverable = true
end
2016-09-08 04:42:19 +00:00
if not (self.data.x) then
2017-04-30 21:13:56 +00:00
if self.parent then
self.data.x = self.parent.data.x
else
self.data.x = 0
end
2016-09-08 04:42:19 +00:00
end
if not (self.data.y) then
2017-04-30 21:13:56 +00:00
if self.parent then
self.data.y = self.parent.data.y
else
self.data.y = 0
end
2016-09-08 04:42:19 +00:00
end
if not (self.data.w) then
self.data.w = 0
end
if not (self.data.h) then
self.data.h = 0
end
2017-08-28 08:17:54 +00:00
if not (self.data.size) then
self.data.size = 0
2016-09-08 04:42:19 +00:00
end
2017-08-28 08:17:54 +00:00
if not (self.data.verticalSize) then
self.data.verticalSize = 0
2016-09-08 04:42:19 +00:00
end
2017-08-28 08:17:54 +00:00
if not (self.data.horizontalSize) then
self.data.horizontalSize = 0
2017-04-12 10:37:29 +00:00
end
2016-11-25 03:37:53 +00:00
if (self.data.align == nil) and self.parent then
self.data.align = true
end
if not (self.data.verticalAlign) then
self.data.verticalAlign = "top"
2016-11-25 03:37:53 +00:00
end
if not (self.data.horizontalAlign) then
self.data.horizontalAlign = "left"
2016-11-25 03:37:53 +00:00
end
2017-05-01 01:01:19 +00:00
if not (self.data.margin) then
self.data.margin = 0
end
if not (self.data.horizontalMargin) then
self.data.horizontalMargin = 0
end
if not (self.data.verticalMargin) then
self.data.verticalMargin = 0
end
2017-04-30 21:13:56 +00:00
if not (self.data.padding) then
self.data.padding = 0
end
2017-05-01 01:01:19 +00:00
if not (self.data.horizontalPadding) then
self.data.horizontalPadding = 0
end
if not (self.data.verticalPadding) then
self.data.verticalPadding = 0
end
self.child = { }
2017-08-28 08:17:54 +00:00
if not (self.__class.align) then
if self.data.size ~= 0 or self.data.verticalSize ~= 0 or self.data.horizontalSize ~= 0 then
return self:setSize(self.parent.data.w * (self.data.size + self.data.verticalSize), self.parent.data.h * (self.data.size + self.data.horizontalSize))
elseif self.data.align then
return self:align()
end
end
2016-08-23 01:19:58 +00:00
end,
2016-08-21 05:29:16 +00:00
__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