Skip to content

Commit

Permalink
Merge pull request #1042 from mzgoddard/target-video-state
Browse files Browse the repository at this point in the history
Configure video device with stage video settings
  • Loading branch information
kchadha committed Apr 13, 2018
2 parents 3ac2922 + cef9f58 commit 0e0d09c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
59 changes: 55 additions & 4 deletions src/extensions/scratch3_video_sensing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -337,7 +386,7 @@ class Scratch3VideoSensingBlocks {
arguments: {
TRANSPARENCY: {
type: ArgumentType.NUMBER,
defaultValue: 0
defaultValue: 50
}
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -424,6 +474,7 @@ class Scratch3VideoSensingBlocks {
* preview to
*/
setVideoTransparency (args) {
this.globalVideoTransparency = args.TRANSPARENCY;
this.runtime.ioDevices.video.setPreviewGhost(Number(args.TRANSPARENCY));
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/sprites/rendered-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -211,7 +219,7 @@ class RenderedTarget extends Target {

/**
* Available states for video input.
* @type {object}
* @enum {string}
*/
static get VIDEO_STATE () {
return {
Expand Down

0 comments on commit 0e0d09c

Please sign in to comment.