~extremepopcorn/dhlib/dhlib_ep

« back to all changes in this revision

Viewing changes to lib/system/Loader.mhx

  • Committer: edA-qa mort-ora-y
  • Date: 2010-02-16 05:36:32 UTC
  • Revision ID: eda-qa@disemia.com-20100216053632-60lt7fndfi3fgblw
first

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* <license>
 
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.
 
4
 * </license> 
 
5
 */
 
6
package system;
 
7
 
 
8
#if flash
 
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;
 
15
 
 
16
#else 
 
17
#error
 
18
#end
 
19
 
 
20
private enum LoadType
 
21
{
 
22
        Binary;
 
23
        Text;
 
24
        Graphic;
 
25
}
 
26
 
 
27
/*final*/ class Loader implements LoaderFeedback
 
28
{
 
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;
 
33
        
 
34
        /**
 
35
         * Indicates URLs are relative to this base object.
 
36
         */
 
37
        static public var loaderBase : flash.display.DisplayObject;
 
38
        
 
39
        /**
 
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
 
43
         * of preloaders.
 
44
         */
 
45
        public static function resolveURL( url : String, ?base : flash.display.DisplayObject  )
 
46
        {
 
47
                if( base == null )
 
48
                {
 
49
                        if( loaderBase == null )
 
50
                                loaderBase = flash.Lib.current.stage;
 
51
                        base = loaderBase;
 
52
                }
 
53
                        
 
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();         
 
58
        }
 
59
        
 
60
        /**
 
61
         * Loads data from a URL as binary data
 
62
         *
 
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
 
66
         */
 
67
        public static function urlBinary( url : String, action : ui.Action )
 
68
        {
 
69
                return new Loader( url, action, LoadType.Binary );
 
70
        }
 
71
        
 
72
        /**
 
73
         * Loads textual data from a URL.
 
74
         *
 
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
 
78
         */
 
79
        public static function urlText( url : String, action : ui.Action )
 
80
        {
 
81
                return new Loader( url, action, LoadType.Text );
 
82
        }
 
83
        
 
84
        /**
 
85
         * Loads data from a URL as binary data
 
86
         *
 
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
 
90
         */
 
91
        public static function urlGraphic( url : String, action : ui.Action )
 
92
        {
 
93
                return new Loader( url, action, LoadType.Graphic );
 
94
        }
 
95
        
 
96
        var loader : URLLoader;
 
97
        var gLoader : flash.display.Loader;
 
98
        var loadType : LoadType;
 
99
        var action : ui.Action;
 
100
        
 
101
        /*private*/ function new( url : String, action : ui.Action, as : LoadType )
 
102
        {
 
103
                this.action = action;
 
104
                this.loadType = as;
 
105
                
 
106
                done = false;
 
107
                error = null;
 
108
                totalSize = null;
 
109
                loadedSize = 0;
 
110
                
 
111
                //NOTE: the flash API follows a pattern, but no interface, so reducing duplication is not so clear here
 
112
                if( loadType == LoadType.Graphic )
 
113
                {
 
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 ) ) );
 
119
                }
 
120
                else
 
121
                {
 
122
                        loader = new URLLoader();
 
123
                        loader.dataFormat = loadType == LoadType.Binary
 
124
                                ?       URLLoaderDataFormat.BINARY
 
125
                                : URLLoaderDataFormat.TEXT
 
126
                                ;
 
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 ) ) );
 
131
                }
 
132
                
 
133
        }
 
134
        
 
135
        function onComplete( evt : Event )
 
136
        {
 
137
                done = true;
 
138
                //see note in URLSprite regarding gLoader.content
 
139
                ui.Action.handle( null, action, [ loadType == LoadType.Graphic ? gLoader : loader.data ] );
 
140
        }
 
141
        
 
142
        function onError( evt : IOErrorEvent )
 
143
        {
 
144
                done = true;
 
145
                error = "" + evt;
 
146
                ui.Action.handle( null, action, [ null ] );
 
147
        }
 
148
        
 
149
        public function getTotalSize() : Null<Int>
 
150
        {
 
151
                if( loadType == LoadType.Graphic )
 
152
                {
 
153
                        var cli = gLoader.contentLoaderInfo;
 
154
                        if( cli.bytesTotal == 0 )
 
155
                                return null;
 
156
                                
 
157
                        return cli.bytesTotal;
 
158
                }       
 
159
                
 
160
                if( loader.bytesTotal == 0)
 
161
                        return null;
 
162
                        
 
163
                return loader.bytesTotal;
 
164
        }
 
165
        
 
166
        public function getLoadedSize() : Int
 
167
        {
 
168
                if( loadType == LoadType.Graphic )
 
169
                        return gLoader.contentLoaderInfo.bytesLoaded;
 
170
                return loader.bytesLoaded;
 
171
        }
 
172
        
 
173
        public function isDone() : Bool
 
174
        {
 
175
                return done;
 
176
        }
 
177
        
 
178
        public function getError() : String
 
179
        {
 
180
                return error;
 
181
        }
 
182
}