From cef9f58c2ad012e8aa20322d7b7df0791ee4375a Mon Sep 17 00:00:00 2001 From: "Michael \"Z\" Goddard" Date: Tue, 10 Apr 2018 13:53:00 -0400 Subject: [PATCH] Set video device state and transparency from values in stage Set the video device state and transparency to the values in the stage target when the video sensing extension is loaded. Changing video state and transparency from the extension's blocks will also store that setting to the stage target. --- .../scratch3_video_sensing/index.js | 59 +++++++++++++++++-- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/src/extensions/scratch3_video_sensing/index.js b/src/extensions/scratch3_video_sensing/index.js index f09468476..23ce3303f 100644 --- a/src/extensions/scratch3_video_sensing/index.js +++ b/src/extensions/scratch3_video_sensing/index.js @@ -54,10 +54,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 + }); } } @@ -99,6 +106,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 @@ -316,7 +365,7 @@ class Scratch3VideoSensingBlocks { arguments: { TRANSPARENCY: { type: ArgumentType.NUMBER, - defaultValue: 0 + defaultValue: 50 } } } @@ -386,6 +435,7 @@ class Scratch3VideoSensingBlocks { */ videoToggle (args) { const state = args.VIDEO_STATE; + this.globalVideoState = state; if (state === VideoState.OFF) { this.runtime.ioDevices.video.disableVideo(); } else { @@ -403,6 +453,7 @@ class Scratch3VideoSensingBlocks { * preview to */ setVideoTransparency (args) { + this.globalVideoTransparency = args.TRANSPARENCY; this.runtime.ioDevices.video.setPreviewGhost(Number(args.TRANSPARENCY)); } }