/ld27

To get this branch, use:
bzr branch http://9ix.org/bzr/ld27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
-- Class: Text
-- Shows text onscreen using an outline (e.g. TrueType) or bitmap font. 
-- You can control the width of the text but the height is ignored for
-- display purposes; it will always draw the entirety of its text property.
-- If you do not specify a height and width, the sprite will size itself
-- so that it fits its entire text as a single line.
--
-- By default, an outline font will display as white. To change its color,
-- change its <Sprite.tint> property.
--
-- Extends:
--		<Sprite>

Text = Sprite:extend{
	-- Property: text
	-- Text string to draw.
	text = '',

	-- Property: font
	-- Font to use to draw. See <Cached.font> for possible values here; if
	-- you need more than one value, use table notation. Some example values:
	-- 		* 12 (default font, size 12)
	--		* 'fonts/bitmapfont.png' (bitmap font, default character order)
	--		* { 'fonts/outlinefont.ttf', 12 } (outline font, font size)
	--		* { 'fonts/bitmapfont.ttf', 'ABCDEF' } (bitmap font, custom character order)
	font = 12,

	-- Property: align
	-- Horizontal alignment, see http://love2d.org/wiki/AlignMode.
	-- This affects how lines wrap relative to each other, not how
	-- a single line will wrap relative to the sprite's width and height.
	-- If <wordWrap> is set to false, then this has no effect. 
	align = 'left',

	-- Property: wordWrap
	-- Wrap lines to the width of the sprite?
	wordWrap = true,

	-- private property: used to check whether our font has changed
	_set = { font = {} },

	new = function (self, obj)
		obj = obj or {}
		self:extend(obj)
		obj:updateFont()
		if obj.onNew then obj:onNew() end
		return obj
	end,

	-- Method: getSize
	-- Returns the width and height of the text onscreen as line-wrapped
	-- to the sprite's boundaries. This disregards the sprite's height property.
	--
	-- Arguments:
	--		none
	-- 
	-- Returns:
	--		width, height in pixels
	
	getSize = function (self)
		if self.text == '' then return 0, 0 end

		-- did our font change on us?

		if type(self.font) == 'table' then
			for key, value in pairs(self.font) do
				if self._set.font[key] ~= self.font[key] then
					self:updateFont()
					break
				end
			end
		else
			if self.font ~= self._set.font then
				self:updateFont()
			end
		end

		local _, lines = self._fontObj:getWrap(self.text, self.width)
		local lineHeight = self._fontObj:getHeight()

		return self.width, lines * lineHeight
	end,

	-- Method: centerAround
	-- Centers the text around a position onscreen.
	--
	-- Arguments:
	--		x - center's x coordinate
	--		y - center's y coordinate
	--		centering - can be either 'horizontal', 'vertical', or 'both';
	--					default 'both'
	
	centerAround = function (self, x, y, centering)
		centering = centering or 'both'
		local width, height = self:getSize()

		if width == 0 then return end

		if centering == 'both' or centering == 'horizontal' then
			self.x = x - width / 2
		end

		if centering == 'both' or centering == 'vertical' then
			self.y = y - height / 2
		end
	end,

	-- private method: updateFont
	-- Updates the _fontObj property based on self.font.
	--
	-- Arguments:
	--		none
	--
	-- Returns:
	--		nothing

	updateFont = function (self)
		if self.font then
			if type(self.font) == 'table' then
				self._fontObj = Cached:font(unpack(self.font))
			else
				self._fontObj = Cached:font(self.font)
			end

			if not self.height then self.height = self._fontObj:getHeight() end
			if not self.width and self.text then self.width = self._fontObj:getWidth(self.text) end
		end
	end,

	draw = function (self, x, y)
		if not self.visible or self.alpha <= 0 or not self.text or not self.font then return end

		x = math.floor(x or self.x)
		y = math.floor(y or self.y)

		if STRICT then
			assert(type(x) == 'number', 'visible text sprite does not have a numeric x property')
			assert(type(y) == 'number', 'visible text sprite does not have a numeric y property')
			assert(type(self.width) == 'number', 'visible text sprite does not have a numeric width property')
			assert(type(self.height) == 'number', 'visible text sprite does not have a numeric height property')
			if not self.text then error('visible text sprite has no text property') end
			if not self.font then error('visible text sprite has no font property') end
		end

		-- did our font change on us?

		if type(self.font) == 'table' then
			for key, value in pairs(self.font) do
				if self._set.font[key] ~= self.font[key] then
					self:updateFont()
					break
				end
			end
		else
			if self.font ~= self._set.font then
				self:updateFont()
			end
		end

		-- rotate and scale

		local scaleX = self.scale * self.distort.x
		local scaleY = self.scale * self.distort.y

		if self.flipX then scaleX = scaleX * -1 end
		if self.flipY then scaleY = scaleY * -1 end

		if scaleX ~= 1 or scaleY ~= 1 or self.rotation ~= 0 then
			love.graphics.push()
			love.graphics.translate(x + self.width / 2, y + self.height / 2)
			love.graphics.scale(scaleX, scaleY)
			love.graphics.rotate(self.rotation)
			love.graphics.translate(- (x + self.width / 2), - (y + self.height / 2))
		end

		-- set color if needed

		local colored = self.alpha ~= 1 or self.tint[1] ~= 1 or self.tint[2] ~= 1 or self.tint[3] ~= 1

		if colored then
			love.graphics.setColor(self.tint[1] * 255, self.tint[2] * 255, self.tint[3] * 255, self.alpha * 255)
		end
		
		love.graphics.setFont(self._fontObj)

		if self.wordWrap then
			love.graphics.printf(self.text, x, y, self.width, self.align)
		else
			love.graphics.print(self.text, x, y)
		end

		-- reset color and rotation
	
		if colored then love.graphics.setColor(255, 255, 255, 255) end

		if scaleX ~= 1 or scaleY ~= 1 or self.rotation ~= 0 then
			love.graphics.pop()
		end
	end,

	__tostring = function (self)
		local result = 'Text (x: ' .. self.x .. ', y: ' .. self.y ..
					   ', w: ' .. self.width .. ', h: ' .. self.height .. ', '

		result = result .. 'font ' .. dump(self.font) .. ', ' .. string.len(self.text) .. ' chars, '

		if self.active then
			result = result .. 'active, '
		else
			result = result .. 'inactive, '
		end

		if self.visible then
			result = result .. 'visible, '
		else
			result = result .. 'invisible, '
		end

		if self.solid then
			result = result .. 'solid'
		else
			result = result .. 'not solid'
		end

		return result .. ')'
	end
}