2
-- Shows text onscreen using an outline (e.g. TrueType) or bitmap font.
3
-- You can control the width of the text but the height is ignored for
4
-- display purposes; it will always draw the entirety of its text property.
5
-- If you do not specify a height and width, the sprite will size itself
6
-- so that it fits its entire text as a single line.
8
-- By default, an outline font will display as white. To change its color,
9
-- change its <Sprite.tint> property.
16
-- Text string to draw.
20
-- Font to use to draw. See <Cached.font> for possible values here; if
21
-- you need more than one value, use table notation. Some example values:
22
-- * 12 (default font, size 12)
23
-- * 'fonts/bitmapfont.png' (bitmap font, default character order)
24
-- * { 'fonts/outlinefont.ttf', 12 } (outline font, font size)
25
-- * { 'fonts/bitmapfont.ttf', 'ABCDEF' } (bitmap font, custom character order)
29
-- Horizontal alignment, see http://love2d.org/wiki/AlignMode.
30
-- This affects how lines wrap relative to each other, not how
31
-- a single line will wrap relative to the sprite's width and height.
32
-- If <wordWrap> is set to false, then this has no effect.
36
-- Wrap lines to the width of the sprite?
39
-- private property: used to check whether our font has changed
42
new = function (self, obj)
46
if obj.onNew then obj:onNew() end
51
-- Returns the width and height of the text onscreen as line-wrapped
52
-- to the sprite's boundaries. This disregards the sprite's height property.
58
-- width, height in pixels
60
getSize = function (self)
61
if self.text == '' then return 0, 0 end
63
-- did our font change on us?
65
if type(self.font) == 'table' then
66
for key, value in pairs(self.font) do
67
if self._set.font[key] ~= self.font[key] then
73
if self.font ~= self._set.font then
78
local _, lines = self._fontObj:getWrap(self.text, self.width)
79
local lineHeight = self._fontObj:getHeight()
81
return self.width, lines * lineHeight
84
-- Method: centerAround
85
-- Centers the text around a position onscreen.
88
-- x - center's x coordinate
89
-- y - center's y coordinate
90
-- centering - can be either 'horizontal', 'vertical', or 'both';
93
centerAround = function (self, x, y, centering)
94
centering = centering or 'both'
95
local width, height = self:getSize()
97
if width == 0 then return end
99
if centering == 'both' or centering == 'horizontal' then
100
self.x = x - width / 2
103
if centering == 'both' or centering == 'vertical' then
104
self.y = y - height / 2
108
-- private method: updateFont
109
-- Updates the _fontObj property based on self.font.
117
updateFont = function (self)
119
if type(self.font) == 'table' then
120
self._fontObj = Cached:font(unpack(self.font))
122
self._fontObj = Cached:font(self.font)
125
if not self.height then self.height = self._fontObj:getHeight() end
126
if not self.width and self.text then self.width = self._fontObj:getWidth(self.text) end
130
draw = function (self, x, y)
131
if not self.visible or self.alpha <= 0 or not self.text or not self.font then return end
133
x = math.floor(x or self.x)
134
y = math.floor(y or self.y)
137
assert(type(x) == 'number', 'visible text sprite does not have a numeric x property')
138
assert(type(y) == 'number', 'visible text sprite does not have a numeric y property')
139
assert(type(self.width) == 'number', 'visible text sprite does not have a numeric width property')
140
assert(type(self.height) == 'number', 'visible text sprite does not have a numeric height property')
141
if not self.text then error('visible text sprite has no text property') end
142
if not self.font then error('visible text sprite has no font property') end
145
-- did our font change on us?
147
if type(self.font) == 'table' then
148
for key, value in pairs(self.font) do
149
if self._set.font[key] ~= self.font[key] then
155
if self.font ~= self._set.font then
162
local scaleX = self.scale * self.distort.x
163
local scaleY = self.scale * self.distort.y
165
if self.flipX then scaleX = scaleX * -1 end
166
if self.flipY then scaleY = scaleY * -1 end
168
if scaleX ~= 1 or scaleY ~= 1 or self.rotation ~= 0 then
170
love.graphics.translate(x + self.width / 2, y + self.height / 2)
171
love.graphics.scale(scaleX, scaleY)
172
love.graphics.rotate(self.rotation)
173
love.graphics.translate(- (x + self.width / 2), - (y + self.height / 2))
176
-- set color if needed
178
local colored = self.alpha ~= 1 or self.tint[1] ~= 1 or self.tint[2] ~= 1 or self.tint[3] ~= 1
181
love.graphics.setColor(self.tint[1] * 255, self.tint[2] * 255, self.tint[3] * 255, self.alpha * 255)
184
love.graphics.setFont(self._fontObj)
186
if self.wordWrap then
187
love.graphics.printf(self.text, x, y, self.width, self.align)
189
love.graphics.print(self.text, x, y)
192
-- reset color and rotation
194
if colored then love.graphics.setColor(255, 255, 255, 255) end
196
if scaleX ~= 1 or scaleY ~= 1 or self.rotation ~= 0 then
201
__tostring = function (self)
202
local result = 'Text (x: ' .. self.x .. ', y: ' .. self.y ..
203
', w: ' .. self.width .. ', h: ' .. self.height .. ', '
205
result = result .. 'font ' .. dump(self.font) .. ', ' .. string.len(self.text) .. ' chars, '
208
result = result .. 'active, '
210
result = result .. 'inactive, '
214
result = result .. 'visible, '
216
result = result .. 'invisible, '
220
result = result .. 'solid'
222
result = result .. 'not solid'