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;
|
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
|
|
|
}
|
|
|
|
|
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 {
|
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
|
|
|
|
};
|
|
|
|
}
|
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
|
|
|
|
},
|
2018-06-06 13:39:23 -04:00
|
|
|
event_whentouchingobject: {
|
|
|
|
restartExistingThreads: false,
|
|
|
|
edgeActivated: true
|
|
|
|
},
|
2018-03-20 09:50:57 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-06-09 11:45:58 -04:00
|
|
|
|
2018-06-06 13:39:23 -04:00
|
|
|
touchingObject (args, util) {
|
2018-06-06 16:34:58 -04:00
|
|
|
return util.target.touchingObject(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);
|
|
|
|
// @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-12-04 18:01:29 -05:00
|
|
|
const broadcastVar = util.runtime.getTargetForStage().lookupBroadcastMsg(
|
2017-11-28 22:33:27 -05:00
|
|
|
args.BROADCAST_OPTION.id, args.BROADCAST_OPTION.name);
|
2017-12-04 18:01:29 -05:00
|
|
|
if (broadcastVar) {
|
|
|
|
const broadcastOption = broadcastVar.name;
|
|
|
|
util.startHats('event_whenbroadcastreceived', {
|
|
|
|
BROADCAST_OPTION: broadcastOption
|
|
|
|
});
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-06-09 11:45:58 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
broadcastAndWait (args, util) {
|
2017-12-04 18:01:29 -05:00
|
|
|
const broadcastVar = util.runtime.getTargetForStage().lookupBroadcastMsg(
|
2017-11-28 22:33:27 -05:00
|
|
|
args.BROADCAST_OPTION.id, args.BROADCAST_OPTION.name);
|
2017-12-04 18:01:29 -05:00
|
|
|
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
|
|
|
}
|
2016-08-23 18:14:05 -04:00
|
|
|
}
|
2017-12-04 18:01:29 -05: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;
|