/ld27

To get this branch, use:
bzr branch http://9ix.org/bzr/ld27

« back to all changes in this revision

Viewing changes to zoetrope/debug/shortcuts.lua

  • Committer: Josh C
  • Date: 2013-08-25 00:16:36 UTC
  • Revision ID: josh@9ix.org-20130825001636-xoivc9byo1mdx0fu
1 goal in each maze

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
--
2
 
--
3
 
--              - Control-Alt-F toggles fullscreen
4
 
--              - Control-Alt-Q quits the app.
5
 
--              - Control-Alt-P deactivates the view.
6
 
--              - Control-Alt-S saves a screenshot to the app's directory --
7
 
--                see https://love2d.org/wiki/love.filesystem for where this is.
8
 
 
9
 
DebugShortcuts = DebugInstrument:extend
10
 
{
11
 
        width = 'narrow',
12
 
        visible = false,
13
 
 
14
 
        -- Property: modifiers
15
 
        -- A table of modifier keys that must be held in order to activate
16
 
        -- a debugging hotkey (set via <add()>). If you want hotkeys to
17
 
        -- activate without having to hold any keys down, set this to nil.
18
 
        modifiers = {'ctrl', 'alt'},
19
 
 
20
 
        -- internal property: _shortcuts
21
 
        -- A table with entries that have desc (long description), key
22
 
        -- (triggering key) and func (function to run) properties.
23
 
        _shortcuts = {},
24
 
 
25
 
        -- internal property: _buttons
26
 
        -- An ordered table of shortcut buttons.
27
 
        _buttons = {},
28
 
 
29
 
        onNew = function (self)
30
 
                self.title.text = 'Shortcuts'
31
 
 
32
 
                self:addShortcut('Fullscreen', 'f', function() the.app:toggleFullscreen() end)
33
 
 
34
 
                self:addShortcut('Pause', 'p', function()
35
 
                        the.view.active = not the.view.active
36
 
                        if the.view.active then
37
 
                                the.view:tint()
38
 
                        else
39
 
                                the.view:tint(0, 0, 0, 200)
40
 
                        end
41
 
                end)
42
 
 
43
 
                self:addShortcut('Quit', 'q', love.event.quit)
44
 
 
45
 
                self:addShortcut('Reload Code', 'r', debugger.reload)
46
 
 
47
 
                self:addShortcut('Save Screenshot', 's', function()
48
 
                        if debugger.console.visible then
49
 
                                debugger.hideConsole()
50
 
                                the.view.timer:after(0.05, bind(the.app, 'saveScreenshot', 'screenshot.png'))
51
 
                                the.view.timer:after(0.1, debugger.showConsole)
52
 
                        else
53
 
                                the.app:saveScreenshot('screenshot.png')
54
 
                        end
55
 
                end)
56
 
 
57
 
                debugger.addListener(bind(self, 'listen'))
58
 
                debugger.addShortcut = function (desc, key, func) self:addShortcut(desc, key, func) end
59
 
                debugger.setShortcutModifiers = function (...) self.modifiers = {...} end
60
 
        end,
61
 
 
62
 
        addShortcut = function (self, desc, key, func)
63
 
                table.insert(self._shortcuts, { desc = desc, key = key, func = func })
64
 
 
65
 
                local label = desc
66
 
 
67
 
                if key then
68
 
                        label = label .. ' (' .. key .. ')'
69
 
                end
70
 
 
71
 
                local button = self:add(DebugInstrumentButton:new
72
 
                {
73
 
                        label = label,
74
 
                        onMouseUp = func
75
 
                })
76
 
 
77
 
                button.label.font = 11
78
 
                button.label.y = 6
79
 
                button.background.height = 24
80
 
 
81
 
                table.insert(self._buttons, button)
82
 
 
83
 
                self.contentHeight = #self._buttons * (DebugInstrumentButton.height + self.spacing) + 2 * self.spacing
84
 
 
85
 
                self.visible = true
86
 
        end,
87
 
 
88
 
        listen = function (self)
89
 
                local modifiers = (self.modifiers == nil)
90
 
 
91
 
                if not modifiers then
92
 
                        modifiers = true
93
 
 
94
 
                        for _, key in pairs(self.modifiers) do
95
 
                                if not the.keys:pressed(key) then
96
 
                                        modifiers = false
97
 
                                        break
98
 
                                end
99
 
                        end
100
 
                end
101
 
 
102
 
                if modifiers then
103
 
                        for _, shortcut in pairs(self._shortcuts) do
104
 
                                if the.keys:justPressed(shortcut.key) then
105
 
                                        shortcut.func(shortcut.key)
106
 
                                end
107
 
                        end
108
 
                end
109
 
        end,
110
 
 
111
 
        onResize = function (self, x, y, width, height)
112
 
                y = y + self.spacing
113
 
                x = x + self.spacing
114
 
                width = width - self.spacing * 2
115
 
 
116
 
                for _, spr in pairs(self._buttons) do
117
 
                        spr.x = x
118
 
                        spr.y = y
119
 
                        spr.background.width = width
120
 
                        spr.label.width = width
121
 
 
122
 
                        y = y + DebugInstrumentButton.height + self.spacing
123
 
                end
124
 
 
125
 
        end
126
 
}