1
-- Class: DebugShortcuts
3
-- This allows for debugging shortcuts that perform tasks that aid
4
-- debugging -- e.g. skipping to a certain level or making a player sprite
5
-- invincible. These can be triggered by a hotkey at any time, or appear
6
-- in a list in the debug console.
9
-- - Control-Alt-F toggles fullscreen
10
-- - Control-Alt-Q quits the app.
11
-- - Control-Alt-P deactivates the view.
12
-- - Control-Alt-R reloads all app code from on disk.
13
-- - Control-Alt-S saves a screenshot to the app's directory --
14
-- see https://love2d.org/wiki/love.filesystem for where this is.
16
DebugShortcuts = DebugInstrument:extend
21
-- Property: modifiers
22
-- A table of modifier keys that must be held in order to activate
23
-- a debugging hotkey (set via <add()>). If you want hotkeys to
24
-- activate without having to hold any keys down, set this to nil.
25
modifiers = {'ctrl', 'alt'},
27
-- internal property: _shortcuts
28
-- A table with entries that have desc (long description), key
29
-- (triggering key) and func (function to run) properties.
32
-- internal property: _buttons
33
-- An ordered table of shortcut buttons.
36
onNew = function (self)
37
self.title.text = 'Shortcuts'
39
self:addShortcut('Fullscreen', 'f', function() the.app:toggleFullscreen() end)
41
self:addShortcut('Pause', 'p', function()
42
the.view.active = not the.view.active
43
if the.view.active then
46
the.view:tint(0, 0, 0, 200)
50
self:addShortcut('Quit', 'q', love.event.quit)
52
self:addShortcut('Reload Code', 'r', debugger.reload)
54
self:addShortcut('Save Screenshot', 's', function()
55
if debugger.console.visible then
56
debugger.hideConsole()
57
the.view.timer:after(0.05, bind(the.app, 'saveScreenshot', 'screenshot.png'))
58
the.view.timer:after(0.1, debugger.showConsole)
60
the.app:saveScreenshot('screenshot.png')
64
debugger.addListener(bind(self, 'listen'))
65
debugger.addShortcut = function (desc, key, func) self:addShortcut(desc, key, func) end
66
debugger.setShortcutModifiers = function (...) self.modifiers = {...} end
69
addShortcut = function (self, desc, key, func)
70
table.insert(self._shortcuts, { desc = desc, key = key, func = func })
75
label = label .. ' (' .. key .. ')'
78
local button = self:add(DebugInstrumentButton:new
84
button.label.font = 11
86
button.background.height = 24
88
table.insert(self._buttons, button)
90
self.contentHeight = #self._buttons * (DebugInstrumentButton.height + self.spacing) + 2 * self.spacing
95
listen = function (self)
96
local modifiers = (self.modifiers == nil)
101
for _, key in pairs(self.modifiers) do
102
if not the.keys:pressed(key) then
110
for _, shortcut in pairs(self._shortcuts) do
111
if the.keys:justPressed(shortcut.key) then
112
shortcut.func(shortcut.key)
118
onResize = function (self, x, y, width, height)
121
width = width - self.spacing * 2
123
for _, spr in pairs(self._buttons) do
126
spr.background.width = width
127
spr.label.width = width
129
y = y + DebugInstrumentButton.height + self.spacing