Pop.Box/pop/elements/box.lua

72 lines
1.7 KiB
Lua
Raw Permalink Normal View History

2016-01-21 22:18:28 +00:00
local lg = love.graphics
2016-01-20 22:34:25 +00:00
local path = string.sub(..., 1, string.len(...) - string.len("/elements/box"))
local class = require(path .. "/lib/middleclass")
local element = require(path .. "/elements/element")
2015-11-18 03:27:06 +00:00
2016-01-21 22:18:28 +00:00
local box = class("pop.box", element)
2015-11-18 03:27:06 +00:00
2016-01-21 23:37:33 +00:00
function box:initialize(pop, parent, background)
element.initialize(self, pop, parent)
2015-11-18 03:27:06 +00:00
2016-01-21 23:37:33 +00:00
self.background = background or false
end
2016-01-21 23:01:15 +00:00
2016-01-21 23:37:33 +00:00
function box:draw() --NOTE these ifs are probably wrong
if self.background then
if type(self.background) == "table" then
lg.setColor(self.background)
2016-01-21 23:01:15 +00:00
lg.rectangle("fill", self.x, self.y, self.w, self.h)
else
lg.setColor(255, 255, 255, 255)
2016-01-21 23:37:33 +00:00
local w, h = self.background:getDimensions()
2016-01-21 23:01:15 +00:00
-- scale!
w = self.w/w
h = self.h/h
2016-01-21 23:37:33 +00:00
lg.draw(self.background, self.x, self.y, 0, w, h)
2016-01-21 23:01:15 +00:00
end
2016-01-21 22:18:28 +00:00
end
2016-01-21 23:37:33 +00:00
return self
end
2016-01-25 21:46:21 +00:00
function box:debugDraw()
2016-01-26 03:02:17 +00:00
lg.setLineWidth(0.5)
2016-01-25 21:46:21 +00:00
lg.setColor(0, 0, 0, 100)
lg.rectangle("fill", self.x, self.y, self.w, self.h)
lg.setColor(0, 0, 200, 200)
lg.rectangle("line", self.x, self.y, self.w, self.h)
lg.setColor(200, 200, 255, 255)
lg.print("b", self.x, self.y)
end
2016-01-21 23:37:33 +00:00
function box:setBackground(background)
self.background = background
return self
end
function box:getBackground()
return self.background
end
function box:setColor(r, g, b, a)
self.background = {r, g, b, a}
if not a then
self.background[4] = 255
2016-01-21 22:18:28 +00:00
end
2016-01-21 23:01:15 +00:00
return self
2016-01-21 22:18:28 +00:00
end
2016-01-21 23:37:33 +00:00
function box:getColor()
if type(self.background) == "table" then
return self.background[1], self.background[1], self.background[3], self.background[4]
else
error("This box doesn't have a color.")
end
end
2015-11-18 03:27:06 +00:00
return box