~eda-qa/dhlib/main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* <license>
 * This file is part of the dis-Emi-A HaXe Library. Copyright (c) edA-qa mort-ora-y
 * For full copyright and license information please refer to doc/license.txt.
 * </license> 
 */
package ui;

import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.display.Loader;
import flash.net.URLRequest;

import ui.anim.lib.CircularProgress;

import DrawBasicTypes;

/**
 * A wrapper class to use a given URL as a display object. This simplifies loading
 * external objects directly into a target widget -- displaying progress as it loads.
 *
 * Events:
 *		Event.COMPLETE when a URL has finished loading
 *   ? > ErrorEvent if something goes wrong
 *			IO_ERROR
 *			SECURITY_ERROR
 */
class URLSprite extends Sprite, implements Widget
{
	var loader : Loader;
	var loaded : flash.display.DisplayObject;
	var tback : Widget; //a temporary background while loading, on start, or on error
	var useTBack : Bool;
	
	/**
	 * @param	url [in] the url of the image to display, null if nothing at creation time
	 * @param	tback	[in] true (default) to show an image on loading/error, false to display nothing
	 */
	public function new( url : String, ?tback : Null<Bool> )
	{
		super();
		MixinWidget();
		
		useTBack = (tback == null) ? true : tback;
		
		loadURL( url );
	}
	
	/**
	 * Sets a new URL for the display in this object.
	 *
	 * TODO: if this is called before the previous one is finished the events from
	 * the previous one will still be triggered and likely cause an error.
	 */
	public function loadURL( url : String )
	{
		//remove existing children
		if( loaded != null )
		{
			removeChild( loaded );
			loaded = null;
		}
		disableTBack();
			
		if( url != null )
		{
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onComplete );
			loader.contentLoaderInfo.addEventListener( flash.events.IOErrorEvent.IO_ERROR, onError );
			loader.contentLoaderInfo.addEventListener( flash.events.SecurityErrorEvent.SECURITY_ERROR, onError );
			loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, onProgress );
			loader.load( new URLRequest( url ) );
		}
		
		if( useTBack )
			enableTBack();
			
		//force update now
		update();
	}
	
	function enableTBack()
	{
		if( tback == null )
		{
			//old drawing of loading...
			//tback = new ui.DrawBox( drawLoading );
			
			//TODO: wrong positioning during loading!
			tback = new CircularProgress( { color: Color.rgb(0,0.8,0) } );
			addChild( tback.getNative() );
		}
	}
	
	function disableTBack()
	{
		if( tback != null )
		{
			removeChild( tback.getNative() );
			tback = null;
		}
	}
	
	function onComplete( evt : Event )
	{
		disableTBack();		
		
		//one would suspect using the loader.content directly might be more managable,
		//but a stupid security error gets in the way, somehow loader.content is considered
		//scripting access and flags the sandbox error, so we live with just making the loader
		//itself a child (adding a server proxy or domain files to all other servers would make
		//direct use possible, but since this seems to work normally there is no need)
		loaded = loader;	//.content;
		addChild( loaded );
		
		update();
		
		//tell listeners that we're done
		dispatchEvent( new Event( Event.COMPLETE ) );
	}
	
	function onError( evt : Event )
	{
		//always show an error display, even if loading graphic not requested...
		disableTBack();
		tback = ui.DrawBox.identity( drawError );
		addChild( tback.getNative() );
		update();
		
		//tell listeners something has gone wrong
		dispatchEvent( evt.clone() );
	}
	
	function onProgress( evt : ProgressEvent )
	{
		//TODO: identify any actor which implements Progress
		if( !Std.is( tback, CircularProgress ) )
			return;
		var cp = cast( tback, CircularProgress );
		cp.actorSetTrait1( cp.trait.General.Level, evt.bytesLoaded * 1.0 / evt.bytesTotal );
	}
	
	function _resize( w : Float, h : Float )
	{
		if( loaded != null )
		{
			loaded.width = w;
			loaded.height = h;
		}
		
		if( tback != null )
		{
			tback.resize( w, h );
			//quick repositioning (perhaps only for default background?)
			var ctr = getCenter().sub( tback.getCenter() );
			tback.move( w * ctr.x, h * ctr.y );
		}
	}
	
	static function drawLoading( graphics : draw.GraphicsX, w : Float, h : Float )
	{
		var lw = w / 10;
		graphics.use( Pen.solid( lw, Color.rgb(0,0.5,0), 0.5 ) );
		var off = 0.1;
		var eoff = 1 - 2 * off;
		graphics.drawEllipse( w * off, h * off, w * eoff, h * eoff );
	}
	
	static function drawError( graphics : draw.GraphicsX, w : Float, h : Float )
	{
		var lw = w / 10;
		graphics.use( Pen.solid( lw, Color.rgb(0.5,0,0), 0.5 ) );
		var off = 0.1;
		var eoff = 1 - 2 * off;
		graphics.moveTo( w * off, h * off );
		graphics.lineTo( w * eoff, h * eoff );
		graphics.moveTo( w * eoff, h * off );
		graphics.lineTo( w * off, h * eoff );
	}
	
	include(`MixinWidget.ihx')
}