Merge pull request #42 from stevage/train-routes
[potlatch2.git] / net / systemeD / potlatch2 / mapfeatures / Feature.as
index 57b2525f4fef4afd1c76cf7251ba626fc15ace94..a1d7c31effe9684f479664c501ea20044358743b 100644 (file)
@@ -7,9 +7,8 @@ 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;
 
 
     /** A "map feature" is sort of a template for a map entity. It consists of a few crucial key/value pairs that define the feature, so that
@@ -33,10 +32,23 @@ package net.systemeD.potlatch2.mapfeatures {
         public function Feature(mapFeatures:MapFeatures, _xml:XML) {
             this.mapFeatures = mapFeatures;
             this._xml = _xml;
+            loadImages();
             parseConditions();
             parseEditors();
         }
 
+        private function loadImages():void {
+            var icon:XMLList = _xml.icon;
+            if ( icon.length() > 0 ) {
+                if ( icon[0].hasOwnProperty("@dnd") ) {
+                    FileBank.getInstance().addFromFile(icon[0].@dnd);
+                }
+                if ( icon[0].hasOwnProperty("@image") ) {
+                    FileBank.getInstance().addFromFile(icon[0].@image);
+                }
+            }
+        }
+
         private function parseConditions():void {
             _tags = [];
            _withins = [];
@@ -139,6 +151,7 @@ package net.systemeD.potlatch2.mapfeatures {
         /** Fetches the feature's image, as defined by the icon element in the feature definition.
         * @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 fileBank:FileBank = FileBank.getInstance();
             var icon:XMLList = _xml.icon;
             var imageURL:String;
 
@@ -148,12 +161,12 @@ package net.systemeD.potlatch2.mapfeatures {
                 imageURL = icon[0].@image;
             }
 
-            if ( imageURL ) {
-                               if (ImageBank.getInstance().hasImage(imageURL)) {
-                                       return ImageBank.getInstance().getAsByteArray(imageURL)
-                               } else {
-                       return CachedDataLoader.loadData(imageURL, imageLoaded);
-                               }
+            if ( imageURL && fileBank.hasFile(imageURL) ) {
+                if (fileBank.fileLoaded(imageURL, imageLoaded)) {
+                    return fileBank.getAsByteArray(imageURL);
+                } else {
+                    return null;
+                }
             }
             var bitmap:BitmapAsset = new missingIconCls() as BitmapAsset;
             return new PNGEncoder().encode(bitmap.bitmapData);
@@ -167,7 +180,7 @@ package net.systemeD.potlatch2.mapfeatures {
                return point.length() > 0 && !(XML(point[0]).attribute("draganddrop")[0] == "no");
         }
 
-        private function imageLoaded(url:String, data:ByteArray):void {
+        private function imageLoaded(fileBank:FileBank, url:String):void {
             dispatchEvent(new Event("imageChanged"));
         }