scratch-vm/src/blocks/scratch3_event.js

131 lines
4.4 KiB
JavaScript
Raw Normal View History

2017-04-17 15:10:04 -04:00
const Cast = require('../util/cast');
2017-04-17 19:42:48 -04:00
class Scratch3EventBlocks {
constructor (runtime) {
/**
* The runtime instantiating this block package.
* @type {Runtime}
*/
this.runtime = runtime;
2018-05-22 17:24:22 -04:00
this.runtime.on('KEY_PRESSED', key => {
this.runtime.startHats('event_whenkeypressed', {
KEY_OPTION: key
});
this.runtime.startHats('event_whenkeypressed', {
KEY_OPTION: 'any'
});
});
2017-04-17 19:42:48 -04:00
}
/**
2017-04-17 19:42:48 -04:00
* Retrieve the block primitives implemented by this package.
* @return {object.<string, Function>} Mapping of opcode to Function.
*/
2017-04-17 19:42:48 -04:00
getPrimitives () {
return {
2018-06-06 13:39:23 -04:00
event_whentouchingobject: this.touchingObject,
2017-04-17 19:42:48 -04:00
event_broadcast: this.broadcast,
event_broadcastandwait: this.broadcastAndWait,
event_whengreaterthan: this.hatGreaterThanPredicate
};
}
2017-04-17 19:42:48 -04:00
getHats () {
return {
event_whenflagclicked: {
restartExistingThreads: true
},
event_whenkeypressed: {
restartExistingThreads: false
},
event_whenthisspriteclicked: {
restartExistingThreads: true
},
2018-06-06 13:39:23 -04:00
event_whentouchingobject: {
restartExistingThreads: false,
edgeActivated: true
},
event_whenstageclicked: {
restartExistingThreads: true
},
2017-04-17 19:42:48 -04:00
event_whenbackdropswitchesto: {
restartExistingThreads: true
},
event_whengreaterthan: {
restartExistingThreads: false,
edgeActivated: true
},
event_whenbroadcastreceived: {
restartExistingThreads: true
}
};
}
2018-06-06 13:39:23 -04:00
touchingObject (args, util) {
return util.target.isTouchingObject(args.TOUCHINGOBJECTMENU);
2018-06-06 13:39:23 -04:00
}
2017-04-17 19:42:48 -04:00
hatGreaterThanPredicate (args, util) {
const option = Cast.toString(args.WHENGREATERTHANMENU).toLowerCase();
const value = Cast.toNumber(args.VALUE);
switch (option) {
case 'timer':
2017-04-17 19:42:48 -04:00
return util.ioQuery('clock', 'projectTimer') > value;
case 'loudness':
return this.runtime.audioEngine && this.runtime.audioEngine.getLoudness() > value;
}
2017-04-17 19:42:48 -04:00
return false;
}
2017-04-17 19:42:48 -04:00
broadcast (args, util) {
const broadcastVar = util.runtime.getTargetForStage().lookupBroadcastMsg(
args.BROADCAST_OPTION.id, args.BROADCAST_OPTION.name);
if (broadcastVar) {
const broadcastOption = broadcastVar.name;
util.startHats('event_whenbroadcastreceived', {
BROADCAST_OPTION: broadcastOption
});
}
2017-04-17 19:42:48 -04:00
}
2017-04-17 19:42:48 -04:00
broadcastAndWait (args, util) {
const broadcastVar = util.runtime.getTargetForStage().lookupBroadcastMsg(
args.BROADCAST_OPTION.id, args.BROADCAST_OPTION.name);
if (broadcastVar) {
const broadcastOption = broadcastVar.name;
// Have we run before, starting threads?
if (!util.stackFrame.startedThreads) {
// No - start hats for this broadcast.
util.stackFrame.startedThreads = util.startHats(
'event_whenbroadcastreceived', {
BROADCAST_OPTION: broadcastOption
}
);
if (util.stackFrame.startedThreads.length === 0) {
// Nothing was started.
return;
2017-04-17 19:42:48 -04:00
}
}
// We've run before; check if the wait is still going on.
const instance = this;
const waiting = util.stackFrame.startedThreads.some(thread => instance.runtime.isActiveThread(thread));
if (waiting) {
// If all threads are waiting for the next tick or later yield
// for a tick as well. Otherwise yield until the next loop of
// the threads.
if (
util.stackFrame.startedThreads
.every(thread => instance.runtime.isWaitingThread(thread))
) {
util.yieldTick();
} else {
util.yield();
}
}
}
}
2017-04-17 19:42:48 -04:00
}
module.exports = Scratch3EventBlocks;