1  /**
     2   * com.sekati.display.ImageClip
     3   * @version 1.0.0
     4   * @author jason m horwitz | sekati.com | tendercreative.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   */
     8  
     9  import com.sekati.display.IImageClip;
    10  import com.sekati.display.BaseClip;
    11  import com.sekati.utils.Delegate;
    12  import com.sekati.utils.MovieClipUtils;
    13  import com.sekati.except.FatalException;
    14  /**
    15   * ImageClip - load an image available for manipulation.
    16   * NOTE: The clip image is automatically hidden (_alpha:0) until manually tweened in by developer.
    17   * 
    18   * {@code Usage:
    19   * 	import com.sekati.utils.*
    20   * 	import caurina.transitions.Tweener;
    21   * 	
    22   * 	function _onProgress(l:Number, t:Number):Void { trace("img_onProgress: "+l+" / "+t); }
    23   * 	function _onInit():Void { Tweener.addTween(image, {_alpha:100, time:0.3, transition:"easeInOutQuint"}); }
    24   * 	function _onError(code:String):Void { trace("oh noes! The image couldnt be loaded: "+ecode); }
    25   * 	
    26   * 	// create an empty ImageClip
    27   * 	var image:MovieClip =  ClassUtils.createEmptyMovieClip (com.sekati.display.ImageClip, this, "image", {_x:0, _y:0});
    28   * 	
    29   * 	// load an image and define onProgress onInit Delegates.
    30   * 	image.load("test.jpg", Delegate.create(this, _onInit), Delegate.create(this, _onProgress), Delegate.create(this, _onError));
    31   * }
    32   */
    33  class com.sekati.display.ImageClip extends BaseClip implements IImageClip {
    34  	public var img:MovieClip;
    35  	private var _uri:String;
    36  	private var _progressCb:Function;
    37  	private var _initCb:Function;
    38  	private var _errorCb:Function;
    39  	private var _loader:MovieClipLoader;
    40  	/**
    41  	 * Constructor
    42  	 */
    43  	public function ImageClip() {
    44  		super( );
    45  		_loader = new MovieClipLoader( );
    46  		_this.onLoadInit = Delegate.create( _this, _onInit );
    47  		_this.onLoadError = Delegate.create( _this, _onError );
    48  		_this.onLoadProgress = Delegate.create( _this, _onProgress );
    49  		_loader.addListener( _this );
    50  	}
    51  	/**
    52  	 * Load an image
    53  	 * @param uri (String)
    54  	 * @param onInit (Function) - cb when image is loaded, converted, ready for use.
    55  	 * @param onProgress (Function) - cb is passed args: bytesLoaded, bytesTotal.
    56  	 * @param onError (Function) - cb is passed arg errorCode, httpStatus.
    57  	 * @return Void
    58  	 */
    59  	public function load(uri:String, onInit:Function, onProgress:Function, onError:Function):Void {
    60  		_uri = uri;
    61  		_progressCb = onProgress;
    62  		_initCb = onInit;
    63  		_errorCb = onError;
    64  		createContainers( );
    65  		_loader.loadClip( _uri, img );	
    66  	}
    67  	/**
    68  	 * Remove the image.
    69  	 * @return Void
    70  	 */
    71  	public function unload():Void {
    72  		_loader.unloadClip( img );
    73  		MovieClipUtils.rmClip( img );
    74  	}
    75  	/**
    76  	 * Create the temporary image Container
    77  	 * @return Void
    78  	 */
    79  	private function createContainers():Void {
    80  		if (img) MovieClipUtils.rmClip( img );
    81  		img = _this.createEmptyMovieClip( "img", _this.getNextHighestDepth( ) );
    82  		_this._alpha = 0;
    83  	}
    84  	/**
    85  	 * Clear existing callbacks in preparation for another load
    86  	 * @return Void
    87  	 */
    88  	private function clearCallbacks():Void {
    89  		_initCb = undefined;
    90  		_progressCb = undefined;
    91  		_errorCb = undefined;		
    92  	}
    93  	/**
    94  	 * Once external image is loaded, run the {@link _initCb} if it was defined during {@link load}.
    95  	 * @return Void
    96  	 */
    97  	private function _onInit():Void {
    98  		_initCb( );
    99  		clearCallbacks( );
   100  	}
   101  	/**
   102  	 * Call the onProgress cb
   103  	 * @param target (MovieClip)
   104  	 * @param bytesLoaded (Number)
   105  	 * @param bytesTotal (Number)
   106  	 * @return Void
   107  	 */
   108  	private function _onProgress(target:MovieClip, bytesLoaded:Number, bytesTotal:Number):Void {
   109  		//trace("loading: "+target + " " + bytesLoaded + "/" + bytesTotal);
   110  		_progressCb( bytesLoaded, bytesTotal );
   111  	}
   112  	/**
   113  	 * Call the onError cb
   114  	 * @param errorCode (String)
   115  	 * @param httpStatus (String)
   116  	 */
   117  	private function _onError(target_mc:MovieClip, errorCode:String, httpStatus:Number):Void {
   118  		_errorCb( errorCode, httpStatus );
   119  		clearCallbacks( );
   120  		throw new FatalException( _this, "Could not load image from url:" + _uri, arguments );
   121  	}
   122  }
   123