1
debugger = debugger or {}
3
debugger.consoleKey = 'tab'
5
debugger.crashed = false
7
debugger._sourceCache = {}
10
-- Instrument classes to add, defaults to all available
14
debugger.init = function()
15
debugger.console = Group:new
19
instruments = { narrow = Group:new(), wide = Group:new() },
20
widths = { wide = 0.7, narrow = 0.3 },
23
_instrumentHeights = {},
25
onNew = function (self)
26
self:add(self.instruments.narrow)
27
self:add(self.instruments.wide)
30
update = function (self, elapsed)
31
if the.keys:justPressed(debugger.consoleKey) then
33
debugger.hideConsole()
35
debugger.showConsole()
39
for _, listener in pairs(self.listeners) do
43
if debugger.console.visible then
44
for spr, height in pairs(debugger.console._instrumentHeights) do
45
if height ~= spr.contentHeight then
46
debugger._resizeInstruments()
50
Group.update(self, elapsed)
55
the.app.meta:add(debugger.console)
57
debugger.addInstrument(DebugStepper:new())
58
debugger.addInstrument(DebugLocals:new())
59
debugger.addInstrument(DebugStack:new())
60
debugger.addInstrument(DebugPerformance:new())
61
debugger.addInstrument(DebugWatch:new())
62
debugger.addInstrument(DebugShortcuts:new())
63
debugger.addInstrument(DebugConsole:new())
71
debugger.showConsole = function()
72
debugger.console.visible = true
80
debugger.hideConsole = function()
81
if not debugger.crashed then
82
debugger.console.visible = false
87
-- instrument - <Group> enclosing the entire instrument
91
debugger.addInstrument = function (instrument)
92
local console = debugger.console
93
assert(instrument.width == 'narrow' or instrument.width == 'wide',
94
"debug instrument's width property must be either 'wide' or 'narrow'")
96
console.instruments[instrument.width]:add(instrument)
97
debugger._resizeInstruments()
101
-- listener - function
105
debugger.addListener = function (func)
106
table.insert(debugger.console.listeners, func)
113
debugger.reload = function()
116
-- create local references to needed variables
117
-- because we're about to blow the global scope away
119
local initialGlobals = debugger._initialGlobals
120
local initialPackages = debugger._initialPackages
122
-- reset global scope
124
for key, _ in pairs(_G) do
125
_G[key] = initialGlobals[key]
128
-- reload main file and restart
130
for key, _ in pairs(package.loaded) do
131
if not initialPackages[key] then
132
package.loaded[key] = nil
141
-- file - filename of the source code
142
-- line - line number to retrieve
144
-- string source or '(source not available')
146
debugger.sourceLine = function (file, line)
148
if not debugger._sourceCache[file] then
149
debugger._sourceCache[file] = {}
151
for line in love.filesystem.lines(file) do
152
table.insert(debugger._sourceCache[file], line)
156
return debugger._sourceCache[file][line]
158
return '(source not available)'
162
debugger._resizeInstruments = function()
163
local console = debugger.console
167
local x = console.spacing
168
local y = console.spacing
169
local width = the.app.width * console.widths.wide
170
local expandables = {}
172
for _, spr in pairs(console.instruments.wide.sprites) do
174
local height = spr:totalHeight()
175
console._instrumentHeights[spr] = spr.contentHeight
177
if height == '*' then
178
table.insert(expandables, spr)
180
spr:resize(x, y, width - console.spacing, height)
181
y = y + height + console.spacing
186
if #expandables > 0 then
187
local height = (the.app.height - y) / #expandables
189
for i, spr in ipairs(expandables) do
190
spr:resize(x, y + height * (i - 1), width - console.spacing, height - console.spacing)
194
-- narrow instruments
198
width = the.app.width * console.widths.narrow
201
for _, spr in pairs(console.instruments.narrow.sprites) do
203
local height = spr:totalHeight()
204
console._instrumentHeights[spr] = spr.contentHeight
206
if height == '*' then
207
table.insert(expandables, spr)
209
spr:resize(x, y, width - 2 * console.spacing, height)
210
y = y + height + console.spacing
215
if #expandables > 0 then
216
local height = (the.app.height - y) / #expandables
218
for i, spr in ipairs(expandables) do
219
spr:resize(x, y + height * (i - 1), width - console.spacing, height - console.spacing)
225
-- forever - run indefinitely, or just for a single frame?
227
-- whether a quit event was detected, and the caller
228
-- should take an action based on this
230
debugger._miniEventLoop = function (forever)
232
local quitNow = false
238
for e, a, b, c, d in love.event.poll() do
240
if not love.quit or not love.quit() then
246
love.handlers[e](a, b, c, d)
252
elapsed = love.timer.getDelta()
255
the.keys:startFrame(elapsed)
256
the.mouse:startFrame(elapsed)
257
debugger.console:startFrame(elapsed)
258
the.keys:update(elapsed)
259
the.mouse:update(elapsed)
260
debugger.console:update(elapsed)
261
the.keys:endFrame(elapsed)
262
the.mouse:endFrame(elapsed)
263
debugger.console:endFrame(elapsed)
265
if the.keys:pressed('escape') then
266
if not love.quit or not love.quit() then
271
if love.graphics then
272
love.graphics.clear()
274
debugger.console:draw()
278
if love.timer then love.timer.sleep(0.03) end
279
if love.graphics then love.graphics.present() end