/** * com.sekati.display.ImageClip * @version 1.0.0 * @author jason m horwitz | sekati.com | tendercreative.com * Copyright (C) 2007 jason m horwitz, Sekat LLC. All Rights Reserved. * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php */ import com.sekati.display.IImageClip; import com.sekati.display.BaseClip; import com.sekati.utils.Delegate; import com.sekati.utils.MovieClipUtils; import com.sekati.except.FatalException; /** * ImageClip - load an image available for manipulation. * NOTE: The clip image is automatically hidden (_alpha:0) until manually tweened in by developer. * * {@code Usage: * import com.sekati.utils.* * import caurina.transitions.Tweener; * * function _onProgress(l:Number, t:Number):Void { trace("img_onProgress: "+l+" / "+t); } * function _onInit():Void { Tweener.addTween(image, {_alpha:100, time:0.3, transition:"easeInOutQuint"}); } * function _onError(code:String):Void { trace("oh noes! The image couldnt be loaded: "+ecode); } * * // create an empty ImageClip * var image:MovieClip = ClassUtils.createEmptyMovieClip (com.sekati.display.ImageClip, this, "image", {_x:0, _y:0}); * * // load an image and define onProgress onInit Delegates. * image.load("test.jpg", Delegate.create(this, _onInit), Delegate.create(this, _onProgress), Delegate.create(this, _onError)); * } */ class com.sekati.display.ImageClip extends BaseClip implements IImageClip { public var img:MovieClip; private var _uri:String; private var _progressCb:Function; private var _initCb:Function; private var _errorCb:Function; private var _loader:MovieClipLoader; /** * Constructor */ public function ImageClip() { super( ); _loader = new MovieClipLoader( ); _this.onLoadInit = Delegate.create( _this, _onInit ); _this.onLoadError = Delegate.create( _this, _onError ); _this.onLoadProgress = Delegate.create( _this, _onProgress ); _loader.addListener( _this ); } /** * Load an image * @param uri (String) * @param onInit (Function) - cb when image is loaded, converted, ready for use. * @param onProgress (Function) - cb is passed args: bytesLoaded, bytesTotal. * @param onError (Function) - cb is passed arg errorCode, httpStatus. * @return Void */ public function load(uri:String, onInit:Function, onProgress:Function, onError:Function):Void { _uri = uri; _progressCb = onProgress; _initCb = onInit; _errorCb = onError; createContainers( ); _loader.loadClip( _uri, img ); } /** * Remove the image. * @return Void */ public function unload():Void { _loader.unloadClip( img ); MovieClipUtils.rmClip( img ); } /** * Create the temporary image Container * @return Void */ private function createContainers():Void { if (img) MovieClipUtils.rmClip( img ); img = _this.createEmptyMovieClip( "img", _this.getNextHighestDepth( ) ); _this._alpha = 0; } /** * Clear existing callbacks in preparation for another load * @return Void */ private function clearCallbacks():Void { _initCb = undefined; _progressCb = undefined; _errorCb = undefined; } /** * Once external image is loaded, run the {@link _initCb} if it was defined during {@link load}. * @return Void */ private function _onInit():Void { _initCb( ); clearCallbacks( ); } /** * Call the onProgress cb * @param target (MovieClip) * @param bytesLoaded (Number) * @param bytesTotal (Number) * @return Void */ private function _onProgress(target:MovieClip, bytesLoaded:Number, bytesTotal:Number):Void { //trace("loading: "+target + " " + bytesLoaded + "/" + bytesTotal); _progressCb( bytesLoaded, bytesTotal ); } /** * Call the onError cb * @param errorCode (String) * @param httpStatus (String) */ private function _onError(target_mc:MovieClip, errorCode:String, httpStatus:Number):Void { _errorCb( errorCode, httpStatus ); clearCallbacks( ); throw new FatalException( _this, "Could not load image from url:" + _uri, arguments ); } }