From 21dcf8d1510ea806fc35ea0b09b73ff4f4d68cd3 Mon Sep 17 00:00:00 2001 From: Fox Date: Thu, 21 Jan 2016 16:04:11 -0800 Subject: [PATCH] added text element! --- docs/Text.md | 3 ++ main.lua | 1 + pop/elements/text.lua | 70 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 docs/Text.md create mode 100644 pop/elements/text.lua diff --git a/docs/Text.md b/docs/Text.md new file mode 100644 index 0000000..29ee295 --- /dev/null +++ b/docs/Text.md @@ -0,0 +1,3 @@ +- set text will modify size +- set font will modify size +- set size just fixes the size, you cannot manually set text size diff --git a/main.lua b/main.lua index 5eb947d..1a32e4d 100644 --- a/main.lua +++ b/main.lua @@ -12,6 +12,7 @@ function love.load() pop.box(align):align("center", "bottom"):setColor(255, 255, 0) pop.box(align):align("right", "bottom"):setColor(0, 255, 255) pop.box(nil, {255, 0, 0, 255}):align("left", "top"):setSize(50, 50) + pop.text(nil, "Hello World!"):align("center"):setText("Hey, I've been modified!") end function love.update(dt) diff --git a/pop/elements/text.lua b/pop/elements/text.lua new file mode 100644 index 0000000..beee56e --- /dev/null +++ b/pop/elements/text.lua @@ -0,0 +1,70 @@ +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() + 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 + +function text:setSize() + local w = self.font:getWidth(self.text) + local h = self.font:getHeight() + + 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 + +return text