/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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
debugger = debugger or {}

-- Property: debugger.consoleKey
-- What key toggles visibility. By default, this is the tab key.
debugger.consoleKey = 'tab'

-- Property: debugger.crashed
-- Records whether the app has crashed.
debugger.crashed = false

-- internal property: debugger._sourceCache
-- A cache of source files used by debugger.sourceLine().
debugger._sourceCache = {}

-- Function: debugger.init()
-- Adds debug instruments to the app. Must be called *after*
-- an app has started running, i.e. in its <App.onRun> event
-- handler.
--
-- Arguments:
--		Instrument classes to add, defaults to all available
--
-- Returns:
--		nothing

debugger.init = function()
	debugger.console = Group:new
	{
		visible = false,
		spacing = 10,
		instruments = { narrow = Group:new(), wide = Group:new() },
		widths = { wide = 0.7, narrow = 0.3 },
		listeners = {},

		_instrumentHeights = {},

		onNew = function (self)
			self:add(self.instruments.narrow)
			self:add(self.instruments.wide)
		end,

		update = function (self, elapsed)
			if the.keys:justPressed(debugger.consoleKey) then
				if self.visible then
					debugger.hideConsole()
				else
					debugger.showConsole()
				end
			end

			for _, listener in pairs(self.listeners) do
				listener()
			end

			if debugger.console.visible then
				for spr, height in pairs(debugger.console._instrumentHeights) do
					if height ~= spr.contentHeight then
						debugger._resizeInstruments()
					end
				end

				Group.update(self, elapsed)
			end
		end
	}

	the.app.meta:add(debugger.console)

	debugger.addInstrument(DebugStepper:new())
	debugger.addInstrument(DebugLocals:new())
	debugger.addInstrument(DebugStack:new())
	debugger.addInstrument(DebugPerformance:new())
	debugger.addInstrument(DebugWatch:new())
	debugger.addInstrument(DebugShortcuts:new())
	debugger.addInstrument(DebugConsole:new())
end

-- Function: debugger.showConsole()
-- Makes the console visible.
--
-- Arguments:
--		none
--
-- Returns:
--		nothing

debugger.showConsole = function()
	debugger.console.visible = true
end

-- Function: debugger.hideConsole
-- Makes the console invisible. If the app has crashed,
-- this has no effect.
--
-- Arguments:
--		none
--
-- Returns:
--		nothing

debugger.hideConsole = function()
	if not debugger.crashed then
		debugger.console.visible = false
	end
end

-- Function: debugger.addInstrument
-- Adds an instrument to the console, creating a container and
-- tab to select it.
--
-- Arguments:
--		instrument - <Group> enclosing the entire instrument
--
-- Returns:
--		nothing

debugger.addInstrument = function (instrument)
	local console = debugger.console
	assert(instrument.width == 'narrow' or instrument.width == 'wide',
	       "debug instrument's width property must be either 'wide' or 'narrow'")

	console.instruments[instrument.width]:add(instrument)
	debugger._resizeInstruments()
end

-- Function: debugger.addListener
-- Adds a function that will be called on every frame,
-- regardless of whether the console is visible.
--
-- Arguments:
--		listener - function
--
-- Returns:
--		nothing

debugger.addListener = function (func)
	table.insert(debugger.console.listeners, func)
end

-- Function: debugger.reload
-- Resets the entire app and forces all code to be reloaded from 
-- on disk. via https://love2d.org/forums/viewtopic.php?f=3&t=7965
-- 
-- Arguments:
--		none
--
-- Returns:
--		nothing

debugger.reload = function()
	love.audio.stop()

	-- create local references to needed variables
	-- because we're about to blow the global scope away

	local initialGlobals = debugger._initialGlobals
	local initialPackages = debugger._initialPackages
	
	-- reset global scope

	for key, _ in pairs(_G) do
		_G[key] = initialGlobals[key]
	end

	-- reload main file and restart

	for key, _ in pairs(package.loaded) do
		if not initialPackages[key] then
			package.loaded[key] = nil
		end
	end

	require('main')
	love.load()
end

-- Function: debugger.sourceLine
-- Retrieves a line of source code. If the source file cannot 
-- be opened, then this returns '(source not available)'. If
-- the line doesn't exist in the file (e.g. you ask for line 200
-- of a 100-line file), this returns nil.
--
-- Arguments:
--		file - filename of the source code
--		line - line number to retrieve
--
-- Returns:
--		string source or '(source not available')

debugger.sourceLine = function (file, line)
	if file then
		if not debugger._sourceCache[file] then
			debugger._sourceCache[file] = {}

			for line in love.filesystem.lines(file) do
				table.insert(debugger._sourceCache[file], line)
			end
		end

		return debugger._sourceCache[file][line]
	else
		return '(source not available)'
	end
end

debugger._resizeInstruments = function()
	local console = debugger.console

	-- wide instruments

	local x = console.spacing
	local y = console.spacing
	local width = the.app.width * console.widths.wide
	local expandables = {}
	
	for _, spr in pairs(console.instruments.wide.sprites) do
		if spr.visible then
			local height = spr:totalHeight()
			console._instrumentHeights[spr] = spr.contentHeight

			if height == '*' then
				table.insert(expandables, spr)
			else
				spr:resize(x, y, width - console.spacing, height)
				y = y + height + console.spacing
			end
		end
	end

	if #expandables > 0 then
		local height = (the.app.height - y) / #expandables

		for i, spr in ipairs(expandables) do
			spr:resize(x, y + height * (i - 1), width - console.spacing, height - console.spacing)
		end
	end

	-- narrow instruments

	x = x + width
	y = console.spacing
	width = the.app.width * console.widths.narrow
	expandables = {}

	for _, spr in pairs(console.instruments.narrow.sprites) do
		if spr.visible then
			local height = spr:totalHeight()
			console._instrumentHeights[spr] = spr.contentHeight

			if height == '*' then
				table.insert(expandables, spr)
			else
				spr:resize(x, y, width - 2 * console.spacing, height)
				y = y + height + console.spacing
			end
		end
	end

	if #expandables > 0 then
		local height = (the.app.height - y) / #expandables

		for i, spr in ipairs(expandables) do
			spr:resize(x, y + height * (i - 1), width - console.spacing, height - console.spacing)
		end
	end
end

-- internal function: debugger._miniEventLoop
-- This replicates the entire LOVE event loop, but only updates/draws
-- the debug console, keyboard, and mouse. This is so that after a crash or
-- during a break, drawing and updates still continue to happen.
--
-- Arguments:
--		forever - run indefinitely, or just for a single frame?
--
-- Returns:
--		whether a quit event was detected, and the caller
--		should take an action based on this

debugger._miniEventLoop = function (forever)
	local elapsed = 0
	local quitNow = false

	repeat
		if love.event then
			love.event.pump()
			
			for e, a, b, c, d in love.event.poll() do
				if e == 'quit' then
					if not love.quit or not love.quit() then
						quitNow = true
						forever = false
					end
				end

				love.handlers[e](a, b, c, d)
			end
		end

		if love.timer then
			love.timer.step()
			elapsed = love.timer.getDelta()
		end

		the.keys:startFrame(elapsed)
		the.mouse:startFrame(elapsed)
		debugger.console:startFrame(elapsed)
		the.keys:update(elapsed)
		the.mouse:update(elapsed)
		debugger.console:update(elapsed)
		the.keys:endFrame(elapsed)
		the.mouse:endFrame(elapsed)
		debugger.console:endFrame(elapsed)

		if the.keys:pressed('escape') then
			if not love.quit or not love.quit() then
				love.event.quit()
			end
		end

		if love.graphics then
			love.graphics.clear()
			if love.draw then
				debugger.console:draw()
			end
		end

		if love.timer then love.timer.sleep(0.03) end
		if love.graphics then love.graphics.present() end
	until not forever 

	return quitNow
end