diff --git a/src/extensions/scratch3_video_sensing/index.js b/src/extensions/scratch3_video_sensing/index.js index c3c4bbfc2..4c6d1b7bd 100644 --- a/src/extensions/scratch3_video_sensing/index.js +++ b/src/extensions/scratch3_video_sensing/index.js @@ -80,10 +80,17 @@ class Scratch3VideoSensingBlocks { // Clear target motion state values when the project starts. this.runtime.on(Runtime.PROJECT_RUN_START, this.reset.bind(this)); - // Boot up the video, canvas to down/up sample the video stream, the - // preview skin and drawable, and kick off looping the analysis - // logic. + // Kick off looping the analysis logic. this._loop(); + + // Configure the video device with values from a globally stored + // location. + this.setVideoTransparency({ + TRANSPARENCY: this.globalVideoTransparency + }); + this.videoToggle({ + VIDEO_STATE: this.globalVideoState + }); } } @@ -125,6 +132,48 @@ class Scratch3VideoSensingBlocks { }; } + /** + * 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; + } + /** * 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 @@ -337,7 +386,7 @@ class Scratch3VideoSensingBlocks { arguments: { TRANSPARENCY: { type: ArgumentType.NUMBER, - defaultValue: 0 + defaultValue: 50 } } } @@ -407,6 +456,7 @@ class Scratch3VideoSensingBlocks { */ videoToggle (args) { const state = args.VIDEO_STATE; + this.globalVideoState = state; if (state === VideoState.OFF) { this.runtime.ioDevices.video.disableVideo(); } else { @@ -424,6 +474,7 @@ class Scratch3VideoSensingBlocks { * preview to */ setVideoTransparency (args) { + this.globalVideoTransparency = args.TRANSPARENCY; this.runtime.ioDevices.video.setPreviewGhost(Number(args.TRANSPARENCY)); } } diff --git a/src/sprites/rendered-target.js b/src/sprites/rendered-target.js index 4a5f278be..19489d575 100644 --- a/src/sprites/rendered-target.js +++ b/src/sprites/rendered-target.js @@ -140,9 +140,17 @@ class RenderedTarget extends Target { /** * The state of the video input (used by extensions with camera input). * This property is global to the project and stored in the stage. + * + * Defaults to ON. This setting does not turn the video by itself. A + * video extension once loaded will set the video device to this + * setting. Set to ON when a video extension is added in the editor the + * video will start ON. If the extension is loaded as part of loading a + * saved project the extension will see the value set when the stage + * was loaded from the saved values including the video state. + * * @type {string} */ - this.videoState = RenderedTarget.VIDEO_STATE.OFF; + this.videoState = RenderedTarget.VIDEO_STATE.ON; } /** @@ -211,7 +219,7 @@ class RenderedTarget extends Target { /** * Available states for video input. - * @type {object} + * @enum {string} */ static get VIDEO_STATE () { return {