Recalculate what imagery is available when the map is moved.
[potlatch2.git] / net / systemeD / potlatch2 / collections / Imagery.as
1 package net.systemeD.potlatch2.collections {
2
3         import flash.events.*;
4         import flash.display.*;
5         import flash.net.*;
6         import flash.text.TextField;
7         import net.systemeD.halcyon.DebugURLRequest;
8         import net.systemeD.halcyon.Map;
9         import net.systemeD.halcyon.MapEvent;
10         import net.systemeD.potlatch2.FunctionKeyManager;
11         import net.systemeD.potlatch2.Yahoo;
12         import mx.collections.ArrayCollection;
13
14         /*
15                 There's lots of further tidying we can do:
16                 - remove all the horrid Yahoo stuff
17                 - remove the backreferences to _map and send events instead
18                 but this will do for now and help remove the clutter from potlatch2.mxml.
19         */
20
21         public class Imagery extends EventDispatcher {
22
23         private static const GLOBAL_INSTANCE:Imagery = new Imagery();
24         public static function instance():Imagery { return GLOBAL_INSTANCE; }
25
26                 public var collection:Array=[];
27                 public var selected:Object={};
28
29                 private var _map:Map;
30                 private var _overlay:Sprite;
31                 private var _yahoo:Yahoo;
32
33                 /* Load catalogue file */
34
35                 public function init(map:Map, overlay:Sprite, yahoo:Yahoo):void {
36                         _map = map;
37                         _overlay = overlay;
38                         _yahoo = yahoo;
39
40                         // load imagery file
41                 var request:DebugURLRequest = new DebugURLRequest("imagery.xml");
42                 var loader:URLLoader = new URLLoader();
43                 loader.addEventListener(Event.COMPLETE, onImageryLoad);
44                 loader.load(request.request);
45
46                         // create map listeners
47                         map.addEventListener(MapEvent.MOVE, moveHandler);
48                         map.addEventListener(MapEvent.RESIZE, resizeHandler);
49                 }
50
51         private function onImageryLoad(event:Event):void {
52                         var xml:XML = new XML(URLLoader(event.target).data);
53                         var saved:Object = {};
54                         var bg:Object;
55                         if (SharedObject.getLocal("user_state").data['background_url']) {
56                                 saved={ name: SharedObject.getLocal("user_state").data['background_name'],
57                                                 url:  SharedObject.getLocal("user_state").data['background_url' ] };
58                         }
59
60                         var isSet:Boolean=false;
61             var backgroundSet:Boolean = false;
62
63                         // Read all values from XML file
64             collection=new Array(
65                                 { name: "None", url: "" },
66                                 { name: "Yahoo", url: "yahoo", sourcetag: "Yahoo" } );
67                         for each(var set:XML in xml.set) {
68                                 var obj:Object={};
69                                 var a:XML;
70                                 for each (a in set.@*) { obj[a.name().localName]=a.toString(); }
71                                 for each (a in set.* ) { obj[a.name()          ]=a.toString(); }
72                 collection.push(obj);
73                                 if ((saved.url  && obj.url ==saved.url) ||
74                                     (saved.name && obj.name==saved.name && obj.name!='Custom')) { isSet=true; }
75                         }
76
77                         // Add user's previous preference (from SharedObject) if we didn't find it in the XML file
78             if (!isSet && saved.name && saved.url && saved.url!='' && saved.url!='yahoo') {
79                 collection.push(saved);
80                 isSet=true;
81             }
82
83                         // Automatically select the user's previous preference
84                         var defaultBackground:Object=null;
85                         for each (bg in collection) {
86                                 if (bg.name==saved.name || bg.url==saved.url) {
87                                         setBackground(bg);
88                     backgroundSet = true;
89                                 } else if (bg.default) {
90                                         defaultBackground=bg;
91                                 }
92                         }
93
94             // Otherwise, set whatever's specified as default
95             if (!backgroundSet && defaultBackground) {
96                 setBackground(defaultBackground);
97             }
98
99                         // Get any attribution and logo details
100                         for each (bg in collection) {
101                                 if (bg.logo) {
102                                         // load the logo
103                                         var loader:Loader = new Loader();
104                                         var thisbg1:Object = bg;                        // scope it for the closure
105                                         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void { onLogoLoad(e,thisbg1); });
106                                         loader.load(new URLRequest(bg.logo));
107                                 }
108                                 if (bg.attribution_url) {
109                                         // load the attribution
110                                         trace("requesting "+bg.attribution_url);
111                                 var urlloader:URLLoader = new URLLoader();
112                                         var thisbg2:Object = bg;                        // scope it for the closure
113                                         urlloader.addEventListener(Event.COMPLETE, function(e:Event):void { onAttributionLoad(e,thisbg2); });
114                                 urlloader.load(new URLRequest(bg.attribution_url));
115                                 }
116                         }
117
118                         // Tell the function key manager that we'd like to receive function key calls
119                         FunctionKeyManager.instance().registerListener('Background imagery',
120                                 function(o:String):void { setBackground(findBackgroundWithName(o)); });
121                         dispatchEvent(new Event("collection_changed"));
122                 }
123                 
124                 public function onLogoLoad(e:Event, bg:Object):void {
125                         bg.logoData  = Bitmap(LoaderInfo(e.target).content).bitmapData;
126                         bg.logoWidth = e.target.loader.width;
127                         bg.logoHeight= e.target.loader.height;
128                         setLogo();
129                 }
130                 
131                 public function onAttributionLoad(e:Event,bg: Object):void {
132                         trace ("onAttributionLoad");
133                         // if we ever need to cope with non-Microsoft attribution, then this should look at bg.scheme
134             default xml namespace = Namespace("http://schemas.microsoft.com/search/local/ws/rest/v1");
135             var xml:XML = new XML(e.target.data);
136                         var attribution:Object = {};
137             for each (var ImageryProvider:XML in xml..ImageryProvider) {
138                 var areas:Array=[];
139                 for each (var CoverageArea:XML in ImageryProvider.CoverageArea) {
140                     areas.push([CoverageArea.ZoomMin,
141                                 CoverageArea.ZoomMax,
142                                 CoverageArea.BoundingBox.SouthLatitude,
143                                 CoverageArea.BoundingBox.WestLongitude,
144                                 CoverageArea.BoundingBox.NorthLatitude,
145                                 CoverageArea.BoundingBox.EastLongitude]);
146                 }
147                 attribution[ImageryProvider.Attribution]=areas;
148             }
149                         default xml namespace = new Namespace("");
150                         bg.attribution=attribution;
151                         setAttribution();
152                 }
153
154                 public function setBackground(bg:Object):void {
155                         // set background
156                         selected=bg;
157                         if (bg.url=='yahoo') { _map.setBackground({url:''}); _yahoo.show(); }
158                                         else { _map.setBackground(bg      ); _yahoo.hide(); }
159                         // update attribution and logo
160                         _overlay.visible=bg.attribution || bg.logo || bg.terms_url;
161                         setLogo(); setAttribution(); setTerms();
162                         // save as SharedObject for next time
163                         var obj:SharedObject = SharedObject.getLocal("user_state");
164                         obj.setProperty('background_url' ,String(bg.url));
165                         obj.setProperty('background_name',String(bg.name));
166                         obj.flush();
167                 }
168                 
169                 private function findBackgroundWithName(name:String):Object {
170                         for each (var bg:Object in collection) {
171                                 if (bg.name==name) { return bg; }
172                         }
173                         return { url:'' };
174                 }
175
176                 private function moveHandler(event:MapEvent):void {
177                         setAttribution();
178                         dispatchEvent(new Event("collection_changed"));
179                 }
180                 private function setAttribution():void {
181                         var tf:TextField=TextField(_overlay.getChildAt(0));
182                         tf.text='';
183                         if (!selected.attribution) return;
184                         var attr:Array=[];
185                         for (var provider:String in selected.attribution) {
186                                 for each (var bounds:Array in selected.attribution[provider]) {
187                                         if (_map.scale>=bounds[0] && _map.scale<=bounds[1] &&
188                                           ((_map.edge_l>bounds[3] && _map.edge_l<bounds[5]) ||
189                                            (_map.edge_r>bounds[3] && _map.edge_r<bounds[5]) ||
190                                    (_map.edge_l<bounds[3] && _map.edge_r>bounds[5])) &&
191                                           ((_map.edge_b>bounds[2] && _map.edge_b<bounds[4]) ||
192                                            (_map.edge_t>bounds[2] && _map.edge_t<bounds[4]) ||
193                                            (_map.edge_b<bounds[2] && _map.edge_t>bounds[4]))) {
194                                                 attr.push(provider);
195                                         }
196                                 }
197                         }
198                         if (attr.length==0) return;
199                         tf.text="Background "+attr.join(", ");
200                         positionAttribution();
201                         dispatchEvent(new MapEvent(MapEvent.BUMP, { y: tf.textHeight }));       // don't let the toolbox obscure it
202                 }
203                 private function positionAttribution():void {
204                         var tf:TextField=TextField(_overlay.getChildAt(0));
205                         tf.x=_map.mapwidth  - 5 - tf.textWidth;
206                         tf.y=_map.mapheight - 5 - tf.textHeight;
207                 }
208
209                 private function setLogo():void {
210                         while (_overlay.numChildren>2) { _overlay.removeChildAt(2); }
211                         if (!selected.logoData) return;
212                         var logo:Sprite=new Sprite();
213                         logo.addChild(new Bitmap(selected.logoData));
214                         if (selected.logo_url) { logo.buttonMode=true; logo.addEventListener(MouseEvent.CLICK, launchLogoLink, false, 0, true); }
215                         _overlay.addChild(logo);
216                         positionLogo();
217                 }
218                 private function positionLogo():void {
219                         _overlay.getChildAt(2).x=5;
220                         _overlay.getChildAt(2).y=_map.mapheight - 5 - selected.logoHeight - (selected.terms_url ? 10 : 0);
221                 }
222                 private function launchLogoLink(e:Event):void {
223                         if (!selected.logo_url) return;
224                         navigateToURL(new URLRequest(selected.logo_url), '_blank');
225                 }
226                 private function setTerms():void {
227                         var terms:TextField=TextField(_overlay.getChildAt(1));
228                         if (!selected.terms_url) { terms.text=''; return; }
229                         terms.text="Background terms of use";
230                         positionTerms();
231                         terms.addEventListener(MouseEvent.CLICK, launchTermsLink, false, 0, true);
232                 }
233                 private function positionTerms():void {
234                         _overlay.getChildAt(1).x=5;
235                         _overlay.getChildAt(1).y=_map.mapheight - 15;
236                 }
237                 private function launchTermsLink(e:Event):void {
238                         if (!selected.terms_url) return;
239                         navigateToURL(new URLRequest(selected.terms_url), '_blank');
240                 }
241
242                 private function resizeHandler(event:MapEvent):void {
243                         if (selected.logoData) positionLogo();
244                         if (selected.terms_url) positionTerms();
245                         if (selected.attribution) positionAttribution();
246                 }
247
248                 [Bindable(event="collection_changed")]
249                 public function getAvailableImagery():ArrayCollection {
250                         var available:Array=[];
251                         for each (var bg:Object in collection) {
252                                 if (bg.minlon) {
253                                         // if there's a bbox, check the current viewport intersects it
254                                         if (((_map.edge_l>bg.minlon && _map.edge_l<bg.maxlon) ||
255                                              (_map.edge_r>bg.minlon && _map.edge_r<bg.maxlon) ||
256                                              (_map.edge_l<bg.minlon && _map.edge_r>bg.maxlon)) &&
257                                             ((_map.edge_b>bg.minlat && _map.edge_b<bg.maxlat) ||
258                                              (_map.edge_t>bg.minlat && _map.edge_t<bg.maxlat) ||
259                                              (_map.edge_b<bg.minlat && _map.edge_t>bg.maxlat))) {
260                                                 available.push(bg);
261                                         }
262                                 } else {
263                                         // if there's no bbox (i.e. global set), include it anyway
264                                         available.push(bg);
265                                 }
266                         }
267                         return new ArrayCollection(available);
268                 }
269
270         }
271         
272 }