1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4   
     5  import org.aswing.Event;
     6  import org.aswing.FloorPane;
     7  import org.aswing.geom.Dimension;
     8  import org.aswing.utils.Delegate;
     9  import org.aswing.utils.MCUtils;
    10  
    11  /**
    12   * JLoadPane, a container load a external image/animation to be its floor.
    13   * @see org.aswing.JAttachPane
    14   * @author iiley
    15   */
    16  class org.aswing.JLoadPane extends FloorPane {
    17  	
    18  	/**
    19  	 * When the file that was loaded.
    20  	 *<br>
    21  	 * onLoadComplete Event{source:Component}
    22  	 */	
    23  	public static var ON_LOAD_COMPLETE:String = "onLoadComplete";
    24  	/**
    25  	 * When the file loaded has failed to load.<br>
    26  	 * errorCode:A string that explains the reason for the failure, either "URLNotFound" or "LoadNeverCompleted".
    27  	 *<br>
    28  	 * onLoadError Event{source:Component, errorCode:String}
    29  	 */	
    30  	public static var ON_LOAD_ERROR:String = "onLoadError";
    31  	/**
    32  	 * When the actions on the first frame of the loaded clip have been executed.
    33  	 *<br>
    34  	 * onLoadInit Event{source:Component}
    35  	 */	
    36  	public static var ON_LOAD_INIT:String = "onLoadInit";
    37  	/**
    38  	 * Fires every time the loading content is written to the hard disk during the loading process
    39  	 *<br>
    40  	 * onLoadProgress Event{source:Component, loadedBytes:Number, totalBytes:Number}
    41  	 */	
    42  	public static var ON_LOAD_PROGRESS:String = "onLoadProgress";
    43  	/**
    44  	 * When the a call has begun to download a file.
    45  	 *<br>
    46  	 * onLoadStart Event{source:Component}
    47  	 */	
    48  	public static var ON_LOAD_START:String = "onLoadStart";
    49  	
    50  	
    51  	private var movieClipLoader:MovieClipLoader;
    52  	private var loaderMC:MovieClip;
    53  	private var loadedError:Boolean;
    54  	
    55  	/**
    56  	 * JLoadPane(path:String, prefferSizeStrategy:Number) <br>
    57  	 * JLoadPane(path:String) prefferSizeStrategy default to PREFER_SIZE_BOTH<br>
    58  	 * JLoadPane() path default to null,prefferSizeStrategy default to PREFER_SIZE_BOTH
    59  	 * <p>
    60  	 * Creates a JLoadPane with a path to load external image or animation file.
    61  	 * @param path the path of the extenal image/animation file.
    62  	 * @param prefferSizeStrategy the prefferedSize count strategy. Must be one of below:
    63  	 * <ul>
    64  	 * <li>{@link org.aswing.FloorPane#PREFER_SIZE_BOTH}
    65  	 * <li>{@link org.aswing.FloorPane#PREFER_SIZE_IMAGE}
    66  	 * <li>{@link org.aswing.FloorPane#PREFER_SIZE_LAYOUT}
    67  	 * </ul>
    68  	 * @see #setPath()
    69  	 */
    70  	public function JLoadPane(path:String, prefferSizeStrategy:Number) {
    71  		super(path, prefferSizeStrategy);
    72  		movieClipLoader = new MovieClipLoader();
    73  		var lis:Object = new Object();
    74  		lis["onLoadComplete"] = Delegate.create(this, __onLoadComplete);
    75  		lis["onLoadError"] = Delegate.create(this, __onLoadError);
    76  		lis["onLoadInit"] = Delegate.create(this, __onLoadInit);
    77  		lis["onLoadProgress"] = Delegate.create(this, __onLoadProgress);
    78  		lis["onLoadStart"] = Delegate.create(this, __onLoadStart);
    79  		movieClipLoader.addListener(lis);
    80  		loadedError = false;
    81  	}
    82  	
    83  	/**
    84  	 * Returns the loaded target movieclip.
    85  	 * @return the loaded target movieclip.
    86  	 * @see #getFloorMC()
    87  	 */
    88  	public function getFloorMC():MovieClip{
    89  		return loaderMC;
    90  	}
    91  	
    92  	/**
    93  	 * Returns is error loaded.
    94  	 * @see #ON_LOAD_ERROR
    95  	 */
    96  	public function isLoadedError():Boolean{
    97  		return loadedError;
    98  	}
    99  	
   100  	/**
   101  	 * load the floor content.
   102  	 * <p> here it is empty.
   103  	 * Subclass must override this method to make loading.
   104  	 */
   105  	private function loadFloor():Void{
   106  		if(MCUtils.isMovieClipExist(loaderMC)){
   107  			loadedError = false;
   108  			movieClipLoader.loadClip(getPath(), loaderMC);
   109  		}
   110  	}
   111  	
   112  	/**
   113  	 * Create the floor mc.
   114  	 * <p> here it is empty.
   115  	 * Subclass must override this method to make creating.
   116  	 */
   117  	private function createFloorMC():MovieClip{
   118  		if(MCUtils.isMovieClipExist(target_mc)){
   119  			floorMC = creater.createMC(target_mc, "floor", getFloorDepth());
   120  			loaderMC = creater.createMCWithName(floorMC, "loader");
   121  		}
   122  		return floorMC;
   123  	}
   124  	
   125  	/**
   126  	 * Returns a object contains <code>bytesLoaded</code> and <code>bytesTotal</code> 
   127  	 * properties that indicate the current loading status.
   128  	 */
   129  	public function getProgress():Object{
   130  		return movieClipLoader.getProgress(loaderMC);
   131  	}
   132  	
   133  	//-----------------------------------------------
   134  
   135  	private function __onLoadComplete(targetMC:MovieClip):Void{
   136  		dispatchEvent(ON_LOAD_COMPLETE, createEventObj(ON_LOAD_COMPLETE));
   137  	}
   138  	
   139  	private function __onLoadError(targetMC:MovieClip, errorCode:String):Void{
   140  		loadedError = true;
   141  		var obj:Event = createEventObj(ON_LOAD_ERROR);
   142  		obj["errorCode"] = errorCode;
   143  		dispatchEvent(ON_LOAD_ERROR, obj);
   144  	}
   145  	
   146  	private function __onLoadInit(targetMC:MovieClip):Void{
   147  		setFloorOriginalSize(new Dimension(floorMC._width, floorMC._height));
   148  		setLoaded(true);
   149  		valid = false;
   150  		dispatchEvent(ON_LOAD_INIT, createEventObj(ON_LOAD_INIT));
   151  		revalidate();
   152  		validate();
   153  	}
   154  	
   155  	private function __onLoadProgress(targetMC:MovieClip, loadedBytes:Number, totalBytes:Number):Void{
   156  		var obj:Event = createEventObj(ON_LOAD_PROGRESS);
   157  		obj["loadedBytes"] = loadedBytes;
   158  		obj["totalBytes"] = totalBytes;
   159  		dispatchEvent(ON_LOAD_PROGRESS, obj);
   160  	}
   161  	
   162  	private function __onLoadStart(targetMC:MovieClip):Void{
   163  		dispatchEvent(ON_LOAD_START, createEventObj(ON_LOAD_START));
   164  	}
   165  }
   166