/ld27

To get this branch, use:
bzr branch /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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
-- Class: Keys
-- This tracks the state of the keyboard, i.e. if a key
-- was just pressed or released this frame. You can look
-- up a key either by its name or its Unicode number.
-- Not all keys sensed have Unicode equivalents (e.g. modifiers
-- like Control or Alt).
--
-- Only one Keys object can be active at one time. The one currently
-- listening to the keyboard can be accessed globally via <the>.keys.
--
-- See http://love2d.org/wiki/KeyConstant for a list of key names.
-- This class aliases modifiers for you, so that if you want to check
-- whether either the left or right Control key is pressed, you can check
-- on 'ctrl' instead of both 'lctrl' and 'rctrl'.
--
-- Extends:
--		<Sprite>

Keys = Sprite:extend
{
	visible = false,

	-- Property: typed
	-- This is literally what is being typed during the current frame.
	-- e.g. if the user holds the shift key and presses the 'a' key,
	-- this will be set to 'A'. Consult <allPressed()> if you
	-- want to know what specific keys are being pressed.

	typed = '',

	-- private property: _thisFrame
	-- what keys are pressed this frame
	-- if you are interested in this, use allPressed() instead

	_thisFrame = {},

	-- private property: _alreadyHandled
	-- was the key already processed by the user code since it was pressed?
	-- Useful for handling repeated keys, check <pressed> and then you can see
	-- if the key was repeated.

	_alreadyHandled = {},

	-- private property: _lastFrame
	-- what keys were pressed last frame
	
	_lastFrame = {},
	
	new = function (self, obj)
		obj = self:extend(obj)
		the.keys = obj
		love.keypressed = function (key, unicode) obj:keyPressed(key, unicode) end
		love.keyreleased = function (key, unicode) obj:keyReleased(key, unicode) end
		love.textinput = function (text) obj:textInput(text) end
		if obj.onNew then obj:onNew() end
		return obj
	end,
	
	-- Method: pressed
	-- Are *any* of the keys passed held down this frame?
	--
	-- Arguments:
	--		string key descriptions passed as individual arguments
	--
	-- Returns:
	-- 		boolean

	pressed = function (self, ...)
		local keys = {...}
		for _, value in pairs(keys) do
			if STRICT then
				assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
			end

			if self._thisFrame[value] then
				return true
			end
		end
		
		return false
	end,

	-- Method: justPressed
	-- Are *any* of the keys passed pressed for the first time this frame?
	--
	-- Arguments:
	--		string key descriptions passed as individual arguments
	--
	-- Returns:
	-- 		boolean

	justPressed = function (self, ...)
		local keys = {...}

		for _, value in pairs(keys) do
			if STRICT then
				assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
			end

			if self._thisFrame[value] and not self._lastFrame[value] then
				return true
			end
		end
		
		return false
	end,

	-- Method: released
	-- Are *all* of the keys passed not held down this frame?
	-- 
	-- Arguments:
	--		string key descriptions passed as individual arguments
	--
	-- Returns:
	-- 		boolean

	released = function (self, ...)
		local keys = {...}

		for _, value in pairs(keys) do
			if STRICT then
				assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
			end

			if self._thisFrame[value] then
				return false
			end
		end
		
		return true
	end,

	-- Method: justReleased
	-- Are *any* of the keys passed released after being held last frame?
	--
	-- Arguments:
	--		string key descriptions passed as individual arguments
	--
	-- Returns:
	-- 		boolean

	justReleased = function (self, ...)
		local keys = {...}

		for _, value in pairs(keys) do
			if STRICT then
				assert(type(value) == 'string', 'all keys are strings; asked to check a ' .. type(value))
			end

			if self._lastFrame[value] and not self._thisFrame[value] then
				return true
			end
		end
		
		return false
	end,

	-- Method: allPressed
	-- Returns all buttons currently pressed this frame.
	--
	-- Arguments:
	--		none
	--
	-- Returns:
	--		string key descriptions; if nothing is pressed, nil

	allPressed = function (self)
		local result = {}

		for key, value in pairs(self._thisFrame) do
			if value then table.insert(result, key) end
		end
		
		return unpack(result)
	end,

	-- Method: allJustPressed
	-- Returns all keys just pressed this frame.
	--
	-- Arguments:
	--		none
	--
	-- Returns:
	--		string key descriptions; if nothing is just pressed, nil

	allJustPressed = function (self)
		local result = {}

		for key, value in pairs(self._thisFrame) do
			if value and not self._lastFrame[key] then table.insert(result, key) end
		end
		
		return unpack(result)
	end,

	-- Method: allJustReleased
	-- Returns all keys just released this frame.
	--
	-- Arguments:
	--		none
	--
	-- Returns:
	--		string key descriptions; if nothing is just pressed, nil

	allJustReleased = function (self)
		local result = {}

		for key, value in pairs(self._thisFrame) do
			if not value and self._lastFrame[key] then table.insert(result, key) end
		end
		
		return unpack(result)
	end,

	-- Method: alreadyHandled
	-- Returns whether this key was already handled since it was pressed.
	-- Also sets the flag that something was handled, when calling.

	alreadyHandled = function (self, key)
		if not self._alreadyHandled[key] then
			-- mark preemptively as handled
			self._alreadyHandled[key] = true
			-- return false, because we need to handle it
			return false
		end
		return true
	end,

	-- Converts a character code to a Unicode string
	-- see http://stackoverflow.com/questions/7780179/what-is-the-way-to-represent-a-unichar-in-lua/7799843

	unicodeChar = function (self, code)
		if code == nil then return nil end
		if code < 32 then return string.format('\\x%02x', code) end
		if code < 126 then return string.char(code) end
		if code < 65539 then return string.format("\\u%04x", code) end
		if code < 1114111 then return string.format("\\u%08x", code) end
	end,

	-- Connects to the love.keypressed callback

	keyPressed = function (self, key, isrepeat)
		self._thisFrame[key] = true

		self._alreadyHandled[key] = false
		--if unicode and unicode >= 0x20 and unicode ~= 127 and unicode < 0x3000 then
		--	self.typed = self.typed .. self:unicodeChar(unicode)
		--end

		-- aliases for modifiers

		if key == 'rshift' or key == 'lshift' or
		   key == 'rctrl' or key == 'lctrl' or
		   key == 'ralt' or key == 'lalt' or
		   key == 'rmeta' or key == 'lmeta' or
		   key == 'rsuper' or key == 'lsuper' then
			self._thisFrame[string.sub(key, 2)] = true
		end
	end,

	-- Connects to the love.textinput callback

	textInput = function (self, text)
		self.typed = self.typed .. text
	end,

	-- Connects to the love.keyreleased callback

	keyReleased = function (self, key, unicode)
		self._thisFrame[key] = false

		-- aliases for modifiers

		if key == 'rshift' or key == 'lshift' or
		   key == 'rctrl' or key == 'lctrl' or
		   key == 'ralt' or key == 'lalt' or
		   key == 'rmeta' or key == 'lmeta' or
		   key == 'rsuper' or key == 'lsuper' then
			self._thisFrame[string.sub(key, 2)] = false
		end
	end,

	endFrame = function (self, elapsed)
		for key, value in pairs(self._thisFrame) do
			self._lastFrame[key] = value
		end

		self.typed = ''
		Sprite.endFrame(self, elapsed)
	end,

	update = function() end
}