Merge branch 'master' of github.com:systemed/potlatch2
[potlatch2.git] / net / systemeD / potlatch2 / FloatingMap.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <s:TitleWindow
3     xmlns:fx="http://ns.adobe.com/mxml/2009"
4         xmlns:mx="library://ns.adobe.com/flex/mx"
5         xmlns:s="library://ns.adobe.com/flex/spark"
6         xmlns:potlatch2="net.systemeD.potlatch2.*"
7         xmlns:controls="net.systemeD.controls.*"
8         skinClass="net.systemeD.controls.ResizableDraggableTitleWindowSkin"
9         title="Potlatch 2"
10         height="400" width="400"
11         close="setFloatingMapVisible(false);"
12         styleName="theFloatingMap">
13
14         <mx:UIComponent id='container' includeInLayout='false' depth='1' />
15
16         <mx:PopUpMenuButton id='imageryMenu' label='Imagery' x='5' y='5' depth='2' labelField='name' 
17         dataProvider="{Imagery.instance().getAvailableImagery(map)}"
18     itemClick="itemClickHandler(event)"
19          />
20
21     <s:CheckBox label="Lock zoom" selected="{zoomLocked}" id="lockZoom" x='5' y='30' depth='3'
22         change="setLockZoom(lockZoom.selected)" />
23
24         <fx:Script><![CDATA[
25
26         import net.systemeD.halcyon.Map;
27         import net.systemeD.halcyon.MapEvent;
28         import net.systemeD.halcyon.TileSet;
29         import net.systemeD.potlatch2.collections.Imagery;
30         import mx.core.UIComponent;
31         import mx.events.ResizeEvent;
32         import mx.events.ListEvent;
33         import mx.events.MenuEvent;
34         import mx.collections.ArrayCollection;
35
36         private var controller:EditController;
37         [Bindable] private var map:Map;
38         [Bindable] private var zoomLocked:Boolean=false;
39         private var maxZoom:uint=50;
40         private var crosshair:Sprite=new Sprite();
41         public var allowControllerKeyboardEvents:Boolean = true;
42
43         /* FloatingMap still to do:
44                 - selection UI
45                 - zoom lock button
46                 - show/hide (and close box)
47                 - auto position (top RH corner?)
48                 - crosshair
49                 - why does the main imagery change?
50                 - credit
51         */
52
53         public function init(controller:EditController):void {
54                 this.controller=controller;
55                 addElement(container);
56
57                 // Attribution and terms
58                 var overlay:Sprite=TileSet.overlaySprite();
59                 crosshair.graphics.lineStyle(1,0);
60                 crosshair.graphics.moveTo(-10,  0);
61                 crosshair.graphics.lineTo( 10,  0);
62                 crosshair.graphics.moveTo(  0,-10);
63                 crosshair.graphics.lineTo(  0, 10);
64
65                 // Create map
66                 map = new Map(overlay);
67         updateSize();
68                 container.addChild(map);
69                 container.addChild(overlay);
70                 container.addChild(crosshair);
71
72                 // Imagery collection
73                 var imagery:Imagery = Imagery.instance();
74                 imagery.addEventListener('imageryLoaded',function(e:Event):void {
75                         var bg:Object=imagery.findBackgroundWithName("Bing aerial imagery");
76             map.tileset.setBackgroundFromImagery(bg,false);
77                         imageryMenu.label=bg.name;
78                         if (visible) map.tileset.update();
79                         controller.map.addEventListener(MapEvent.MOUSE_MOVE,function(e:MapEvent):void {
80                                 if (visible) map.updateCoordsFromLatLon(e.params.y,e.params.x);
81                         });
82                         controller.map.addEventListener(MapEvent.SCALE,changeZoom);
83                         addEventListener(ResizeEvent.RESIZE,function(e:ResizeEvent):void { updateSize(); });
84                         ArrayCollection(imageryMenu.dataProvider).refresh();
85                 });
86                 imagery.addEventListener('refreshAttribution',function(e:Event):void {
87                         if (visible) {
88                                 map.tileset.setLogo(); map.tileset.setAttribution(); map.tileset.setTerms(); 
89                         }
90                 });
91
92                 // Initialise map and tileset
93                 map.init(controller.map.centre_lat || 51,
94                              controller.map.centre_lon || 0.1,
95                              controller.map.scale || 16);
96         }
97
98         protected function itemClickHandler(event:MenuEvent):void {
99                 map.tileset.setBackgroundFromImagery(event.item,false);
100                 imageryMenu.label=event.item.name;
101                 maxZoom=(event.item.extent && event.item.extent.max_zoom) ? event.item.extent.max_zoom : 50;
102                 map.changeScale(Math.min(controller.map.scale,maxZoom));
103         }
104         
105         public function setFloatingMapVisible(v:Boolean):void {
106                 visible=v;
107                 if (v) {
108                         map.updateCoordsFromLatLon(controller.map.centre_lat,controller.map.centre_lon);
109                         if (!zoomLocked) map.changeScale(Math.min(controller.map.scale,maxZoom));
110                         map.tileset.update();
111                         map.tileset.setLogo(); map.tileset.setAttribution(); map.tileset.setTerms(); 
112                 }
113         }
114
115         public function setLockZoom(v:Boolean):void {
116                 zoomLocked=v;
117                 if (!v && map.scale!=controller.map.scale) map.changeScale(Math.min(controller.map.scale,maxZoom));
118         }
119
120         public function changeZoom(e:MapEvent):void { 
121                 if (zoomLocked || !visible) return;
122                 map.changeScale(Math.min(e.params.scale,maxZoom));
123         }
124
125         public function updateSize():void {
126                 map.updateSize(width-2,height-34);
127                 crosshair.x=(width-2)/2; crosshair.y=(height-34)/2;
128                 map.tileset.setLogo(); map.tileset.setAttribution(); map.tileset.setTerms(); 
129         }
130
131         ]]>
132         </fx:Script>
133 </s:TitleWindow>