| const Cast = require('../util/cast'); |
| const SandboxRunner = require('../util/sandboxed-javascript-runner.js'); |
|
|
| class Scratch3EventBlocks { |
| constructor (runtime) { |
| |
| |
| |
| |
| this.runtime = runtime; |
|
|
| this.runtime.on('KEY_PRESSED', key => { |
| this.runtime.startHats('event_whenkeypressed', { |
| KEY_OPTION: key |
| }); |
| this.runtime.startHats('event_whenkeypressed', { |
| KEY_OPTION: 'any' |
| }); |
| }); |
|
|
| this.runtime.on('KEY_HIT', key => { |
| this.runtime.startHats('event_whenkeyhit', { |
| KEY_OPTION: key |
| }); |
| this.runtime.startHats('event_whenkeyhit', { |
| KEY_OPTION: 'any' |
| }); |
| }); |
|
|
| this.isStarting = false; |
| this.stopEventNeedsInit = true; |
| this.runtime.on('PROJECT_START_BEFORE_RESET', () => { |
| |
| |
| this.isStarting = true; |
|
|
| |
| |
| if (this.stopEventNeedsInit) this.stopEventNeedsInit = false; |
| }); |
| this.runtime.on('PROJECT_STOP_ALL', () => { |
| |
| if (this.isStarting) { |
| this.isStarting = false; |
| return; |
| } |
|
|
| |
| if (this.stopEventNeedsInit) return; |
|
|
| |
| |
| this.runtime.once('RUNTIME_STEP_START', () => { |
| this.runtime.startHats('event_whenstopclicked'); |
| }) |
| this.isStarting = false; |
| }); |
| this.runtime.on('RUNTIME_STEP_START', () => { |
| this.runtime.startHats('event_always'); |
| }); |
|
|
| this.runtime.on("AFTER_EXECUTE", () => { |
| |
| setTimeout(() => { |
| const stage = this.runtime.getTargetForStage(); |
| if (!stage) return; |
| const stageVars = stage.variables; |
| for (const key in stageVars) { |
| if (stageVars[key].isSent !== undefined) stageVars[key].isSent = false; |
| } |
| }, 10); |
| }); |
| } |
|
|
| |
| |
| |
| |
| getPrimitives () { |
| return { |
| event_whenanything: this.whenanything, |
| event_whenjavascript: this.whenjavascript, |
| event_whentouchingobject: this.touchingObject, |
| event_broadcast: this.broadcast, |
| event_broadcastandwait: this.broadcastAndWait, |
| event_whengreaterthan: this.hatGreaterThanPredicate |
| }; |
| } |
|
|
| whenanything (args) { |
| return Cast.toBoolean(args.ANYTHING); |
| } |
|
|
| whenjavascript (args) { |
| return new Promise((resolve) => { |
| const js = Cast.toString(args.JS); |
| SandboxRunner.execute(js).then(result => { |
| resolve(result.value === true); |
| }) |
| }) |
| } |
|
|
| getHats () { |
| return { |
| event_whenflagclicked: { |
| restartExistingThreads: true |
| }, |
| event_whenstopclicked: { |
| restartExistingThreads: true |
| }, |
| event_always: { |
| restartExistingThreads: false |
| }, |
| event_whenkeypressed: { |
| restartExistingThreads: false |
| }, |
| event_whenkeyhit: { |
| restartExistingThreads: false |
| }, |
| event_whenmousescrolled: { |
| restartExistingThreads: false |
| }, |
| event_whenanything: { |
| restartExistingThreads: false, |
| edgeActivated: true |
| }, |
| event_whenjavascript: { |
| restartExistingThreads: false, |
| edgeActivated: true |
| }, |
| event_whenthisspriteclicked: { |
| restartExistingThreads: true |
| }, |
| event_whentouchingobject: { |
| restartExistingThreads: false, |
| edgeActivated: true |
| }, |
| event_whenstageclicked: { |
| restartExistingThreads: true |
| }, |
| event_whenbackdropswitchesto: { |
| restartExistingThreads: true |
| }, |
| event_whengreaterthan: { |
| restartExistingThreads: false, |
| edgeActivated: true |
| }, |
| event_whenbroadcastreceived: { |
| restartExistingThreads: true |
| } |
| }; |
| } |
|
|
| touchingObject (args, util) { |
| return util.target.isTouchingObject(args.TOUCHINGOBJECTMENU); |
| } |
|
|
| hatGreaterThanPredicate (args, util) { |
| const option = Cast.toString(args.WHENGREATERTHANMENU).toLowerCase(); |
| const value = Cast.toNumber(args.VALUE); |
| switch (option) { |
| case 'timer': |
| return util.ioQuery('clock', 'projectTimer') > value; |
| case 'loudness': |
| return this.runtime.audioEngine && this.runtime.audioEngine.getLoudness() > value; |
| } |
| return false; |
| } |
|
|
| broadcast (args, util) { |
| const broadcastVar = util.runtime.getTargetForStage().lookupBroadcastMsg( |
| args.BROADCAST_OPTION.id, args.BROADCAST_OPTION.name); |
| if (broadcastVar) { |
| const broadcastOption = broadcastVar.name; |
| broadcastVar.isSent = true; |
| util.startHats('event_whenbroadcastreceived', { |
| BROADCAST_OPTION: broadcastOption |
| }); |
| } |
| } |
|
|
| broadcastAndWait (args, util) { |
| if (!util.stackFrame.broadcastVar) { |
| util.stackFrame.broadcastVar = util.runtime.getTargetForStage().lookupBroadcastMsg( |
| args.BROADCAST_OPTION.id, args.BROADCAST_OPTION.name); |
| } |
| if (util.stackFrame.broadcastVar) { |
| const broadcastOption = util.stackFrame.broadcastVar.name; |
| |
| if (!util.stackFrame.startedThreads) { |
| broadcastVar.isSent = true; |
| |
| util.stackFrame.startedThreads = util.startHats( |
| 'event_whenbroadcastreceived', { |
| BROADCAST_OPTION: broadcastOption |
| } |
| ); |
| if (util.stackFrame.startedThreads.length === 0) { |
| |
| return; |
| } |
| } |
| |
| const instance = this; |
| |
| |
| |
| |
| const waiting = util.stackFrame.startedThreads |
| .some(thread => instance.runtime.threads.indexOf(thread) !== -1); |
| if (waiting) { |
| |
| |
| |
| if ( |
| util.stackFrame.startedThreads |
| .every(thread => instance.runtime.isWaitingThread(thread)) |
| ) { |
| util.yieldTick(); |
| } else { |
| util.yield(); |
| } |
| } |
| } |
| } |
| } |
|
|
| module.exports = Scratch3EventBlocks; |
|
|