tweak missing image size so that it's the same as that of the p2 presets - looks...
[potlatch2.git] / net / systemeD / potlatch2 / utils / CachedDataLoader.as
1 package net.systemeD.potlatch2.utils {
2
3         import flash.events.*;
4         import flash.net.*;
5         import flash.utils.ByteArray;
6         import flash.display.BitmapData;
7         import mx.graphics.codec.PNGEncoder;
8
9     import net.systemeD.halcyon.ExtendedURLLoader;
10         import net.systemeD.halcyon.DebugURLRequest;
11
12         public class CachedDataLoader {
13
14         private static var allData:Object = {};
15         private static var requestsMade:Object = {};
16         private static var missingImage:ByteArray = null;
17         
18                 public static function loadData(url:String, onLoadHandler:Function = null):ByteArray {
19                     var data:ByteArray = allData[url];
20                     if ( data != null )
21                         return data;
22                     
23                     var requests:Array = requestsMade[url];
24                     if ( requests == null ) {
25                         requests = [];
26                         requestsMade[url] = requests;
27
28                                 var request:DebugURLRequest = new DebugURLRequest(url);
29                     var loader:ExtendedURLLoader = new ExtendedURLLoader();
30                     loader.info = url;
31                 loader.addEventListener(Event.COMPLETE, imageLoaded);
32                 loader.addEventListener(IOErrorEvent.IO_ERROR, imageLoadFailed);
33                 loader.dataFormat = URLLoaderDataFormat.BINARY;
34                 loader.load(request.request);
35                     }
36                     requests.push(onLoadHandler);
37                     
38                     return allData[url];
39                 }
40
41         private static function imageLoaded(event:Event):void {
42             var loader:ExtendedURLLoader = ExtendedURLLoader(event.target);
43             var url:String = loader.info as String;
44             allData[url] = loader.data;
45             dispatchEvents(url);
46         }
47         
48         private static function imageLoadFailed(event:Event):void {
49             var loader:ExtendedURLLoader = ExtendedURLLoader(event.target);
50             var url:String = loader.info as String;
51             
52             allData[url] = getMissingImage();
53             dispatchEvents(url);
54         }
55         
56         private static function dispatchEvents(url:String):void {
57             var requests:Array = requestsMade[url];
58             for each ( var handler:Function in requests ) {
59                 handler(url, allData[url]);
60             }
61             
62             delete requestsMade[url];
63         }
64         
65         private static function getMissingImage():ByteArray {
66             if ( missingImage == null ) {
67                 var bitmap:BitmapData = new BitmapData(24, 24, false);
68                 for ( var i:uint = 0; i < 24; i++ ) {
69                     bitmap.setPixel(i, i, 0xff0000);
70                     bitmap.setPixel(23-i, i, 0xff0000);
71                 }
72                 missingImage = new PNGEncoder().encode(bitmap);
73             }
74             return missingImage;
75         }
76         }
77 }