From d4037808a70c3aa2b4262d41761b58afa6a93189 Mon Sep 17 00:00:00 2001 From: "Michael \"Z\" Goddard" Date: Fri, 6 Apr 2018 13:11:10 -0400 Subject: [PATCH] Add VideoState enum to Scratch3VideoSensingBlocks --- .../scratch3_video_sensing/index.js | 58 ++++++++++++++++--- src/serialization/sb2.js | 8 +-- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/extensions/scratch3_video_sensing/index.js b/src/extensions/scratch3_video_sensing/index.js index 36a91ef59..1d4d818e1 100644 --- a/src/extensions/scratch3_video_sensing/index.js +++ b/src/extensions/scratch3_video_sensing/index.js @@ -7,6 +7,22 @@ const Video = require('../../io/video'); const VideoMotion = require('./library'); +/** + * 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' +}; + /** * Class for the motion-related blocks in Scratch 3.0 * @param {Runtime} runtime - the runtime instantiating this block package. @@ -140,7 +156,7 @@ class Scratch3VideoSensingBlocks { return info.map((entry, index) => { const obj = {}; obj.text = entry.name; - obj.value = String(index + 1); + obj.value = entry.value || String(index + 1); return obj; }); } @@ -213,11 +229,35 @@ class Scratch3VideoSensingBlocks { return 2; } + /** + * 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 + */ get VIDEO_STATE_INFO () { return [ - {name: 'off'}, - {name: 'on'}, - {name: 'on-flipped'} + { + name: 'off', + value: VideoState.OFF + }, + { + name: 'on', + value: VideoState.ON + }, + { + name: 'on flipped', + value: VideoState.ON_FLIPPED + } ]; } @@ -266,7 +306,7 @@ class Scratch3VideoSensingBlocks { VIDEO_STATE: { type: ArgumentType.NUMBER, menu: 'VIDEO_STATE', - defaultValue: 1 + defaultValue: VideoState.ON } } }, @@ -339,13 +379,13 @@ class Scratch3VideoSensingBlocks { } videoToggle (args) { - const state = Number(args.VIDEO_STATE); - // 1 == off, 2 & 3 are on (3 is flipped) - if (state === 1) { + const state = args.VIDEO_STATE; + if (state === VideoState.OFF) { this.runtime.ioDevices.video.disableVideo(); } else { this.runtime.ioDevices.video.enableVideo(); - this.runtime.ioDevices.video.mirror = state === 2; + // Mirror if state is ON. Do not mirror if state is ON_FLIPPED. + this.runtime.ioDevices.video.mirror = state === VideoState.ON; } } diff --git a/src/serialization/sb2.js b/src/serialization/sb2.js index 74b1ff36d..5ba65fc8b 100644 --- a/src/serialization/sb2.js +++ b/src/serialization/sb2.js @@ -629,13 +629,7 @@ const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extension } } else if (expectedArg.inputOp === 'videoSensing.menu.VIDEO_STATE') { if (shadowObscured) { - fieldValue = 2; - } else if (fieldValue === 'off') { - fieldValue = 1; - } else if (fieldValue === 'on') { - fieldValue = 2; - } else if (fieldValue === 'on-flipped') { - fieldValue = 3; + fieldValue = 'on'; } } else if (shadowObscured) { // Filled drop-down menu.