Merge remote-tracking branch 'systemed/filebank' into filebank
authorTom Hughes <tom@compton.nu>
Sat, 24 Mar 2012 10:32:44 +0000 (10:32 +0000)
committerTom Hughes <tom@compton.nu>
Sat, 24 Mar 2012 10:32:44 +0000 (10:32 +0000)
net/systemeD/halcyon/FileBank.as [moved from net/systemeD/halcyon/ImageBank.as with 54% similarity]
net/systemeD/halcyon/MarkerUI.as
net/systemeD/halcyon/NodeUI.as
net/systemeD/halcyon/WayUI.as
net/systemeD/halcyon/styleparser/RuleSet.as
net/systemeD/halcyon/styleparser/StyleChooser.as
net/systemeD/potlatch2/Preloader.as
net/systemeD/potlatch2/mapfeatures/Feature.as

similarity index 54%
rename from net/systemeD/halcyon/ImageBank.as
rename to net/systemeD/halcyon/FileBank.as
index eca8547dba5714f270d839a8e0f3c904c469306f..d82a2d713dbcc84d4f85e32f1ddbe7a90ce74924 100755 (executable)
@@ -6,68 +6,78 @@ package net.systemeD.halcyon {
        import nochump.util.zip.*;
 
        /*
-               ImageBank stores and retrieves bitmap images.
-               All images are internally stored as Loader.
+               FileBank stores and retrieves bitmap images and other files.
+               Images are internally stored as Loader. Other files are stored as strings.
 
                See blog.yoz.sk/2009/10/bitmap-bitmapdata-bytearray/ for a really useful conversion guide!
        */
 
-    public class ImageBank extends EventDispatcher{
-               private var images:Object={};
-               private var imagesRequested:uint=0;
-               private var imagesReceived:uint=0;
+    public class FileBank extends EventDispatcher{
+               private var files:Object={};
+               private var filesRequested:uint=0;
+               private var filesReceived:uint=0;
                
-               public static const IMAGES_LOADED:String="imagesLoaded";
+               public static const FILES_LOADED:String="filesLoaded";
                public static const ZIP_LOADED:String="zipLoaded";
                
-               private static const GLOBAL_INSTANCE:ImageBank = new ImageBank();
-               public static function getInstance():ImageBank { return GLOBAL_INSTANCE; }
+               private static const GLOBAL_INSTANCE:FileBank = new FileBank();
+               public static function getInstance():FileBank { return GLOBAL_INSTANCE; }
 
-               public function hasImage(name:String):Boolean {
-                       if (images[name]) return true;
+               public function hasFile(name:String):Boolean {
+                       if (files[name]) return true;
                        return false;
                }
 
                /* ==========================================================================================
-                  Populate with images 
+                  Add an individual file to bank (not from a .zip file)
+                  Used when we want to load a file for use later on (e.g. an image referenced in a stylesheet)
                   ========================================================================================== */
 
-               public function loadImage(filename:String):void {
-                       if (images[filename]) return;
-                       imagesRequested++;
+               public function addFromFile(filename:String):void {
+                       if (files[filename]) return;
+                       filesRequested++;
 
-                       var loader:Loader=new Loader();
-                       images[filename]=loader;
                        var request:URLRequest=new URLRequest(filename);
-                       loader.contentLoaderInfo.addEventListener(Event.COMPLETE,                                               loadedImage);
-                       loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS,                  httpStatusHandler);
-                       loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,    securityErrorHandler);
-                       loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,                                ioErrorHandler);
-                       loader.load(request);
+                       if (isImageType(filename)) {
+                               var loader:Loader=new Loader();
+                               files[filename]=loader;
+                               loader.contentLoaderInfo.addEventListener(Event.COMPLETE,                                               loadedImage);
+                               loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS,                  httpStatusHandler);
+                               loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,    securityErrorHandler);
+                               loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,                                ioErrorHandler);
+                               loader.load(request);
+                       } else {
+                               var urlloader:URLLoader = new URLLoader();
+                               urlloader.addEventListener(Event.COMPLETE,                    loadedImage);
+                               urlloader.addEventListener(HTTPStatusEvent.HTTP_STATUS,           httpStatusHandler);
+                               urlloader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
+                               urlloader.addEventListener(IOErrorEvent.IO_ERROR,             ioErrorHandler);
+                               urlloader.load(request);
+                       }
                }
 
                private function loadedImage(event:Event):void {
-                       imageReceived();
+                       fileReceived();
                }
                private function httpStatusHandler(event:HTTPStatusEvent):void { }
                private function securityErrorHandler(event:SecurityErrorEvent):void { 
                        trace("securityErrorEvent: "+event.target.url);
-                       imageReceived();
+                       fileReceived();
                }
                private function ioErrorHandler(event:IOErrorEvent):void { 
                        trace("ioErrorEvent: "+event.target.url); 
-                       imageReceived();
+                       fileReceived();
                }
-               private function imageReceived():void {
-                       imagesReceived++;
-                       if (imagesReceived==imagesRequested) { dispatchEvent(new Event(IMAGES_LOADED)); }
+               private function fileReceived():void {
+                       filesReceived++;
+                       if (filesReceived==filesRequested) { dispatchEvent(new Event(FILES_LOADED)); }
                }
 
                /* ==========================================================================================
-                  Load from .zip file
+                  Add files to bank from .zip file
                   ========================================================================================== */
                
-               public function loadFromZip(filename:String, prefix:String=""):void {
+               public function addFromZip(filename:String, prefix:String=""):void {
                        var loader:URLLoader = new URLLoader();
                        loader.dataFormat="binary";
                        loader.addEventListener(Event.COMPLETE, function(e:Event):void { zipReady(e,prefix); } );
@@ -78,38 +88,55 @@ package net.systemeD.halcyon {
                        for (var i:uint=0; i<zip.entries.length; i++) {
                                var fileref:ZipEntry = zip.entries[i];
                                var data:ByteArray = zip.getInput(fileref);
-                               var loader:Loader=new Loader();
-                               images[prefix+fileref.name]=loader;
-                               loader.loadBytes(data);
+                               if (isImageType(fileref.name)) {
+                                       // Store as an image
+                                       var loader:Loader=new Loader();
+                                       files[prefix+fileref.name]=loader;
+                                       loader.loadBytes(data);
+                               } else {
+                                       // Store as a document
+                                       files[prefix+fileref.name]=data.toString();
+                               }
                        }
                        dispatchEvent(new Event(ZIP_LOADED));
                }
-
+               private function isImageType(filename:String):Boolean {
+                       if (filename.match(/\.jpe?g$/i) ||
+                               filename.match(/\.png$/i) ||
+                               filename.match(/\.gif$/i) ||
+                               filename.match(/\.swf$/i)) { return true; }
+                       return false;
+               }
 
                /* ==========================================================================================
-                  Get images 
+                  Get files
+                  get(filename)
                   getAsDisplayObject(filename)
                   getAsBitmapData(filename)
                   getAsByteArray(filename)
                   ========================================================================================== */
 
+               public function get(name:String):String {
+                       return files[name];
+               }
+
                public function getAsDisplayObject(name:String):DisplayObject {
                        /* If the image hasn't loaded yet, then add an EventListener for when it does. */
                        if (getWidth(name)==0) {
                                var loader:Loader = new Loader();
-                               images[name].contentLoaderInfo.addEventListener(Event.COMPLETE,
+                               files[name].contentLoaderInfo.addEventListener(Event.COMPLETE,
                                        function(e:Event):void { loaderReady(e, loader) });
                                return loader;
                        }
                        /* Otherwise, create a new Bitmap, because just returning the raw Loader
-                          (i.e. images[name]) would only allow it to be added to one parent. (The other 
+                          (i.e. files[name]) would only allow it to be added to one parent. (The other 
                           way to do this would be by copying the bytes as loaderReady does.). */
                        return new Bitmap(getAsBitmapData(name));
                }
                
                public function getOriginalDisplayObject(name:String):DisplayObject {
                        /* But if we're going to clone it later, this'll work fine. */
-                       return images[name];
+                       return files[name];
                }
 
                private function loaderReady(event:Event, loader:Loader):void {
@@ -120,12 +147,12 @@ package net.systemeD.halcyon {
 
                public function getAsBitmapData(name:String):BitmapData {
                        var bitmapData:BitmapData=new BitmapData(getWidth(name), getHeight(name), true, 0xFFFFFF);
-                       bitmapData.draw(images[name]);
+                       bitmapData.draw(files[name]);
                        return bitmapData;
                }
                
                public function getAsByteArray(name:String):ByteArray {
-                       return images[name].contentLoaderInfo.bytes;
+                       return files[name].contentLoaderInfo.bytes;
                }
 
                /* ==========================================================================================
@@ -133,12 +160,12 @@ package net.systemeD.halcyon {
                   ========================================================================================== */
 
                public function getWidth(name:String):int { 
-                       try { return images[name].contentLoaderInfo.width; }
+                       try { return files[name].contentLoaderInfo.width; }
                        catch (error:Error) { } return 0;
                }
 
                public function getHeight(name:String):int { 
-                       try { return images[name].contentLoaderInfo.height; }
+                       try { return files[name].contentLoaderInfo.height; }
                        catch (error:Error) { } return 0;
                }
 
index 14801bc19265cb9ebe0be83a8ee1cf5fd7d34632..512d3398b36657e5aa4fdf06e31c64949cd467ac 100644 (file)
@@ -10,7 +10,7 @@ package net.systemeD.halcyon {
     import flash.geom.Point;
     import net.systemeD.halcyon.styleparser.*;
     import net.systemeD.halcyon.connection.*;
-    import net.systemeD.halcyon.ImageBank;
+    import net.systemeD.halcyon.FileBank;
 
     public class MarkerUI extends EntityUI {
 
@@ -100,9 +100,9 @@ package net.systemeD.halcyon {
                             icon.graphics.drawCircle(w,w,w);
                             if (s.interactive) { maxwidth=Math.max(w,maxwidth); }
 
-                                               } else if (ImageBank.getInstance().hasImage(s.icon_image)) {
+                                               } else if (FileBank.getInstance().hasFile(s.icon_image)) {
                                                        // load icon from library
-                                                       icon.addChild(ImageBank.getInstance().getAsDisplayObject(s.icon_image));
+                                                       icon.addChild(FileBank.getInstance().getAsDisplayObject(s.icon_image));
 //                                                     addHitSprite(icon.width);                       // ** check this - we're doing it below too
 //                                                     loaded=true; updatePosition();          // ** check this
                                                        if (s.interactive) { maxwidth=Math.max(icon.width,maxwidth); }
index 11bcc02fdd8a2daec449d3d49e43220927235333..9e26cc87571c1117f7355fb33a35addba7ed5077 100644 (file)
@@ -10,7 +10,7 @@ package net.systemeD.halcyon {
        import flash.geom.Point;
        import net.systemeD.halcyon.styleparser.*;
     import net.systemeD.halcyon.connection.*;
-    import net.systemeD.halcyon.ImageBank;
+    import net.systemeD.halcyon.FileBank;
        
        /** The graphical representation of a Node (including POIs and nodes that are part of Ways). */
        public class NodeUI extends EntityUI {
@@ -122,9 +122,9 @@ package net.systemeD.halcyon {
                                                        icon.graphics.drawCircle(w,w,w);
                                                        if (s.interactive) { maxwidth=Math.max(w,maxwidth); }
 
-                                               } else if (ImageBank.getInstance().hasImage(s.icon_image)) {
+                                               } else if (FileBank.getInstance().hasFile(s.icon_image)) {
                                                        // load icon from library
-                                                       icon.addChild(ImageBank.getInstance().getAsDisplayObject(s.icon_image));
+                                                       icon.addChild(FileBank.getInstance().getAsDisplayObject(s.icon_image));
 //                                                     addHitSprite(icon.width);                       // ** check this - we're doing it below too
                                                        loaded=true; updatePosition();          // ** check this
                                                        if (s.interactive) { maxwidth=Math.max(icon.width,maxwidth); }
index f7a5945247f0f140752c152fb14c158798d02540..990f43bbe946681b92ee50e37e46bcd95101cd0a 100644 (file)
@@ -9,7 +9,7 @@ package net.systemeD.halcyon {
        
        import net.systemeD.halcyon.connection.*;
        import net.systemeD.halcyon.styleparser.*;
-    import net.systemeD.halcyon.ImageBank;
+    import net.systemeD.halcyon.FileBank;
 
        /** The graphical representation of a Way. */ 
        public class WayUI extends EntityUI {
@@ -296,7 +296,7 @@ package net.systemeD.halcyon {
                                                fill=new Shape(); addToLayer(fill,FILLSPRITE,s.sublayer);
                                                fill.graphics.moveTo(x0,y0);
                                                if (s.fill_image) {
-                                                       fill.graphics.beginBitmapFill(ImageBank.getInstance().getAsBitmapData(s.fill_image));
+                                                       fill.graphics.beginBitmapFill(FileBank.getInstance().getAsBitmapData(s.fill_image));
                                                } else {
                                                        s.applyFill(fill.graphics);
                                                }
index 6a8d6a015fef1dda52b38a08015078c7a0bf23f5..d0842145ce594d55702a28b1c891750a8de6e025 100644 (file)
@@ -6,7 +6,7 @@ package net.systemeD.halcyon.styleparser {
        import net.systemeD.halcyon.ExtendedURLLoader;
        import net.systemeD.halcyon.DebugURLRequest;
     import net.systemeD.halcyon.connection.Entity;
-    import net.systemeD.halcyon.ImageBank;
+    import net.systemeD.halcyon.FileBank;
 
     import net.systemeD.halcyon.connection.*;
 
@@ -294,7 +294,7 @@ package net.systemeD.halcyon.styleparser {
                /** Load all images referenced in the RuleSet (for example, icons or bitmap fills). */
                
                private function loadImages():void {
-                       ImageBank.getInstance().addEventListener(ImageBank.IMAGES_LOADED,doIconCallback);
+                       FileBank.getInstance().addEventListener(FileBank.FILES_LOADED,doIconCallback);
                        var filename:String;
                        for each (var chooser:StyleChooser in choosers) {
                                for each (var style:Style in chooser.styles) {
@@ -304,7 +304,7 @@ package net.systemeD.halcyon.styleparser {
                                        else { continue; }
 
                                        if (filename!='square' && filename!='circle')
-                                               ImageBank.getInstance().loadImage(filename);
+                                               FileBank.getInstance().addFromFile(filename);
                                }
                        }
                }
index 9d9d2f93fc4648a94287b5f8d3582eafedb6ff83..f0e95bc50745152858eabec3721315a1b5927622 100644 (file)
@@ -1,7 +1,7 @@
 package net.systemeD.halcyon.styleparser {
 
        import net.systemeD.halcyon.connection.Entity;
-    import net.systemeD.halcyon.ImageBank;
+    import net.systemeD.halcyon.FileBank;
 
        public class StyleChooser {
 
@@ -68,8 +68,8 @@ package net.systemeD.halcyon.styleparser {
                                                        if (PointStyle(r).icon_width && !PointStyle(r).evals['icon_width']) {
                                                                // ** FIXME: we should check this is the bit being used for 'square', 'circle' etc.
                                                                w=PointStyle(r).icon_width;
-                                                       } else if (PointStyle(r).icon_image && ImageBank.getInstance().hasImage(PointStyle(r).icon_image)) {
-                                                               w=ImageBank.getInstance().getWidth(PointStyle(r).icon_image);
+                                                       } else if (PointStyle(r).icon_image && FileBank.getInstance().hasFile(PointStyle(r).icon_image)) {
+                                                               w=FileBank.getInstance().getWidth(PointStyle(r).icon_image);
                                                        }
                                                        if (w>sl.maxwidth) { sl.maxwidth=w; }
                                                } else if (r is InstructionStyle) {
index 583e610339e1e70a58657efe38c55654738efa2f..3b2c9c83859dae6c56feb1069c6f0f0893a78881 100644 (file)
@@ -6,7 +6,7 @@ package net.systemeD.potlatch2 {
     import flash.geom.Matrix;
     import flash.text.TextField;
     import flash.text.TextFormat;
-    import net.systemeD.halcyon.ImageBank;
+    import net.systemeD.halcyon.FileBank;
 
 
     public class Preloader extends net.systemeD.potlatch2.PreloaderDisplayBase {
@@ -45,11 +45,11 @@ package net.systemeD.potlatch2 {
 
                        // request .zip files
                        if (loaderInfo.parameters['assets']) {
-                               ImageBank.getInstance().addEventListener(ImageBank.ZIP_LOADED, zipLoaded);
+                               FileBank.getInstance().addEventListener(FileBank.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]);
+                                       FileBank.getInstance().addFromZip(asset[0],asset[1]);
                                }
                        }
         }
index 0c246d2fb4b25c01a9e385b35f3823a1e9a4623b..38d2839924c16b4584cda3239c4a46270196f6f5 100644 (file)
@@ -8,7 +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.FileBank;
     import net.systemeD.halcyon.connection.Entity;
     import net.systemeD.potlatch2.utils.CachedDataLoader;
 
@@ -148,8 +148,8 @@ package net.systemeD.potlatch2.mapfeatures {
             }
 
             if ( imageURL ) {
-                               if (ImageBank.getInstance().hasImage(imageURL)) {
-                                       return ImageBank.getInstance().getAsByteArray(imageURL)
+                               if (FileBank.getInstance().hasFile(imageURL)) {
+                                       return FileBank.getInstance().getAsByteArray(imageURL);
                                } else {
                        return CachedDataLoader.loadData(imageURL, imageLoaded);
                                }