import mx.transitions.* import mx.core.UIObject import mx.core.UIComponent import mx.events.EventDispatcher import flash.display.* import flash.display.BitmapData.* import flash.filters.* import flash.geom.* class it.sephiroth.vcr extends UIComponent { private var _speed:Number private var _autospin:Boolean private var _minZoom:Number private var _maxZoom:Number private var _zoom:Number private var mainImage:BitmapData private var srcImage :BitmapData private var boundingBox: MovieClip private var mainMask: MovieClip private var mainContainer:MovieClip private var current : MovieClip public var previous: MovieClip private var defaultImage: MovieClip private var controller: MovieClip private var mloader:MovieClipLoader private var scrolled_text:MovieClip function init(){ super.init() boundingBox._visible = false EventDispatcher.initialize( this ) } function vcr(){ _speed = 5 _zoom = 100 _maxZoom = 200 _minZoom = 50 mloader = new MovieClipLoader() mainContainer = this.createEmptyMovieClip("mainContainer", 1) controller = this.createEmptyMovieClip("mainContainer", 2) defaultImage = mainContainer.createEmptyMovieClip("image", 1) mainMask = boundingBox.duplicateMovieClip('mainMask', 3, {_width:__width, _height:__height, _visible:false}) mainImage = new BitmapData(2880, __height, false) srcImage = new BitmapData(2880, __height, false) defaultImage.attachBitmap(srcImage, 1) mainContainer.setMask(mainMask) } public function getSize():Array{ return [__width, __height]; } private function createChildren(){ var w = __width var h = __height } public function showDescription(testo:String, vel:Number, mc){ if(mc){ scrolled_text.setTextFormat(mc) } scrolled_text.text = testo scrolled_text.start(60) } /** * * @usage * @param img * @return */ public function loadImage(img:String):Void{ var image:MovieClip = mainContainer.createEmptyMovieClip('image_' + mainContainer.getNextHighestDepth(), mainContainer.getNextHighestDepth()) var image0:MovieClip = image.createEmptyMovieClip('image0', 1) image.createEmptyMovieClip('image1', 2) image.createEmptyMovieClip('image2', 3) current = image image._visible = false mloader.addListener( this ) mloader.loadClip(img, image0) } public function attachImage(img:MovieClip):Void{ var image:MovieClip = mainContainer.createEmptyMovieClip('image_' + mainContainer.getNextHighestDepth(), mainContainer.getNextHighestDepth()) var image0:MovieClip = image.createEmptyMovieClip('image0', 1) image.createEmptyMovieClip('image1', 2) image.createEmptyMovieClip('image2', 3) current = image image._visible = false var bmp:BitmapData = new BitmapData(img._width, img._height, true) bmp.draw(img, img.transform.matrix, img.transform.colorTransform, "normal", new Rectangle(0,0, img._width, img._height)) image0.attachBitmap(bmp, 1) createDuplicates(current) adjustPosition(current) showImage(current) this.dispatchEvent({type:"loaded", target:this}) } private function onLoadInit(target:MovieClip):Void{ createDuplicates(current) adjustPosition(current) showImage(current) dispatchEvent({type:"loaded", target:this}) } private function onLoadStart(target:MovieClip):Void{ } /** * create 2 copies of the loaded image * @usage * @param mc * @return */ private function createDuplicates(mc:MovieClip){ var img:BitmapData = new BitmapData(mc.image0._width, mc.image0._height) var img1:BitmapData = new BitmapData(mc.image0._width, mc.image0._height) img.draw(mc.image0, mc.image0.transform.matrix, mc.image0.transform.colorTransform, "normal", new Rectangle(0,0, mc.image0._width, mc.image0._height)) img1.copyPixels(img, new Rectangle(0, 0, mc.image0._width, mc.image0._height), new Point(0,0)) mc.image1.attachBitmap(img1, 1) mc.image2.attachBitmap(img1, 1) } /** * set the correct size and position of the loaded images * @usage * @param mc * @return */ private function adjustPosition(mc:MovieClip){ current.image0._x -= current.image0._width/2 current.image1._x -= current.image1._width/2 current.image2._x -= current.image2._width/2 for(var a:Number = 0; a < 3; a++){ current['image'+a]._x = -current['image'+a]._width + current['image'+a]._width*(a-1); } } /** * Show the current loaded image * @usage * @param mc * @return */ private function showImage(mc:MovieClip){ if(previous != undefined){ stop() hideImage() } else { mainImage.draw(mc, mc.transform.matrix, mc.transform.colorTransform, "normal", new Rectangle(mc._x, mc._y, mc._width, mc._height)) pixelDissolve(srcImage, mainImage) } } public function hideImage(){ trace('====> unloadImage: ' + previous) defaultImage._visible = true defaultImage.swapDepths(previous) var oldx = previous._x var oldy = previous._y mainImage = new BitmapData(2880, __height, false) srcImage = new BitmapData(2880, __height, false) mainImage.draw(previous, previous.transform.matrix, previous.transform.colorTransform, "normal", new Rectangle(0, 0, previous._x + previous._width, previous._height)) srcImage.draw(current, current.transform.matrix, current.transform.colorTransform, "normal", new Rectangle(current._x, current._y, current._width, current._height)) defaultImage.attachBitmap(mainImage, defaultImage.getNextHighestDepth()) previous.removeMovieClip() pixelDissolve(mainImage, srcImage) } /** * dissolve images */ private function pixelDissolve(src, dest){ var percent:Number = 0 this.onEnterFrame = function(){ percent += 1.5 src.pixelDissolve(dest, new Rectangle(0, 0, __width, __height), new Point(0, 0), 0, 550*400*percent/100); if(percent >= 100){ previous = current current._visible = true defaultImage._visible = false srcImage.dispose() mainImage.dispose() delete this.onEnterFrame activateController() } } } /** * stop everything */ public function stop(){ controller.__x = controller.__y = 0 delete controller.onEnterFrame } private function activateController():Void{ controller.onEnterFrame = function(){ var clip:MovieClip = this._parent.current if(this.isMouseDown) { var end_x = this._parent._parent._xmouse; var end_y = this._parent._parent._ymouse; this.__x = (end_x - this.x_origin)/this._parent.speed; this.__y = (end_y - this.y_origin)/this._parent.speed; } else { this.__y *= 0.7; this.__x *= 0.7; } if(Math.abs(this.__y) < .2) this.__y = 0 var bx = Math.abs(this.__x) this._parent.mainContainer.filters = [new BlurFilter(bx, 0, 1)] clip._x -= this.__x; clip._y -= this.__y; if(this._parent.autospin){ clip._x += this._parent.speed } var imageBound = clip.getBounds(this._parent); var maskBound = this._parent.mainMask.getBounds(this._parent); // check vertical position if(imageBound.yMax <= maskBound.yMax){ clip._y = maskBound.yMax - clip._height - (imageBound.yMin - clip._y); } if(imageBound.yMin >= maskBound.yMin){ clip._y = maskBound.yMin - (imageBound.yMin - clip._y); } // check horizontal position if((imageBound.xMin + clip._width/6) > maskBound.xMin){ clip._x -= clip._width/3; } if((imageBound.xMax - clip._width/6) < maskBound.xMax){ clip._x += clip._width/3; } } mainContainer.onPress = function(){ this._parent.controller.isMouseDown = true this._parent.autospin = false this._parent.controller.x_origin = this._parent._parent._xmouse this._parent.controller.y_origin = this._parent._parent._ymouse } controller.onMouseUp = function(){ this.isMouseDown = false } } /** * copy a source movieclip into destination movieclip * @usage * @param source * @param destination * @return */ private function copyImage(source:MovieClip, destination:MovieClip){ var _img:BitmapData = new BitmapData(source._width, source._height) _img.draw(source, source.transform.matrix, source.transform.colorTransform, "normal", new Rectangle(source._x, source._y, source._width, source._height)) destination.attachBitmap(_img, destination.getNextHighestDepth()) } /* set the image zoom */ public function set zoom(value:Number){ var clip:MovieClip = current if(value < minZoom) value = minZoom if(value > maxZoom) value = maxZoom var current_x = clip._x var oldx:Number = clip._xscale var oldy:Number = clip._yscale clip._xscale = clip._yscale = value clip._x *= (clip._xscale/oldx) clip._y *= (clip._yscale/oldy) if(clip._xscale > oldx){ clip._x -= (clip._xscale/oldx) clip._y -= (clip._yscale/oldx) } else { clip._x += (clip._xscale/oldx) clip._y += (clip._yscale/oldx) } } /** return the current zoom factor */ public function get zoom():Number{ return current._xscale } /** set the maximum zoom factor */ public function set maxZoom(value:Number){ if(current._height*(value/100) < (mainMask._height)){ value = (mainMask._height/current.image0._height)*100 } if(value < zoom){ zoom = value } _maxZoom = value } public function get maxZoom():Number{ return _maxZoom } /** set the minimum zoom factor */ public function set minZoom(value:Number){ if(current._height*(value/100) < (mainMask._height)){ _minZoom = (mainMask._height/current.image0._height)*100 return } _minZoom = value } public function get minZoom():Number{ return _minZoom } /** set autospin */ public function set autospin(v:Boolean){ _autospin = v } public function get autospin():Boolean{ return _autospin } /** set autospin */ public function set speed(v:Number){ _speed = v } public function get speed():Number{ return _speed } public function getPrevious():MovieClip{ return previous } }