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.
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;
14
import ui.anim.lib.CircularProgress;
16
import DrawBasicTypes;
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.
23
* Event.COMPLETE when a URL has finished loading
24
* ? > ErrorEvent if something goes wrong
28
class URLSprite extends Sprite, implements Widget
31
var loaded : flash.display.DisplayObject;
32
var tback : Widget; //a temporary background while loading, on start, or on error
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
39
public function new( url : String, ?tback : Null<Bool> )
44
useTBack = (tback == null) ? true : tback;
50
* Sets a new URL for the display in this object.
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.
55
public function loadURL( url : String )
57
//remove existing children
60
removeChild( loaded );
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 ) );
82
function enableTBack()
86
//old drawing of loading...
87
//tback = new ui.DrawBox( drawLoading );
89
//TODO: wrong positioning during loading!
90
tback = new CircularProgress( { color: Color.rgb(0,0.8,0) } );
91
addChild( tback.getNative() );
95
function disableTBack()
99
removeChild( tback.getNative() );
104
function onComplete( evt : Event )
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;
118
//tell listeners that we're done
119
dispatchEvent( new Event( Event.COMPLETE ) );
122
function onError( evt : Event )
124
//always show an error display, even if loading graphic not requested...
126
tback = ui.DrawBox.identity( drawError );
127
addChild( tback.getNative() );
130
//tell listeners something has gone wrong
131
dispatchEvent( evt.clone() );
134
function onProgress( evt : ProgressEvent )
136
//TODO: identify any actor which implements Progress
137
if( !Std.is( tback, CircularProgress ) )
139
var cp = cast( tback, CircularProgress );
140
cp.actorSetTrait1( cp.trait.General.Level, evt.bytesLoaded * 1.0 / evt.bytesTotal );
143
function _resize( w : Float, h : Float )
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 );
160
static function drawLoading( graphics : draw.GraphicsX, w : Float, h : Float )
163
graphics.use( Pen.solid( lw, Color.rgb(0,0.5,0), 0.5 ) );
165
var eoff = 1 - 2 * off;
166
graphics.drawEllipse( w * off, h * off, w * eoff, h * eoff );
169
static function drawError( graphics : draw.GraphicsX, w : Float, h : Float )
172
graphics.use( Pen.solid( lw, Color.rgb(0.5,0,0), 0.5 ) );
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 );
181
include(`MixinWidget.ihx')