2017-04-17 15:10:04 -04:00
|
|
|
const Cast = require('../util/cast');
|
2016-09-12 13:14:16 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
class Scratch3EventBlocks {
|
|
|
|
constructor (runtime) {
|
|
|
|
/**
|
|
|
|
* The runtime instantiating this block package.
|
|
|
|
* @type {Runtime}
|
|
|
|
*/
|
|
|
|
this.runtime = runtime;
|
|
|
|
}
|
|
|
|
|
2016-06-09 11:45:58 -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.
|
2016-06-09 11:45:58 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
getPrimitives () {
|
|
|
|
return {
|
|
|
|
event_broadcast: this.broadcast,
|
|
|
|
event_broadcastandwait: this.broadcastAndWait,
|
|
|
|
event_whengreaterthan: this.hatGreaterThanPredicate
|
|
|
|
};
|
|
|
|
}
|
2016-06-09 11:45:58 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
getHats () {
|
|
|
|
return {
|
|
|
|
event_whenflagclicked: {
|
|
|
|
restartExistingThreads: true
|
|
|
|
},
|
|
|
|
event_whenkeypressed: {
|
|
|
|
restartExistingThreads: false
|
|
|
|
},
|
|
|
|
event_whenthisspriteclicked: {
|
|
|
|
restartExistingThreads: true
|
|
|
|
},
|
|
|
|
event_whenbackdropswitchesto: {
|
|
|
|
restartExistingThreads: true
|
|
|
|
},
|
|
|
|
event_whengreaterthan: {
|
|
|
|
restartExistingThreads: false,
|
|
|
|
edgeActivated: true
|
|
|
|
},
|
|
|
|
event_whenbroadcastreceived: {
|
|
|
|
restartExistingThreads: true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-06-09 11:45:58 -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);
|
|
|
|
// @todo: Other cases :)
|
|
|
|
if (option === 'timer') {
|
|
|
|
return util.ioQuery('clock', 'projectTimer') > value;
|
2016-08-23 18:14:05 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
return false;
|
2016-08-23 18:14:05 -04:00
|
|
|
}
|
2016-06-09 11:45:58 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
broadcast (args, util) {
|
2017-11-21 16:48:48 -05:00
|
|
|
// TODO KARISHMA adding lookupOrCreate here temporarily (LLK/scratch-blocks#1258)
|
|
|
|
util.target.lookupOrCreateBroadcastMsg(args.BROADCAST_OPTION.id,
|
|
|
|
args.BROADCAST_OPTION.name);
|
|
|
|
const broadcastOption = Cast.toString(args.BROADCAST_OPTION.name);
|
2017-04-17 19:42:48 -04:00
|
|
|
util.startHats('event_whenbroadcastreceived', {
|
|
|
|
BROADCAST_OPTION: broadcastOption
|
|
|
|
});
|
|
|
|
}
|
2016-06-09 11:45:58 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
broadcastAndWait (args, util) {
|
2017-11-21 16:48:48 -05:00
|
|
|
// TODO KARISHMA adding lookupOrCreate here temporarily (LLK/scratch-blocks#1258)
|
|
|
|
util.target.lookupOrCreateBroadcastMsg(args.BROADCAST_OPTION.id,
|
|
|
|
args.BROADCAST_OPTION.name);
|
|
|
|
const broadcastOption = Cast.toString(args.BROADCAST_OPTION.name);
|
2017-04-17 19:42:48 -04:00
|
|
|
// 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;
|
2016-08-23 18:14:05 -04:00
|
|
|
}
|
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) {
|
|
|
|
util.yield();
|
2016-08-23 18:14:05 -04:00
|
|
|
}
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-06-09 11:45:58 -04:00
|
|
|
|
|
|
|
module.exports = Scratch3EventBlocks;
|