Update src/blocks/scratch3_event.js
Browse files- src/blocks/scratch3_event.js +171 -603
src/blocks/scratch3_event.js
CHANGED
@@ -1,10 +1,7 @@
|
|
1 |
const Cast = require('../util/cast');
|
2 |
-
const
|
3 |
-
const MathUtil = require('../util/math-util');
|
4 |
-
const getMonitorIdForBlockWithArgs = require('../util/get-monitor-id');
|
5 |
-
const { validateRegex } = require('../util/json-block-utilities');
|
6 |
|
7 |
-
class
|
8 |
constructor (runtime) {
|
9 |
/**
|
10 |
* The runtime instantiating this block package.
|
@@ -12,47 +9,67 @@ class Scratch3SensingBlocks {
|
|
12 |
*/
|
13 |
this.runtime = runtime;
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
this.
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
* @type {!Array}
|
42 |
-
*/
|
43 |
-
this._loudnessList = [];
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
this.runtime.on(
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
}
|
57 |
|
58 |
/**
|
@@ -61,600 +78,151 @@ class Scratch3SensingBlocks {
|
|
61 |
*/
|
62 |
getPrimitives () {
|
63 |
return {
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
sensing_timer: this.getTimer,
|
71 |
-
sensing_resettimer: this.resetTimer,
|
72 |
-
sensing_of: this.getAttributeOf,
|
73 |
-
sensing_mousex: this.getMouseX,
|
74 |
-
sensing_mousey: this.getMouseY,
|
75 |
-
sensing_setdragmode: this.setDragMode,
|
76 |
-
sensing_mousedown: this.getMouseDown,
|
77 |
-
sensing_keypressed: this.getKeyPressed,
|
78 |
-
sensing_current: this.current,
|
79 |
-
sensing_dayssince2000: this.daysSince2000,
|
80 |
-
sensing_loudness: this.getLoudness,
|
81 |
-
sensing_loud: this.isLoud,
|
82 |
-
sensing_askandwait: this.askAndWait,
|
83 |
-
sensing_answer: this.getAnswer,
|
84 |
-
sensing_username: this.getUsername,
|
85 |
-
sensing_loggedin: this.getLoggedIn,
|
86 |
-
sensing_userid: () => {}, // legacy no-op block
|
87 |
-
sensing_regextest: this.regextest,
|
88 |
-
sensing_thing_is_number: this.thing_is_number,
|
89 |
-
sensing_thing_has_number: this.thing_has_number,
|
90 |
-
sensing_mobile: this.mobile,
|
91 |
-
sensing_thing_is_text: this.thing_is_text,
|
92 |
-
sensing_getspritewithattrib: this.getspritewithattrib,
|
93 |
-
sensing_directionTo: this.getDirectionToFrom,
|
94 |
-
sensing_distanceTo: this.getDistanceToFrom,
|
95 |
-
sensing_isUpperCase: this.isCharecterUppercase,
|
96 |
-
sensing_mouseclicked: this.mouseClicked,
|
97 |
-
sensing_keyhit: this.keyHit,
|
98 |
-
sensing_mousescrolling: this.mouseScrolling,
|
99 |
-
sensing_fingerdown: this.fingerDown,
|
100 |
-
sensing_fingertapped: this.fingerTapped,
|
101 |
-
sensing_fingerx: this.getFingerX,
|
102 |
-
sensing_fingery: this.getFingerY,
|
103 |
-
sensing_setclipboard: this.setClipboard,
|
104 |
-
sensing_getclipboard: this.getClipboard,
|
105 |
-
sensing_getdragmode: this.getDragMode,
|
106 |
-
sensing_getoperatingsystem: this.getOS,
|
107 |
-
sensing_getbrowser: this.getBrowser,
|
108 |
-
sensing_geturl: this.getUrl,
|
109 |
-
sensing_getxyoftouchingsprite: this.getXYOfTouchingSprite
|
110 |
};
|
111 |
}
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
const agent = navigator.userAgent;
|
116 |
-
if (agent.includes('Windows')) {
|
117 |
-
return 'Windows';
|
118 |
-
}
|
119 |
-
if (agent.includes('Android')) {
|
120 |
-
return 'Android';
|
121 |
-
}
|
122 |
-
if (agent.includes('iPad') || agent.includes('iPod') || agent.includes('iPhone')) {
|
123 |
-
return 'iOS';
|
124 |
-
}
|
125 |
-
if (agent.includes('Linux')) {
|
126 |
-
return 'Linux';
|
127 |
-
}
|
128 |
-
if (agent.includes('CrOS')) {
|
129 |
-
return 'ChromeOS';
|
130 |
-
}
|
131 |
-
if (agent.includes('Mac OS')) {
|
132 |
-
return 'MacOS';
|
133 |
-
}
|
134 |
-
return 'Unknown';
|
135 |
-
}
|
136 |
-
getBrowser () {
|
137 |
-
if (!('userAgent' in navigator)) return 'Unknown';
|
138 |
-
const agent = navigator.userAgent;
|
139 |
-
if ('userAgentData' in navigator) {
|
140 |
-
const agentData = JSON.stringify(navigator.userAgentData.brands);
|
141 |
-
if (agentData.includes('Google Chrome')) {
|
142 |
-
return 'Chrome';
|
143 |
-
}
|
144 |
-
if (agentData.includes('Opera')) {
|
145 |
-
return 'Opera';
|
146 |
-
}
|
147 |
-
if (agentData.includes('Microsoft Edge')) {
|
148 |
-
return 'Edge';
|
149 |
-
}
|
150 |
-
}
|
151 |
-
if (agent.includes('Chrome')) {
|
152 |
-
return 'Chrome';
|
153 |
-
}
|
154 |
-
if (agent.includes('Firefox')) {
|
155 |
-
return 'Firefox';
|
156 |
-
}
|
157 |
-
// PenguinMod cannot be loaded in IE 11 (the last supported version)
|
158 |
-
// if (agent.includes('MSIE') || agent.includes('rv:')) {
|
159 |
-
// return 'Internet Explorer';
|
160 |
-
// }
|
161 |
-
if (agent.includes('Safari')) {
|
162 |
-
return 'Safari';
|
163 |
-
}
|
164 |
-
return 'Unknown';
|
165 |
-
}
|
166 |
-
getUrl () {
|
167 |
-
if (!('href' in location)) return '';
|
168 |
-
return location.href;
|
169 |
-
}
|
170 |
-
|
171 |
-
setClipboard (args) {
|
172 |
-
const text = Cast.toString(args.ITEM);
|
173 |
-
if (!navigator) return;
|
174 |
-
if (('clipboard' in navigator) && ('writeText' in navigator.clipboard)) {
|
175 |
-
navigator.clipboard.writeText(text);
|
176 |
-
}
|
177 |
-
}
|
178 |
-
getClipboard () {
|
179 |
-
if (!navigator) return '';
|
180 |
-
if (('clipboard' in navigator) && ('readText' in navigator.clipboard)) {
|
181 |
-
return navigator.clipboard.readText();
|
182 |
-
} else {
|
183 |
-
return '';
|
184 |
-
}
|
185 |
}
|
186 |
|
187 |
-
|
188 |
-
return
|
|
|
|
|
|
|
|
|
|
|
189 |
}
|
190 |
|
191 |
-
|
192 |
-
return util.ioQuery('mouse', 'getIsClicked');
|
193 |
-
}
|
194 |
-
keyHit (args, util) {
|
195 |
-
return util.ioQuery('keyboard', 'getKeyIsHit', [args.KEY_OPTION]);
|
196 |
-
}
|
197 |
-
mouseScrolling (args, util) {
|
198 |
-
const delta = util.ioQuery('mouseWheel', 'getScrollDelta');
|
199 |
-
const option = args.SCROLL_OPTION;
|
200 |
-
switch (option) {
|
201 |
-
case "up":
|
202 |
-
return delta < 0;
|
203 |
-
case "down":
|
204 |
-
return delta > 0;
|
205 |
-
default:
|
206 |
-
return false;
|
207 |
-
}
|
208 |
-
}
|
209 |
-
|
210 |
-
isCharecterUppercase (args) {
|
211 |
-
return (/[A-Z]/g).test(args.text);
|
212 |
-
}
|
213 |
-
|
214 |
-
getDirectionToFrom (args) {
|
215 |
-
const dx = args.x2 - args.x1;
|
216 |
-
const dy = args.y2 - args.y1;
|
217 |
-
const direction = MathUtil.wrapClamp(90 - MathUtil.radToDeg(Math.atan2(dy, dx)), -179, 180);
|
218 |
-
return direction;
|
219 |
-
}
|
220 |
-
|
221 |
-
getDistanceToFrom (args) {
|
222 |
-
const dx = args.x2 - args.x1;
|
223 |
-
const dy = args.y2 - args.y1;
|
224 |
-
return Math.sqrt((dx * dx) + (dy * dy));
|
225 |
-
}
|
226 |
-
|
227 |
-
getspritewithattrib (args, util) {
|
228 |
-
// strip out usless data
|
229 |
-
const sprites = util.runtime.targets.map(x => ({
|
230 |
-
id: x.id,
|
231 |
-
name: x.sprite ? x.sprite.name : "Unknown",
|
232 |
-
variables: Object.values(x.variables).reduce((obj, value) => {
|
233 |
-
if (!value.name) return obj;
|
234 |
-
obj[value.name] = String(value.value);
|
235 |
-
return obj;
|
236 |
-
}, {})
|
237 |
-
}));
|
238 |
-
// get the target with variable x set to y
|
239 |
-
let res = "No sprites found";
|
240 |
-
for (
|
241 |
-
// define the index and the sprite
|
242 |
-
let idx = 1, sprite = sprites[0];
|
243 |
-
// standard for loop thing
|
244 |
-
idx < sprites.length;
|
245 |
-
// set sprite to a new item
|
246 |
-
sprite = sprites[idx++]
|
247 |
-
) {
|
248 |
-
if (sprite.variables[args.var] === args.val) {
|
249 |
-
res = `{"id": "${sprite.id}", "name": "${sprite.name}"}`;
|
250 |
-
break;
|
251 |
-
}
|
252 |
-
}
|
253 |
-
|
254 |
-
return res;
|
255 |
-
}
|
256 |
-
thing_is_number (args) {
|
257 |
-
// i hate js
|
258 |
-
// i also hate regex
|
259 |
-
// so im gonna do this the lazy way
|
260 |
-
// no. String(Number(value)) === value does infact do the job X)
|
261 |
-
// also what was originaly here was inificiant as hell
|
262 |
-
|
263 |
-
// jg: why dont you literally just do what "is text" did but the opposite
|
264 |
-
// except also account for numbers that end with . (that aint a number)
|
265 |
-
if (Cast.toString(args.TEXT1).trim().endsWith(".")) {
|
266 |
-
return false;
|
267 |
-
}
|
268 |
-
return !this.thing_is_text(args);
|
269 |
-
}
|
270 |
-
thing_is_text (args) {
|
271 |
-
// WHY IS NAN NOT EQUAL TO ITSELF
|
272 |
-
// HOW IS NAN A NUMBER
|
273 |
-
// because nan is how numbers say the value put into me is not a number
|
274 |
-
return isNaN(Number(args.TEXT1));
|
275 |
-
}
|
276 |
-
|
277 |
-
thing_has_number(args) {
|
278 |
-
return /\d/.test(Cast.toString(args.TEXT1));
|
279 |
-
}
|
280 |
-
|
281 |
-
mobile () {
|
282 |
-
return typeof window !== 'undefined' && 'ontouchstart' in window;
|
283 |
-
}
|
284 |
-
|
285 |
-
regextest (args) {
|
286 |
-
if (!validateRegex(args.reg, args.regrule)) return false;
|
287 |
-
const regex = new RegExp(args.reg, args.regrule);
|
288 |
-
return regex.test(args.text);
|
289 |
-
}
|
290 |
-
|
291 |
-
getMonitored () {
|
292 |
return {
|
293 |
-
|
294 |
-
|
295 |
},
|
296 |
-
|
297 |
-
|
298 |
},
|
299 |
-
|
300 |
-
|
301 |
},
|
302 |
-
|
303 |
-
|
304 |
},
|
305 |
-
|
306 |
-
|
307 |
},
|
308 |
-
|
309 |
-
|
310 |
},
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
},
|
315 |
-
|
316 |
-
|
|
|
317 |
},
|
318 |
-
|
319 |
-
|
320 |
},
|
321 |
-
|
322 |
-
|
|
|
323 |
},
|
324 |
-
|
325 |
-
|
326 |
},
|
327 |
-
|
328 |
-
|
329 |
-
// importing multiple monitors from the same opcode from sb2 files,
|
330 |
-
// something that is not currently supported in scratch 3.
|
331 |
-
getId: (_, fields) => getMonitorIdForBlockWithArgs('current', fields) // _${param}`
|
332 |
},
|
333 |
-
|
334 |
-
|
|
|
335 |
},
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
_onAnswer (answer) {
|
340 |
-
this._answer = answer;
|
341 |
-
const questionObj = this._questionList.shift();
|
342 |
-
if (questionObj) {
|
343 |
-
const [_question, resolve, target, wasVisible, wasStage] = questionObj;
|
344 |
-
// If the target was visible when asked, hide the say bubble unless the target was the stage.
|
345 |
-
if (wasVisible && !wasStage) {
|
346 |
-
this.runtime.emit('SAY', target, 'say', '');
|
347 |
-
}
|
348 |
-
resolve();
|
349 |
-
this._askNextQuestion();
|
350 |
-
}
|
351 |
-
}
|
352 |
-
|
353 |
-
_resetAnswer () {
|
354 |
-
this._answer = '';
|
355 |
-
}
|
356 |
-
|
357 |
-
_enqueueAsk (question, resolve, target, wasVisible, wasStage) {
|
358 |
-
this._questionList.push([question, resolve, target, wasVisible, wasStage]);
|
359 |
-
}
|
360 |
-
|
361 |
-
_askNextQuestion () {
|
362 |
-
if (this._questionList.length > 0) {
|
363 |
-
const [question, _resolve, target, wasVisible, wasStage] = this._questionList[0];
|
364 |
-
// If the target is visible, emit a blank question and use the
|
365 |
-
// say event to trigger a bubble unless the target was the stage.
|
366 |
-
if (wasVisible && !wasStage) {
|
367 |
-
this.runtime.emit('SAY', target, 'say', question);
|
368 |
-
this.runtime.emit('QUESTION', '');
|
369 |
-
} else {
|
370 |
-
this.runtime.emit('QUESTION', question);
|
371 |
}
|
372 |
-
}
|
373 |
-
}
|
374 |
-
|
375 |
-
_clearAllQuestions () {
|
376 |
-
this._questionList = [];
|
377 |
-
this.runtime.emit('QUESTION', null);
|
378 |
-
}
|
379 |
-
|
380 |
-
_clearTargetQuestions (stopTarget) {
|
381 |
-
const currentlyAsking = this._questionList.length > 0 && this._questionList[0][2] === stopTarget;
|
382 |
-
this._questionList = this._questionList.filter(question => (
|
383 |
-
question[2] !== stopTarget
|
384 |
-
));
|
385 |
-
|
386 |
-
if (currentlyAsking) {
|
387 |
-
this.runtime.emit('SAY', stopTarget, 'say', '');
|
388 |
-
if (this._questionList.length > 0) {
|
389 |
-
this._askNextQuestion();
|
390 |
-
} else {
|
391 |
-
this.runtime.emit('QUESTION', null);
|
392 |
-
}
|
393 |
-
}
|
394 |
-
}
|
395 |
-
|
396 |
-
askAndWait (args, util) {
|
397 |
-
const _target = util.target;
|
398 |
-
return new Promise(resolve => {
|
399 |
-
const isQuestionAsked = this._questionList.length > 0;
|
400 |
-
this._enqueueAsk(String(args.QUESTION), resolve, _target, _target.visible, _target.isStage);
|
401 |
-
if (!isQuestionAsked) {
|
402 |
-
this._askNextQuestion();
|
403 |
-
}
|
404 |
-
});
|
405 |
-
}
|
406 |
-
|
407 |
-
getAnswer () {
|
408 |
-
return this._answer;
|
409 |
-
}
|
410 |
-
|
411 |
-
objectTouchingObject (args, util) {
|
412 |
-
const object1 = (args.FULLTOUCHINGOBJECTMENU) === "_myself_" ? util.target.getName() : args.FULLTOUCHINGOBJECTMENU;
|
413 |
-
const object2 = args.SPRITETOUCHINGOBJECTMENU;
|
414 |
-
if (object2 === "_myself_") {
|
415 |
-
return util.target.isTouchingObject(object1);
|
416 |
-
}
|
417 |
-
const target = this.runtime.getSpriteTargetByName(object2);
|
418 |
-
if (!target) return false;
|
419 |
-
return target.isTouchingObject(object1);
|
420 |
-
}
|
421 |
-
objectTouchingCloneOfSprite (args, util) {
|
422 |
-
const object1 = args.FULLTOUCHINGOBJECTMENU;
|
423 |
-
let object2 = args.SPRITETOUCHINGOBJECTMENU;
|
424 |
-
if (object2 === "_myself_") {
|
425 |
-
object2 = util.target.getName();
|
426 |
-
}
|
427 |
-
if (object1 === "_myself_") {
|
428 |
-
return util.target.isTouchingObject(object2, true);
|
429 |
-
}
|
430 |
-
|
431 |
-
const target = this.runtime.getSpriteTargetByName(object2);
|
432 |
-
if (!target) return false;
|
433 |
-
if (object1 === "_mouse_") {
|
434 |
-
if (!this.runtime.ioDevices.mouse) return false;
|
435 |
-
const mouseX = this.runtime.ioDevices.mouse.getClientX();
|
436 |
-
const mouseY = this.runtime.ioDevices.mouse.getClientY();
|
437 |
-
const clones = target.sprite.clones.filter(clone => !clone.isOriginal && clone.isTouchingPoint(mouseX, mouseY));
|
438 |
-
return clones.length > 0;
|
439 |
-
} else if (object1 === '_edge_') {
|
440 |
-
const clones = target.sprite.clones.filter(clone => !clone.isOriginal && clone.isTouchingEdge());
|
441 |
-
return clones.length > 0;
|
442 |
-
}
|
443 |
-
|
444 |
-
const originalSprite = this.runtime.getSpriteTargetByName(object1);
|
445 |
-
if (!originalSprite) return false;
|
446 |
-
return originalSprite.isTouchingObject(object2, true);
|
447 |
}
|
448 |
|
449 |
touchingObject (args, util) {
|
450 |
return util.target.isTouchingObject(args.TOUCHINGOBJECTMENU);
|
451 |
}
|
452 |
|
453 |
-
|
454 |
-
const
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
|
462 |
-
|
463 |
-
|
464 |
-
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
const dx = util.target.x - targetX;
|
499 |
-
const dy = util.target.y - targetY;
|
500 |
-
return Math.sqrt((dx * dx) + (dy * dy));
|
501 |
-
}
|
502 |
-
|
503 |
-
setDragMode (args, util) {
|
504 |
-
util.target.setDraggable(args.DRAG_MODE === 'draggable');
|
505 |
-
}
|
506 |
-
|
507 |
-
getTimer (args, util) {
|
508 |
-
return util.ioQuery('clock', 'projectTimer');
|
509 |
-
}
|
510 |
-
|
511 |
-
resetTimer (args, util) {
|
512 |
-
util.ioQuery('clock', 'resetProjectTimer');
|
513 |
-
}
|
514 |
-
|
515 |
-
getMouseX (args, util) {
|
516 |
-
return util.ioQuery('mouse', 'getScratchX');
|
517 |
-
}
|
518 |
-
|
519 |
-
getMouseY (args, util) {
|
520 |
-
return util.ioQuery('mouse', 'getScratchY');
|
521 |
-
}
|
522 |
-
|
523 |
-
getMouseDown (args, util) {
|
524 |
-
return util.ioQuery('mouse', 'getIsDown');
|
525 |
-
}
|
526 |
-
|
527 |
-
getFingerX (args, util) {
|
528 |
-
return util.ioQuery('touch', 'getScratchX', [Cast.toNumber(args.FINGER_OPTION) - 1]);
|
529 |
-
}
|
530 |
-
|
531 |
-
getFingerY (args, util) {
|
532 |
-
return util.ioQuery('touch', 'getScratchY', [Cast.toNumber(args.FINGER_OPTION) - 1]);
|
533 |
-
}
|
534 |
-
|
535 |
-
fingerDown (args, util) {
|
536 |
-
return util.ioQuery('touch', 'getIsDown', [Cast.toNumber(args.FINGER_OPTION) - 1]);
|
537 |
-
}
|
538 |
-
|
539 |
-
fingerTapped (args, util) {
|
540 |
-
return util.ioQuery('touch', 'getIsTapped', [Cast.toNumber(args.FINGER_OPTION) - 1]);
|
541 |
-
}
|
542 |
-
|
543 |
-
current (args) {
|
544 |
-
const menuOption = Cast.toString(args.CURRENTMENU).toLowerCase();
|
545 |
-
const date = new Date();
|
546 |
-
switch (menuOption) {
|
547 |
-
case 'year': return date.getFullYear();
|
548 |
-
case 'month': return date.getMonth() + 1; // getMonth is zero-based
|
549 |
-
case 'date': return date.getDate();
|
550 |
-
case 'dayofweek': return date.getDay() + 1; // getDay is zero-based, Sun=0
|
551 |
-
case 'hour': return date.getHours();
|
552 |
-
case 'minute': return date.getMinutes();
|
553 |
-
case 'second': return date.getSeconds();
|
554 |
-
case 'timestamp': return Date.now();
|
555 |
-
}
|
556 |
-
return 0;
|
557 |
-
}
|
558 |
-
|
559 |
-
getKeyPressed (args, util) {
|
560 |
-
return util.ioQuery('keyboard', 'getKeyIsDown', [args.KEY_OPTION]);
|
561 |
-
}
|
562 |
-
|
563 |
-
daysSince2000 () {
|
564 |
-
const msPerDay = 24 * 60 * 60 * 1000;
|
565 |
-
const start = new Date(2000, 0, 1); // Months are 0-indexed.
|
566 |
-
const today = new Date();
|
567 |
-
const dstAdjust = today.getTimezoneOffset() - start.getTimezoneOffset();
|
568 |
-
let mSecsSinceStart = today.valueOf() - start.valueOf();
|
569 |
-
mSecsSinceStart += ((today.getTimezoneOffset() - dstAdjust) * 60 * 1000);
|
570 |
-
return mSecsSinceStart / msPerDay;
|
571 |
-
}
|
572 |
-
|
573 |
-
getLoudness () {
|
574 |
-
if (typeof this.runtime.audioEngine === 'undefined') return -1;
|
575 |
-
if (this.runtime.currentStepTime === null) return -1;
|
576 |
-
|
577 |
-
// Only measure loudness once per step
|
578 |
-
const timeSinceLoudness = this._timer.time() - this._cachedLoudnessTimestamp;
|
579 |
-
if (timeSinceLoudness < this.runtime.currentStepTime) {
|
580 |
-
return this._cachedLoudness;
|
581 |
-
}
|
582 |
-
|
583 |
-
this._cachedLoudnessTimestamp = this._timer.time();
|
584 |
-
this._cachedLoudness = this.runtime.audioEngine.getLoudness();
|
585 |
-
this.pushLoudness(this._cachedLoudness);
|
586 |
-
return this._cachedLoudness;
|
587 |
-
}
|
588 |
-
|
589 |
-
isLoud () {
|
590 |
-
this.pushLoudness();
|
591 |
-
let sum = this._loudnessList.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
|
592 |
-
sum /= this._loudnessList.length;
|
593 |
-
return this.getLoudness() > sum + 15;
|
594 |
-
}
|
595 |
-
pushLoudness (value) {
|
596 |
-
if (this._loudnessList.length >= 30) this._loudnessList.shift(); // remove first item
|
597 |
-
this._loudnessList.push(value ?? this.getLoudness());
|
598 |
-
}
|
599 |
-
|
600 |
-
getAttributeOf (args) {
|
601 |
-
let attrTarget;
|
602 |
-
|
603 |
-
if (args.OBJECT === '_stage_') {
|
604 |
-
attrTarget = this.runtime.getTargetForStage();
|
605 |
-
} else {
|
606 |
-
args.OBJECT = Cast.toString(args.OBJECT);
|
607 |
-
attrTarget = this.runtime.getSpriteTargetByName(args.OBJECT);
|
608 |
-
}
|
609 |
-
|
610 |
-
// attrTarget can be undefined if the target does not exist
|
611 |
-
// (e.g. single sprite uploaded from larger project referencing
|
612 |
-
// another sprite that wasn't uploaded)
|
613 |
-
if (!attrTarget) return 0;
|
614 |
-
|
615 |
-
// Generic attributes
|
616 |
-
if (attrTarget.isStage) {
|
617 |
-
switch (args.PROPERTY) {
|
618 |
-
// Scratch 1.4 support
|
619 |
-
case 'background #': return attrTarget.currentCostume + 1;
|
620 |
-
|
621 |
-
case 'backdrop #': return attrTarget.currentCostume + 1;
|
622 |
-
case 'backdrop name':
|
623 |
-
return attrTarget.getCostumes()[attrTarget.currentCostume].name;
|
624 |
-
case 'volume': return attrTarget.volume;
|
625 |
}
|
626 |
-
|
627 |
-
|
628 |
-
|
629 |
-
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
-
|
634 |
-
|
635 |
-
|
636 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
637 |
}
|
638 |
}
|
639 |
-
|
640 |
-
// Target variables.
|
641 |
-
const varName = args.PROPERTY;
|
642 |
-
const variable = attrTarget.lookupVariableByNameAndType(varName, '', true);
|
643 |
-
if (variable) {
|
644 |
-
return variable.value;
|
645 |
-
}
|
646 |
-
|
647 |
-
// Otherwise, 0
|
648 |
-
return 0;
|
649 |
-
}
|
650 |
-
|
651 |
-
getUsername (args, util) {
|
652 |
-
return util.ioQuery('userData', 'getUsername');
|
653 |
-
}
|
654 |
-
|
655 |
-
getLoggedIn(args, util) {
|
656 |
-
return util.ioQuery('userData', 'getLoggedIn');
|
657 |
}
|
658 |
}
|
659 |
|
660 |
-
module.exports =
|
|
|
1 |
const Cast = require('../util/cast');
|
2 |
+
const SandboxRunner = require('../util/sandboxed-javascript-runner.js');
|
|
|
|
|
|
|
3 |
|
4 |
+
class Scratch3EventBlocks {
|
5 |
constructor (runtime) {
|
6 |
/**
|
7 |
* The runtime instantiating this block package.
|
|
|
9 |
*/
|
10 |
this.runtime = runtime;
|
11 |
|
12 |
+
this.runtime.on('KEY_PRESSED', key => {
|
13 |
+
this.runtime.startHats('event_whenkeypressed', {
|
14 |
+
KEY_OPTION: key
|
15 |
+
});
|
16 |
+
this.runtime.startHats('event_whenkeypressed', {
|
17 |
+
KEY_OPTION: 'any'
|
18 |
+
});
|
19 |
+
});
|
20 |
|
21 |
+
this.runtime.on('KEY_HIT', key => {
|
22 |
+
this.runtime.startHats('event_whenkeyhit', {
|
23 |
+
KEY_OPTION: key
|
24 |
+
});
|
25 |
+
this.runtime.startHats('event_whenkeyhit', {
|
26 |
+
KEY_OPTION: 'any'
|
27 |
+
});
|
28 |
+
});
|
29 |
|
30 |
+
this.isStarting = false;
|
31 |
+
this.stopEventNeedsInit = true;
|
32 |
+
this.runtime.on('PROJECT_START_BEFORE_RESET', () => {
|
33 |
+
// we need to remember that the project is starting
|
34 |
+
// otherwise the stop block will run when flag is clicked
|
35 |
+
this.isStarting = true;
|
36 |
|
37 |
+
// we need to check if this is the first time we run the stop event
|
38 |
+
// otherwise it interferes with the green flag on project load
|
39 |
+
if (this.stopEventNeedsInit) this.stopEventNeedsInit = false;
|
40 |
+
});
|
41 |
+
this.runtime.on('PROJECT_STOP_ALL', () => {
|
42 |
+
// if green flag is clicked, dont bother starting the hat
|
43 |
+
if (this.isStarting) {
|
44 |
+
this.isStarting = false;
|
45 |
+
return;
|
46 |
+
}
|
47 |
|
48 |
+
// if this is part of loading the project, do nothing
|
49 |
+
if (this.stopEventNeedsInit) return;
|
|
|
|
|
|
|
50 |
|
51 |
+
// we need to wait for runtime to step once
|
52 |
+
// otherwise the hat will be stopped as soon as it starts
|
53 |
+
this.runtime.once('RUNTIME_STEP_START', () => {
|
54 |
+
this.runtime.startHats('event_whenstopclicked');
|
55 |
+
})
|
56 |
+
this.isStarting = false;
|
57 |
+
});
|
58 |
+
this.runtime.on('RUNTIME_STEP_START', () => {
|
59 |
+
this.runtime.startHats('event_always');
|
60 |
+
});
|
61 |
|
62 |
+
this.runtime.on("AFTER_EXECUTE", () => {
|
63 |
+
// Use a timeout as regular Block Threads and Events Blocks dont run at the Same Speed
|
64 |
+
setTimeout(() => {
|
65 |
+
const stage = this.runtime.getTargetForStage();
|
66 |
+
if (!stage) return; // happens when project is loading
|
67 |
+
const stageVars = stage.variables;
|
68 |
+
for (const key in stageVars) {
|
69 |
+
if (stageVars[key].isSent !== undefined) stageVars[key].isSent = false;
|
70 |
+
}
|
71 |
+
}, 10);
|
72 |
+
});
|
73 |
}
|
74 |
|
75 |
/**
|
|
|
78 |
*/
|
79 |
getPrimitives () {
|
80 |
return {
|
81 |
+
event_whenanything: this.whenanything,
|
82 |
+
event_whenjavascript: this.whenjavascript,
|
83 |
+
event_whentouchingobject: this.touchingObject,
|
84 |
+
event_broadcast: this.broadcast,
|
85 |
+
event_broadcastandwait: this.broadcastAndWait,
|
86 |
+
event_whengreaterthan: this.hatGreaterThanPredicate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
};
|
88 |
}
|
89 |
|
90 |
+
whenanything (args) {
|
91 |
+
return Cast.toBoolean(args.ANYTHING);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
}
|
93 |
|
94 |
+
whenjavascript (args) {
|
95 |
+
return new Promise((resolve) => {
|
96 |
+
const js = Cast.toString(args.JS);
|
97 |
+
SandboxRunner.execute(js).then(result => {
|
98 |
+
resolve(result.value === true);
|
99 |
+
})
|
100 |
+
})
|
101 |
}
|
102 |
|
103 |
+
getHats () {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
return {
|
105 |
+
event_whenflagclicked: {
|
106 |
+
restartExistingThreads: true
|
107 |
},
|
108 |
+
event_whenstopclicked: {
|
109 |
+
restartExistingThreads: true
|
110 |
},
|
111 |
+
event_always: {
|
112 |
+
restartExistingThreads: false
|
113 |
},
|
114 |
+
event_whenkeypressed: {
|
115 |
+
restartExistingThreads: false
|
116 |
},
|
117 |
+
event_whenkeyhit: {
|
118 |
+
restartExistingThreads: false
|
119 |
},
|
120 |
+
event_whenmousescrolled: {
|
121 |
+
restartExistingThreads: false
|
122 |
},
|
123 |
+
event_whenanything: {
|
124 |
+
restartExistingThreads: false,
|
125 |
+
edgeActivated: true
|
126 |
},
|
127 |
+
event_whenjavascript: {
|
128 |
+
restartExistingThreads: false,
|
129 |
+
edgeActivated: true
|
130 |
},
|
131 |
+
event_whenthisspriteclicked: {
|
132 |
+
restartExistingThreads: true
|
133 |
},
|
134 |
+
event_whentouchingobject: {
|
135 |
+
restartExistingThreads: false,
|
136 |
+
edgeActivated: true
|
137 |
},
|
138 |
+
event_whenstageclicked: {
|
139 |
+
restartExistingThreads: true
|
140 |
},
|
141 |
+
event_whenbackdropswitchesto: {
|
142 |
+
restartExistingThreads: true
|
|
|
|
|
|
|
143 |
},
|
144 |
+
event_whengreaterthan: {
|
145 |
+
restartExistingThreads: false,
|
146 |
+
edgeActivated: true
|
147 |
},
|
148 |
+
event_whenbroadcastreceived: {
|
149 |
+
restartExistingThreads: true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
}
|
151 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
}
|
153 |
|
154 |
touchingObject (args, util) {
|
155 |
return util.target.isTouchingObject(args.TOUCHINGOBJECTMENU);
|
156 |
}
|
157 |
|
158 |
+
hatGreaterThanPredicate (args, util) {
|
159 |
+
const option = Cast.toString(args.WHENGREATERTHANMENU).toLowerCase();
|
160 |
+
const value = Cast.toNumber(args.VALUE);
|
161 |
+
switch (option) {
|
162 |
+
case 'timer':
|
163 |
+
return util.ioQuery('clock', 'projectTimer') > value;
|
164 |
+
case 'loudness':
|
165 |
+
return this.runtime.audioEngine && this.runtime.audioEngine.getLoudness() > value;
|
166 |
+
}
|
167 |
+
return false;
|
168 |
+
}
|
169 |
+
|
170 |
+
broadcast (args, util) {
|
171 |
+
const broadcastVar = util.runtime.getTargetForStage().lookupBroadcastMsg(
|
172 |
+
args.BROADCAST_OPTION.id, args.BROADCAST_OPTION.name);
|
173 |
+
if (broadcastVar) {
|
174 |
+
const broadcastOption = broadcastVar.name;
|
175 |
+
broadcastVar.isSent = true;
|
176 |
+
util.startHats('event_whenbroadcastreceived', {
|
177 |
+
BROADCAST_OPTION: broadcastOption
|
178 |
+
});
|
179 |
+
}
|
180 |
+
}
|
181 |
+
|
182 |
+
broadcastAndWait (args, util) {
|
183 |
+
if (!util.stackFrame.broadcastVar) {
|
184 |
+
util.stackFrame.broadcastVar = util.runtime.getTargetForStage().lookupBroadcastMsg(
|
185 |
+
args.BROADCAST_OPTION.id, args.BROADCAST_OPTION.name);
|
186 |
+
}
|
187 |
+
if (util.stackFrame.broadcastVar) {
|
188 |
+
const broadcastOption = util.stackFrame.broadcastVar.name;
|
189 |
+
// Have we run before, starting threads?
|
190 |
+
if (!util.stackFrame.startedThreads) {
|
191 |
+
broadcastVar.isSent = true;
|
192 |
+
// No - start hats for this broadcast.
|
193 |
+
util.stackFrame.startedThreads = util.startHats(
|
194 |
+
'event_whenbroadcastreceived', {
|
195 |
+
BROADCAST_OPTION: broadcastOption
|
196 |
+
}
|
197 |
+
);
|
198 |
+
if (util.stackFrame.startedThreads.length === 0) {
|
199 |
+
// Nothing was started.
|
200 |
+
return;
|
201 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
}
|
203 |
+
// We've run before; check if the wait is still going on.
|
204 |
+
const instance = this;
|
205 |
+
// Scratch 2 considers threads to be waiting if they are still in
|
206 |
+
// runtime.threads. Threads that have run all their blocks, or are
|
207 |
+
// marked done but still in runtime.threads are still considered to
|
208 |
+
// be waiting.
|
209 |
+
const waiting = util.stackFrame.startedThreads
|
210 |
+
.some(thread => instance.runtime.threads.indexOf(thread) !== -1);
|
211 |
+
if (waiting) {
|
212 |
+
// If all threads are waiting for the next tick or later yield
|
213 |
+
// for a tick as well. Otherwise yield until the next loop of
|
214 |
+
// the threads.
|
215 |
+
if (
|
216 |
+
util.stackFrame.startedThreads
|
217 |
+
.every(thread => instance.runtime.isWaitingThread(thread))
|
218 |
+
) {
|
219 |
+
util.yieldTick();
|
220 |
+
} else {
|
221 |
+
util.yield();
|
222 |
+
}
|
223 |
}
|
224 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
}
|
226 |
}
|
227 |
|
228 |
+
module.exports = Scratch3EventBlocks;
|