Pop.Box/pop/elements/element.lua

177 lines
3.7 KiB
Lua
Raw Normal View History

2016-01-25 21:46:21 +00:00
local lg = love.graphics
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-21 23:37:33 +00:00
function element:initialize(pop, parent)
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
2016-01-21 22:18:28 +00:00
self.horizontal = "left"
self.vertical = "top"
2016-01-20 22:34:25 +00:00
end
2016-01-25 21:46:21 +00:00
function element:debugDraw()
lg.setLineWidth(1)
lg.setColor(0, 0, 0, 100)
lg.rectangle("fill", self.x, self.y, self.w, self.h)
lg.setColor(0, 200, 0, 200)
lg.rectangle("line", self.x, self.y, self.w, self.h)
lg.setColor(200, 255, 200, 255)
lg.print("e", self.x, self.y)
end
2016-01-20 22:34:25 +00:00
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,#self.child do
if not self.child[i].excludeMovement then
self.child[i]:move(x - oldX, y - oldY)
2016-01-21 22:18:28 +00:00
end
end
2016-01-21 23:01:15 +00:00
return self
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,#self.child do
if not self.child[i].excludeMovement then
self.child[i]:move(x - oldX, y - oldY)
2016-01-21 22:18:28 +00:00
end
end
2016-01-21 23:01:15 +00:00
return self
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)
if w then
if self.horizontal == "center" then
self.x = self.x - (w - self.w)/2
elseif self.horizontal == "right" then
self.x = self.x - (w - self.w)
end
self.w = w
2016-01-21 22:18:28 +00:00
end
if h then
if self.vertical == "center" then
self.y = self.y - (h - self.h)/2
elseif self.vertical == "bottom" then
self.y = self.y - (h - self.h)
end
self.h = h
2016-01-20 22:34:25 +00:00
end
2016-01-21 22:18:28 +00:00
2016-01-21 23:01:15 +00:00
return self
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
function element:adjustSize(x, y)
local X, Y = self:getSize()
if x then
X = X + x
end
if y then
Y = Y + y
end
self:setSize(X, Y)
return self
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
2016-01-21 23:01:15 +00:00
return self
2016-01-20 22:34:25 +00:00
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
2016-01-21 23:01:15 +00:00
return self
2016-01-20 22:34:25 +00:00
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-21 23:01:15 +00:00
return self
2016-01-20 22:34:25 +00:00
end
2015-11-18 03:27:06 +00:00
return element