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.
This commit is contained in:
Michael "Z" Goddard 2018-04-10 13:53:00 -04:00
parent b9911332c7
commit cef9f58c2a
No known key found for this signature in database
GPG key ID: 762CD40DD5349872

View file

@ -54,10 +54,17 @@ class Scratch3VideoSensingBlocks {
// Clear target motion state values when the project starts. // Clear target motion state values when the project starts.
this.runtime.on(Runtime.PROJECT_RUN_START, this.reset.bind(this)); this.runtime.on(Runtime.PROJECT_RUN_START, this.reset.bind(this));
// Boot up the video, canvas to down/up sample the video stream, the // Kick off looping the analysis logic.
// preview skin and drawable, and kick off looping the analysis
// logic.
this._loop(); 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 * 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 * for example old frames, so the first analyzed frame will not be compared
@ -316,7 +365,7 @@ class Scratch3VideoSensingBlocks {
arguments: { arguments: {
TRANSPARENCY: { TRANSPARENCY: {
type: ArgumentType.NUMBER, type: ArgumentType.NUMBER,
defaultValue: 0 defaultValue: 50
} }
} }
} }
@ -386,6 +435,7 @@ class Scratch3VideoSensingBlocks {
*/ */
videoToggle (args) { videoToggle (args) {
const state = args.VIDEO_STATE; const state = args.VIDEO_STATE;
this.globalVideoState = state;
if (state === VideoState.OFF) { if (state === VideoState.OFF) {
this.runtime.ioDevices.video.disableVideo(); this.runtime.ioDevices.video.disableVideo();
} else { } else {
@ -403,6 +453,7 @@ class Scratch3VideoSensingBlocks {
* preview to * preview to
*/ */
setVideoTransparency (args) { setVideoTransparency (args) {
this.globalVideoTransparency = args.TRANSPARENCY;
this.runtime.ioDevices.video.setPreviewGhost(Number(args.TRANSPARENCY)); this.runtime.ioDevices.video.setPreviewGhost(Number(args.TRANSPARENCY));
} }
} }