2
* This file is part of the dis-Emi-A HaXe Library. Copyright © edA-qa mort-ora-y
3
* For full copyright and license information please refer to doc/license.txt.
9
import flash.events.Event;
10
import flash.events.IOErrorEvent;
11
import flash.events.SecurityErrorEvent;
12
import flash.net.URLLoader;
13
import flash.net.URLLoaderDataFormat;
14
import flash.net.URLRequest;
27
/*final*/ class Loader implements LoaderFeedback
29
public var done(default,null) : Bool;
30
public var error(default,null) : String;
31
public var totalSize(getTotalSize,null) : Null<Int>; //if null it means totalSize is currently unknown
32
public var loadedSize(getLoadedSize,null) : Int;
35
* Indicates URLs are relative to this base object.
37
static public var loaderBase : flash.display.DisplayObject;
40
* Resolves a URL relative to the current loaderInfo URL. This is quite different
41
* than what passing the URL directly to the URLRequest does, in which case
42
* it is relative to the outermost enclosing URL -- which is different in the case
45
public static function resolveURL( url : String, ?base : flash.display.DisplayObject )
49
if( loaderBase == null )
50
loaderBase = flash.Lib.current.stage;
54
var baseURL = URI.parsed( base.loaderInfo.loaderURL );
55
var target = URI.parsed( url, true /*allow relative*/ );
56
var full = baseURL.transformReference( target );
57
return full.toString();
61
* Loads data from a URL as binary data
63
* @param url [in] the string url to load, relative to current program
64
* @param action [in] the action to execute on completion, it will be
65
* called with 1 parameter, the ByteArray if loaded, or null if failed
67
public static function urlBinary( url : String, action : ui.Action )
69
return new Loader( url, action, LoadType.Binary );
73
* Loads textual data from a URL.
75
* @param url [in] the string url to load, relative to current program, or absolute
76
* @param action [in] the aciton to execute on completion, it will
77
* be called with 1 parameter, the String if loaded, or null if failed
79
public static function urlText( url : String, action : ui.Action )
81
return new Loader( url, action, LoadType.Text );
85
* Loads data from a URL as binary data
87
* @param url [in] the string url to load, relative to current program
88
* @param action [in] the action to execute on completion, it will be
89
* called with 1 parameter, the flash.display.Loader if loaded, or null if failed
91
public static function urlGraphic( url : String, action : ui.Action )
93
return new Loader( url, action, LoadType.Graphic );
96
var loader : URLLoader;
97
var gLoader : flash.display.Loader;
98
var loadType : LoadType;
99
var action : ui.Action;
101
/*private*/ function new( url : String, action : ui.Action, as : LoadType )
103
this.action = action;
111
//NOTE: the flash API follows a pattern, but no interface, so reducing duplication is not so clear here
112
if( loadType == LoadType.Graphic )
114
gLoader = new flash.display.Loader();
115
gLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, onComplete );
116
gLoader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, onError );
117
gLoader.contentLoaderInfo.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onError );
118
gLoader.load( new URLRequest( resolveURL( url ) ) );
122
loader = new URLLoader();
123
loader.dataFormat = loadType == LoadType.Binary
124
? URLLoaderDataFormat.BINARY
125
: URLLoaderDataFormat.TEXT
127
loader.addEventListener( Event.COMPLETE, onComplete );
128
loader.addEventListener( IOErrorEvent.IO_ERROR, onError );
129
loader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onError );
130
loader.load( new URLRequest( resolveURL( url ) ) );
135
function onComplete( evt : Event )
138
//see note in URLSprite regarding gLoader.content
139
ui.Action.handle( null, action, [ loadType == LoadType.Graphic ? gLoader : loader.data ] );
142
function onError( evt : IOErrorEvent )
146
ui.Action.handle( null, action, [ null ] );
149
public function getTotalSize() : Null<Int>
151
if( loadType == LoadType.Graphic )
153
var cli = gLoader.contentLoaderInfo;
154
if( cli.bytesTotal == 0 )
157
return cli.bytesTotal;
160
if( loader.bytesTotal == 0)
163
return loader.bytesTotal;
166
public function getLoadedSize() : Int
168
if( loadType == LoadType.Graphic )
169
return gLoader.contentLoaderInfo.bytesLoaded;
170
return loader.bytesLoaded;
173
public function isDone() : Bool
178
public function getError() : String