/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/debugger.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
 
debugger = debugger or {}
2
 
 
3
 
debugger.consoleKey = 'tab'
4
 
 
5
 
debugger.crashed = false
6
 
 
7
 
debugger._sourceCache = {}
8
 
 
9
 
--
10
 
--              Instrument classes to add, defaults to all available
11
 
--
12
 
--              nothing
13
 
 
14
 
debugger.init = function()
15
 
        debugger.console = Group:new
16
 
        {
17
 
                visible = false,
18
 
                spacing = 10,
19
 
                instruments = { narrow = Group:new(), wide = Group:new() },
20
 
                widths = { wide = 0.7, narrow = 0.3 },
21
 
                listeners = {},
22
 
 
23
 
                _instrumentHeights = {},
24
 
 
25
 
                onNew = function (self)
26
 
                        self:add(self.instruments.narrow)
27
 
                        self:add(self.instruments.wide)
28
 
                end,
29
 
 
30
 
                update = function (self, elapsed)
31
 
                        if the.keys:justPressed(debugger.consoleKey) then
32
 
                                if self.visible then
33
 
                                        debugger.hideConsole()
34
 
                                else
35
 
                                        debugger.showConsole()
36
 
                                end
37
 
                        end
38
 
 
39
 
                        for _, listener in pairs(self.listeners) do
40
 
                                listener()
41
 
                        end
42
 
 
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()
47
 
                                        end
48
 
                                end
49
 
 
50
 
                                Group.update(self, elapsed)
51
 
                        end
52
 
                end
53
 
        }
54
 
 
55
 
        the.app.meta:add(debugger.console)
56
 
 
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())
64
 
end
65
 
 
66
 
--
67
 
--              none
68
 
--
69
 
--              nothing
70
 
 
71
 
debugger.showConsole = function()
72
 
        debugger.console.visible = true
73
 
end
74
 
 
75
 
--
76
 
--              none
77
 
--
78
 
--              nothing
79
 
 
80
 
debugger.hideConsole = function()
81
 
        if not debugger.crashed then
82
 
                debugger.console.visible = false
83
 
        end
84
 
end
85
 
 
86
 
--
87
 
--              instrument - <Group> enclosing the entire instrument
88
 
--
89
 
--              nothing
90
 
 
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'")
95
 
 
96
 
        console.instruments[instrument.width]:add(instrument)
97
 
        debugger._resizeInstruments()
98
 
end
99
 
 
100
 
--
101
 
--              listener - function
102
 
--
103
 
--              nothing
104
 
 
105
 
debugger.addListener = function (func)
106
 
        table.insert(debugger.console.listeners, func)
107
 
end
108
 
 
109
 
--              none
110
 
--
111
 
--              nothing
112
 
 
113
 
debugger.reload = function()
114
 
        love.audio.stop()
115
 
 
116
 
        -- create local references to needed variables
117
 
        -- because we're about to blow the global scope away
118
 
 
119
 
        local initialGlobals = debugger._initialGlobals
120
 
        local initialPackages = debugger._initialPackages
121
 
        
122
 
        -- reset global scope
123
 
 
124
 
        for key, _ in pairs(_G) do
125
 
                _G[key] = initialGlobals[key]
126
 
        end
127
 
 
128
 
        -- reload main file and restart
129
 
 
130
 
        for key, _ in pairs(package.loaded) do
131
 
                if not initialPackages[key] then
132
 
                        package.loaded[key] = nil
133
 
                end
134
 
        end
135
 
 
136
 
        require('main')
137
 
        love.load()
138
 
end
139
 
 
140
 
--
141
 
--              file - filename of the source code
142
 
--              line - line number to retrieve
143
 
--
144
 
--              string source or '(source not available')
145
 
 
146
 
debugger.sourceLine = function (file, line)
147
 
        if file then
148
 
                if not debugger._sourceCache[file] then
149
 
                        debugger._sourceCache[file] = {}
150
 
 
151
 
                        for line in love.filesystem.lines(file) do
152
 
                                table.insert(debugger._sourceCache[file], line)
153
 
                        end
154
 
                end
155
 
 
156
 
                return debugger._sourceCache[file][line]
157
 
        else
158
 
                return '(source not available)'
159
 
        end
160
 
end
161
 
 
162
 
debugger._resizeInstruments = function()
163
 
        local console = debugger.console
164
 
 
165
 
        -- wide instruments
166
 
 
167
 
        local x = console.spacing
168
 
        local y = console.spacing
169
 
        local width = the.app.width * console.widths.wide
170
 
        local expandables = {}
171
 
        
172
 
        for _, spr in pairs(console.instruments.wide.sprites) do
173
 
                if spr.visible then
174
 
                        local height = spr:totalHeight()
175
 
                        console._instrumentHeights[spr] = spr.contentHeight
176
 
 
177
 
                        if height == '*' then
178
 
                                table.insert(expandables, spr)
179
 
                        else
180
 
                                spr:resize(x, y, width - console.spacing, height)
181
 
                                y = y + height + console.spacing
182
 
                        end
183
 
                end
184
 
        end
185
 
 
186
 
        if #expandables > 0 then
187
 
                local height = (the.app.height - y) / #expandables
188
 
 
189
 
                for i, spr in ipairs(expandables) do
190
 
                        spr:resize(x, y + height * (i - 1), width - console.spacing, height - console.spacing)
191
 
                end
192
 
        end
193
 
 
194
 
        -- narrow instruments
195
 
 
196
 
        x = x + width
197
 
        y = console.spacing
198
 
        width = the.app.width * console.widths.narrow
199
 
        expandables = {}
200
 
 
201
 
        for _, spr in pairs(console.instruments.narrow.sprites) do
202
 
                if spr.visible then
203
 
                        local height = spr:totalHeight()
204
 
                        console._instrumentHeights[spr] = spr.contentHeight
205
 
 
206
 
                        if height == '*' then
207
 
                                table.insert(expandables, spr)
208
 
                        else
209
 
                                spr:resize(x, y, width - 2 * console.spacing, height)
210
 
                                y = y + height + console.spacing
211
 
                        end
212
 
                end
213
 
        end
214
 
 
215
 
        if #expandables > 0 then
216
 
                local height = (the.app.height - y) / #expandables
217
 
 
218
 
                for i, spr in ipairs(expandables) do
219
 
                        spr:resize(x, y + height * (i - 1), width - console.spacing, height - console.spacing)
220
 
                end
221
 
        end
222
 
end
223
 
 
224
 
--
225
 
--              forever - run indefinitely, or just for a single frame?
226
 
--
227
 
--              whether a quit event was detected, and the caller
228
 
--              should take an action based on this
229
 
 
230
 
debugger._miniEventLoop = function (forever)
231
 
        local elapsed = 0
232
 
        local quitNow = false
233
 
 
234
 
        repeat
235
 
                if love.event then
236
 
                        love.event.pump()
237
 
                        
238
 
                        for e, a, b, c, d in love.event.poll() do
239
 
                                if e == 'quit' then
240
 
                                        if not love.quit or not love.quit() then
241
 
                                                quitNow = true
242
 
                                                forever = false
243
 
                                        end
244
 
                                end
245
 
 
246
 
                                love.handlers[e](a, b, c, d)
247
 
                        end
248
 
                end
249
 
 
250
 
                if love.timer then
251
 
                        love.timer.step()
252
 
                        elapsed = love.timer.getDelta()
253
 
                end
254
 
 
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)
264
 
 
265
 
                if the.keys:pressed('escape') then
266
 
                        if not love.quit or not love.quit() then
267
 
                                love.event.quit()
268
 
                        end
269
 
                end
270
 
 
271
 
                if love.graphics then
272
 
                        love.graphics.clear()
273
 
                        if love.draw then
274
 
                                debugger.console:draw()
275
 
                        end
276
 
                end
277
 
 
278
 
                if love.timer then love.timer.sleep(0.03) end
279
 
                if love.graphics then love.graphics.present() end
280
 
        until not forever 
281
 
 
282
 
        return quitNow
283
 
end