2018-03-30 14:50:53 -04:00
|
|
|
const Runtime = require('../../engine/runtime');
|
|
|
|
|
2018-03-22 11:51:44 -04:00
|
|
|
const ArgumentType = require('../../extension-support/argument-type');
|
|
|
|
const BlockType = require('../../extension-support/block-type');
|
|
|
|
const Clone = require('../../util/clone');
|
2018-04-13 13:47:05 -04:00
|
|
|
const Cast = require('../../util/cast');
|
2018-05-10 13:41:41 -04:00
|
|
|
const formatMessage = require('format-message');
|
2018-04-03 11:44:44 -04:00
|
|
|
const Video = require('../../io/video');
|
2018-03-22 11:51:44 -04:00
|
|
|
|
2018-03-30 14:53:37 -04:00
|
|
|
const VideoMotion = require('./library');
|
2018-03-22 11:51:44 -04:00
|
|
|
|
2018-04-06 14:21:20 -04:00
|
|
|
/**
|
|
|
|
* Sensor attribute video sensor block should report.
|
|
|
|
* @readonly
|
|
|
|
* @enum {string}
|
|
|
|
*/
|
|
|
|
const SensingAttribute = {
|
|
|
|
/** The amount of motion. */
|
|
|
|
MOTION: 'motion',
|
|
|
|
|
|
|
|
/** The direction of the motion. */
|
|
|
|
DIRECTION: 'direction'
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subject video sensor block should report for.
|
|
|
|
* @readonly
|
|
|
|
* @enum {string}
|
|
|
|
*/
|
|
|
|
const SensingSubject = {
|
|
|
|
/** The sensor traits of the whole stage. */
|
|
|
|
STAGE: 'Stage',
|
|
|
|
|
|
|
|
/** The senosr traits of the area overlapped by this sprite. */
|
|
|
|
SPRITE: 'this sprite'
|
|
|
|
};
|
|
|
|
|
2018-04-06 13:11:10 -04:00
|
|
|
/**
|
|
|
|
* States the video sensing activity can be set to.
|
|
|
|
* @readonly
|
|
|
|
* @enum {string}
|
|
|
|
*/
|
|
|
|
const VideoState = {
|
|
|
|
/** Video turned off. */
|
|
|
|
OFF: 'off',
|
|
|
|
|
|
|
|
/** Video turned on with default y axis mirroring. */
|
|
|
|
ON: 'on',
|
|
|
|
|
|
|
|
/** Video turned on without default y axis mirroring. */
|
|
|
|
ON_FLIPPED: 'on-flipped'
|
|
|
|
};
|
|
|
|
|
2018-03-22 11:51:44 -04:00
|
|
|
/**
|
|
|
|
* Class for the motion-related blocks in Scratch 3.0
|
|
|
|
* @param {Runtime} runtime - the runtime instantiating this block package.
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
class Scratch3VideoSensingBlocks {
|
|
|
|
constructor (runtime) {
|
|
|
|
/**
|
|
|
|
* The runtime instantiating this block package.
|
|
|
|
* @type {Runtime}
|
|
|
|
*/
|
|
|
|
this.runtime = runtime;
|
|
|
|
|
2018-03-30 14:50:53 -04:00
|
|
|
/**
|
|
|
|
* The motion detection algoritm used to power the motion amount and
|
|
|
|
* direction values.
|
|
|
|
* @type {VideoMotion}
|
|
|
|
*/
|
2018-03-22 11:51:44 -04:00
|
|
|
this.detect = new VideoMotion();
|
|
|
|
|
2018-03-30 14:50:53 -04:00
|
|
|
/**
|
|
|
|
* The last millisecond epoch timestamp that the video stream was
|
|
|
|
* analyzed.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2018-03-22 11:51:44 -04:00
|
|
|
this._lastUpdate = null;
|
|
|
|
|
2018-04-05 14:30:50 -04:00
|
|
|
if (this.runtime.ioDevices) {
|
|
|
|
// Clear target motion state values when the project starts.
|
|
|
|
this.runtime.on(Runtime.PROJECT_RUN_START, this.reset.bind(this));
|
|
|
|
|
2018-04-10 13:53:00 -04:00
|
|
|
// Kick off looping the analysis logic.
|
2018-04-03 17:46:31 -04:00
|
|
|
this._loop();
|
2018-04-10 13:53:00 -04:00
|
|
|
|
|
|
|
// Configure the video device with values from a globally stored
|
|
|
|
// location.
|
|
|
|
this.setVideoTransparency({
|
|
|
|
TRANSPARENCY: this.globalVideoTransparency
|
|
|
|
});
|
|
|
|
this.videoToggle({
|
|
|
|
VIDEO_STATE: this.globalVideoState
|
|
|
|
});
|
2018-04-05 14:30:50 -04:00
|
|
|
}
|
2018-03-22 11:51:44 -04:00
|
|
|
}
|
|
|
|
|
2018-04-05 17:47:07 -04:00
|
|
|
/**
|
|
|
|
* After analyzing a frame the amount of milliseconds until another frame
|
|
|
|
* is analyzed.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
static get INTERVAL () {
|
|
|
|
return 33;
|
|
|
|
}
|
|
|
|
|
2018-03-30 14:50:53 -04:00
|
|
|
/**
|
|
|
|
* Dimensions the video stream is analyzed at after its rendered to the
|
|
|
|
* sample canvas.
|
|
|
|
* @type {Array.<number>}
|
|
|
|
*/
|
2018-03-22 11:51:44 -04:00
|
|
|
static get DIMENSIONS () {
|
|
|
|
return [480, 360];
|
|
|
|
}
|
|
|
|
|
2018-03-30 14:50:53 -04:00
|
|
|
/**
|
|
|
|
* The key to load & store a target's motion-related state.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
static get STATE_KEY () {
|
|
|
|
return 'Scratch.videoSensing';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default motion-related state, to be used when a target has no existing motion state.
|
|
|
|
* @type {MotionState}
|
|
|
|
*/
|
|
|
|
static get DEFAULT_MOTION_STATE () {
|
|
|
|
return {
|
|
|
|
motionFrameNumber: 0,
|
|
|
|
motionAmount: 0,
|
|
|
|
motionDirection: 0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-10 13:53:00 -04:00
|
|
|
/**
|
|
|
|
* The transparency setting of the video preview stored in a value
|
|
|
|
* accessible by any object connected to the virtual machine.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
get globalVideoTransparency () {
|
|
|
|
const stage = this.runtime.getTargetForStage();
|
|
|
|
if (stage) {
|
|
|
|
return stage.videoTransparency;
|
|
|
|
}
|
|
|
|
return 50;
|
|
|
|
}
|
|
|
|
|
|
|
|
set globalVideoTransparency (transparency) {
|
|
|
|
const stage = this.runtime.getTargetForStage();
|
|
|
|
if (stage) {
|
|
|
|
stage.videoTransparency = transparency;
|
|
|
|
}
|
|
|
|
return transparency;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The video state of the video preview stored in a value accessible by any
|
|
|
|
* object connected to the virtual machine.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
get globalVideoState () {
|
|
|
|
const stage = this.runtime.getTargetForStage();
|
|
|
|
if (stage) {
|
|
|
|
return stage.videoState;
|
|
|
|
}
|
|
|
|
return VideoState.ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
set globalVideoState (state) {
|
|
|
|
const stage = this.runtime.getTargetForStage();
|
|
|
|
if (stage) {
|
|
|
|
stage.videoState = state;
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2018-03-30 14:50:53 -04:00
|
|
|
/**
|
|
|
|
* Reset the extension's data motion detection data. This will clear out
|
|
|
|
* for example old frames, so the first analyzed frame will not be compared
|
|
|
|
* against a frame from before reset was called.
|
|
|
|
*/
|
|
|
|
reset () {
|
|
|
|
this.detect.reset();
|
|
|
|
|
|
|
|
const targets = this.runtime.targets;
|
|
|
|
for (let i = 0; i < targets.length; i++) {
|
|
|
|
const state = targets[i].getCustomState(Scratch3VideoSensingBlocks.STATE_KEY);
|
|
|
|
if (state) {
|
|
|
|
state.motionAmount = 0;
|
|
|
|
state.motionDirection = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Occasionally step a loop to sample the video, stamp it to the preview
|
|
|
|
* skin, and add a TypedArray copy of the canvas's pixel data.
|
|
|
|
* @private
|
|
|
|
*/
|
2018-03-22 11:51:44 -04:00
|
|
|
_loop () {
|
2018-04-03 11:44:44 -04:00
|
|
|
setTimeout(this._loop.bind(this), Math.max(this.runtime.currentStepTime, Scratch3VideoSensingBlocks.INTERVAL));
|
2018-03-22 11:51:44 -04:00
|
|
|
|
|
|
|
// Add frame to detector
|
|
|
|
const time = Date.now();
|
2018-04-03 11:44:44 -04:00
|
|
|
if (this._lastUpdate === null) {
|
|
|
|
this._lastUpdate = time;
|
|
|
|
}
|
2018-03-22 11:51:44 -04:00
|
|
|
const offset = time - this._lastUpdate;
|
|
|
|
if (offset > Scratch3VideoSensingBlocks.INTERVAL) {
|
2018-04-03 11:44:44 -04:00
|
|
|
const frame = this.runtime.ioDevices.video.getFrame({
|
|
|
|
format: Video.FORMAT_IMAGE_DATA,
|
|
|
|
dimensions: Scratch3VideoSensingBlocks.DIMENSIONS
|
|
|
|
});
|
|
|
|
if (frame) {
|
|
|
|
this._lastUpdate = time;
|
|
|
|
this.detect.addFrame(frame.data);
|
|
|
|
}
|
2018-03-22 11:51:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-30 14:50:53 -04:00
|
|
|
* Create data for a menu in scratch-blocks format, consisting of an array
|
|
|
|
* of objects with text and value properties. The text is a translated
|
|
|
|
* string, and the value is one-indexed.
|
|
|
|
* @param {object[]} info - An array of info objects each having a name
|
|
|
|
* property.
|
2018-03-22 11:51:44 -04:00
|
|
|
* @return {array} - An array of objects with text and value properties.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_buildMenu (info) {
|
|
|
|
return info.map((entry, index) => {
|
|
|
|
const obj = {};
|
|
|
|
obj.text = entry.name;
|
2018-04-06 13:11:10 -04:00
|
|
|
obj.value = entry.value || String(index + 1);
|
2018-03-22 11:51:44 -04:00
|
|
|
return obj;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Target} target - collect motion state for this target.
|
2018-03-30 14:50:53 -04:00
|
|
|
* @returns {MotionState} the mutable motion state associated with that
|
|
|
|
* target. This will be created if necessary.
|
2018-03-22 11:51:44 -04:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_getMotionState (target) {
|
|
|
|
let motionState = target.getCustomState(Scratch3VideoSensingBlocks.STATE_KEY);
|
|
|
|
if (!motionState) {
|
|
|
|
motionState = Clone.simple(Scratch3VideoSensingBlocks.DEFAULT_MOTION_STATE);
|
|
|
|
target.setCustomState(Scratch3VideoSensingBlocks.STATE_KEY, motionState);
|
|
|
|
}
|
|
|
|
return motionState;
|
|
|
|
}
|
|
|
|
|
2018-04-06 14:21:20 -04:00
|
|
|
static get SensingAttribute () {
|
|
|
|
return SensingAttribute;
|
|
|
|
}
|
|
|
|
|
2018-03-22 11:51:44 -04:00
|
|
|
/**
|
2018-03-30 14:50:53 -04:00
|
|
|
* An array of choices of whether a reporter should return the frame's
|
|
|
|
* motion amount or direction.
|
2018-04-06 14:21:20 -04:00
|
|
|
* @type {object[]} an array of objects
|
|
|
|
* @param {string} name - the translatable name to display in sensor
|
|
|
|
* attribute menu
|
|
|
|
* @param {string} value - the serializable value of the attribute
|
2018-03-22 11:51:44 -04:00
|
|
|
*/
|
2018-04-06 14:21:20 -04:00
|
|
|
get ATTRIBUTE_INFO () {
|
2018-03-22 11:51:44 -04:00
|
|
|
return [
|
|
|
|
{
|
2018-05-10 13:41:41 -04:00
|
|
|
name: formatMessage({
|
|
|
|
id: 'videoSensing.motion',
|
|
|
|
default: 'motion',
|
|
|
|
description: 'Attribute for the "video [ATTRIBUTE] on [SUBJECT]" block'
|
|
|
|
}),
|
2018-04-06 14:21:20 -04:00
|
|
|
value: SensingAttribute.MOTION
|
2018-03-22 11:51:44 -04:00
|
|
|
},
|
|
|
|
{
|
2018-05-10 13:41:41 -04:00
|
|
|
name: formatMessage({
|
|
|
|
id: 'videoSensing.direction',
|
|
|
|
default: 'direction',
|
|
|
|
description: 'Attribute for the "video [ATTRIBUTE] on [SUBJECT]" block'
|
|
|
|
}),
|
2018-04-06 14:21:20 -04:00
|
|
|
value: SensingAttribute.DIRECTION
|
2018-03-22 11:51:44 -04:00
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-04-06 14:21:20 -04:00
|
|
|
static get SensingSubject () {
|
|
|
|
return SensingSubject;
|
2018-04-05 14:30:50 -04:00
|
|
|
}
|
|
|
|
|
2018-03-22 11:51:44 -04:00
|
|
|
/**
|
2018-04-06 14:21:20 -04:00
|
|
|
* An array of info about the subject choices.
|
|
|
|
* @type {object[]} an array of objects
|
|
|
|
* @param {string} name - the translatable name to display in the subject menu
|
|
|
|
* @param {string} value - the serializable value of the subject
|
2018-03-22 11:51:44 -04:00
|
|
|
*/
|
2018-04-06 14:21:20 -04:00
|
|
|
get SUBJECT_INFO () {
|
2018-03-22 11:51:44 -04:00
|
|
|
return [
|
|
|
|
{
|
2018-05-10 13:41:41 -04:00
|
|
|
name: formatMessage({
|
|
|
|
id: 'videoSensing.sprite',
|
|
|
|
default: 'sprite',
|
|
|
|
description: 'Subject for the "video [ATTRIBUTE] on [SUBJECT]" block'
|
|
|
|
}),
|
2018-04-06 14:21:20 -04:00
|
|
|
value: SensingSubject.SPRITE
|
2018-04-23 13:00:20 -04:00
|
|
|
},
|
|
|
|
{
|
2018-05-10 13:41:41 -04:00
|
|
|
name: formatMessage({
|
|
|
|
id: 'videoSensing.stage',
|
|
|
|
default: 'stage',
|
|
|
|
description: 'Subject for the "video [ATTRIBUTE] on [SUBJECT]" block'
|
|
|
|
}),
|
2018-04-23 13:00:20 -04:00
|
|
|
value: SensingSubject.STAGE
|
2018-03-22 11:51:44 -04:00
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-04-06 13:11:10 -04:00
|
|
|
/**
|
|
|
|
* States the video sensing activity can be set to.
|
|
|
|
* @readonly
|
|
|
|
* @enum {string}
|
|
|
|
*/
|
|
|
|
static get VideoState () {
|
|
|
|
return VideoState;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of info on video state options for the "turn video [STATE]" block.
|
|
|
|
* @type {object[]} an array of objects
|
|
|
|
* @param {string} name - the translatable name to display in the video state menu
|
|
|
|
* @param {string} value - the serializable value stored in the block
|
|
|
|
*/
|
2018-04-03 17:46:31 -04:00
|
|
|
get VIDEO_STATE_INFO () {
|
|
|
|
return [
|
2018-04-06 13:11:10 -04:00
|
|
|
{
|
2018-05-10 13:41:41 -04:00
|
|
|
name: formatMessage({
|
|
|
|
id: 'videoSensing.off',
|
|
|
|
default: 'off',
|
|
|
|
description: 'Option for the "turn video [STATE]" block'
|
|
|
|
}),
|
2018-04-06 13:11:10 -04:00
|
|
|
value: VideoState.OFF
|
|
|
|
},
|
|
|
|
{
|
2018-05-10 13:41:41 -04:00
|
|
|
name: formatMessage({
|
|
|
|
id: 'videoSensing.on',
|
|
|
|
default: 'on',
|
|
|
|
description: 'Option for the "turn video [STATE]" block'
|
|
|
|
}),
|
2018-04-06 13:11:10 -04:00
|
|
|
value: VideoState.ON
|
|
|
|
},
|
|
|
|
{
|
2018-05-10 13:41:41 -04:00
|
|
|
name: formatMessage({
|
|
|
|
id: 'videoSensing.onFlipped',
|
|
|
|
default: 'on flipped',
|
2018-05-10 15:25:55 -04:00
|
|
|
description: 'Option for the "turn video [STATE]" block that causes the video to be flipped' +
|
|
|
|
' horizontally (reversed as in a mirror)'
|
2018-05-10 13:41:41 -04:00
|
|
|
}),
|
2018-04-06 13:11:10 -04:00
|
|
|
value: VideoState.ON_FLIPPED
|
|
|
|
}
|
2018-04-03 17:46:31 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-03-22 11:51:44 -04:00
|
|
|
/**
|
|
|
|
* @returns {object} metadata for this extension and its blocks.
|
|
|
|
*/
|
|
|
|
getInfo () {
|
2018-04-23 13:03:56 -04:00
|
|
|
// Enable the video layer
|
|
|
|
this.runtime.ioDevices.video.enableVideo();
|
|
|
|
|
|
|
|
// Return extension definition
|
2018-03-22 11:51:44 -04:00
|
|
|
return {
|
|
|
|
id: 'videoSensing',
|
2018-06-18 11:37:10 -04:00
|
|
|
name: formatMessage({
|
|
|
|
id: 'videoSensing.categoryName',
|
2018-07-18 10:11:08 -04:00
|
|
|
default: 'Video Sensing',
|
|
|
|
description: 'Label for the video sensing extension category'
|
2018-06-18 11:37:10 -04:00
|
|
|
}),
|
2018-03-22 11:51:44 -04:00
|
|
|
blocks: [
|
2018-04-23 12:57:14 -04:00
|
|
|
{
|
|
|
|
// @todo this hat needs to be set itself to restart existing
|
|
|
|
// threads like Scratch 2's behaviour.
|
|
|
|
opcode: 'whenMotionGreaterThan',
|
2018-05-10 13:41:41 -04:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'videoSensing.whenMotionGreaterThan',
|
|
|
|
default: 'when video motion > [REFERENCE]',
|
|
|
|
description: 'Event that triggers when the amount of motion is greater than [REFERENCE]'
|
|
|
|
}),
|
2018-04-23 12:57:14 -04:00
|
|
|
blockType: BlockType.HAT,
|
|
|
|
arguments: {
|
|
|
|
REFERENCE: {
|
|
|
|
type: ArgumentType.NUMBER,
|
|
|
|
defaultValue: 10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2018-03-22 11:51:44 -04:00
|
|
|
{
|
|
|
|
opcode: 'videoOn',
|
|
|
|
blockType: BlockType.REPORTER,
|
2018-05-10 13:41:41 -04:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'videoSensing.videoOn',
|
|
|
|
default: 'video [ATTRIBUTE] on [SUBJECT]',
|
|
|
|
description: 'Reporter that returns the amount of [ATTRIBUTE] for the selected [SUBJECT]'
|
|
|
|
}),
|
2018-03-22 11:51:44 -04:00
|
|
|
arguments: {
|
2018-04-06 14:21:20 -04:00
|
|
|
ATTRIBUTE: {
|
2018-03-22 11:51:44 -04:00
|
|
|
type: ArgumentType.NUMBER,
|
2018-04-06 14:21:20 -04:00
|
|
|
menu: 'ATTRIBUTE',
|
|
|
|
defaultValue: SensingAttribute.MOTION
|
2018-03-22 11:51:44 -04:00
|
|
|
},
|
2018-04-06 14:21:20 -04:00
|
|
|
SUBJECT: {
|
2018-03-22 11:51:44 -04:00
|
|
|
type: ArgumentType.NUMBER,
|
2018-04-06 14:21:20 -04:00
|
|
|
menu: 'SUBJECT',
|
2018-04-23 13:00:20 -04:00
|
|
|
defaultValue: SensingSubject.SPRITE
|
2018-03-22 11:51:44 -04:00
|
|
|
}
|
|
|
|
}
|
2018-03-30 14:48:07 -04:00
|
|
|
},
|
2018-04-03 17:46:31 -04:00
|
|
|
{
|
|
|
|
opcode: 'videoToggle',
|
2018-05-10 13:41:41 -04:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'videoSensing.videoToggle',
|
|
|
|
default: 'turn video [VIDEO_STATE]',
|
2018-05-10 15:25:55 -04:00
|
|
|
description: 'Controls display of the video preview layer'
|
2018-05-10 13:41:41 -04:00
|
|
|
}),
|
2018-04-03 17:46:31 -04:00
|
|
|
arguments: {
|
|
|
|
VIDEO_STATE: {
|
|
|
|
type: ArgumentType.NUMBER,
|
|
|
|
menu: 'VIDEO_STATE',
|
2018-04-06 13:11:10 -04:00
|
|
|
defaultValue: VideoState.ON
|
2018-04-03 17:46:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'setVideoTransparency',
|
2018-05-10 13:41:41 -04:00
|
|
|
text: formatMessage({
|
|
|
|
id: 'videoSensing.setVideoTransparency',
|
|
|
|
default: 'set video transparency to [TRANSPARENCY]',
|
|
|
|
description: 'Controls transparency of the video preview layer'
|
|
|
|
}),
|
2018-04-03 17:46:31 -04:00
|
|
|
arguments: {
|
|
|
|
TRANSPARENCY: {
|
|
|
|
type: ArgumentType.NUMBER,
|
2018-04-10 13:53:00 -04:00
|
|
|
defaultValue: 50
|
2018-04-03 17:46:31 -04:00
|
|
|
}
|
|
|
|
}
|
2018-03-22 11:51:44 -04:00
|
|
|
}
|
|
|
|
],
|
|
|
|
menus: {
|
2018-04-06 14:21:20 -04:00
|
|
|
ATTRIBUTE: this._buildMenu(this.ATTRIBUTE_INFO),
|
|
|
|
SUBJECT: this._buildMenu(this.SUBJECT_INFO),
|
2018-04-03 17:46:31 -04:00
|
|
|
VIDEO_STATE: this._buildMenu(this.VIDEO_STATE_INFO)
|
2018-03-22 11:51:44 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-30 14:48:07 -04:00
|
|
|
/**
|
|
|
|
* Analyze a part of the frame that a target overlaps.
|
|
|
|
* @param {Target} target - a target to determine where to analyze
|
|
|
|
* @returns {MotionState} the motion state for the given target
|
|
|
|
*/
|
|
|
|
_analyzeLocalMotion (target) {
|
|
|
|
const drawable = this.runtime.renderer._allDrawables[target.drawableID];
|
|
|
|
const state = this._getMotionState(target);
|
|
|
|
this.detect.getLocalMotion(drawable, state);
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A scratch reporter block handle that analyzes the last two frames and
|
|
|
|
* depending on the arguments, returns the motion or direction for the
|
|
|
|
* whole stage or just the target sprite.
|
|
|
|
* @param {object} args - the block arguments
|
|
|
|
* @param {BlockUtility} util - the block utility
|
|
|
|
* @returns {number} the motion amount or direction of the stage or sprite
|
|
|
|
*/
|
2018-03-22 11:51:44 -04:00
|
|
|
videoOn (args, util) {
|
|
|
|
this.detect.analyzeFrame();
|
|
|
|
|
|
|
|
let state = this.detect;
|
2018-04-06 14:21:20 -04:00
|
|
|
if (args.SUBJECT === SensingSubject.SPRITE) {
|
2018-03-30 14:48:07 -04:00
|
|
|
state = this._analyzeLocalMotion(util.target);
|
2018-03-22 11:51:44 -04:00
|
|
|
}
|
|
|
|
|
2018-04-06 14:21:20 -04:00
|
|
|
if (args.ATTRIBUTE === SensingAttribute.MOTION) {
|
2018-03-22 11:51:44 -04:00
|
|
|
return state.motionAmount;
|
|
|
|
}
|
|
|
|
return state.motionDirection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-30 14:48:07 -04:00
|
|
|
* A scratch hat block edge handle that analyzes the last two frames where
|
|
|
|
* the target sprite overlaps and if it has more motion than the given
|
|
|
|
* reference value.
|
|
|
|
* @param {object} args - the block arguments
|
|
|
|
* @param {BlockUtility} util - the block utility
|
|
|
|
* @returns {boolean} true if the sprite overlaps more motion than the
|
|
|
|
* reference
|
2018-03-22 11:51:44 -04:00
|
|
|
*/
|
2018-03-30 14:48:07 -04:00
|
|
|
whenMotionGreaterThan (args, util) {
|
|
|
|
this.detect.analyzeFrame();
|
|
|
|
const state = this._analyzeLocalMotion(util.target);
|
|
|
|
return state.motionAmount > Number(args.REFERENCE);
|
2018-03-22 11:51:44 -04:00
|
|
|
}
|
2018-04-03 17:46:31 -04:00
|
|
|
|
2018-04-10 14:26:21 -04:00
|
|
|
/**
|
|
|
|
* A scratch command block handle that configures the video state from
|
|
|
|
* passed arguments.
|
|
|
|
* @param {object} args - the block arguments
|
|
|
|
* @param {VideoState} args.VIDEO_STATE - the video state to set the device to
|
|
|
|
*/
|
2018-04-03 17:46:31 -04:00
|
|
|
videoToggle (args) {
|
2018-04-06 13:11:10 -04:00
|
|
|
const state = args.VIDEO_STATE;
|
2018-04-10 13:53:00 -04:00
|
|
|
this.globalVideoState = state;
|
2018-04-06 13:11:10 -04:00
|
|
|
if (state === VideoState.OFF) {
|
2018-04-03 17:46:31 -04:00
|
|
|
this.runtime.ioDevices.video.disableVideo();
|
2018-04-03 18:09:05 -04:00
|
|
|
} else {
|
2018-04-04 14:20:13 -04:00
|
|
|
this.runtime.ioDevices.video.enableVideo();
|
2018-04-06 13:11:10 -04:00
|
|
|
// Mirror if state is ON. Do not mirror if state is ON_FLIPPED.
|
|
|
|
this.runtime.ioDevices.video.mirror = state === VideoState.ON;
|
2018-04-03 17:46:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 14:26:21 -04:00
|
|
|
/**
|
|
|
|
* A scratch command block handle that configures the video preview's
|
|
|
|
* transparency from passed arguments.
|
|
|
|
* @param {object} args - the block arguments
|
|
|
|
* @param {number} args.TRANSPARENCY - the transparency to set the video
|
|
|
|
* preview to
|
|
|
|
*/
|
2018-04-03 17:46:31 -04:00
|
|
|
setVideoTransparency (args) {
|
2018-04-13 13:47:05 -04:00
|
|
|
const transparency = Cast.toNumber(args.TRANSPARENCY);
|
|
|
|
this.globalVideoTransparency = transparency;
|
|
|
|
this.runtime.ioDevices.video.setPreviewGhost(transparency);
|
2018-04-03 17:46:31 -04:00
|
|
|
}
|
2018-03-22 11:51:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Scratch3VideoSensingBlocks;
|