|
19 | 19 |
|
20 | 20 | this.vmEventBound = false; |
21 | 21 |
|
22 | | - this._loadAutoStart(); |
| 22 | + this.loadAutoStart(); |
23 | 23 | } |
24 | 24 |
|
25 | | - _loadAutoStart() { |
| 25 | + loadAutoStart() { |
26 | 26 | if (!isPenguinMod) { |
27 | 27 | try { |
28 | 28 | const vm = Scratch.vm; |
|
35 | 35 | } |
36 | 36 | } |
37 | 37 |
|
38 | | - _saveAutoStart() { |
| 38 | + saveAutoStart() { |
39 | 39 | if (!isPenguinMod) { |
40 | 40 | try { |
41 | 41 | const vm = Scratch.vm; |
|
252 | 252 |
|
253 | 253 | toggleAutoStart() { |
254 | 254 | this.autoStart = !this.autoStart; |
255 | | - this._saveAutoStart(); |
| 255 | + this.saveAutoStart(); |
256 | 256 | Scratch.vm.extensionManager.refreshBlocks('beatSync'); |
257 | 257 | } |
258 | 258 |
|
259 | 259 | startBeat() { |
260 | 260 | if (!this.isRunning) { |
261 | 261 | this.isRunning = true; |
262 | | - this.startAudioTime = this.getAudioCtx().currentTime; |
| 262 | + const ctx = this.getAudioCtx(); |
| 263 | + this.startAudioTime = ctx.currentTime; |
263 | 264 | } |
264 | 265 | } |
265 | 266 |
|
|
275 | 276 | this.totalBeats = 0; |
276 | 277 | this.beatPosition = 0; |
277 | 278 | if (this.isRunning) { |
278 | | - this.startAudioTime = this.getAudioCtx().currentTime; |
| 279 | + const ctx = this.getAudioCtx(); |
| 280 | + this.startAudioTime = ctx.currentTime; |
279 | 281 | } |
280 | 282 | } |
281 | 283 |
|
|
284 | 286 | const startBeat = Math.floor(this.totalBeats); |
285 | 287 | return new Promise(resolve => { |
286 | 288 | const poll = () => { |
| 289 | + this.updateTime(); |
287 | 290 | if (Math.floor(this.totalBeats) > startBeat) { |
288 | 291 | resolve(); |
289 | 292 | } else { |
|
331 | 334 | const beatInMeasure = this.totalBeats % this.beatsPerMeasure; |
332 | 335 | return beatInMeasure < 0.5; |
333 | 336 | } |
| 337 | + |
| 338 | + onGreenFlag() { |
| 339 | + this.resetBeat(); |
| 340 | + if (this.autoStart) { |
| 341 | + this.startBeat(); |
| 342 | + } |
| 343 | + } |
| 344 | + |
| 345 | + onStopAll() { |
| 346 | + this.stopBeat(); |
| 347 | + this.resetBeat(); |
| 348 | + } |
334 | 349 | } |
335 | 350 |
|
336 | 351 | const extensionInstance = new BeatSync(); |
337 | | - |
338 | 352 | const runtime = Scratch.vm.runtime; |
339 | 353 |
|
340 | 354 | extensionInstance.setupVMEvents(); |
341 | 355 |
|
342 | | - const originalGreenFlag = runtime.greenFlag; |
343 | | - runtime.greenFlag = function() { |
344 | | - extensionInstance.resetBeat(); |
345 | | - if (extensionInstance.autoStart) { |
346 | | - extensionInstance.startBeat(); |
347 | | - } |
348 | | - return originalGreenFlag.apply(this, arguments); |
349 | | - }; |
350 | | - |
351 | | - const originalStopAll = runtime.stopAll; |
352 | | - runtime.stopAll = function() { |
353 | | - extensionInstance.stopBeat(); |
354 | | - extensionInstance.resetBeat(); |
355 | | - return originalStopAll.apply(this, arguments); |
356 | | - }; |
| 356 | + runtime.on('PROJECT_START', () => { |
| 357 | + extensionInstance.onGreenFlag(); |
| 358 | + }); |
| 359 | + |
| 360 | + runtime.on('PROJECT_STOP_ALL', () => { |
| 361 | + extensionInstance.onStopAll(); |
| 362 | + }); |
357 | 363 |
|
358 | 364 | Scratch.extensions.register(extensionInstance); |
359 | 365 | })(Scratch); |
0 commit comments