/* * Zooming.as - Live Chroma Key ActionScript Library * Copyright (c) 2009 Yusuke Kawasaki http://www.kawa.net/ * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * */ package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.PixelSnapping; import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFormat; import flash.text.TextFormatAlign; import flash.ui.Keyboard; import org.libspark.LiveChromaKey.LCK_Core; public class Zooming extends Sprite { private var chromakey:LCK_Core; private var spFore:Sprite; private var spBack:Sprite; private var zoomBack:Number = 0.0; private var zoomFore:Number = 0.0; private var horiFore:Number = 0.0; private var displayX:int = 640; private var displayY:int = 480; private var message:TextField; private var cntReady:int = 0; private var spGray:Sprite; public function Zooming ():void { spGray = new Sprite(); spGray.graphics.beginFill( 0x000000 ); spGray.graphics.drawRect(0, displayY-40, displayX, 40); spGray.graphics.endFill(); spGray.alpha = 0.5; this.addChild( spGray ); var fmt:TextFormat = new TextFormat(); fmt.font = 'Comic Sans MS'; fmt.size = 30; fmt.color = 0xFFFFFF; fmt.bold = true; fmt.align = TextFormatAlign.CENTER; message = new TextField(); message.defaultTextFormat = fmt; message.multiline = true; message.visible = true; message.text = 'CLICK HERE TO START'; message.x = 0; message.y = displayY - 42; message.width = displayX; this.addChild( message ); stage.addEventListener( MouseEvent.CLICK, this.start ); } private function start(e:MouseEvent):void { stage.removeEventListener( MouseEvent.CLICK, this.start ); chromakey = new LCK_Core(); chromakey.displayX = displayX; chromakey.displayY = displayY; chromakey.workX = 160; chromakey.workY = 120; chromakey.smoothing = true; chromakey.init(); spBack = chromakey.getBackground(); spFore = chromakey.getForeground(); this.addChildAt(spBack, 0); this.addChildAt(spFore, 1); stage.addEventListener( MouseEvent.MOUSE_WHEEL, this.onMouseWheel ); stage.addEventListener( KeyboardEvent.KEY_DOWN, this.onKeyDown ); stage.addEventListener( Event.ENTER_FRAME, this.onEnterFrame ); } private function onEnterFrame (e:Event):void { this.showMessage(); spFore.visible = chromakey.ready; } private function showMessage ():void { if ( chromakey.ready ) { if ( cntReady > 0 ) { message.text = 'READY'; message.visible = true; spGray.visible = true; } else if ( cntReady == 0 ) { message.visible = false; spGray.visible = false; } else { var match:Number = chromakey.getMatchLevel(); if ( match < 0.1 ) { message.text = 'PUSH ESC KEY TO RESET'; message.visible = true; spGray.visible = true; } else { message.visible = false; spGray.visible = false; } } cntReady --; } else { if ( cntReady < 1 ) { message.text = 'WAIT A MOMENT'; message.visible = true; spGray.visible = true; cntReady = 10; } } } private function onKeyDown (e:KeyboardEvent):void { if ( e.keyCode == Keyboard.HOME ) { this.homeZooming(); } else if ( e.keyCode == Keyboard.ESCAPE ) { chromakey.runDetector(); } else if ( e.keyCode == Keyboard.PAGE_UP || e.keyCode == Keyboard.INSERT ) { zoomBack += 0.05; } else if ( e.keyCode == Keyboard.PAGE_DOWN || e.keyCode == Keyboard.DELETE ) { zoomBack -= 0.05; } else if ( e.keyCode == Keyboard.UP ) { zoomFore += 0.05; } else if ( e.keyCode == Keyboard.DOWN ) { zoomFore -= 0.05; } else if ( e.keyCode == Keyboard.RIGHT ) { horiFore += 0.2 * (zoomFore < 0 ? 1.0 : -1.0); } else if ( e.keyCode == Keyboard.LEFT ) { horiFore -= 0.2 * (zoomFore < 0 ? 1.0 : -1.0); } this.updateZooming(); } private function onMouseWheel (e:MouseEvent):void { zoomFore += e.delta / 10.0; this.updateZooming(); } private function homeZooming ():void { zoomBack = 0.0; zoomFore = 0.0; horiFore = 0.0; } private function updateZooming ():void { if ( zoomBack < 0.0 ) zoomBack = 0.0; if ( zoomBack > 4.0 ) zoomBack = 4.0; // 480 * 4 * 4 < 8192 if ( zoomFore > 4.0 ) zoomFore = 4.0; // 480 * 2 ** 4 < 8192 if ( horiFore > 1.0 ) horiFore = 1.0; if ( horiFore < -1.0 ) horiFore = -1.0; var aBack:Number = zoomBack * zoomBack; spBack.x = - displayX / 2.0 * aBack; spBack.y = - displayY * aBack; spBack.scaleX = 1.0 + aBack; spBack.scaleY = 1.0 + aBack; var aFore:Number = Math.pow( 2.0, zoomFore ); spFore.x = displayX * (1 - aFore) * (horiFore + 1.0) / 2.0; spFore.y = displayY * (1 - aFore); spFore.scaleX = aFore; spFore.scaleY = aFore; } } }