/ld26

To get this branch, use:
bzr branch /bzr/ld26
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
-- Class: Recorder
-- This records the user's inputs for playback at a later time,
-- or saving to a file. You must start recording *after* all input
-- objects are set up, and you should only run one recorder at a time.
--
-- Extends:
--		<Sprite>

Recorder = Sprite:extend{
	-- private property: elapsed
	-- either time elapsed while recording or playing back, in seconds
	_elapsed = 0,

	-- private property: mousePosTimer
	-- used to make sure mouse motions are recorded even if no other event occurs
	_mousePosTimer = 0,

	-- Constant: IDLE
	-- The recorder is currently doing nothing.
	IDLE = 'idle',

	-- Constant: RECORDING
	-- The recorder is currently recording user input.
	RECORDING = 'recording',

	-- Constant: PLAYING
	-- The recorder is currently playing back user input.
	PLAYING = 'playing',

	-- Property: mousePosInterval
	-- How often, in seconds, we should capture the mouse position.
	-- This records the mouse position when other events occur, too, so
	-- it's possible the interval will be even higher in reality.
	-- Setting this number lower will burn memory.
	mousePosInterval = 0.05,

	-- Property: state
	-- One of the state constants, indicates what the recorder is currently doing.

	-- Property: record
	-- A table of inputs with timing information.

	new = function (self, obj)
		obj = self:extend(obj)
		obj.state = Recorder.IDLE

		Sprite.new(obj)
		return obj
	end,

	-- Method: startRecording
	-- Begins recording user inputs. If the recorder is already recording,
	-- this has no effect.
	--
	-- Arguments:
	--		record - Record to use. Any existing data is appended to.
	--				 If omitted, the current record is used. If the current
	--				 record is unset, this creates a new record.
	--
	-- Returns:
	--		nothing

	startRecording = function (self, record)
		if self.state ~= Recorder.IDLE then return end

		-- set up properties
		self.record = record or self.record or {}
		self.state = Recorder.RECORDING
		self._elapsed = 0
		self._mousePosTimer = 0

		-- insert ourselves into event handlers
		self:stealInputs()
	end,

	-- Method: stopRecording
	-- Stops recording user inputs. If the recorder wasn't recording anyway,
	-- this does nothing.
	-- 
	-- Arguments:
	--		none
	-- 
	-- Returns:
	--		nothing

	stopRecording = function (self)
		if self.state ~= Recorder.RECORDING then return end

		self.state = Recorder.IDLE
		self:restoreInputs()
		love.keypressed = self.origKeyPressed
		love.keyreleased = self.origKeyReleased
	end,

	-- Method: startPlaying
	-- Starts playing back user inputs. If this is already playing
	-- a recording, this restarts it.
	--
	-- Arguments:
	--		record - Record to play back. If omitted, this uses
	--				 the recorder's record property.
	--
	-- Returns:
	--		nothing

	startPlaying = function (self, record)
		record = record or self.record

		-- if we are currently recording, ignore the request

		if self.state == Recorder.RECORDING then return end

		-- restart if needed

		if self.state == Recorder.PLAYING then
			self:stopPlaying()	
		end

		self.state = Recorder.PLAYING 

		self._elapsed = 0
		self.playbackIndex = 1
		self:stealInputs()
	end,

	-- Method: stopPlaying
	-- Stops playing back user inputs.
	--
	-- Arguments:
	--		none
	--
	-- Returns:
	--		nothing

	stopPlaying = function (self)
		if not self.state == Recorder.PLAYING then return end
		self.state = Recorder.IDLE
		self:restoreInputs()
	end,

	stealInputs = function (self)
		local this = self

		self.origKeyPressed = love.keypressed
		love.keypressed = function (key, code) this:recordKeyPress(key, code) end
		self.origKeyReleased = love.keyreleased
		love.keyreleased = function (key, code) this:recordKeyRelease(key, code) end
		self.origMousePressed = love.mousepressed
		love.mousepressed = function (x, y, button) this:recordMousePress(x, y, button) end
		self.origMouseReleased = love.mousereleased
		love.mousereleased = function (x, y, button) this:recordMouseRelease(x, y, button) end
	end,

	restoreInputs = function (self)
		love.keypressed = self.origKeyPressed
		love.keyreleased = self.origKeyReleased
		love.mousepressed = self.origMousePressed
		love.mousereleased = self.origMouseReleased
	end,
	
	recordKeyPress = function (self, key, unicode)
		table.insert(self.record, { self._elapsed, the.mouse.x, the.mouse.y, 'keypress', key, unicode })
		self._mousePosTimer = 0

		if self.origKeyPressed then
			self.origKeyPressed(key, unicode)
		end
	end,

	recordKeyRelease = function (self, key, unicode)
		table.insert(self.record, { self._elapsed, the.mouse.x, the.mouse.y, 'keyrelease', key, unicode })
		self._mousePosTimer = 0

		if self.origKeyReleased then
			self.origKeyReleased(key, unicode)
		end
	end,

	recordMousePress = function (self, x, y, button)
		table.insert(self.record, { self._elapsed, x, y, 'mousepress', button })
		self._mousePosTimer = 0

		if self.origMousePressed then
			self.origMousePressed(x, y, button)
		end
	end,

	recordMouseRelease = function (self, x, y, button)
		table.insert(self.record, { self._elapsed, x, y, 'mouserelease', button })
		self._mousePosTimer = 0

		if self.origMouseReleased then
			self.origMouseReleased(x, y, button)
		end
	end,

	update = function (self, elapsed)
		-- increment timers

		if self.state ~= Recorder.IDLE then
			self._elapsed = self._elapsed + elapsed
		end

		-- record mouse position if the timer has expired

		if self.state == Recorder.RECORDING then
			self._mousePosTimer = self._mousePosTimer + elapsed

			if self._mousePosTimer > self.mousePosInterval then
				table.insert(self.record, { self._elapsed, the.mouse.x, the.mouse.y })

				self._mousePosTimer = 0
			end
		end

		-- handle playback

		if self.state == Recorder.PLAYING and self._elapsed >= self.record[self.playbackIndex][1] then
			local event = self.record[self.playbackIndex]
	
			love.mouse.setPosition(event[2], event[3])

			if event[4] == 'keypress' and self.origKeyPressed then
				self.origKeyPressed(event[5], event[6])
			elseif event[4] == 'keyrelease' and self.origKeyReleased then
				self.origKeyReleased(event[5], event[6])
			elseif event[4] == 'mousepress' and self.origMousePressed then
				self.origMousePressed(event[2], event[3], event[5])
			elseif event[4] == 'mouserelease' and self.origMouseReleased then
				self.origMouseReleased(event[2], event[3], event[5])
			end

			self.playbackIndex = self.playbackIndex + 1

			if self.playbackIndex > #self.record then
				self:stopPlaying()
			end
		end
	end
}