1  /**
     2   * com.sekati.ui.Image
     3   * @version 2.5.5
     4   * @author jason m horwitz | sekati.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.CoreClip;
    10  import com.sekati.transitions.Mot;
    11  import com.sekati.utils.Delegate;
    12  import caurina.transitions.Tweener;
    13  
    14  /**
    15   * image loader. Required: mc_tween2
    16   */
    17  class com.sekati.ui.Image extends CoreClip {
    18  
    19  	var img:MovieClip;
    20  	var box:MovieClip;
    21  	var spinner:MovieClip;
    22  
    23  	// constructor
    24  	private function Image() {
    25  	}
    26  
    27  	/**
    28  	 * init called via CoreClip onLoad event
    29  	 * @return Void
    30  	 */
    31  	private function configUI():Void {
    32  		_this.spinner._visible = false;
    33  	}
    34  
    35  	/**
    36  	 * wrapper for setup and init methods
    37  	 * @param uri (String) image uri
    38  	 * @param w (Number) image width
    39  	 * @param h (Number) image height
    40  	 * @param color (Number) box hexadecimal color
    41  	 * @param color2 (Number) spinner & border hexadecimal color	
    42  	 * @param alpha (Number) box alpha
    43  	 * @param border (Boolean) display border
    44  	 * @param cb (Function) optional callback function		
    45  	 * @return Void
    46  	 * 
    47  	 * {@code Usage:
    48  	 * myMc.load("test.jpg", 160, 120, 0x242424, 0x99abc1, 80, true, cb);
    49  	 * }
    50  	 */
    51  	public function load(uri:String, w:Number, h:Number, color:Number, color2:Number, alpha:Number, border:Boolean, cb:Function):Void {
    52  		_this.setup( w, h, color, color2, alpha, border );
    53  		_this.init( uri, cb );
    54  	}
    55  
    56  	/**
    57  	 * draw a rectangle (various init options)
    58  	 * @param w (Number) image width
    59  	 * @param h (Number) image height
    60  	 * @param color (Number) box hexadecimal color
    61  	 * @param color2 (Number) spinner & border hexadecimal color	
    62  	 * @param alpha (Number) box alpha
    63  	 * @param border (Boolean) display border
    64  	 * @return Void
    65  	 * 
    66  	 * {@code Usage:
    67  	 * myMc.setup(160, 120, 0x242424, 0x99abc1, 80, true);
    68  	 * }
    69  	 */
    70  	public function setup(w:Number, h:Number, color:Number, color2:Number, alpha:Number, border:Boolean):Void {
    71  		// make instance reusable
    72  		if (_this.box != undefined) {
    73  			_this.box.removeMovieClip( );
    74  		}
    75  		if (_this.img != undefined) {
    76  			_this.img.removeMovieClip( );
    77  		}
    78  		_this.createEmptyMovieClip( "img", _this.getNextHighestDepth( ) );
    79  		// create box & enable spinner if a box color was passed
    80  		if (color) {
    81  			_this.createEmptyMovieClip( "box", _this.getNextHighestDepth( ) );
    82  			var c:Number = (color) ? color : 0xFFFFFF;
    83  			var a:Number = (alpha) ? alpha : 100;
    84  			var lw:Number = (border) ? 1 : 0;
    85  			var c2:Number = (color2) ? color2 : 0x000000;
    86  			// offset for 1px border
    87  			if (lw == 1) {
    88  				w += lw;
    89  				h += lw;
    90  				_this.img._x += lw;
    91  				_this.img._y += lw;
    92  			}
    93  			if (border) {
    94  				box.lineStyle( lw, c2, a );
    95  			}
    96  			box.beginFill( c, a );
    97  			box.moveTo( 0, 0 );
    98  			box.lineTo( w, 0 );
    99  			box.lineTo( w, h );
   100  			box.lineTo( 0, h );
   101  			box.endFill( );
   102  		}
   103  		// setup spinner                                                           
   104  		_this.spinner._x = int( (w / 2) );
   105  		_this.spinner._y = int( (h / 2) );
   106  		Tweener.addTween(_this.spinner, {_color:c2, time:0});
   107  	}
   108  
   109  	/**
   110  	 * load an image after setup has been called
   111  	 * @param uri (String) image uri
   112  	 * @param cb (Function) optional callback function
   113  	 * @return Void
   114  	 * 
   115  	 * {@code Usage:
   116  	 * myMc.init ("test.jpg", cb);
   117  	 * }
   118  	 */
   119  	public function init(uri:String, cb:Function):Void {
   120  		_this.img.unloadMovie( );
   121  		_this.img._alpha = 0;
   122  		_this.img.swapDepths( _this.getNextHighestDepth( ) );
   123  		_this.spinner.swapDepths( _this.getNextHighestDepth( ) );
   124  		_this.img.loadMovie( uri );
   125  		_this.spinner._visible = true;
   126  		_this.spinner._alpha = 100;
   127  		delete _this.spinner.onEnterFrame;
   128  		_this.spinner.onEnterFrame = function ():Void {
   129  			this._rotation -= 20;
   130  		};
   131  		_this.onEnterFrame = function ():Void {
   132  			var l:Number = _this.img.getBytesLoaded( );
   133  			var t:Number = _this.img.getBytesTotal( );
   134  			if (l >= t && t > 5) {
   135  				delete _this.onEnterFrame;
   136  				Tweener.removeTweens(_this.img);
   137  				Tweener.removeTweens(_this.spinner);
   138  				_this.img._alpha = 0;
   139  				_this.spinner._alpha = 100;
   140  				Tweener.addTween(_this.img, {_alpha:100, time:0.6, delay:0.1, transition:Mot.io.quad});
   141  				var spinCb:Function = function ():Void {
   142  					delete _this.spinner.onEnterFrame;
   143  					_this.spinner._visible = false;
   144  					_this.spinner._alpha = 100;
   145  				};
   146  				Tweener.addTween(_this.spinner, {_alpha:0, time:0.5, delay:0.3, transition:Mot.o.quint, onComplete:Delegate.create(_this, spinCb)});
   147  				if (cb) {
   148  					cb( );
   149  				}
   150  			}
   151  		};
   152  	}
   153  }