Add the ability to group image assets into a .zip file, removing the number of indivi...
authorRichard Fairhurst <richard@systemeD.net>
Sat, 3 Sep 2011 17:19:52 +0000 (18:19 +0100)
committerRichard Fairhurst <richard@systemeD.net>
Sat, 3 Sep 2011 17:19:52 +0000 (18:19 +0100)
To use, supply a Flash parameter with the location of the zips and the place they're "substituting" for, like this:
args["assets"] = "assets/icons.zip=icons/;assets/features_pois.zip=features/pois/";
Current limitations:
- Preloader indication is not smooth. (There was a trac ticket for this anyway :) )
- Only features and MapCSS icons are loaded - other image references don't yet look in ImageBank
- Nested folders in the .zip file are not supported (a limitation of the library we're using)

net/systemeD/halcyon/ImageBank.as
net/systemeD/potlatch2/Preloader.as
net/systemeD/potlatch2/mapfeatures/Feature.as
potlatch2.mxml

index ce5caec..eca8547 100755 (executable)
@@ -18,6 +18,7 @@ package net.systemeD.halcyon {
                private var imagesReceived:uint=0;
                
                public static const IMAGES_LOADED:String="imagesLoaded";
+               public static const ZIP_LOADED:String="zipLoaded";
                
                private static const GLOBAL_INSTANCE:ImageBank = new ImageBank();
                public static function getInstance():ImageBank { return GLOBAL_INSTANCE; }
@@ -67,12 +68,13 @@ package net.systemeD.halcyon {
                   ========================================================================================== */
                
                public function loadFromZip(filename:String, prefix:String=""):void {
-                       var urlstream:URLStream = new URLStream();
-                       urlstream.addEventListener(Event.COMPLETE, function(e:Event):void { zipReady(e,prefix); } );
-                       urlstream.load(new URLRequest(filename));
+                       var loader:URLLoader = new URLLoader();
+                       loader.dataFormat="binary";
+                       loader.addEventListener(Event.COMPLETE, function(e:Event):void { zipReady(e,prefix); } );
+                       loader.load(new URLRequest(filename));
                }
                private function zipReady(event:Event, prefix:String):void {
-                       var zip:ZipFile = new ZipFile(URLStream(event.target));
+                       var zip:ZipFile = new ZipFile(event.target.data);
                        for (var i:uint=0; i<zip.entries.length; i++) {
                                var fileref:ZipEntry = zip.entries[i];
                                var data:ByteArray = zip.getInput(fileref);
@@ -80,6 +82,7 @@ package net.systemeD.halcyon {
                                images[prefix+fileref.name]=loader;
                                loader.loadBytes(data);
                        }
+                       dispatchEvent(new Event(ZIP_LOADED));
                }
 
 
index 1014c0a..1b25214 100644 (file)
@@ -6,6 +6,7 @@ package net.systemeD.potlatch2 {
     import flash.geom.Matrix;
     import flash.text.TextField;
     import flash.text.TextFormat;
+    import net.systemeD.halcyon.ImageBank;
 
 
     public class Preloader extends net.systemeD.potlatch2.PreloaderDisplayBase {
@@ -20,6 +21,9 @@ package net.systemeD.potlatch2 {
         private var barFrame:Sprite;
         private var mainColor:uint=0x045966;
 
+               private var assetscount:uint=0;
+               private var queuedEvents:Array=[];
+
         public function Preloader()
         {
             super();
@@ -38,7 +42,33 @@ package net.systemeD.potlatch2 {
 
             //creates all visual elements
             createAssets();
+
+                       // request .zip files
+                       if (loaderInfo.parameters['assets']) {
+                               ImageBank.getInstance().addEventListener(ImageBank.ZIP_LOADED, zipLoaded);
+                               for each (var file:String in loaderInfo.parameters['assets'].split(';')) {
+                                       var asset:Array=file.split('=');
+                                       assetscount++;
+                                       ImageBank.getInstance().loadFromZip(asset[0],asset[1]);
+                               }
+                       }
         }
+
+               override protected function initProgressHandler(e:Event):void {
+                       if (assetscount>0) {
+                               queuedEvents.push(e);
+                               e.stopImmediatePropagation();
+                       }
+                       draw();
+               }
+               
+               private function zipLoaded(e:Event):void {
+                       assetscount--; if (assetscount>0) return;
+                       for each (var q:Event in queuedEvents) {
+                               dispatchEvent(q);
+                       }
+               }
+               
         //this is our "animation" bit
         override protected function draw():void
         {
index 0cda0b1..290cb2d 100644 (file)
@@ -8,6 +8,7 @@ package net.systemeD.potlatch2.mapfeatures {
     import mx.core.BitmapAsset;
     import mx.graphics.codec.PNGEncoder;
     
+    import net.systemeD.halcyon.ImageBank;
     import net.systemeD.halcyon.connection.Entity;
     import net.systemeD.potlatch2.utils.CachedDataLoader;
 
@@ -138,8 +139,7 @@ package net.systemeD.potlatch2.mapfeatures {
         * @param dnd if true, overrides the normal image and returns the one defined by the dnd property instead. */
         private function getImage(dnd:Boolean = false):ByteArray {
             var icon:XMLList = _xml.icon;
-            var imageURL:String = null;
-            var img:ByteArray;
+            var imageURL:String;
 
             if ( dnd && icon.length() > 0 && icon[0].hasOwnProperty("@dnd") ) {
                 imageURL = icon[0].@dnd;
@@ -147,11 +147,12 @@ package net.systemeD.potlatch2.mapfeatures {
                 imageURL = icon[0].@image;
             }
 
-            if ( imageURL != null ) {
-                img = CachedDataLoader.loadData(imageURL, imageLoaded);
-            }
-            if (img) {
-              return img;
+            if ( imageURL ) {
+                               if (ImageBank.getInstance().hasImage(imageURL)) {
+                                       return ImageBank.getInstance().getAsByteArray(imageURL)
+                               } else {
+                       return CachedDataLoader.loadData(imageURL, imageLoaded);
+                               }
             }
             var bitmap:BitmapAsset = new missingIconCls() as BitmapAsset;
             return new PNGEncoder().encode(bitmap.bitmapData);
index fbf7aee..ba14534 100644 (file)
             if (loaderInfo.parameters['locale']) {
               dispatchEvent(new LocaleEvent(LocaleEvent.LOAD_LOCALE,loaderInfo.parameters['locale']));
             }
-
-                       // Load font and don't do anything until that's done
                        var loader:Loader = new Loader();
-                       loader.contentLoaderInfo.addEventListener(Event.COMPLETE, continueInit);
+                       loader.contentLoaderInfo.addEventListener(Event.COMPLETE, startApp);
                        loader.load(new URLRequest("FontLibrary.swf"));
                }
                
-               private function continueInit(r:Event):void {
+               private function startApp(event:Event):void {
                        controlbar.setStyle('dropShadowEnabled',"false");       // Flex 4 doesn't permit this in Application.css
 
                        // Set font
-                       var FontLibrary:Class = r.target.applicationDomain.getDefinition("FontLibrary") as Class;
+                       var FontLibrary:Class = event.target.applicationDomain.getDefinition("FontLibrary") as Class;
                        Font.registerFont(FontLibrary.DejaVu);
 
                        Globals.vars.map_area = map_area;