From 8ebd659094e915face278f4cedbc852bb88df10b Mon Sep 17 00:00:00 2001 From: "Michael \"Z\" Goddard" Date: Fri, 6 Apr 2018 14:21:20 -0400 Subject: [PATCH] Use string enumerated values for videoSensing sensor block --- .../scratch3_video_sensing/index.js | 101 +++++++++++------- src/serialization/sb2.js | 16 +-- src/serialization/sb2_specmap.js | 8 +- test/unit/extension_video_sensing.js | 16 +-- 4 files changed, 77 insertions(+), 64 deletions(-) diff --git a/src/extensions/scratch3_video_sensing/index.js b/src/extensions/scratch3_video_sensing/index.js index f09468476..c3c4bbfc2 100644 --- a/src/extensions/scratch3_video_sensing/index.js +++ b/src/extensions/scratch3_video_sensing/index.js @@ -7,6 +7,32 @@ const Video = require('../../io/video'); const VideoMotion = require('./library'); +/** + * 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' +}; + /** * States the video sensing activity can be set to. * @readonly @@ -176,59 +202,54 @@ class Scratch3VideoSensingBlocks { return motionState; } + static get SensingAttribute () { + return SensingAttribute; + } + /** * An array of choices of whether a reporter should return the frame's * motion amount or direction. - * @type {object[]} an array of objects. - * @param {string} name - the translatable name to display in the drums menu. - * @param {string} fileName - the name of the audio file containing the drum sound. + * @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 */ - get MOTION_DIRECTION_INFO () { + get ATTRIBUTE_INFO () { return [ { - name: 'motion' + name: 'motion', + value: SensingAttribute.MOTION }, { - name: 'direction' + name: 'direction', + value: SensingAttribute.DIRECTION } ]; } - static get MOTION () { - return 1; - } - - static get DIRECTION () { - return 2; + static get SensingSubject () { + return SensingSubject; } /** - * An array of info about each drum. - * @type {object[]} an array of objects. - * @param {string} name - the translatable name to display in the drums - * menu. - * @param {string} fileName - the name of the audio file containing the - * drum sound. + * 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 */ - get STAGE_SPRITE_INFO () { + get SUBJECT_INFO () { return [ { - name: 'stage' + name: 'stage', + value: SensingSubject.STAGE }, { - name: 'sprite' + name: 'sprite', + value: SensingSubject.SPRITE } ]; } - static get STAGE () { - return 1; - } - - static get SPRITE () { - return 2; - } - /** * States the video sensing activity can be set to. * @readonly @@ -272,17 +293,17 @@ class Scratch3VideoSensingBlocks { { opcode: 'videoOn', blockType: BlockType.REPORTER, - text: 'video [MOTION_DIRECTION] on [STAGE_SPRITE]', + text: 'video [ATTRIBUTE] on [SUBJECT]', arguments: { - MOTION_DIRECTION: { + ATTRIBUTE: { type: ArgumentType.NUMBER, - menu: 'MOTION_DIRECTION', - defaultValue: Scratch3VideoSensingBlocks.MOTION + menu: 'ATTRIBUTE', + defaultValue: SensingAttribute.MOTION }, - STAGE_SPRITE: { + SUBJECT: { type: ArgumentType.NUMBER, - menu: 'STAGE_SPRITE', - defaultValue: Scratch3VideoSensingBlocks.STAGE + menu: 'SUBJECT', + defaultValue: SensingSubject.STAGE } } }, @@ -322,8 +343,8 @@ class Scratch3VideoSensingBlocks { } ], menus: { - MOTION_DIRECTION: this._buildMenu(this.MOTION_DIRECTION_INFO), - STAGE_SPRITE: this._buildMenu(this.STAGE_SPRITE_INFO), + ATTRIBUTE: this._buildMenu(this.ATTRIBUTE_INFO), + SUBJECT: this._buildMenu(this.SUBJECT_INFO), VIDEO_STATE: this._buildMenu(this.VIDEO_STATE_INFO) } }; @@ -353,11 +374,11 @@ class Scratch3VideoSensingBlocks { this.detect.analyzeFrame(); let state = this.detect; - if (Number(args.STAGE_SPRITE) === Scratch3VideoSensingBlocks.SPRITE) { + if (args.SUBJECT === SensingSubject.SPRITE) { state = this._analyzeLocalMotion(util.target); } - if (Number(args.MOTION_DIRECTION) === Scratch3VideoSensingBlocks.MOTION) { + if (args.ATTRIBUTE === SensingAttribute.MOTION) { return state.motionAmount; } return state.motionDirection; diff --git a/src/serialization/sb2.js b/src/serialization/sb2.js index 5ba65fc8b..063aa7abb 100644 --- a/src/serialization/sb2.js +++ b/src/serialization/sb2.js @@ -611,21 +611,13 @@ const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extension if (shadowObscured) { fieldValue = 1; } - } else if (expectedArg.inputOp === 'videoSensing.menu.MOTION_DIRECTION') { + } else if (expectedArg.inputOp === 'videoSensing.menu.ATTRIBUTE') { if (shadowObscured) { - fieldValue = 1; - } else if (fieldValue === 'motion') { - fieldValue = 1; - } else if (fieldValue === 'direction') { - fieldValue = 2; + fieldValue = 'motion'; } - } else if (expectedArg.inputOp === 'videoSensing.menu.STAGE_SPRITE') { + } else if (expectedArg.inputOp === 'videoSensing.menu.SUBJECT') { if (shadowObscured) { - fieldValue = 2; - } else if (fieldValue === 'Stage') { - fieldValue = 1; - } else if (fieldValue === 'this sprite') { - fieldValue = 2; + fieldValue = 'this sprite'; } } else if (expectedArg.inputOp === 'videoSensing.menu.VIDEO_STATE') { if (shadowObscured) { diff --git a/src/serialization/sb2_specmap.js b/src/serialization/sb2_specmap.js index b9a07ddae..025148561 100644 --- a/src/serialization/sb2_specmap.js +++ b/src/serialization/sb2_specmap.js @@ -614,13 +614,13 @@ const specMap = { argMap: [ { type: 'input', - inputOp: 'videoSensing.menu.MOTION_DIRECTION', - inputName: 'MOTION_DIRECTION' + inputOp: 'videoSensing.menu.ATTRIBUTE', + inputName: 'ATTRIBUTE' }, { type: 'input', - inputOp: 'videoSensing.menu.STAGE_SPRITE', - inputName: 'STAGE_SPRITE' + inputOp: 'videoSensing.menu.SUBJECT', + inputName: 'SUBJECT' } ] }, diff --git a/test/unit/extension_video_sensing.js b/test/unit/extension_video_sensing.js index 35f0381fa..5a217589f 100644 --- a/test/unit/extension_video_sensing.js +++ b/test/unit/extension_video_sensing.js @@ -337,8 +337,8 @@ test('videoOn returns value dependent on arguments', t => { sensing.detect.addFrame(frames.left); const motionAmount = sensing.videoOn({ - MOTION_DIRECTION: VideoSensing.MOTION, - STAGE_SPRITE: VideoSensing.STAGE + ATTRIBUTE: VideoSensing.SensingAttribute.MOTION, + SUBJECT: VideoSensing.SensingSubject.STAGE }, fakeBlockUtility); t.ok( motionAmount > 10, @@ -346,8 +346,8 @@ test('videoOn returns value dependent on arguments', t => { ); const localMotionAmount = sensing.videoOn({ - MOTION_DIRECTION: VideoSensing.MOTION, - STAGE_SPRITE: VideoSensing.SPRITE + ATTRIBUTE: VideoSensing.SensingAttribute.MOTION, + SUBJECT: VideoSensing.SensingSubject.SPRITE }, fakeBlockUtility); t.ok( localMotionAmount > 10, @@ -355,8 +355,8 @@ test('videoOn returns value dependent on arguments', t => { ); const motionDirection = sensing.videoOn({ - MOTION_DIRECTION: VideoSensing.DIRECTION, - STAGE_SPRITE: VideoSensing.STAGE + ATTRIBUTE: VideoSensing.SensingAttribute.DIRECTION, + SUBJECT: VideoSensing.SensingSubject.STAGE }, fakeBlockUtility); t.ok( isNearAngle(motionDirection, -90), @@ -364,8 +364,8 @@ test('videoOn returns value dependent on arguments', t => { ); const localMotionDirection = sensing.videoOn({ - MOTION_DIRECTION: VideoSensing.DIRECTION, - STAGE_SPRITE: VideoSensing.SPRITE + ATTRIBUTE: VideoSensing.SensingAttribute.DIRECTION, + SUBJECT: VideoSensing.SensingSubject.SPRITE }, fakeBlockUtility); t.ok( isNearAngle(localMotionDirection, -90),