From b9911332c7aac8d4154b0ea8a1beca2ab97170f7 Mon Sep 17 00:00:00 2001 From: "Michael \"Z\" Goddard" Date: Tue, 10 Apr 2018 13:36:39 -0400 Subject: [PATCH 1/2] Default stage.videoState to ON Set to ON, adding a video extension when a user selects a video extension in the gui, the video extension will use stage.videoState to turn on the video device. Adding a video extension as part of loading a project, the stage's videoState will be set to the stored value in the project's saved data, and the extension will set the video device's state with that saved value. --- src/sprites/rendered-target.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/sprites/rendered-target.js b/src/sprites/rendered-target.js index aba3aef64..fcd57ca53 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,13 +219,13 @@ class RenderedTarget extends Target { /** * Available states for video input. - * @type {object} + * @enum {string} */ static get VIDEO_STATE () { return { - 'OFF': 'off', - 'ON': 'on', - 'ON-FLIPPED': 'on-flipped' + OFF: 'off', + ON: 'on', + ON_FLIPPED: 'on-flipped' }; } From cef9f58c2ad012e8aa20322d7b7df0791ee4375a Mon Sep 17 00:00:00 2001 From: "Michael \"Z\" Goddard" Date: Tue, 10 Apr 2018 13:53:00 -0400 Subject: [PATCH 2/2] 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)); } }