~eda-qa/dhlib/main

« back to all changes in this revision

Viewing changes to lib/ui/URLSprite.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 (c) edA-qa mort-ora-y
 
3
 * For full copyright and license information please refer to doc/license.txt.
 
4
 * </license> 
 
5
 */
 
6
package ui;
 
7
 
 
8
import flash.display.Sprite;
 
9
import flash.events.Event;
 
10
import flash.events.ProgressEvent;
 
11
import flash.display.Loader;
 
12
import flash.net.URLRequest;
 
13
 
 
14
import ui.anim.lib.CircularProgress;
 
15
 
 
16
import DrawBasicTypes;
 
17
 
 
18
/**
 
19
 * A wrapper class to use a given URL as a display object. This simplifies loading
 
20
 * external objects directly into a target widget -- displaying progress as it loads.
 
21
 *
 
22
 * Events:
 
23
 *              Event.COMPLETE when a URL has finished loading
 
24
 *   ? > ErrorEvent if something goes wrong
 
25
 *                      IO_ERROR
 
26
 *                      SECURITY_ERROR
 
27
 */
 
28
class URLSprite extends Sprite, implements Widget
 
29
{
 
30
        var loader : Loader;
 
31
        var loaded : flash.display.DisplayObject;
 
32
        var tback : Widget; //a temporary background while loading, on start, or on error
 
33
        var useTBack : Bool;
 
34
        
 
35
        /**
 
36
         * @param       url [in] the url of the image to display, null if nothing at creation time
 
37
         * @param       tback   [in] true (default) to show an image on loading/error, false to display nothing
 
38
         */
 
39
        public function new( url : String, ?tback : Null<Bool> )
 
40
        {
 
41
                super();
 
42
                MixinWidget();
 
43
                
 
44
                useTBack = (tback == null) ? true : tback;
 
45
                
 
46
                loadURL( url );
 
47
        }
 
48
        
 
49
        /**
 
50
         * Sets a new URL for the display in this object.
 
51
         *
 
52
         * TODO: if this is called before the previous one is finished the events from
 
53
         * the previous one will still be triggered and likely cause an error.
 
54
         */
 
55
        public function loadURL( url : String )
 
56
        {
 
57
                //remove existing children
 
58
                if( loaded != null )
 
59
                {
 
60
                        removeChild( loaded );
 
61
                        loaded = null;
 
62
                }
 
63
                disableTBack();
 
64
                        
 
65
                if( url != null )
 
66
                {
 
67
                        loader = new Loader();
 
68
                        loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onComplete );
 
69
                        loader.contentLoaderInfo.addEventListener( flash.events.IOErrorEvent.IO_ERROR, onError );
 
70
                        loader.contentLoaderInfo.addEventListener( flash.events.SecurityErrorEvent.SECURITY_ERROR, onError );
 
71
                        loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, onProgress );
 
72
                        loader.load( new URLRequest( url ) );
 
73
                }
 
74
                
 
75
                if( useTBack )
 
76
                        enableTBack();
 
77
                        
 
78
                //force update now
 
79
                update();
 
80
        }
 
81
        
 
82
        function enableTBack()
 
83
        {
 
84
                if( tback == null )
 
85
                {
 
86
                        //old drawing of loading...
 
87
                        //tback = new ui.DrawBox( drawLoading );
 
88
                        
 
89
                        //TODO: wrong positioning during loading!
 
90
                        tback = new CircularProgress( { color: Color.rgb(0,0.8,0) } );
 
91
                        addChild( tback.getNative() );
 
92
                }
 
93
        }
 
94
        
 
95
        function disableTBack()
 
96
        {
 
97
                if( tback != null )
 
98
                {
 
99
                        removeChild( tback.getNative() );
 
100
                        tback = null;
 
101
                }
 
102
        }
 
103
        
 
104
        function onComplete( evt : Event )
 
105
        {
 
106
                disableTBack();         
 
107
                
 
108
                //one would suspect using the loader.content directly might be more managable,
 
109
                //but a stupid security error gets in the way, somehow loader.content is considered
 
110
                //scripting access and flags the sandbox error, so we live with just making the loader
 
111
                //itself a child (adding a server proxy or domain files to all other servers would make
 
112
                //direct use possible, but since this seems to work normally there is no need)
 
113
                loaded = loader;        //.content;
 
114
                addChild( loaded );
 
115
                
 
116
                update();
 
117
                
 
118
                //tell listeners that we're done
 
119
                dispatchEvent( new Event( Event.COMPLETE ) );
 
120
        }
 
121
        
 
122
        function onError( evt : Event )
 
123
        {
 
124
                //always show an error display, even if loading graphic not requested...
 
125
                disableTBack();
 
126
                tback = ui.DrawBox.identity( drawError );
 
127
                addChild( tback.getNative() );
 
128
                update();
 
129
                
 
130
                //tell listeners something has gone wrong
 
131
                dispatchEvent( evt.clone() );
 
132
        }
 
133
        
 
134
        function onProgress( evt : ProgressEvent )
 
135
        {
 
136
                //TODO: identify any actor which implements Progress
 
137
                if( !Std.is( tback, CircularProgress ) )
 
138
                        return;
 
139
                var cp = cast( tback, CircularProgress );
 
140
                cp.actorSetTrait1( cp.trait.General.Level, evt.bytesLoaded * 1.0 / evt.bytesTotal );
 
141
        }
 
142
        
 
143
        function _resize( w : Float, h : Float )
 
144
        {
 
145
                if( loaded != null )
 
146
                {
 
147
                        loaded.width = w;
 
148
                        loaded.height = h;
 
149
                }
 
150
                
 
151
                if( tback != null )
 
152
                {
 
153
                        tback.resize( w, h );
 
154
                        //quick repositioning (perhaps only for default background?)
 
155
                        var ctr = getCenter().sub( tback.getCenter() );
 
156
                        tback.move( w * ctr.x, h * ctr.y );
 
157
                }
 
158
        }
 
159
        
 
160
        static function drawLoading( graphics : draw.GraphicsX, w : Float, h : Float )
 
161
        {
 
162
                var lw = w / 10;
 
163
                graphics.use( Pen.solid( lw, Color.rgb(0,0.5,0), 0.5 ) );
 
164
                var off = 0.1;
 
165
                var eoff = 1 - 2 * off;
 
166
                graphics.drawEllipse( w * off, h * off, w * eoff, h * eoff );
 
167
        }
 
168
        
 
169
        static function drawError( graphics : draw.GraphicsX, w : Float, h : Float )
 
170
        {
 
171
                var lw = w / 10;
 
172
                graphics.use( Pen.solid( lw, Color.rgb(0.5,0,0), 0.5 ) );
 
173
                var off = 0.1;
 
174
                var eoff = 1 - 2 * off;
 
175
                graphics.moveTo( w * off, h * off );
 
176
                graphics.lineTo( w * eoff, h * eoff );
 
177
                graphics.moveTo( w * eoff, h * off );
 
178
                graphics.lineTo( w * off, h * eoff );
 
179
        }
 
180
        
 
181
        include(`MixinWidget.ihx')
 
182
}