/ld27

To get this branch, use:
bzr branch /bzr/ld27

« back to all changes in this revision

Viewing changes to zoetrope/debug/shortcuts.lua

  • Committer: Josh C
  • Date: 2014-07-03 14:41:57 UTC
  • Revision ID: josh@9ix.org-20140703144157-xydxt62xzcdq6bdk
cluke009 zoetrope + my spritebatch changes

Show diffs side-by-side

added added

removed removed

 
1
-- Class: DebugShortcuts
 
2
--
 
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.
 
7
--
 
8
-- Out of the box:
 
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.
 
15
 
 
16
DebugShortcuts = DebugInstrument:extend
 
17
{
 
18
        width = 'narrow',
 
19
        visible = false,
 
20
 
 
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'},
 
26
 
 
27
        -- internal property: _shortcuts
 
28
        -- A table with entries that have desc (long description), key
 
29
        -- (triggering key) and func (function to run) properties.
 
30
        _shortcuts = {},
 
31
 
 
32
        -- internal property: _buttons
 
33
        -- An ordered table of shortcut buttons.
 
34
        _buttons = {},
 
35
 
 
36
        onNew = function (self)
 
37
                self.title.text = 'Shortcuts'
 
38
 
 
39
                self:addShortcut('Fullscreen', 'f', function() the.app:toggleFullscreen() end)
 
40
 
 
41
                self:addShortcut('Pause', 'p', function()
 
42
                        the.view.active = not the.view.active
 
43
                        if the.view.active then
 
44
                                the.view:tint()
 
45
                        else
 
46
                                the.view:tint(0, 0, 0, 200)
 
47
                        end
 
48
                end)
 
49
 
 
50
                self:addShortcut('Quit', 'q', love.event.quit)
 
51
 
 
52
                self:addShortcut('Reload Code', 'r', debugger.reload)
 
53
 
 
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)
 
59
                        else
 
60
                                the.app:saveScreenshot('screenshot.png')
 
61
                        end
 
62
                end)
 
63
 
 
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
 
67
        end,
 
68
 
 
69
        addShortcut = function (self, desc, key, func)
 
70
                table.insert(self._shortcuts, { desc = desc, key = key, func = func })
 
71
 
 
72
                local label = desc
 
73
 
 
74
                if key then
 
75
                        label = label .. ' (' .. key .. ')'
 
76
                end
 
77
 
 
78
                local button = self:add(DebugInstrumentButton:new
 
79
                {
 
80
                        label = label,
 
81
                        onMouseUp = func
 
82
                })
 
83
 
 
84
                button.label.font = 11
 
85
                button.label.y = 6
 
86
                button.background.height = 24
 
87
 
 
88
                table.insert(self._buttons, button)
 
89
 
 
90
                self.contentHeight = #self._buttons * (DebugInstrumentButton.height + self.spacing) + 2 * self.spacing
 
91
 
 
92
                self.visible = true
 
93
        end,
 
94
 
 
95
        listen = function (self)
 
96
                local modifiers = (self.modifiers == nil)
 
97
 
 
98
                if not modifiers then
 
99
                        modifiers = true
 
100
 
 
101
                        for _, key in pairs(self.modifiers) do
 
102
                                if not the.keys:pressed(key) then
 
103
                                        modifiers = false
 
104
                                        break
 
105
                                end
 
106
                        end
 
107
                end
 
108
 
 
109
                if modifiers then
 
110
                        for _, shortcut in pairs(self._shortcuts) do
 
111
                                if the.keys:justPressed(shortcut.key) then
 
112
                                        shortcut.func(shortcut.key)
 
113
                                end
 
114
                        end
 
115
                end
 
116
        end,
 
117
 
 
118
        onResize = function (self, x, y, width, height)
 
119
                y = y + self.spacing
 
120
                x = x + self.spacing
 
121
                width = width - self.spacing * 2
 
122
 
 
123
                for _, spr in pairs(self._buttons) do
 
124
                        spr.x = x
 
125
                        spr.y = y
 
126
                        spr.background.width = width
 
127
                        spr.label.width = width
 
128
 
 
129
                        y = y + DebugInstrumentButton.height + self.spacing
 
130
                end
 
131
 
 
132
        end
 
133
}