package com.oxylusflash.book { import caurina.transitions.Tweener; import caurina.transitions.properties.FilterShortcuts; import flash.display.DisplayObject; import flash.display.Sprite; import flash.events.Event; import flash.geom.Rectangle; public class ZoomSourceWrapper extends Sprite { private var hitZone:Sprite; public var content:Sprite; public static const PADDING_X:Number = 50; public static const PADDING_Y:Number = 20; private var w:Number = 0; private var h:Number = 0; private static const BLUR_ALPHA:Number = 0.75; private static const BLUR:Number = 8; /** * Zoom source content wrapper. */ public function ZoomSourceWrapper() { FilterShortcuts.init(); this.addEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler, false, 0, true); } private function removedFromStageHandler(e:Event):void { this.removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler); this.removeChild(content); content = null; this.removeChild(hitZone); hitZone.graphics.clear(); hitZone = null; } /** * Add content. * @param pContent Content to be added. */ public function addContent(pContent:DisplayObject):void { if (content == null) { content = new Sprite; content.addChild(pContent); content.alpha = 0; content.x = PADDING_X; content.y = PADDING_Y; w = pContent.loaderInfo.width; h = pContent.loaderInfo.height; content.scrollRect = new Rectangle(0, 0, w, h); hitZone = new Sprite; hitZone.graphics.beginFill(0, 0); hitZone.graphics.drawRect(0, 0, this.width, this.height); hitZone.graphics.endFill(); this.addChild(hitZone); this.addChild(content); this.scrollRect = new Rectangle(0, 0, this.width, this.height); Tweener.addTween(content, { alpha: 1, base: Global.baseTween } ); } } /** * Make the page ready for print. * @param w Print width. * @param h Print height. */ public function changeToPrintSize(w:Number, h:Number):void { if (content) { content.scaleX = Math.min(1, w / content.width, h / content.height); content.scaleY = content.scaleX; } } /** * Reset page after print. */ public function changeToOriginalSize():void { if (content) { content.scaleX = 1; content.scaleY = 1; } } /** * Blur current content when loading a new one. */ public function blurContent():void { if (content) { Tweener.addTween(content, { alpha: BLUR_ALPHA, base: Global.baseTween } ); } } /** * Kill current content. */ public function die():void { if (content) { Tweener.addTween(content, { alpha: 0, base: Global.baseTween, onComplete: onContentBlurComplete } ); } else { onContentBlurComplete(); } } private function onContentBlurComplete():void { if (this.parent) { this.parent.removeChild(this); } } /** * Overrides. */ override public function get x():Number { return super.x; } override public function set x(value:Number):void { super.x = Math.round(value); } override public function get y():Number { return super.y; } override public function set y(value:Number):void { super.y = Math.round(value); } override public function get width():Number { return w + 2 * PADDING_X; } override public function set width(value:Number):void { } override public function get height():Number { return h + 2 * PADDING_Y; } override public function set height(value:Number):void { } override public function get alpha():Number { return super.alpha; } override public function set alpha(value:Number):void { if (super.alpha != value) { super.alpha = value; this.visible = value > 0; } } } }