Pop.Box/pop/elements/text.lua

95 lines
1.9 KiB
Lua
Raw Normal View History

2016-01-22 00:04:11 +00:00
local lg = love.graphics
local path = string.sub(..., 1, string.len(...) - string.len("/elements/text"))
local class = require(path .. "/lib/middleclass")
local element = require(path .. "/elements/element")
local text = class("pop.text", element)
function text:initialize(pop, parent, text, color)
element.initialize(self, pop, parent)
self.font = lg.newFont(14)
2016-01-22 00:04:11 +00:00
self:setText(text or "")
self.color = color or {255, 255, 255, 255}
end
function text:draw()
lg.setColor(self.color)
lg.setFont(self.font)
lg.print(self.text, self.x, self.y)
return self
end
2016-01-25 21:46:21 +00:00
function text: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(200, 0, 0, 200)
lg.rectangle("line", self.x, self.y, self.w, self.h)
lg.setColor(255, 200, 200, 255)
lg.print("t", self.x, self.y)
end
2016-01-22 00:04:11 +00:00
function text:setSize()
local w = self.font:getWidth(self.text)
2016-01-26 03:02:17 +00:00
local h = self.font:getHeight() * (select(2, self.text:gsub("\n", "\n")) + 1)
2016-01-22 00:04:11 +00:00
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
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.w = w
self.h = h
return self
end
function text:setText(text)
self.text = text
self:setSize()
return self
end
function text:getText()
return self.text
end
function text:setFont(font)
self.font = font
self:setSize()
return self
end
function text:getFont()
return self.font
end
function text:setColor(r, g, b, a)
self.color = {r, g, b, a}
if not a then
self.color[4] = 255
end
return self
end
function text:getColor()
return self.color[1], self.color[1], self.color[3], self.color[4]
end
2016-01-22 00:04:11 +00:00
return text