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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
-- Class: DebugConsole
-- It can be used to keep track of fps, the position of a sprite,
-- and so on. It only updates when visible.
--
-- This also allows debugging hotkeys -- e.g. you could set it so that
-- pressing Control-Alt-I toggles invincibility of the player sprite.
-- Out of the box:
-- - Control-Alt-F toggles fullscreen
-- - Control-Alt-Q quits the app.
-- - Control-Alt-P deactivates the view.
-- - Control-Alt-R reloads all app code from on disk.
-- - Control-Alt-S saves a screenshot to the app's directory --
-- see https://love2d.org/wiki/love.filesystem for where this is.
DebugConsole = Group:extend
{
-- Property: toggleKey
-- What key toggles visibility. By default, this is the tab key.
toggleKey = 'tab',
-- Property: hotkeyModifiers
-- A table of modifier keys that must be held in order to activate
-- a debugging hotkey (set via <addHotkey()>). If you want hotkeys to
-- activate without having to hold any keys down, set this to nil.
hotkeyModifiers = {'ctrl', 'alt'},
-- Property: watchBasics
-- If true, the console will automatically start watching the frames
-- per second and memory usage. Changing this value after the object has
-- been created has no effect.
watchBasics = true,
-- Property: watchWidth
-- How wide the sidebar, where watch values are displayed, should be.
watchWidth = 150,
-- Property: inputHistory
-- A table of previously-entered commands.
inputHistory = {},
-- Property: inputHistoryIndex
-- Which history entry, if any, we are displaying.
inputHistoryIndex = 1,
-- Property: bg
-- The background <Fill> used to darken the view.
-- Property: log
-- The <Text> sprite showing recent lines in the log.
-- Property: watchList
-- The <Text> sprite showing the state of all watched variables.
-- Property: input
-- The <TextInput> that the user types into to enter commands.
-- Property: prompt
-- The <Text> sprite that shows a > in front of commands.
-- internal property: _bindings
-- Keeps track of debugging hotkeys.
new = function (self, obj)
local width = the.app.width
local height = the.app.height
obj = self:extend(obj)
obj.visible = false
obj._watches = {}
obj._hotkeys = {}
obj.fill = Fill:new{ x = 0, y = 0, width = width, height = height, fill = {0, 0, 0, 200} }
obj:add(obj.fill)
obj.log = Text:new{ x = 4, y = 4, width = width - self.watchWidth - 8, height = height - 8, text = '' }
obj:add(obj.log)
obj.watchList = Text:new{ x = width - self.watchWidth - 4, y = 4,
width = self.watchWidth - 8, height = height - 8, text = '', wordWrap = false }
obj:add(obj.watchList)
obj.prompt = Text:new{ x = 4, y = 0, width = 100, text = '>' }
obj:add(obj.prompt)
local inputIndent = obj.log._fontObj:getWidth('>') + 4
obj.input = TextInput:new
{
x = inputIndent, y = 0, width = the.app.width,
active = false,
onType = function (self, char)
return char ~= the.console.toggleKey
end
}
obj:add(obj.input)
-- some default behavior
obj:addHotkey('f', function() the.app:toggleFullscreen() end)
obj:addHotkey('p', function()
the.view.active = not the.view.active
if the.view.active then
the.view:tint()
else
the.view:tint(0, 0, 0, 200)
end
end)
obj:addHotkey('q', love.event.quit)
if debugger then obj:addHotkey('r', debugger.reload) end
obj:addHotkey('s', function() the.app:saveScreenshot('screenshot.png') end)
if obj.watchBasics then
obj:watch('FPS', 'love.timer.getFPS()')
obj:watch('Memory', 'math.floor(collectgarbage("count") / 1024) .. "M"')
end
-- hijack print function
-- this is nasty to debug if it goes wrong, be careful
obj._oldPrint = print
print = function (...)
local caller = debug.getinfo(2)
if caller.linedefined ~= 0 then
obj.log.text = obj.log.text .. '(' .. caller.short_src .. ':' .. caller.linedefined .. ') '
end
for _, value in pairs{...} do
obj.log.text = obj.log.text .. tostring(value) .. ' '
end
obj.log.text = obj.log.text .. '\n'
obj._updateLog = true
obj._oldPrint(...)
end
debugger._unsourcedPrint = function (...)
for _, value in pairs{...} do
obj.log.text = obj.log.text .. tostring(value) .. ' '
end
obj.log.text = obj.log.text .. '\n'
obj._updateLog = true
obj._oldPrint(...)
end
the.console = obj
if obj.onNew then obj.onNew() end
return obj
end,
-- Method: watch
-- Adds an expression to be watched.
--
-- Arguments:
-- label - string label
-- expression - expression to evaluate as a string
watch = function (self, label, expression)
table.insert(self._watches, { label = label,
func = loadstring('return ' .. expression) })
end,
-- Method: addHotkey
-- Adds a hotkey to execute a function. This hotkey will require
-- holding down whatever modifiers are set in <hotkeyModifiers>.
--
-- Arguments:
-- key - key to trigger the hotkey
-- func - function to run. This will receive the key that
-- was pressed, so you can re-use functions (i.e.
-- the 1 key loads level 1, the 2 key loads level 2).
--
-- Returns:
-- nothing
addHotkey = function (self, key, func)
table.insert(self._hotkeys, { key = key, func = func })
end,
-- Method: execute
-- Safely executes a string of code and prints the result.
--
-- Arguments:
-- code - string code to execute
--
-- Returns:
-- string result
execute = function (self, code)
if string.sub(code, 1, 1) == '=' then
code = 'debugger._unsourcedPrint (' .. string.sub(code, 2) .. ')'
end
local func, err = loadstring(code)
if func then
local ok, result = pcall(func)
if not ok then
debugger._unsourcedPrint('Error, ' .. tostring(result) .. '\n')
else
debugger._unsourcedPrint('')
end
return tostring(result)
else
debugger._unsourcedPrint('Syntax error, ' .. string.gsub(tostring(err), '^.*:', '') .. '\n')
end
end,
-- Method: show
-- Shows the debug console.
--
-- Arguments:
-- none
--
-- Returns:
-- nothing
show = function (self)
self.visible = true
self.input.active = true
end,
-- Method: hide
-- Hides the debug console.
--
-- Arguments:
-- none
--
-- Returns:
-- nothing
hide = function (self)
self.visible = false
self.input.active = false
end,
update = function (self, elapsed)
-- listen for visibility key
if the.keys:justPressed(self.toggleKey) then
self.visible = not self.visible
self.input.active = self.visible
end
-- listen for hotkeys
local modifiers = (self.hotkeyModifiers == nil)
if not modifiers then
modifiers = true
for _, key in pairs(self.hotkeyModifiers) do
if not the.keys:pressed(key) then
modifiers = false
break
end
end
end
if modifiers then
for _, hotkey in pairs(self._hotkeys) do
if the.keys:justPressed(hotkey.key) then
hotkey.func(hotkey.key)
end
end
end
if self.visible then
-- update watches
self.watchList.text = ''
for _, watch in pairs(self._watches) do
local ok, value = pcall(watch.func)
if not ok then value = nil end
self.watchList.text = self.watchList.text .. watch.label .. ': ' .. tostring(value) .. '\n'
end
-- update log
if self._updateLog then
local lineHeight = self.log._fontObj:getHeight()
local _, height = self.log:getSize()
local linesToDelete = math.ceil((height - the.app.height - 20) / lineHeight)
if linesToDelete > 0 then
self.log.text = string.gsub(self.log.text, '.-\n', '', linesToDelete)
height = height - linesToDelete * lineHeight
end
self.prompt.y = height + 4
self.input.y = height + 4
self._updateLog = false
end
-- handle special keys at the console
if the.keys:pressed('ctrl') and the.keys:justPressed('a') then
self.input.caret = 0
end
if the.keys:pressed('ctrl') and the.keys:justPressed('e') then
self.input.caret = string.len(self.input.text)
end
if the.keys:pressed('ctrl') and the.keys:justPressed('k') then
self.input.caret = 0
self.input.text = ''
end
if the.keys:justPressed('up') and self.inputHistoryIndex > 1 then
-- save what the user was in the middle of typing
self.inputHistory[self.inputHistoryIndex] = self.input.text
self.input.text = self.inputHistory[self.inputHistoryIndex - 1]
self.input.caret = string.len(self.input.text)
self.inputHistoryIndex = self.inputHistoryIndex - 1
end
if the.keys:justPressed('down') and self.inputHistoryIndex < #self.inputHistory then
self.input.text = self.inputHistory[self.inputHistoryIndex + 1]
self.input.caret = string.len(self.input.text)
self.inputHistoryIndex = self.inputHistoryIndex + 1
end
if the.keys:justPressed('return') then
debugger._unsourcedPrint('>' .. self.input.text)
self:execute(self.input.text)
table.insert(self.inputHistory, self.inputHistoryIndex, self.input.text)
while #self.inputHistory > self.inputHistoryIndex do
table.remove(self.inputHistory)
end
self.inputHistoryIndex = self.inputHistoryIndex + 1
self.input.text = ''
self.input.caret = 0
end
end
Group.update(self, elapsed)
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
if debugger then
debugger.reload = function()
if DEBUG then
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
end
end
|