2017-04-17 15:10:04 -04:00
|
|
|
const Cast = require('../util/cast');
|
2016-09-12 13:14:16 -04:00
|
|
|
|
2017-04-17 15:10:04 -04:00
|
|
|
const Scratch3EventBlocks = function (runtime) {
|
2016-06-09 11:45:58 -04:00
|
|
|
/**
|
|
|
|
* The runtime instantiating this block package.
|
|
|
|
* @type {Runtime}
|
|
|
|
*/
|
|
|
|
this.runtime = runtime;
|
2016-10-23 17:55:31 -04:00
|
|
|
};
|
2016-06-09 11:45:58 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the block primitives implemented by this package.
|
2017-02-01 15:59:50 -05:00
|
|
|
* @return {object.<string, Function>} Mapping of opcode to Function.
|
2016-06-09 11:45:58 -04:00
|
|
|
*/
|
2016-10-23 12:41:45 -04:00
|
|
|
Scratch3EventBlocks.prototype.getPrimitives = function () {
|
2016-06-09 11:45:58 -04:00
|
|
|
return {
|
2016-10-24 11:02:19 -04:00
|
|
|
event_broadcast: this.broadcast,
|
|
|
|
event_broadcastandwait: this.broadcastAndWait,
|
|
|
|
event_whengreaterthan: this.hatGreaterThanPredicate
|
2016-06-09 11:45:58 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-08-23 18:14:05 -04:00
|
|
|
Scratch3EventBlocks.prototype.getHats = function () {
|
|
|
|
return {
|
2016-10-24 11:02:19 -04:00
|
|
|
event_whenflagclicked: {
|
2016-08-23 18:14:05 -04:00
|
|
|
restartExistingThreads: true
|
|
|
|
},
|
2016-10-24 11:02:19 -04:00
|
|
|
event_whenkeypressed: {
|
2016-08-23 18:14:05 -04:00
|
|
|
restartExistingThreads: false
|
|
|
|
},
|
2016-10-24 11:02:19 -04:00
|
|
|
event_whenthisspriteclicked: {
|
2016-08-23 18:14:05 -04:00
|
|
|
restartExistingThreads: true
|
|
|
|
},
|
2016-10-24 11:02:19 -04:00
|
|
|
event_whenbackdropswitchesto: {
|
2016-08-23 18:14:05 -04:00
|
|
|
restartExistingThreads: true
|
2016-09-08 09:40:27 -04:00
|
|
|
},
|
2016-10-24 11:02:19 -04:00
|
|
|
event_whengreaterthan: {
|
2016-08-23 18:14:05 -04:00
|
|
|
restartExistingThreads: false,
|
2016-08-29 10:26:26 -04:00
|
|
|
edgeActivated: true
|
2016-08-23 18:14:05 -04:00
|
|
|
},
|
2016-10-24 11:02:19 -04:00
|
|
|
event_whenbroadcastreceived: {
|
2016-08-23 18:14:05 -04:00
|
|
|
restartExistingThreads: true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2016-06-09 11:45:58 -04:00
|
|
|
|
2016-08-23 18:14:05 -04:00
|
|
|
Scratch3EventBlocks.prototype.hatGreaterThanPredicate = function (args, util) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const option = Cast.toString(args.WHENGREATERTHANMENU).toLowerCase();
|
|
|
|
const value = Cast.toNumber(args.VALUE);
|
2016-08-23 18:14:05 -04:00
|
|
|
// @todo: Other cases :)
|
2016-10-23 17:55:31 -04:00
|
|
|
if (option === 'timer') {
|
2016-09-12 13:14:16 -04:00
|
|
|
return util.ioQuery('clock', 'projectTimer') > value;
|
2016-08-23 18:14:05 -04:00
|
|
|
}
|
|
|
|
return false;
|
2016-06-09 11:45:58 -04:00
|
|
|
};
|
|
|
|
|
2016-10-23 12:41:45 -04:00
|
|
|
Scratch3EventBlocks.prototype.broadcast = function (args, util) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const broadcastOption = Cast.toString(args.BROADCAST_OPTION);
|
2016-08-29 10:26:26 -04:00
|
|
|
util.startHats('event_whenbroadcastreceived', {
|
2016-10-24 11:02:19 -04:00
|
|
|
BROADCAST_OPTION: broadcastOption
|
2016-08-23 18:14:05 -04:00
|
|
|
});
|
2016-06-09 11:45:58 -04:00
|
|
|
};
|
|
|
|
|
2016-08-23 18:14:05 -04:00
|
|
|
Scratch3EventBlocks.prototype.broadcastAndWait = function (args, util) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const broadcastOption = Cast.toString(args.BROADCAST_OPTION);
|
2016-08-29 10:26:26 -04:00
|
|
|
// Have we run before, starting threads?
|
|
|
|
if (!util.stackFrame.startedThreads) {
|
|
|
|
// No - start hats for this broadcast.
|
|
|
|
util.stackFrame.startedThreads = util.startHats(
|
2016-08-23 18:14:05 -04:00
|
|
|
'event_whenbroadcastreceived', {
|
2016-10-24 11:02:19 -04:00
|
|
|
BROADCAST_OPTION: broadcastOption
|
2016-08-23 18:14:05 -04:00
|
|
|
}
|
|
|
|
);
|
2016-10-23 17:55:31 -04:00
|
|
|
if (util.stackFrame.startedThreads.length === 0) {
|
2016-08-23 18:14:05 -04:00
|
|
|
// Nothing was started.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We've run before; check if the wait is still going on.
|
2017-04-17 15:10:04 -04:00
|
|
|
const instance = this;
|
|
|
|
const waiting = util.stackFrame.startedThreads.some(thread => instance.runtime.isActiveThread(thread));
|
2016-08-23 18:14:05 -04:00
|
|
|
if (waiting) {
|
2016-10-17 23:23:16 -04:00
|
|
|
util.yield();
|
2016-08-23 18:14:05 -04:00
|
|
|
}
|
2016-06-09 11:45:58 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Scratch3EventBlocks;
|