/** -----------------------------------------------------------
* Image Holder v1.1
* -----------------------------------------------------------
* Description: Loads the images
* - ---------------------------------------------------------
* Created by:
[email protected]* Modified by: chrisaiv
* Date Modified: December 5, 2008
* - ---------------------------------------------------------
* Copyright ©2008
* - ---------------------------------------------------------
*
* Notes:
* If you have any interest in embedding fonts, Adobe offers an explainer:
* http://www.adobe.com/devnet/flash/quickstart/label_component_as3/#section7
*
*/
package src
{
import fl.controls.*;
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.*;
import flash.geom.Point;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.system.SecurityDomain;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import gs.TweenLite;
public class ImageHolder extends Sprite
{
private var context:LoaderContext;
private var contentLoaded:uint;
private var imgLoader1:Loader;
private var imgLoader2:Loader;
private var tf:TextFormat;
private var imgData:Array;
private var containerMC:MovieClip;
private var containerParams:Object;
private var captionLBL:Label;
private var imgMask:Sprite;
private var currentIndex:uint;
private var appStage:Stage;
public function ImageHolder( data:Array, stg:Stage, container:MovieClip, caption:Label, currentImage:Number, productionMode:Boolean )
{
imgData = data;
appStage = stg;
containerMC = container;
containerParams = new Object();
containerParams.width = containerMC.width;
containerParams.height = containerMC.height;
captionLBL = caption;
captionLBL.wordWrap = true;
captionLBL.textField.antiAliasType = AntiAliasType.ADVANCED
formatCaptionBox( captionLBL );
//captionLBL.setStyle( "embedFonts", true );
//Get the proper access needed to load images
context = new LoaderContext();
context.checkPolicyFile = true;
//If we are not on a domain authorized by the cross policy file, allow the SWF to catch the error
//This is needed when you start uploading the SWF to a server that accesses data from a different domain
if( productionMode ) context.securityDomain = SecurityDomain.currentDomain;
imgLoader1 = new Loader();
imgLoader1.name = "loader1";
imgLoader2 = new Loader();
imgLoader2.name = "loader2";
loadImage( currentImage );
buildMask();
}
public function loadImage( index:uint ):void
{
currentIndex = index;
var imgURL:String = imgData[currentIndex].src;
//Load the Images + Handle most of the Events
enableBasicListeners( imgLoader1.contentLoaderInfo );
enableBasicListeners( imgLoader2.contentLoaderInfo );
if( contentLoaded == 1)
imgLoader2.load( new URLRequest( imgURL ), context );
else
imgLoader1.load( new URLRequest( imgURL ), context );
}
/*************************************************************
* Image Event Listeners
*************************************************************/
private function enableBasicListeners( dispatcher:IEventDispatcher ):void
{
dispatcher.addEventListener( Event.COMPLETE, imgLoadedHandler, false, 0, true );
dispatcher.addEventListener( ProgressEvent.PROGRESS, imgProgressHandler, false, 0, true );
dispatcher.addEventListener( HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true );
dispatcher.addEventListener( SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true );
dispatcher.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true );
}
private function disableBasicListeners( dispatcher:IEventDispatcher ):void
{
dispatcher.removeEventListener( Event.COMPLETE, imgLoadedHandler );
dispatcher.removeEventListener( ProgressEvent.PROGRESS, imgProgressHandler );
dispatcher.removeEventListener( HTTPStatusEvent.HTTP_STATUS, httpStatusHandler );
dispatcher.removeEventListener( SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler );
dispatcher.removeEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );
}
/*************************************************************
* Helpers
*************************************************************/
private function buildMask():void
{
imgMask = new Sprite();
imgMask.graphics.beginFill( 0xFFFFFF );
imgMask.graphics.drawRect( 0, 0, containerMC.width, containerMC.height );
imgMask.graphics.endFill();
containerMC.addChild( imgMask );
containerMC.mask = imgMask;
}
private function formatCaptionBox( box:Label ):void
{
//Style the Caption Box
tf = new TextFormat();
tf.size = 25;
tf.font = "Verdana";
tf.color = 0x333333;
tf.leading = 1;
box.setStyle( "textFormat", tf );
}
/*************************************************************
* Bitmap Image Helpers
*************************************************************/
private function centerBitmap( bmp:Bitmap ):void
{
var centerPoint:Point = getCenterPoint();
bmp.x = ( centerPoint.x ) - ( bmp.width / 2 );
bmp.y = ( centerPoint.y ) - ( bmp.height / 2 );
}
private function getCenterPoint():Point
{
//var center:Point = new Point( appStage.stageWidth / 2, appStage.stageHeight / 2 );
var center:Point = new Point( containerParams.width / 2, containerParams.height / 2 );
return center;
}
private function getAspectRatio( width:Number, height:Number ):Number
{
if (width > height)
return ( width / height );
else
return ( height / width );
}
private function adjustImageSize( bmp:Bitmap ):void
{
var aspectRatio:Number = getAspectRatio( bmp.width, bmp.height );
//imgContainerMc is a Dynamic Class so it will change heigth/width with each new image load.
// therefore, you must have consistent width/height that wont change => containerArgs.width
if( bmp.height > bmp.width ){
bmp.height = containerParams.height;
bmp.width = bmp.height / aspectRatio;
}
else if ( bmp.width > bmp.height ) {
bmp.width = containerParams.width;
bmp.height = bmp.width / aspectRatio;
}
bmp.smoothing = true;
}
/*************************************************************
* Event Handlers
*************************************************************/
private function imgLoadedHandler( e:Event ):void
{
var currentLoader:Loader = Loader( e.currentTarget.loader );
//Drop the Alpha of the Image so that I can tween
currentLoader.alpha = 0;
//If the currentLoader is a Bitmap image, center it
if( currentLoader.content is Bitmap ){
var bmp:Bitmap = e.currentTarget.content as Bitmap;
//If the Image loaded is larger than the Canvas, resize the image and center it
if( bmp.width > containerParams.width || bmp.height > containerParams.height ) adjustImageSize( bmp );
//Center the Bitmap Image
centerBitmap( bmp );
}
//Otherwise do something else
else if( currentLoader.content is MovieClip ){
}
//Fade out the Previous Image
var prevLoader:String = currentLoader.name.substring( 0,( currentLoader.name.length - 1)) + contentLoaded;
TweenLite.to( containerMC.getChildByName( prevLoader ), 0.4, { alpha:0, onUpdate:onProgressUpdate, onUpdateParams:[containerMC.getChildByName( prevLoader)] } );
//This Toggle between which Image Loader to use
if( contentLoaded == 1) contentLoaded = 2;
else contentLoaded = 1;
//Add the Image to the Container MovieClip
containerMC.addChild( currentLoader );
//Tweeen in the Image
TweenLite.to( currentLoader, 0.5, { alpha:1, onStart:updateCaption } );
}
private function updateCaption():void
{
//Update the Caption
captionLBL.htmlText = imgData[currentIndex].caption;;
}
/*************************************************************
* Basic Event Handlers
*************************************************************/
private function onProgressUpdate( l:Loader ):void
{
//trace( "Previous Loader.alpha: " + l.alpha );
}
private function imgProgressHandler( e:ProgressEvent ):void
{
//captionLBL.text = Math.floor( e.bytesLoaded / e.bytesTotal * 100) + "%" ;
}
private function httpStatusHandler( e:HTTPStatusEvent ):void
{
//captionLBL.text = ( "httpStatusHandler:" + e ).toString();
}
private function securityErrorHandler( e:SecurityErrorEvent ):void
{
captionLBL.text = ( "securityErrorHandler:" + e ).toString();
}
private function ioErrorHandler( e:IOErrorEvent ):void
{
captionLBL.text = ( "ioErrorHandler: " + e ).toString();
}
}
}
0 commit comments