load imagery from XML file
[potlatch2.git] / potlatch2.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:Application 
3         xmlns:mx="http://www.adobe.com/2006/mxml" 
4         xmlns:halcyon="net.systemeD.halcyon.*"
5         xmlns:potlatch2="net.systemeD.potlatch2.*"
6         layout="vertical"
7         horizontalAlign="center" 
8         addedToStage="initApp()">
9         
10         <mx:Style source="styles/Application.css"/>
11
12     <mx:Glow id="glowImage" duration="100" 
13         alphaFrom="0.3" alphaTo="1.0" 
14         blurXFrom="0.0" blurXTo="5.0" 
15         blurYFrom="0.0" blurYTo="5.0" 
16         color="0xFF8000"/>
17     <mx:Glow id="unglowImage" duration="100" 
18         alphaFrom="1.0" alphaTo="0.3" 
19         blurXFrom="5.0" blurXTo="0.0" 
20         blurYFrom="5.0" blurYTo="0.0" 
21         color="0xFF8000"/>
22     <mx:WipeLeft id="wipeOut" duration="250"/>
23     <mx:WipeRight id="wipeIn" duration="250"/>
24
25     <mx:ApplicationControlBar dock="true">
26         <mx:PopUpButton id="bgButton" label="Background" openAlways="true"
27             creationComplete="bgButton.popUp = new BackgroundSelector();"/>
28         <mx:PopUpButton id="styleButton" label="Map Style" openAlways="true"
29             creationComplete="styleButton.popUp = new StyleSelector();"/>
30         <mx:Spacer width="100%"/>
31         <mx:Button label="Undo" click="MainUndoStack.getGlobalStack().undo();"
32             enabled="{MainUndoStack.getGlobalStack().canUndo()}"/>
33         <mx:Button label="Redo" click="MainUndoStack.getGlobalStack().redo();"
34             enabled="{MainUndoStack.getGlobalStack().canRedo()}"/>
35         <mx:Spacer width="100%"/>
36         <mx:Button label="Help" click="new HelpDialog().init();" />
37         <mx:Button label="Options" click="new OptionsDialog().init();" />
38         <mx:Button label="Save" icon="@Embed('embedded/save.svg')" click="SaveManager.saveChanges();"/>
39     </mx:ApplicationControlBar>
40     
41     <mx:HDividedBox width="100%" height="100%">
42
43           <!-- Tag viewer -->
44           <potlatch2:TagViewer width="25%" height="100%" id="tagViewer"/>
45
46       <mx:Canvas width="75%" height="100%">
47         <mx:Canvas id="map_area" resize="onResizeMap()"
48             top="0" left="0" width="100%" height="100%" dragEnter="dragEnterHandler(event);" dragDrop="dragDropHandler(event);">
49         </mx:Canvas>
50         <mx:Image source="@Embed('embedded/zoomIn.svg')" right="3" top="3" click="theMap.zoomIn();"
51             rollOverEffect="glowImage" rollOutEffect="unglowImage"/>
52         <mx:Image source="@Embed('embedded/zoomOut.svg')" right="3" top="20" click="theMap.zoomOut();"
53             rollOverEffect="glowImage" rollOutEffect="unglowImage"/>
54         <mx:TextArea id="dataWorking" text="" right="20" top="3" disabledColor="black" backgroundDisabledColor="0xFFFFEA" height="18"
55             enabled="false" borderThickness="0"
56             showEffect="{wipeIn}" hideEffect="{wipeOut}"/>
57
58                 <potlatch2:Toolbox id="toolbox" 
59                         height="46" width="253" 
60                         headerHeight="6" headerColors="[black, gray]" 
61                         borderThicknessRight="0" borderThicknessLeft="0" borderThicknessBottom="0" 
62                         paddingLeft="4" paddingTop="4" layout="horizontal"
63                         includeInLayout="false" 
64                         x="10" y="300">
65
66                 <mx:Button icon="@Embed('embedded/delete.svg')"
67                                 click='toolbox.doDelete();' 
68                                 enabled="{toolbox.canDo('delete')}" 
69                                 alpha="{toolbox.getAlpha('delete')}" 
70                                 width="28" height="28" textAlign="left" paddingLeft="6" paddingRight="0" />
71                 <mx:Button icon="@Embed('embedded/direction.svg')" 
72                 click='toolbox.doReverseDirection();'
73                                 enabled="{toolbox.canDo('reverseDirection')}" 
74                                 alpha="{toolbox.getAlpha('reverseDirection')}" 
75                                 width="28" height="28" textAlign="left" paddingLeft="8" paddingRight="0" />
76                 <mx:Button icon="@Embed('embedded/cut.svg')" 
77                 click='toolbox.doSplit();'
78                                 enabled="{toolbox.canDo('split')}" 
79                                 alpha="{toolbox.getAlpha('split')}" 
80                                 width="28" height="28" textAlign="left" paddingLeft="8" paddingRight="0" />
81                 <mx:Button icon="@Embed('embedded/straighten.svg')" 
82                                 click='toolbox.doStraighten();' 
83                                 enabled="{toolbox.canDo('straighten')}" 
84                                 alpha="{toolbox.getAlpha('straighten')}" 
85                                 width="28" height="28" textAlign="left" paddingLeft="5" paddingRight="0" />
86                 <mx:Button icon="@Embed('embedded/circle.svg')" 
87                                 click='toolbox.doCircularise();' 
88                                 enabled="{toolbox.canDo('circularise')}" 
89                                 alpha="{toolbox.getAlpha('circularise')}" 
90                                 width="28" height="28" textAlign="left" paddingLeft="4" paddingRight="0" />
91                 <mx:Button icon="@Embed('embedded/quadrilateralise.svg')" 
92                                 click='toolbox.doQuadrilateralise();' 
93                                 enabled="{toolbox.canDo('quadrilateralise')}" 
94                                 alpha="{toolbox.getAlpha('quadrilateralise')}" 
95                                 width="28" height="28" textAlign="left" paddingLeft="6" paddingRight="0" />
96                 <mx:Button icon="@Embed('embedded/parallel.svg')" 
97                                 click='toolbox.doParallelise();' 
98                                 enabled="{toolbox.canDo('parallelise')}" 
99                                 alpha="{toolbox.getAlpha('parallelise')}" 
100                                 width="28" height="28" textAlign="left" paddingLeft="8" paddingRight="0" />
101
102                 </potlatch2:Toolbox>
103
104     </mx:Canvas>
105
106     </mx:HDividedBox>
107
108         <mx:Script><![CDATA[
109                 import net.systemeD.halcyon.*;
110                 import net.systemeD.halcyon.connection.*;
111                 import net.systemeD.potlatch2.*;
112                 import net.systemeD.potlatch2.save.SaveManager;
113                 import net.systemeD.potlatch2.controller.*;
114                 import net.systemeD.potlatch2.help.*;
115                 import net.systemeD.potlatch2.options.*;
116                 import net.systemeD.potlatch2.utils.*;
117                 import mx.managers.PopUpManager;
118                 import flash.system.Security;
119                 import flash.net.*;
120                 import flash.events.MouseEvent;
121                 import flash.display.Sprite;
122                 import mx.core.IChildList;
123         import mx.containers.Canvas;
124                 import mx.core.Application;
125         import mx.events.DragEvent;
126         import mx.managers.DragManager;
127         import mx.core.DragSource;
128
129         public var theMap:Map;
130         public var theController:EditController;
131
132                 private var savecount:uint=0;
133                 private var loadcount:uint=0;
134
135                 private function initApp():void {
136
137                         Globals.vars.map_area = map_area;
138                         Globals.vars.root = map_area.rawChildren;                       // set up global reference to root level
139                         var _root:IChildList = map_area.rawChildren;            // convenient local shorthand
140
141                         // map backdrop object
142             var w:uint = map_area.width;
143             var h:uint = map_area.height;
144             var b:Sprite = new Sprite();
145             b.height=h; b.width=w;
146             b.graphics.beginFill(0xFFFFEA,100); 
147             b.graphics.drawRect(0,0,w,h); 
148             b.graphics.endFill();
149                         _root.addChild(b);
150
151                         // create map and Yahoo
152                         theMap=new Map(this.loaderInfo.parameters);
153                         theMap.backdrop=b;
154                         var yahoo:Yahoo=new Yahoo(w,h,theMap);
155                         yahoo.hide();
156                         Globals.vars.yahoo = yahoo;
157                         _root.addChild(yahoo);
158                         _root.addChild(theMap);
159             theMap.updateSize(w,h);
160
161                         // add mask for map
162                         var s:Sprite=new Sprite();
163                         s.graphics.beginFill(0xFFFFFF,100);
164                         s.graphics.drawRect(0,0,w,h);
165                         s.graphics.endFill();
166                         _root.addChild(s);
167                         theMap.mask=s;
168
169                         // mouse-up handler attached to stage, so the user can release outside the map
170                         stage.addEventListener(MouseEvent.MOUSE_UP, theMap.mouseUpHandler);
171                         Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_MOVE, theMap.mouseMoveHandler);
172                         Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_DOWN, theMap.mouseDownHandler);
173
174                         // keyboard event attached to stage
175                         stage.addEventListener(KeyboardEvent.KEY_UP, theMap.keyUpHandler);
176
177                         // load imagery XML
178                 var request:URLRequest = new URLRequest("imagery.xml?"+Math.random());
179                 var loader:URLLoader = new URLLoader();
180                 loader.addEventListener(Event.COMPLETE, onImageryLoad);
181                 loader.load(request);
182
183                         // add debug field
184                         var t:TextField=new TextField();
185                         t.width=500; t.height=150; t.border=true;
186                         t.multiline=true;
187                         _root.addChild(t);
188                         Globals.vars.debug=t;
189             t.visible = loaderInfo.parameters["show_debug"] == 'true';
190                         Globals.vars.root=theMap;       // just for the addDebug function
191
192             theController = new EditController(theMap, tagViewer, toolbox);
193             theController.setActive();
194             
195             var conn:Connection = Connection.getConnectionInstance();
196             conn.addEventListener(Connection.LOAD_STARTED, onDataStart);
197             conn.addEventListener(Connection.LOAD_COMPLETED, onDataComplete);
198             conn.addEventListener(Connection.SAVE_STARTED, onDataStart);
199             conn.addEventListener(Connection.SAVE_COMPLETED, onDataComplete);
200                 }
201
202         public function onResizeMap():void {
203             if ( theMap != null )
204                 theMap.updateSize(map_area.width, map_area.height);
205         }
206
207         private function onDataStart(event:Event):void {
208                         switch (event.type) {
209                                 case Connection.LOAD_STARTED:   loadcount++; break;
210                                 case Connection.SAVE_STARTED:   savecount++; break;
211                         }
212                         updateDataWorking();
213         }
214         private function onDataComplete(event:Event):void {
215                         switch (event.type) {
216                                 case Connection.LOAD_COMPLETED: loadcount--; break;
217                                 case Connection.SAVE_COMPLETED: savecount--; break;
218                         }
219             updateDataWorking();
220         }
221                 private function updateDataWorking():void {
222                         if (loadcount>0 && savecount>0) { dataWorking.text="Loading/saving..."; }
223                         else if (loadcount>0)           { dataWorking.text="Loading data..."; }
224                         else if (savecount>0)           { dataWorking.text="Saving data..."; }
225                         else                            { dataWorking.text=""; }
226                         dataWorking.visible=(dataWorking.text!="");
227                 }
228         
229         private function onImageryLoad(event:Event):void {
230                         var imagery:XML = new XML(URLLoader(event.target).data);
231                         BackgroundSelector(bgButton.popUp).populate(imagery);
232                 }
233
234         
235         private function dragEnterHandler(event:DragEvent):void {
236             // Get the drop target component from the event object.
237             var dropTarget:Canvas=event.currentTarget as Canvas;
238             // Accept the drag only if the user is dragging poi with tags
239             if (event.dragSource.hasFormat('tags')) 
240             {
241                 DragManager.acceptDragDrop(dropTarget);
242             }
243         }
244         
245         private function dragDropHandler(event:DragEvent):void {
246             // Get the data identified by the color format from the drag source.
247             // Blame http://www.adobe.com/devnet/flex/quickstart/adding_drag_and_drop/#manual
248             // for whatever horrid abuse of "color format" this is doing
249             var tags:Array = event.dragSource.dataForFormat('tags') as Array;
250             var mapLoc:Point = Globals.vars.root.globalToLocal(new Point(event.stageX, event.stageY));
251             var lat:Number = Globals.vars.root.coord2lat(mapLoc.y);
252             var lon:Number = Globals.vars.root.coord2lon(mapLoc.x);
253             
254             var createAction:CompositeUndoableAction = new CompositeUndoableAction("Create POI");
255             
256             var node:Node = Connection.getConnectionInstance().createNode({}, lat, lon, createAction.push);
257             for each ( var tag:Object in tags ) {
258                 node.setTag(tag.k, tag.v, createAction.push);
259             }
260                         Connection.getConnectionInstance().registerPOI(node);
261                         MainUndoStack.getGlobalStack().addAction(createAction);
262             theController.setState(new SelectedPOINode(node));
263         }
264
265         ]]></mx:Script>
266
267 </mx:Application>
268