nnnnnnnnnnnnnnnnnnnnnngh
[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     </mx:HDividedBox>
62
63         <mx:Script><![CDATA[
64                 import net.systemeD.halcyon.*;
65                 import net.systemeD.halcyon.connection.*;
66                 import net.systemeD.potlatch2.*;
67                 import net.systemeD.potlatch2.save.SaveManager;
68                 import net.systemeD.potlatch2.controller.*;
69                 import net.systemeD.potlatch2.help.*;
70                 import net.systemeD.potlatch2.options.*;
71                 import net.systemeD.potlatch2.utils.*;
72                 import mx.managers.PopUpManager;
73                 import flash.system.Security;
74                 import flash.net.*;
75                 import flash.events.MouseEvent;
76                 import flash.display.Sprite;
77                 import mx.core.IChildList;
78         import mx.containers.Canvas;
79                 import mx.core.Application;
80         import mx.events.DragEvent;
81         import mx.managers.DragManager;
82         import mx.core.DragSource;
83
84         public var theMap:Map;
85         public var theController:EditController;
86                 public var yahoo:Yahoo;
87                 public var toolbox:Toolbox;
88
89                 private var savecount:uint=0;
90                 private var loadcount:uint=0;
91                 
92                 public var version:String="(Foxbase) Alpha";                    // mxml won't read it if it's a const. Go figure
93
94                 private function initApp():void {
95
96                         Globals.vars.map_area = map_area;
97                         Globals.vars.root = map_area.rawChildren;                       // set up global reference to root level
98                         var _root:IChildList = map_area.rawChildren;            // convenient local shorthand
99
100                         // map backdrop object
101             var w:uint = map_area.width;
102             var h:uint = map_area.height;
103             var b:Sprite = new Sprite();
104             b.height=h; b.width=w;
105             b.graphics.beginFill(0xFFFFEA,100); 
106             b.graphics.drawRect(0,0,w,h); 
107             b.graphics.endFill();
108                         _root.addChild(b);
109
110                         // create map and Yahoo
111                         theMap=new Map(this.loaderInfo.parameters);
112                         theMap.backdrop=b;
113                         yahoo=new Yahoo(w,h,theMap);
114                         yahoo.hide();
115                         _root.addChild(yahoo);
116                         _root.addChild(theMap);
117             theMap.updateSize(w,h);
118                         if (loaderInfo.parameters['tileurl']) {
119                                 setBackground('custom',loaderInfo.parameters['tileurl'],2);
120                         }
121
122                         // add mask for map
123                         var s:Sprite=new Sprite();
124                         s.graphics.beginFill(0xFFFFFF,100);
125                         s.graphics.drawRect(0,0,w,h);
126                         s.graphics.endFill();
127                         _root.addChild(s);
128                         theMap.mask=s;
129
130                         // mouse-up handler attached to stage, so the user can release outside the map
131                         stage.addEventListener(MouseEvent.MOUSE_UP, theMap.mouseUpHandler);
132                         Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_MOVE, theMap.mouseMoveHandler);
133                         Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_DOWN, theMap.mouseDownHandler);
134
135                         // keyboard event attached to stage
136                         stage.addEventListener(KeyboardEvent.KEY_UP, theMap.keyUpHandler);
137
138                         // load imagery XML
139                 var request:URLRequest = new URLRequest("imagery.xml?"+Math.random());
140                 var loader:URLLoader = new URLLoader();
141                 loader.addEventListener(Event.COMPLETE, onImageryLoad);
142                 loader.load(request);
143
144                         // position toolbox
145                         toolbox=Toolbox(PopUpManager.createPopUp(this,Toolbox,false));
146                         toolbox.init(theController);
147                         toolbox.x=stage.stageWidth-toolbox.width-15;
148                         toolbox.y=stage.stageHeight-toolbox.height-15;
149
150                         // add debug field
151                         var t:TextField=new TextField();
152                         t.width=500; t.height=150; t.border=true;
153                         t.multiline=true;
154                         _root.addChild(t);
155                         Globals.vars.debug=t;
156             t.visible = loaderInfo.parameters["show_debug"] == 'true';
157                         Globals.vars.root=theMap;       // just for the addDebug function
158
159             theController = new EditController(theMap, tagViewer, toolbox);
160             theController.setActive();
161             
162             var conn:Connection = Connection.getConnectionInstance();
163             conn.addEventListener(Connection.LOAD_STARTED, onDataStart);
164             conn.addEventListener(Connection.LOAD_COMPLETED, onDataComplete);
165             conn.addEventListener(Connection.SAVE_STARTED, onDataStart);
166             conn.addEventListener(Connection.SAVE_COMPLETED, onDataComplete);
167             conn.addEventListener(Connection.DATA_DIRTY, onDataDirty);
168             conn.addEventListener(Connection.DATA_CLEAN, onDataClean);
169                 }
170                 
171         public function onResizeMap():void {
172             if ( theMap != null )
173                 theMap.updateSize(map_area.width, map_area.height);
174
175                         if (toolbox) {
176                                 toolbox.x=Math.min(toolbox.x,stage.stageWidth-toolbox.width-15);
177                                 toolbox.y=Math.min(toolbox.y,stage.stageHeight-toolbox.height-15);
178                         }
179         }
180
181         private function onDataStart(event:Event):void {
182                         switch (event.type) {
183                                 case Connection.LOAD_STARTED:   loadcount++; break;
184                                 case Connection.SAVE_STARTED:   savecount++; break;
185                         }
186                         updateDataWorking();
187         }
188         private function onDataComplete(event:Event):void {
189                         switch (event.type) {
190                                 case Connection.LOAD_COMPLETED: loadcount--; break;
191                                 case Connection.SAVE_COMPLETED: savecount--; break;
192                         }
193             updateDataWorking();
194         }
195                 private function updateDataWorking():void {
196                         if (loadcount>0 && savecount>0) { dataWorking.text="Loading/saving..."; }
197                         else if (loadcount>0)           { dataWorking.text="Loading data..."; }
198                         else if (savecount>0)           { dataWorking.text="Saving data..."; }
199                         else                            { dataWorking.text=""; }
200                         dataWorking.visible=(dataWorking.text!="");
201                 }
202         private function onDataDirty(event:Event):void {
203                         saveButton.enabled=true;
204                 }
205                 private function onDataClean(event:Event):void {
206                         saveButton.enabled=false;
207                 }
208                 
209         private function onImageryLoad(event:Event):void {
210                         var xml:XML = new XML(URLLoader(event.target).data);
211
212                         // create menu
213             theController.imagery=new Array(
214                                 { label: "None", data: "" },
215                                 { label: "Yahoo", data: "yahoo" } );
216                         for each(var set:XML in xml.set) {
217                 theController.imagery.push({ label:set.child("name"), data:set.child("url") });
218                         }
219
220                         // set imagery from shared object
221                         var previous:String = SharedObject.getLocal("user_state").data['background'];
222                         for each (var bg:Object in theController.imagery) {
223                                 if (bg['label']==previous) { 
224                                         setBackground(bg['label'], bg['data'], 1);
225                                 }
226                         }
227                 }
228
229                 public function setBackground(name:String,url:String,priority:uint=2):void {
230                         // ** this should take an object with all parameters (source tag, etc.)
231                         var set:Boolean;
232                         if (url=='yahoo') {
233                                 set=theMap.setBackground('',2);
234                                 yahoo.show();
235                         } else {
236                                 set=theMap.setBackground(url,priority)
237                                 yahoo.hide();
238                         }
239                         if (set) {
240                                 var obj:SharedObject = SharedObject.getLocal("user_state");
241                                 obj.setProperty("background", name);
242                                 obj.flush();
243                         }
244                 }
245
246         private function dragEnterHandler(event:DragEvent):void {
247             // Get the drop target component from the event object.
248             var dropTarget:Canvas=event.currentTarget as Canvas;
249             // Accept the drag only if the user is dragging poi with tags
250             if (event.dragSource.hasFormat('tags')) 
251             {
252                 DragManager.acceptDragDrop(dropTarget);
253             }
254         }
255         
256         private function dragDropHandler(event:DragEvent):void {
257             // Get the data identified by the color format from the drag source.
258             // Blame http://www.adobe.com/devnet/flex/quickstart/adding_drag_and_drop/#manual
259             // for whatever horrid abuse of "color format" this is doing
260             var tags:Array = event.dragSource.dataForFormat('tags') as Array;
261             var mapLoc:Point = Globals.vars.root.globalToLocal(new Point(event.stageX, event.stageY));
262             var lat:Number = Globals.vars.root.coord2lat(mapLoc.y);
263             var lon:Number = Globals.vars.root.coord2lon(mapLoc.x);
264             
265             var createAction:CompositeUndoableAction = new CompositeUndoableAction("Create POI");
266             
267             var node:Node = Connection.getConnectionInstance().createNode({}, lat, lon, createAction.push);
268             for each ( var tag:Object in tags ) {
269                 node.setTag(tag.k, tag.v, createAction.push);
270             }
271                         Connection.getConnectionInstance().registerPOI(node);
272                         MainUndoStack.getGlobalStack().addAction(createAction);
273             theController.setState(new SelectedPOINode(node));
274         }
275
276         ]]></mx:Script>
277
278 </mx:Application>
279