Add two rules for rendering power lines
[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         horizontalScrollPolicy="off"
8         verticalScrollPolicy="off"
9         horizontalAlign="center" 
10         addedToStage="initApp()">
11         
12         <mx:Style source="styles/Application.css"/>
13
14     <mx:Glow id="glowImage" duration="100" 
15         alphaFrom="0.3" alphaTo="1.0" 
16         blurXFrom="0.0" blurXTo="5.0" 
17         blurYFrom="0.0" blurYTo="5.0" 
18         color="0xFF8000"/>
19     <mx:Glow id="unglowImage" duration="100" 
20         alphaFrom="1.0" alphaTo="0.3" 
21         blurXFrom="5.0" blurXTo="0.0" 
22         blurYFrom="5.0" blurYTo="0.0" 
23         color="0xFF8000"/>
24     <mx:WipeLeft id="wipeOut" duration="250"/>
25     <mx:WipeRight id="wipeIn" duration="250"/>
26
27     <mx:ApplicationControlBar dock="true">
28         <mx:PopUpButton id="bgButton" label="Background" openAlways="true"
29             creationComplete="bgButton.popUp = new BackgroundSelector();"/>
30         <mx:PopUpButton id="styleButton" label="Map Style" openAlways="true"
31             creationComplete="styleButton.popUp = new StyleSelector();"/>
32         <mx:Button label="GPS" click="trackLoader.load();"/>
33         <mx:Spacer width="100%"/>
34         <mx:Button label="Undo" click="MainUndoStack.getGlobalStack().undo();"
35             enabled="{MainUndoStack.getGlobalStack().canUndo()}"/>
36         <mx:Button label="Redo" click="MainUndoStack.getGlobalStack().redo();"
37             enabled="{MainUndoStack.getGlobalStack().canRedo()}"/>
38         <mx:Spacer width="100%"/>
39         <mx:Button label="Help" click="new HelpDialog().init();" />
40         <mx:Button label="Options" click="new OptionsDialog().init();" /> 
41         <mx:Button label="Save" icon="@Embed('embedded/save.svg')" click="SaveManager.saveChanges();" id="saveButton" 
42                         enabled="false"/>
43     </mx:ApplicationControlBar>
44     
45     <mx:HDividedBox width="100%" height="100%">
46
47           <!-- Tag viewer -->
48           <potlatch2:TagViewer width="25%" height="100%" id="tagViewer"/>
49
50       <mx:Canvas width="75%" height="100%">
51         <mx:Canvas id="map_area" resize="onResizeMap()"
52             top="0" left="0" width="100%" height="100%" dragEnter="dragEnterHandler(event);" dragDrop="dragDropHandler(event);">
53         </mx:Canvas>
54         <mx:Image source="@Embed('embedded/zoomIn.svg')" right="3" top="3" click="theMap.zoomIn();"
55             rollOverEffect="glowImage" rollOutEffect="unglowImage"/>
56         <mx:Image source="@Embed('embedded/zoomOut.svg')" right="3" top="20" click="theMap.zoomOut();"
57             rollOverEffect="glowImage" rollOutEffect="unglowImage"/>
58         <mx:TextArea id="dataWorking" text="" right="20" top="3" disabledColor="black" backgroundDisabledColor="0xFFFFEA" height="18"
59             enabled="false" borderThickness="0"
60             showEffect="{wipeIn}" hideEffect="{wipeOut}"/>
61
62     </mx:Canvas>
63
64     </mx:HDividedBox>
65
66         <mx:Script><![CDATA[
67                 import net.systemeD.halcyon.*;
68                 import net.systemeD.halcyon.connection.*;
69                 import net.systemeD.potlatch2.*;
70                 import net.systemeD.potlatch2.save.SaveManager;
71                 import net.systemeD.potlatch2.controller.*;
72                 import net.systemeD.potlatch2.help.*;
73                 import net.systemeD.potlatch2.options.*;
74                 import net.systemeD.potlatch2.utils.*;
75                 import mx.managers.PopUpManager;
76                 import flash.system.Security;
77                 import flash.net.*;
78                 import flash.events.MouseEvent;
79                 import flash.display.Sprite;
80                 import mx.core.IChildList;
81         import mx.containers.Canvas;
82                 import mx.core.Application;
83         import mx.events.DragEvent;
84         import mx.managers.DragManager;
85         import mx.core.DragSource;
86         import mx.controls.Alert;
87
88         public var theMap:Map;
89         public var theController:EditController;
90                 public var yahoo:Yahoo;
91                 public var trackLoader:TrackLoader;
92                 public var toolbox:Toolbox;
93
94                 private var savecount:uint=0;
95                 private var loadcount:uint=0;
96                 
97                 public var version:String="(Foxbase) Alpha";                    // mxml won't read it if it's a const. Go figure
98
99                 private function initApp():void {
100
101                         Globals.vars.map_area = map_area;
102                         Globals.vars.root = map_area.rawChildren;                       // set up global reference to root level
103                         var _root:IChildList = map_area.rawChildren;            // convenient local shorthand
104                         Globals.vars.nocache = loaderInfo.parameters['nocache'] == 'true';
105
106                         // populate sharedObject with loaderInfo parameters if supplied
107                         var obj:SharedObject = SharedObject.getLocal("user_state");
108                         var objChanged:Boolean = false;
109                         if (loaderInfo.parameters['tileurl']) {
110                                 obj.setProperty('background_url',loaderInfo.parameters['tileurl']); 
111                                 obj.setProperty('background_name','Custom'); 
112                                 objChanged=true;
113                         }
114                         if (loaderInfo.parameters['style']) { 
115                                 obj.setProperty('stylesheet_url',loaderInfo.parameters['style']);
116                                 obj.setProperty('stylesheet_name','Custom');
117                                 objChanged=true; 
118                         }
119                         if (objChanged) { obj.flush(); }
120
121                         // load imagery and style XML
122                 var request:DebugURLRequest = new DebugURLRequest("imagery.xml");
123                 var loader:URLLoader = new URLLoader();
124                 loader.addEventListener(Event.COMPLETE, onImageryLoad);
125                 loader.load(request.request);
126
127                         var request2:DebugURLRequest = new DebugURLRequest("stylesheets.xml");
128                         var loader2:URLLoader = new URLLoader();
129                 loader2.addEventListener(Event.COMPLETE, onStylesheetsLoad);
130                 loader2.load(request2.request);
131
132                         // map backdrop object
133             var w:uint = map_area.width;
134             var h:uint = map_area.height;
135             var b:Sprite = new Sprite();
136             b.height=h; b.width=w;
137             b.graphics.beginFill(0xFFFFEA,100); 
138             b.graphics.drawRect(0,0,w,h); 
139             b.graphics.endFill();
140                         _root.addChild(b);
141
142                         // create map and Yahoo
143                         theMap=new Map(this.loaderInfo.parameters);
144                         theMap.backdrop=b;
145                         yahoo=new Yahoo(w,h,theMap);
146                         yahoo.hide();
147                         _root.addChild(yahoo);
148                         _root.addChild(theMap);
149             theMap.updateSize(w,h);
150
151                         // add mask for map
152                         var s:Sprite=new Sprite();
153                         s.graphics.beginFill(0xFFFFFF,100);
154                         s.graphics.drawRect(0,0,w,h);
155                         s.graphics.endFill();
156                         _root.addChild(s);
157                         theMap.mask=s;
158
159                         // mouse-up handler attached to stage, so the user can release outside the map
160                         stage.addEventListener(MouseEvent.MOUSE_UP, theMap.mouseUpHandler);
161                         Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_MOVE, theMap.mouseMoveHandler);
162                         Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_DOWN, theMap.mouseDownHandler);
163
164                         // keyboard event attached to stage
165                         stage.addEventListener(KeyboardEvent.KEY_UP, theMap.keyUpHandler);
166
167                         // position toolbox
168                         toolbox=Toolbox(PopUpManager.createPopUp(this,Toolbox,false));
169                         toolbox.init(theController);
170                         toolbox.x=stage.stageWidth-toolbox.width-15;
171                         toolbox.y=stage.stageHeight-toolbox.height-15;
172
173                         // add debug field
174                         var t:TextField=new TextField();
175                         t.width=500; t.height=150; t.border=true;
176                         t.multiline=true;
177                         _root.addChild(t);
178                         Globals.vars.debug=t;
179             t.visible = loaderInfo.parameters["show_debug"] == 'true';
180                         Globals.vars.root=theMap;       // just for the addDebug function
181
182             theController = new EditController(theMap, tagViewer, toolbox);
183             theController.setActive();
184             
185             var conn:Connection = Connection.getConnectionInstance();
186             conn.addEventListener(Connection.LOAD_STARTED, onDataStart);
187             conn.addEventListener(Connection.LOAD_COMPLETED, onDataComplete);
188             conn.addEventListener(Connection.SAVE_STARTED, onDataStart);
189             conn.addEventListener(Connection.SAVE_COMPLETED, onDataComplete);
190             conn.addEventListener(Connection.DATA_DIRTY, onDataDirty);
191             conn.addEventListener(Connection.DATA_CLEAN, onDataClean);
192                         conn.addEventListener(MapEvent.ERROR, onMapError);
193
194                         // create GPS trackloader
195                         trackLoader=new TrackLoader(theMap,conn.apiBase);
196
197             // show help dialog on startup, if required
198             // show_help == always => on every startup
199             // show_help == once => show on first startup only
200             var show_help:String = loaderInfo.parameters["show_help"];
201
202             if (show_help == 'always' || (show_help == 'once' && obj.data["help_shown"] != "true")) {
203               new HelpDialog().init();
204             }
205                 }
206                 
207                 public function onMapError(event:MapEvent):void {
208                         Alert.show(event.params.message, 'Error', mx.controls.Alert.OK);
209                 }
210
211         public function onResizeMap():void {
212             if ( theMap != null )
213                 theMap.updateSize(map_area.width, map_area.height);
214
215                         if (toolbox) {
216                                 toolbox.x=Math.min(toolbox.x,stage.stageWidth-toolbox.width-15);
217                                 toolbox.y=Math.min(toolbox.y,stage.stageHeight-toolbox.height-15);
218                         }
219         }
220
221         private function onDataStart(event:Event):void {
222                         switch (event.type) {
223                                 case Connection.LOAD_STARTED:   loadcount++; break;
224                                 case Connection.SAVE_STARTED:   savecount++; break;
225                         }
226                         updateDataWorking();
227         }
228         private function onDataComplete(event:Event):void {
229                         switch (event.type) {
230                                 case Connection.LOAD_COMPLETED: loadcount--; break;
231                                 case Connection.SAVE_COMPLETED: savecount--; break;
232                         }
233             updateDataWorking();
234         }
235                 private function updateDataWorking():void {
236                         if (loadcount>0 && savecount>0) { dataWorking.text="Loading/saving..."; }
237                         else if (loadcount>0)           { dataWorking.text="Loading data..."; }
238                         else if (savecount>0)           { dataWorking.text="Saving data..."; }
239                         else                            { dataWorking.text=""; }
240                         dataWorking.visible=(dataWorking.text!="");
241                 }
242         private function onDataDirty(event:Event):void {
243                         saveButton.enabled=true;
244             if (ExternalInterface.available) {
245               ExternalInterface.call("markChanged", false);
246             }
247                 }
248                 private function onDataClean(event:Event):void {
249                         saveButton.enabled=false;
250             if (ExternalInterface.available) {
251               ExternalInterface.call("markChanged", true);
252             }
253                 }
254                 
255         private function onImageryLoad(event:Event):void {
256                         var xml:XML = new XML(URLLoader(event.target).data);
257                         var saved_url:String = SharedObject.getLocal("user_state").data['background_url'];
258                         var saved_name:String= SharedObject.getLocal("user_state").data['background_name'];
259                         var xml_url:String, xml_name:String, isSet:Boolean=false;
260             theController.imagery=new Array(
261                                 { label: "None", data: "" },
262                                 { label: "Yahoo", data: "yahoo" } );
263                         for each(var set:XML in xml.set) {
264                                 xml_url= set.child("url");
265                                 xml_name=set.child("name");
266                 theController.imagery.push({ label:xml_name, data:xml_url });
267                                 if (xml_url==saved_url || (xml_name==saved_name && xml_name!='Custom')) { isSet=true; }
268                         }
269                         if (!isSet) { theController.imagery.push({ label:saved_name, data:saved_url }); }
270
271                         for each (var bg:Object in theController.imagery) {
272                                 if (bg['label']==saved_nameĀ || bg['data']==saved_url) {
273                                         setBackground(bg['label'], bg['data']);
274                                 }
275                         }
276                 }
277                 
278                 public function setBackground(name:String,url:String):void {
279                         // ** this should take an object with all parameters (source tag, etc.)
280                         if (url=='yahoo') { theMap.setBackground('') ; yahoo.show(); }
281                                      else { theMap.setBackground(url); yahoo.hide(); }
282                         var obj:SharedObject = SharedObject.getLocal("user_state");
283                         obj.setProperty("background_url",url );
284                         obj.setProperty("background_name",name);
285                         obj.flush();
286                 }
287
288                 private function onStylesheetsLoad(event:Event):void {
289                         var xml:XML = new XML(URLLoader(event.target).data);
290                         var saved_url:String = SharedObject.getLocal("user_state").data['stylesheet_url'];
291                         var saved_name:String= SharedObject.getLocal("user_state").data['stylesheet_name'];
292                         var xml_url:String, xml_name:String, isInMenu:Boolean=false, isSet:Boolean=false;
293
294             // first, build the menu from the stylesheet list.
295             // Also ensure the saved_url is in the menu (might be either saved from before, or supplied via loaderInfo)
296             theController.stylesheets=new Array();
297                         for each(var set:XML in xml.stylesheet) {
298                                 xml_url= set.child("url");
299                                 xml_name=set.child("name");
300                 theController.stylesheets.push({ label:xml_name, data:xml_url });
301                                 if (xml_url==saved_url || (xml_name==saved_name && xml_name!='Custom')) { isInMenu=true; }
302                         }
303                         if (saved_url && !isInMenu) { theController.stylesheets.push({ label:saved_name, data:saved_url }); }
304
305             // pick a stylesheet to be set. It should be the saved one, if it is in the menu
306             // or alternatively the first one on the menu,
307             // or finally try 'potlatch.css'
308                         for each (var ss:Object in theController.stylesheets) {
309                                 if (ss['label']==saved_nameĀ || ss['data']==saved_url) {
310                                         setStylesheet(ss['label'], ss['data']);
311                     isSet = true;
312                     break;
313                                 }
314                         }
315             if (!isSet) {
316               if(theController.stylesheets.length > 0) {
317                 var s:Object = theController.stylesheets[0];
318                 setStylesheet(s['label'], s['data']);
319               } else {
320                 //hit and hope. FIXME should this be an error state?
321                 theController.stylesheets.push({ label:'Potlatch', data:'potlatch.css'});
322                 setStylesheet('Potlatch','potlatch.css');
323               }
324             }
325                 }
326
327                 public function setStylesheet(name:String,url:String):void {
328                         theMap.setStyle(url);
329                         var obj:SharedObject = SharedObject.getLocal("user_state");
330                         obj.setProperty("stylesheet_url",url);
331                         obj.setProperty("stylesheet_name",name);
332                         obj.flush();
333                 }
334
335
336         private function dragEnterHandler(event:DragEvent):void {
337             // Get the drop target component from the event object.
338             var dropTarget:Canvas=event.currentTarget as Canvas;
339             // Accept the drag only if the user is dragging poi with tags
340             if (event.dragSource.hasFormat('tags')) 
341             {
342                 DragManager.acceptDragDrop(dropTarget);
343             }
344         }
345         
346         private function dragDropHandler(event:DragEvent):void {
347                         // Deselect the dragged icon
348                         if (event.dragSource.dataForFormat('container')) {
349                                 event.dragSource.dataForFormat('container').selectedItem=-1;
350                         }
351
352                         // Get the tags
353             var tags:Array = event.dragSource.dataForFormat('tags') as Array;
354             var mapLoc:Point = Globals.vars.root.globalToLocal(new Point(event.stageX, event.stageY));
355             var lat:Number = Globals.vars.root.coord2lat(mapLoc.y);
356             var lon:Number = Globals.vars.root.coord2lon(mapLoc.x);
357             
358             var createAction:CompositeUndoableAction = new CompositeUndoableAction("Create POI");
359             
360             var node:Node = Connection.getConnectionInstance().createNode({}, lat, lon, createAction.push);
361             for each ( var tag:Object in tags ) {
362                 node.setTag(tag.k, tag.v, createAction.push);
363             }
364                         Connection.getConnectionInstance().registerPOI(node);
365                         MainUndoStack.getGlobalStack().addAction(createAction);
366             theController.setState(new SelectedPOINode(node));
367         }
368
369         ]]></mx:Script>
370
371 </mx:Application>
372