update toolbox icons depending on what's selected (and add the useful W keypress)
[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                         x="10" y="300">
64
65                 <mx:Button icon="@Embed('embedded/delete.svg')"
66                                 click='toolbox.doDelete();' 
67                                 enabled="{toolbox.canDo('delete')}" 
68                                 alpha="{toolbox.getAlpha('delete')}" 
69                                 width="28" height="28" textAlign="left" paddingLeft="6" paddingRight="0" />
70                 <mx:Button icon="@Embed('embedded/direction.svg')" 
71                 click='toolbox.doReverseDirection();'
72                                 enabled="{toolbox.canDo('reverseDirection')}" 
73                                 alpha="{toolbox.getAlpha('reverseDirection')}" 
74                                 width="28" height="28" textAlign="left" paddingLeft="8" paddingRight="0" />
75                 <mx:Button icon="@Embed('embedded/cut.svg')" 
76                 click='toolbox.doSplit();'
77                                 enabled="{toolbox.canDo('split')}" 
78                                 alpha="{toolbox.getAlpha('split')}" 
79                                 width="28" height="28" textAlign="left" paddingLeft="8" paddingRight="0" />
80                 <mx:Button icon="@Embed('embedded/straighten.svg')" 
81                                 click='toolbox.doStraighten();' 
82                                 enabled="{toolbox.canDo('straighten')}" 
83                                 alpha="{toolbox.getAlpha('straighten')}" 
84                                 width="28" height="28" textAlign="left" paddingLeft="5" paddingRight="0" />
85                 <mx:Button icon="@Embed('embedded/circle.svg')" 
86                                 click='toolbox.doCircularise();' 
87                                 enabled="{toolbox.canDo('circularise')}" 
88                                 alpha="{toolbox.getAlpha('circularise')}" 
89                                 width="28" height="28" textAlign="left" paddingLeft="4" paddingRight="0" />
90                 <mx:Button icon="@Embed('embedded/quadrilateralise.svg')" 
91                                 click='toolbox.doQuadrilateralise();' 
92                                 enabled="{toolbox.canDo('quadrilateralise')}" 
93                                 alpha="{toolbox.getAlpha('quadrilateralise')}" 
94                                 width="28" height="28" textAlign="left" paddingLeft="6" paddingRight="0" />
95                 <mx:Button icon="@Embed('embedded/parallel.svg')" 
96                                 click='toolbox.doParallelise();' 
97                                 enabled="{toolbox.canDo('parallelise')}" 
98                                 alpha="{toolbox.getAlpha('parallelise')}" 
99                                 width="28" height="28" textAlign="left" paddingLeft="8" paddingRight="0" />
100
101                 </potlatch2:Toolbox>
102
103     </mx:Canvas>
104
105     </mx:HDividedBox>
106
107         <mx:Script><![CDATA[
108                 import net.systemeD.halcyon.*;
109                 import net.systemeD.halcyon.connection.*;
110                 import net.systemeD.potlatch2.*;
111                 import net.systemeD.potlatch2.save.SaveManager;
112                 import net.systemeD.potlatch2.controller.*;
113                 import net.systemeD.potlatch2.help.*;
114                 import net.systemeD.potlatch2.options.*;
115                 import net.systemeD.potlatch2.utils.*;
116                 import mx.managers.PopUpManager;
117                 import flash.system.Security;
118                 import flash.net.*;
119                 import flash.events.MouseEvent;
120                 import flash.display.Sprite;
121                 import mx.core.IChildList;
122         import mx.containers.Canvas;
123                 import mx.core.Application;
124         import mx.events.DragEvent;
125         import mx.managers.DragManager;
126         import mx.core.DragSource;
127
128         public var theMap:Map;
129         public var theController:EditController;
130
131                 private function initApp():void {
132
133                         Globals.vars.map_area = map_area;
134                         Globals.vars.root = map_area.rawChildren;                       // set up global reference to root level
135                         var _root:IChildList = map_area.rawChildren;            // convenient local shorthand
136
137                         // map backdrop object
138             var w:uint = map_area.width;
139             var h:uint = map_area.height;
140             var b:Sprite = new Sprite();
141             b.height=h; b.width=w;
142             b.graphics.beginFill(0xFFFFEA,100); 
143             b.graphics.drawRect(0,0,w,h); 
144             b.graphics.endFill();
145                         _root.addChild(b);
146
147                         // create map and Yahoo
148                         theMap=new Map(this.loaderInfo.parameters);
149                         theMap.backdrop=b;
150                         var yahoo:Yahoo=new Yahoo(w,h,theMap);
151                         yahoo.hide();
152                         Globals.vars.yahoo = yahoo;
153                         _root.addChild(yahoo);
154                         _root.addChild(theMap);
155             theMap.updateSize(w,h);
156
157                         // add mask for map
158                         var s:Sprite=new Sprite();
159                         s.graphics.beginFill(0xFFFFFF,100);
160                         s.graphics.drawRect(0,0,w,h);
161                         s.graphics.endFill();
162                         _root.addChild(s);
163                         theMap.mask=s;
164
165                         // add crosshair
166                         /*s=new Sprite();
167                         s.graphics.lineStyle(2,0);
168                         s.graphics.moveTo(stage.stageWidth/2-20,stage.stageHeight/2);
169                         s.graphics.lineTo(stage.stageWidth/2+20,stage.stageHeight/2);
170                         s.graphics.moveTo(stage.stageWidth/2,stage.stageHeight/2-20);
171                         s.graphics.lineTo(stage.stageWidth/2,stage.stageHeight/2+20);
172                         _root.addChild(s);*/
173
174                         // mouse-up handler attached to stage, so the user can release outside the map
175                         stage.addEventListener(MouseEvent.MOUSE_UP, theMap.mouseUpHandler);
176                         Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_MOVE, theMap.mouseMoveHandler);
177                         Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_DOWN, theMap.mouseDownHandler);
178
179                         // keyboard event attached to stage
180                         stage.addEventListener(KeyboardEvent.KEY_UP, theMap.keyUpHandler);
181
182                         // example listener event
183                         var myListenerObj:Object = new Object();
184                         myListenerObj.mapHandler=function(event:MapEvent):void {
185                                 Globals.vars.debug.appendText("Download event fired - "+event.params.minlat+","+event.params.minlon+"\n");
186                         };
187                         theMap.addEventListener(MapEvent.DOWNLOAD, myListenerObj.mapHandler);
188
189                         // add debug field
190                         var t:TextField=new TextField();
191                         t.width=500; t.height=150; t.border=true;
192                         t.multiline=true;
193                         _root.addChild(t);
194                         Globals.vars.debug=t;
195             t.visible = loaderInfo.parameters["show_debug"] == 'true';
196                         Globals.vars.root=theMap;       // just for the addDebug function
197
198             theController = new EditController(theMap, tagViewer, toolbox);
199             theController.setActive();
200             
201             var conn:Connection = Connection.getConnectionInstance();
202             conn.addEventListener(Connection.LOAD_STARTED, onDataStart);
203             conn.addEventListener(Connection.LOAD_COMPLETED, onDataComplete);
204
205                         // and to import a shapefile...
206 //                      var importer:ShpImporter = new ShpImporter(theMap, ["http://127.0.0.1/~richard/shp/wildrnp020.shp","http://127.0.0.1/~richard/shp/wildrnp020.shx","http://127.0.0.1/~richard/shp/wildrnp020.dbf"]);
207                 }
208
209         public function onResizeMap():void {
210             if ( theMap != null )
211                 theMap.updateSize(map_area.width, map_area.height);
212         }
213
214         private function onDataStart(event:Event):void {
215             dataWorking.text = event.type == Connection.LOAD_STARTED ? "Loading data..." : "Saving Data...";
216             dataWorking.visible = true;
217         }
218         private function onDataComplete(event:Event):void {
219             dataWorking.visible = false;
220         }
221         
222
223         
224         private function dragEnterHandler(event:DragEvent):void {
225             // Get the drop target component from the event object.
226             var dropTarget:Canvas=event.currentTarget as Canvas;
227             // Accept the drag only if the user is dragging poi with tags
228             if (event.dragSource.hasFormat('tags')) 
229             {
230                 DragManager.acceptDragDrop(dropTarget);
231             }
232         }
233         
234         private function dragDropHandler(event:DragEvent):void {
235             // Get the data identified by the color format from the drag source.
236             // Blame http://www.adobe.com/devnet/flex/quickstart/adding_drag_and_drop/#manual
237             // for whatever horrid abuse of "color format" this is doing
238             var tags:Array = event.dragSource.dataForFormat('tags') as Array;
239             var mapLoc:Point = Globals.vars.root.globalToLocal(new Point(event.stageX, event.stageY));
240             var lat:Number = Globals.vars.root.coord2lat(mapLoc.y);
241             var lon:Number = Globals.vars.root.coord2lon(mapLoc.x);
242             
243             var createAction:CompositeUndoableAction = new CompositeUndoableAction("Create POI");
244             
245             var node:Node = Connection.getConnectionInstance().createNode({}, lat, lon, createAction.push);
246             for each ( var tag:Object in tags ) {
247                 node.setTag(tag.k, tag.v, createAction.push);
248             }
249                         Connection.getConnectionInstance().registerPOI(node);
250                         MainUndoStack.getGlobalStack().addAction(createAction);
251             theController.setState(new SelectedPOINode(node));
252         }
253
254         ]]></mx:Script>
255
256 </mx:Application>
257