Pop.Box/pop/elements/element.lua

145 lines
3.3 KiB
Lua
Raw Normal View History

2016-01-20 22:34:25 +00:00
local path = string.sub(..., 1, string.len(...) - string.len("/elements/element"))
local class = require(path .. "/lib/middleclass")
2015-11-18 03:27:06 +00:00
2016-01-21 22:18:28 +00:00
local element = class("pop.element")
2015-11-18 03:27:06 +00:00
2016-01-20 22:34:25 +00:00
function element:initialize(pop, parent, skin)
2015-11-18 03:27:06 +00:00
self.parent = parent
self.child = {}
2016-01-20 22:34:25 +00:00
self.x = parent.x or 0
self.y = parent.y or 0
self.w = 10
self.h = 10
self.skin = pop.skins[skin] or pop.skins[pop.currentSkin]
2016-01-21 22:18:28 +00:00
self.horizontal = "left"
self.vertical = "top"
2016-01-20 22:34:25 +00:00
end
function element:move(x, y)
self.x = self.x + x
self.y = self.y + y
2016-01-21 22:18:28 +00:00
for i=1,#element.child do
if not element.child[i].excludeMovement then
element.child[i]:move(x - oldX, y - oldY)
end
end
2016-01-20 22:34:25 +00:00
end
function element:setPosition(x, y)
2016-01-21 22:18:28 +00:00
local oldX = self.x
local oldY = self.y
if self.horizontal == "left" then
2016-01-20 22:34:25 +00:00
self.x = x
2016-01-21 22:18:28 +00:00
elseif self.horizontal == "center" then
2016-01-20 22:34:25 +00:00
self.x = x - self.w/2
2016-01-21 22:18:28 +00:00
elseif self.horizontal == "right" then
2016-01-20 22:34:25 +00:00
self.x = x - self.w
2016-01-21 22:18:28 +00:00
end
if self.vertical == "top" then
2016-01-20 22:34:25 +00:00
self.y = y
2016-01-21 22:18:28 +00:00
elseif self.vertical == "center" then
2016-01-20 22:34:25 +00:00
self.y = y - self.h/2
2016-01-21 22:18:28 +00:00
elseif self.vertical == "bottom" then
2016-01-20 22:34:25 +00:00
self.y = y - self.h
end
2016-01-21 22:18:28 +00:00
for i=1,#element.child do
if not element.child[i].excludeMovement then
element.child[i]:move(x - oldX, y - oldY)
end
end
2016-01-20 22:34:25 +00:00
end
function element:getPosition()
2016-01-21 22:18:28 +00:00
local resultX = self.x
local resultY = self.y
if self.horizontal == "center" then
resultX = resultX + self.w/2
elseif self.horizontal == "right" then
resultX = resultX + self.w
2016-01-20 22:34:25 +00:00
end
2016-01-21 22:18:28 +00:00
if self.vertical == "center" then
resultY = resultY + self.h/2
elseif self.vertical == "bottom" then
resultY = resultY + self.h
end
return resultX, resultY
2016-01-20 22:34:25 +00:00
end
function element:setSize(w, h)
2016-01-21 22:18:28 +00:00
if self.horizontal == "center" then
2016-01-20 22:34:25 +00:00
self.x = self.x - (w - self.w)/2
2016-01-21 22:18:28 +00:00
elseif self.horizontal == "right" then
2016-01-20 22:34:25 +00:00
self.x = self.x - (w - self.w)
2016-01-21 22:18:28 +00:00
end
if self.vertical == "center" then
2016-01-20 22:34:25 +00:00
self.y = self.y - (h - self.h)/2
2016-01-21 22:18:28 +00:00
elseif self.vertical == "bottom" then
2016-01-20 22:34:25 +00:00
self.y = self.y - (h - self.h)
end
2016-01-21 22:18:28 +00:00
self.w = w
self.h = h
2015-11-18 03:27:06 +00:00
end
2015-11-20 11:30:40 +00:00
function element:getSize()
2016-01-20 22:34:25 +00:00
return self.w, self.h
end
2016-01-21 22:18:28 +00:00
function element:align(horizontal, vertical)
self:setAlignment(horizontal, vertical)
self.x = self.parent.x
self.y = self.parent.y
if self.horizontal == "center" then
self.x = self.x + (self.parent.w - self.w)/2
elseif self.horizontal == "right" then
self.x = self.x + (self.parent.w - self.w)
end
if self.vertical == "center" then
self.y = self.y + (self.parent.h - self.h)/2
elseif self.vertical == "bottom" then
self.y = self.y + (self.parent.h - self.h)
2016-01-20 22:34:25 +00:00
end
end
2016-01-21 22:18:28 +00:00
function element:alignTo(element, horizontal, vertical)
2016-01-20 22:34:25 +00:00
local realParent = self.parent
self.parent = element
self:align(alignment)
self.parent = realParent
end
2016-01-21 22:18:28 +00:00
function element:setAlignment(horizontal, vertical)
if horizontal then
self.horizontal = horizontal
end
if vertical then
self.vertical = vertical
end
2016-01-20 22:34:25 +00:00
end
function element:setSkin(skin)
if type(skin) == "string" then
self.skin = pop.skins[skin]
else
self.skin = skin
end
2015-11-18 03:27:06 +00:00
end
return element