one's a worksforme and the other isn't really a blocker at all. So: woohoo!
[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();" id="saveButton" 
39                         enabled="false"/>
40     </mx:ApplicationControlBar>
41     
42     <mx:HDividedBox width="100%" height="100%">
43
44           <!-- Tag viewer -->
45           <potlatch2:TagViewer width="25%" height="100%" id="tagViewer"/>
46
47       <mx:Canvas width="75%" height="100%">
48         <mx:Canvas id="map_area" resize="onResizeMap()"
49             top="0" left="0" width="100%" height="100%" dragEnter="dragEnterHandler(event);" dragDrop="dragDropHandler(event);">
50         </mx:Canvas>
51         <mx:Image source="@Embed('embedded/zoomIn.svg')" right="3" top="3" click="theMap.zoomIn();"
52             rollOverEffect="glowImage" rollOutEffect="unglowImage"/>
53         <mx:Image source="@Embed('embedded/zoomOut.svg')" right="3" top="20" click="theMap.zoomOut();"
54             rollOverEffect="glowImage" rollOutEffect="unglowImage"/>
55         <mx:TextArea id="dataWorking" text="" right="20" top="3" disabledColor="black" backgroundDisabledColor="0xFFFFEA" height="18"
56             enabled="false" borderThickness="0"
57             showEffect="{wipeIn}" hideEffect="{wipeOut}"/>
58
59     </mx:Canvas>
60
61                 <potlatch2:Toolbox id="toolbox" 
62                         height="46" width="253" 
63                         headerHeight="6" headerColors="[black, gray]" 
64                         borderThicknessRight="0" borderThicknessLeft="0" borderThicknessBottom="0" 
65                         paddingLeft="4" paddingTop="4" layout="horizontal"
66                         includeInLayout="false" 
67                         creationComplete="toolboxClose.y=-6; toolboxClose.x=239;">
68
69                         <mx:Image data="@Embed('embedded/close_small.png')"
70                             includeInLayout="false" id="toolboxClose" click="toolbox.toggle();" />
71
72                 <mx:Button icon="@Embed('embedded/delete.svg')"
73                                 click='toolbox.doDelete();' 
74                                 enabled="{toolbox.canDo('delete')}" 
75                                 alpha="{toolbox.getAlpha('delete')}" 
76                                 toolTip="Delete item" 
77                                 width="28" height="28" textAlign="left" paddingLeft="6" paddingRight="0" />
78                 <mx:Button icon="@Embed('embedded/direction.svg')" 
79                 click='toolbox.doReverseDirection();'
80                                 enabled="{toolbox.canDo('reverseDirection')}" 
81                                 alpha="{toolbox.getAlpha('reverseDirection')}" 
82                                 toolTip="Reverse direction" 
83                                 width="28" height="28" textAlign="left" paddingLeft="8" paddingRight="0" />
84                 <mx:Button icon="@Embed('embedded/cut.svg')" 
85                 click='toolbox.doSplit();'
86                                 enabled="{toolbox.canDo('split')}" 
87                                 alpha="{toolbox.getAlpha('split')}" 
88                                 toolTip="Split way" 
89                                 width="28" height="28" textAlign="left" paddingLeft="8" paddingRight="0" />
90                 <mx:Button icon="@Embed('embedded/straighten.svg')" 
91                                 click='toolbox.doStraighten();' 
92                                 enabled="{toolbox.canDo('straighten')}" 
93                                 alpha="{toolbox.getAlpha('straighten')}" 
94                                 toolTip="Straighten way" 
95                                 width="28" height="28" textAlign="left" paddingLeft="5" paddingRight="0" />
96                 <mx:Button icon="@Embed('embedded/circle.svg')" 
97                                 click='toolbox.doCircularise();' 
98                                 enabled="{toolbox.canDo('circularise')}" 
99                                 alpha="{toolbox.getAlpha('circularise')}" 
100                                 toolTip="Make circular" 
101                                 width="28" height="28" textAlign="left" paddingLeft="4" paddingRight="0" />
102                 <mx:Button icon="@Embed('embedded/quadrilateralise.svg')" 
103                                 click='toolbox.doQuadrilateralise();' 
104                                 enabled="{toolbox.canDo('quadrilateralise')}" 
105                                 alpha="{toolbox.getAlpha('quadrilateralise')}" 
106                                 toolTip="Make right-angled" 
107                                 width="28" height="28" textAlign="left" paddingLeft="6" paddingRight="0" />
108                 <mx:Button icon="@Embed('embedded/parallel.svg')" 
109                                 click='toolbox.doParallelise();' 
110                                 enabled="{toolbox.canDo('parallelise')}" 
111                                 alpha="{toolbox.getAlpha('parallelise')}" 
112                                 toolTip="Create parallel way" 
113                                 width="28" height="28" textAlign="left" paddingLeft="8" paddingRight="0" />
114
115                 </potlatch2:Toolbox>
116
117     </mx:HDividedBox>
118
119         <mx:Script><![CDATA[
120                 import net.systemeD.halcyon.*;
121                 import net.systemeD.halcyon.connection.*;
122                 import net.systemeD.potlatch2.*;
123                 import net.systemeD.potlatch2.save.SaveManager;
124                 import net.systemeD.potlatch2.controller.*;
125                 import net.systemeD.potlatch2.help.*;
126                 import net.systemeD.potlatch2.options.*;
127                 import net.systemeD.potlatch2.utils.*;
128                 import mx.managers.PopUpManager;
129                 import flash.system.Security;
130                 import flash.net.*;
131                 import flash.events.MouseEvent;
132                 import flash.display.Sprite;
133                 import mx.core.IChildList;
134         import mx.containers.Canvas;
135                 import mx.core.Application;
136         import mx.events.DragEvent;
137         import mx.managers.DragManager;
138         import mx.core.DragSource;
139
140         public var theMap:Map;
141         public var theController:EditController;
142                 public var yahoo:Yahoo;
143
144                 private var savecount:uint=0;
145                 private var loadcount:uint=0;
146                 
147                 public var version:String="(Foxbase) Alpha";                    // mxml won't read it if it's a const. Go figure
148
149                 private function initApp():void {
150
151                         Globals.vars.map_area = map_area;
152                         Globals.vars.root = map_area.rawChildren;                       // set up global reference to root level
153                         var _root:IChildList = map_area.rawChildren;            // convenient local shorthand
154
155                         // map backdrop object
156             var w:uint = map_area.width;
157             var h:uint = map_area.height;
158             var b:Sprite = new Sprite();
159             b.height=h; b.width=w;
160             b.graphics.beginFill(0xFFFFEA,100); 
161             b.graphics.drawRect(0,0,w,h); 
162             b.graphics.endFill();
163                         _root.addChild(b);
164
165                         // create map and Yahoo
166                         theMap=new Map(this.loaderInfo.parameters);
167                         theMap.backdrop=b;
168                         yahoo=new Yahoo(w,h,theMap);
169                         yahoo.hide();
170                         _root.addChild(yahoo);
171                         _root.addChild(theMap);
172             theMap.updateSize(w,h);
173                         if (loaderInfo.parameters['tileurl']) {
174                                 setBackground('custom',loaderInfo.parameters['tileurl'],2);
175                         }
176
177                         // add mask for map
178                         var s:Sprite=new Sprite();
179                         s.graphics.beginFill(0xFFFFFF,100);
180                         s.graphics.drawRect(0,0,w,h);
181                         s.graphics.endFill();
182                         _root.addChild(s);
183                         theMap.mask=s;
184
185                         // mouse-up handler attached to stage, so the user can release outside the map
186                         stage.addEventListener(MouseEvent.MOUSE_UP, theMap.mouseUpHandler);
187                         Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_MOVE, theMap.mouseMoveHandler);
188                         Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_DOWN, theMap.mouseDownHandler);
189
190                         // keyboard event attached to stage
191                         stage.addEventListener(KeyboardEvent.KEY_UP, theMap.keyUpHandler);
192
193                         // load imagery XML
194                 var request:URLRequest = new URLRequest("imagery.xml?"+Math.random());
195                 var loader:URLLoader = new URLLoader();
196                 loader.addEventListener(Event.COMPLETE, onImageryLoad);
197                 loader.load(request);
198
199                         // position toolbox
200                         toolbox.x=stage.stageWidth-275;
201                         toolbox.y=stage.stageHeight-100;
202
203                         // add debug field
204                         var t:TextField=new TextField();
205                         t.width=500; t.height=150; t.border=true;
206                         t.multiline=true;
207                         _root.addChild(t);
208                         Globals.vars.debug=t;
209             t.visible = loaderInfo.parameters["show_debug"] == 'true';
210                         Globals.vars.root=theMap;       // just for the addDebug function
211
212             theController = new EditController(theMap, tagViewer, toolbox);
213             theController.setActive();
214             
215             var conn:Connection = Connection.getConnectionInstance();
216             conn.addEventListener(Connection.LOAD_STARTED, onDataStart);
217             conn.addEventListener(Connection.LOAD_COMPLETED, onDataComplete);
218             conn.addEventListener(Connection.SAVE_STARTED, onDataStart);
219             conn.addEventListener(Connection.SAVE_COMPLETED, onDataComplete);
220             conn.addEventListener(Connection.DATA_DIRTY, onDataDirty);
221             conn.addEventListener(Connection.DATA_CLEAN, onDataClean);
222                 }
223                 
224         public function onResizeMap():void {
225             if ( theMap != null )
226                 theMap.updateSize(map_area.width, map_area.height);
227         }
228
229         private function onDataStart(event:Event):void {
230                         switch (event.type) {
231                                 case Connection.LOAD_STARTED:   loadcount++; break;
232                                 case Connection.SAVE_STARTED:   savecount++; break;
233                         }
234                         updateDataWorking();
235         }
236         private function onDataComplete(event:Event):void {
237                         switch (event.type) {
238                                 case Connection.LOAD_COMPLETED: loadcount--; break;
239                                 case Connection.SAVE_COMPLETED: savecount--; break;
240                         }
241             updateDataWorking();
242         }
243                 private function updateDataWorking():void {
244                         if (loadcount>0 && savecount>0) { dataWorking.text="Loading/saving..."; }
245                         else if (loadcount>0)           { dataWorking.text="Loading data..."; }
246                         else if (savecount>0)           { dataWorking.text="Saving data..."; }
247                         else                            { dataWorking.text=""; }
248                         dataWorking.visible=(dataWorking.text!="");
249                 }
250         private function onDataDirty(event:Event):void {
251                         saveButton.enabled=true;
252                 }
253                 private function onDataClean(event:Event):void {
254                         saveButton.enabled=false;
255                 }
256                 
257         private function onImageryLoad(event:Event):void {
258                         var xml:XML = new XML(URLLoader(event.target).data);
259
260                         // create menu
261             theController.imagery=new Array(
262                                 { label: "None", data: "" },
263                                 { label: "Yahoo", data: "yahoo" } );
264                         for each(var set:XML in xml.set) {
265                 theController.imagery.push({ label:set.child("name"), data:set.child("url") });
266                         }
267
268                         // set imagery from shared object
269                         var previous:String = SharedObject.getLocal("user_state").data['background'];
270                         for each (var bg:Object in theController.imagery) {
271                                 if (bg['label']==previous) { 
272                                         setBackground(bg['label'], bg['data'], 1);
273                                 }
274                         }
275                 }
276
277                 public function setBackground(name:String,url:String,priority:uint=2):void {
278                         // ** this should take an object with all parameters (source tag, etc.)
279                         var set:Boolean;
280                         if (url=='yahoo') {
281                                 set=theMap.setBackground('',2);
282                                 yahoo.show();
283                         } else {
284                                 set=theMap.setBackground(url,priority)
285                                 yahoo.hide();
286                         }
287                         if (set) {
288                                 var obj:SharedObject = SharedObject.getLocal("user_state");
289                                 obj.setProperty("background", name);
290                                 obj.flush();
291                         }
292                 }
293
294         private function dragEnterHandler(event:DragEvent):void {
295             // Get the drop target component from the event object.
296             var dropTarget:Canvas=event.currentTarget as Canvas;
297             // Accept the drag only if the user is dragging poi with tags
298             if (event.dragSource.hasFormat('tags')) 
299             {
300                 DragManager.acceptDragDrop(dropTarget);
301             }
302         }
303         
304         private function dragDropHandler(event:DragEvent):void {
305             // Get the data identified by the color format from the drag source.
306             // Blame http://www.adobe.com/devnet/flex/quickstart/adding_drag_and_drop/#manual
307             // for whatever horrid abuse of "color format" this is doing
308             var tags:Array = event.dragSource.dataForFormat('tags') as Array;
309             var mapLoc:Point = Globals.vars.root.globalToLocal(new Point(event.stageX, event.stageY));
310             var lat:Number = Globals.vars.root.coord2lat(mapLoc.y);
311             var lon:Number = Globals.vars.root.coord2lon(mapLoc.x);
312             
313             var createAction:CompositeUndoableAction = new CompositeUndoableAction("Create POI");
314             
315             var node:Node = Connection.getConnectionInstance().createNode({}, lat, lon, createAction.push);
316             for each ( var tag:Object in tags ) {
317                 node.setTag(tag.k, tag.v, createAction.push);
318             }
319                         Connection.getConnectionInstance().registerPOI(node);
320                         MainUndoStack.getGlobalStack().addAction(createAction);
321             theController.setState(new SelectedPOINode(node));
322         }
323
324         ]]></mx:Script>
325
326 </mx:Application>
327