From 90166dc73221dff2be18d79dedcf4b2d64839d5b Mon Sep 17 00:00:00 2001 From: Corey Frang Date: Tue, 3 Apr 2018 17:46:31 -0400 Subject: [PATCH] Add extra control blocks for video transparency and on/off control --- .../scratch3_video_sensing/index.js | 52 ++++++++++++++++--- src/io/video.js | 8 +-- src/serialization/sb2_specmap.js | 8 +-- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/src/extensions/scratch3_video_sensing/index.js b/src/extensions/scratch3_video_sensing/index.js index 3a276768..58796cf0 100644 --- a/src/extensions/scratch3_video_sensing/index.js +++ b/src/extensions/scratch3_video_sensing/index.js @@ -41,11 +41,7 @@ class Scratch3VideoSensingBlocks { // Boot up the video, canvas to down/up sample the video stream, the // preview skin and drawable, and kick off looping the analysis // logic. - this.runtime.ioDevices.video.requestVideo() - .then(({release}) => { - this.releaseVideo = release; - this._loop(); - }); + this._loop(); } } @@ -218,6 +214,14 @@ class Scratch3VideoSensingBlocks { return 2; } + get VIDEO_STATE_INFO () { + return [ + {name: 'off'}, + {name: 'on'}, + {name: 'on-flipped'} + ]; + } + /** * @returns {object} metadata for this extension and its blocks. */ @@ -255,11 +259,33 @@ class Scratch3VideoSensingBlocks { defaultValue: 10 } } + }, + { + opcode: 'videoToggle', + text: 'turn video [VIDEO_STATE]', + arguments: { + VIDEO_STATE: { + type: ArgumentType.NUMBER, + menu: 'VIDEO_STATE', + defaultValue: 1 + } + } + }, + { + opcode: 'setVideoTransparency', + text: 'set video transparency to [TRANSPARENCY]', + arguments: { + TRANSPARENCY: { + type: ArgumentType.NUMBER, + defaultValue: 0 + } + } } ], menus: { MOTION_DIRECTION: this._buildMenu(this.MOTION_DIRECTION_INFO), - STAGE_SPRITE: this._buildMenu(this.STAGE_SPRITE_INFO) + STAGE_SPRITE: this._buildMenu(this.STAGE_SPRITE_INFO), + VIDEO_STATE: this._buildMenu(this.VIDEO_STATE_INFO) } }; } @@ -312,6 +338,20 @@ class Scratch3VideoSensingBlocks { const state = this._analyzeLocalMotion(util.target); return state.motionAmount > Number(args.REFERENCE); } + + videoToggle (args) { + const state = Number(args.VIDEO_STATE); + // 1 == off, 2 & 3 are on (3 is flipped) + if (state > 1) { + this.runtime.ioDevices.video.requestVideo(); + } else { + this.runtime.ioDevices.video.disableVideo(); + } + } + + setVideoTransparency (args) { + this.runtime.ioDevices.video.setPreviewGhost(Number(args.TRANSPARENCY)); + } } module.exports = Scratch3VideoSensingBlocks; diff --git a/src/io/video.js b/src/io/video.js index 1394095b..c1888e1a 100644 --- a/src/io/video.js +++ b/src/io/video.js @@ -201,7 +201,7 @@ class Video { */ setPreviewGhost (ghost) { if (this._drawable) { - this._drawable.updateProperties({ghost}); + this.runtime.renderer.updateDrawableProperties(this._drawable, {ghost}); } } @@ -248,7 +248,7 @@ class Video { _disablePreview () { if (this._skin && this._drawable) { this._skin.clear(); - this._drawable.updateProperties({visible: false}); + this.runtime.renderer.updateDrawableProperties(this._drawable, {visible: false}); } this._renderPreviewFrame = null; } @@ -272,7 +272,9 @@ class Video { // if we haven't already created and started a preview frame render loop, do so if (!this._renderPreviewFrame) { - this._drawable.updateProperties({visible: true}); + renderer.updateDrawableProperties(this._drawable, { + visible: true + }); this._renderPreviewFrame = () => { if (!this._renderPreviewFrame) { diff --git a/src/serialization/sb2_specmap.js b/src/serialization/sb2_specmap.js index e5bb65b6..b9a07dda 100644 --- a/src/serialization/sb2_specmap.js +++ b/src/serialization/sb2_specmap.js @@ -980,17 +980,17 @@ const specMap = { // ] // }, 'setVideoState': { - opcode: 'sensing_videotoggle', + opcode: 'videoSensing.videoToggle', argMap: [ { type: 'input', - inputOp: 'sensing_videotogglemenu', - inputName: 'VIDEOTOGGLEMENU' + inputOp: 'videoSensing.menu.VIDEO_STATE', + inputName: 'VIDEO_STATE' } ] }, 'setVideoTransparency': { - opcode: 'sensing_setvideotransparency', + opcode: 'videoSensing.setVideoTransparency', argMap: [ { type: 'input',