support GPX 1.1 too
[potlatch2.git] / net / systemeD / potlatch2 / controller / DrawWay.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3         import flash.geom.*;
4         import flash.display.DisplayObject;
5         import flash.ui.Keyboard;
6         import net.systemeD.potlatch2.EditController;
7         import net.systemeD.halcyon.connection.*;
8     import net.systemeD.halcyon.connection.actions.*;
9         import net.systemeD.halcyon.Elastic;
10         import net.systemeD.halcyon.Globals;
11         import net.systemeD.halcyon.MapPaint;
12
13         public class DrawWay extends SelectedWay {
14                 private var elastic:Elastic;
15                 private var editEnd:Boolean;
16                 private var leaveNodeSelected:Boolean;
17                 private var lastClick:Entity=null;
18                 private var lastClickTime:Date;
19                 private var hoverEntity:Entity;                 // keep track of the currently rolled-over object, because
20                                                                                                 // Flash can fire a mouseDown from the map even if you
21                                                                                                 // haven't rolled out of the way
22                 
23                 public function DrawWay(way:Way, editEnd:Boolean, leaveNodeSelected:Boolean) {
24                         super(way);
25                         this.editEnd = editEnd;
26                         this.leaveNodeSelected = leaveNodeSelected;
27                         if (way.length==1 && way.getNode(0).parentWays.length==1) {
28                                 // drawing new way, so keep track of click in case creating a POI
29                                 lastClick=way.getNode(0);
30                                 lastClickTime=new Date();
31                         }
32             way.addEventListener(Connection.WAY_NODE_REMOVED, fixElastic);
33             way.addEventListener(Connection.WAY_NODE_ADDED, fixElastic);
34                 }
35                 
36                 override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
37                         var mouse:Point;
38                         var node:Node;
39                         var paint:MapPaint = getMapPaint(DisplayObject(event.target));
40                         var isBackground:Boolean = paint && paint.isBackground;
41
42                         if (entity == null && hoverEntity) { entity=hoverEntity; }
43                         var focus:Entity = getTopLevelFocusEntity(entity);
44
45                         if ( event.type == MouseEvent.MOUSE_UP ) {
46                 controller.map.mouseUpHandler(); // in case you're still in the drag-tolerance zone, and mouse up over something.
47                                 if ( entity == null || isBackground ) {
48                                         node = createAndAddNode(event, MainUndoStack.getGlobalStack().addAction);
49                     controller.map.setHighlight(node, { selectedway: true });
50                     controller.map.setPurgable([node], false);
51                                         resetElastic(node);
52                                         lastClick=node;
53                                 } else if ( entity is Node ) {
54                                         if (entity==lastClick && (new Date().getTime()-lastClickTime.getTime())<1000) {
55                                                 if (Way(firstSelected).length==1 && Way(firstSelected).getNode(0).parentWays.length==1) {
56                                                         // double-click to create new POI
57                             stopDrawing();
58                             MainUndoStack.getGlobalStack().undo(); // undo the BeginWayAction that (presumably?) just happened
59                             
60                             var newPoiAction:CreatePOIAction = new CreatePOIAction(
61                                                                 {},
62                                                                 controller.map.coord2lat(event.localY),
63                                                                 controller.map.coord2lon(event.localX));
64                             MainUndoStack.getGlobalStack().addAction(newPoiAction);
65                             return new SelectedPOINode(newPoiAction.getNode());
66                                                 } else {
67                                                         // double-click at end of way
68                                                         return stopDrawing();
69                                                 }
70                     } else if (entity==lastClick) {
71                         // clicked slowly on the end node - do nothing
72                         return this;
73                                         } else {
74                                                 appendNode(entity as Node, MainUndoStack.getGlobalStack().addAction);
75                                                 if (focus is Way) {
76                           controller.map.setHighlightOnNodes(focus as Way, { hoverway: false });
77                         }
78                                                 controller.map.setHighlight(entity, { selectedway: true });
79                                                 resetElastic(entity as Node);
80                                                 lastClick=entity;
81                                                 if (Way(firstSelected).getNode(0)==Way(firstSelected).getLastNode()) {
82                                                         return new SelectedWay(firstSelected as Way);
83                                                 }
84                                         }
85                                 } else if ( entity is Way ) {
86                                         if (entity==firstSelected) {
87                                                 // add junction node - self-intersecting way
88                                     var lat:Number = controller.map.coord2lat(event.localY);
89                                     var lon:Number = controller.map.coord2lon(event.localX);
90                                     var undo:CompositeUndoableAction = new CompositeUndoableAction("Insert node");
91                                     node = controller.connection.createNode({}, lat, lon, undo.push);
92                                     Way(firstSelected).insertNodeAtClosestPosition(node, true, undo.push);
93                                                 appendNode(node,undo.push);
94                                     MainUndoStack.getGlobalStack().addAction(undo);
95                                         } else {
96                         // add junction node - another way
97                         var jnct:CompositeUndoableAction = new CompositeUndoableAction("Junction Node");
98                         node = createAndAddNode(event, jnct.push);
99                         Way(entity).insertNodeAtClosestPosition(node, true, jnct.push);
100                         MainUndoStack.getGlobalStack().addAction(jnct);
101                         controller.map.setHighlight(node, { selectedway: true });
102                         controller.map.setPurgable([node], false);
103                                         }
104                                         resetElastic(node);
105                                         lastClick=node;
106                                         controller.map.setHighlightOnNodes(entity as Way, { hoverway: false });
107                                         controller.map.setHighlightOnNodes(firstSelected as Way, { selectedway: true });
108                                 }
109                                 lastClickTime=new Date();
110                         } else if ( event.type == MouseEvent.MOUSE_MOVE && elastic ) {
111                                 mouse = new Point(
112                                                   controller.map.coord2lon(event.localX),
113                                                   controller.map.coord2latp(event.localY));
114                                 elastic.end = mouse;
115                         } else if ( event.type == MouseEvent.ROLL_OVER && !isBackground ) {
116                                 if (focus is Way && focus!=firstSelected) {
117                                         hoverEntity=focus;
118                                         controller.map.setHighlightOnNodes(focus as Way, { hoverway: true });
119                                 }
120                                 if (entity is Node && focus is Way && Way(focus).endsWith(Node(entity))) {
121                                         if (focus==firstSelected) { controller.setCursor(controller.pen_so); }
122                                                              else { controller.setCursor(controller.pen_o); }
123                                 } else if (entity is Node) {
124                                         controller.setCursor(controller.pen_x);
125                                 } else {
126                                         controller.setCursor(controller.pen_plus);
127                                 }
128                         } else if ( event.type == MouseEvent.MOUSE_OUT && !isBackground ) {
129                                 if (focus is Way && entity!=firstSelected) {
130                                         hoverEntity=null;
131                                         controller.map.setHighlightOnNodes(focus as Way, { hoverway: false });
132                                         // ** We could do with an optional way of calling WayUI.redraw to only do the nodes, which would be a
133                                         // useful optimisation.
134                                 }
135                                 controller.setCursor(controller.pen);
136                         }
137
138                         return this;
139                 }
140                 
141                 protected function resetElastic(node:Node):void {
142                         var mouse:Point = new Point(node.lon, node.latp);
143                         elastic.start = mouse;
144                         elastic.end = mouse;
145                 }
146
147         /* Fix up the elastic after a WayNode event - e.g. triggered by undo */
148         private function fixElastic(event:Event):void {
149             if (firstSelected == null) return;
150             var node:Node;
151             if (editEnd) {
152               node = Way(firstSelected).getLastNode();
153             } else {
154               node = Way(firstSelected).getNode(0);
155             }
156             if (node) { //maybe selectedWay doesn't have any nodes left
157               elastic.start = new Point(node.lon, node.latp);
158             }
159         }
160
161                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
162                         switch (event.keyCode) {
163                                 case 13:                                        return keyExitDrawing();
164                                 case 27:                                        return keyExitDrawing();
165                                 case Keyboard.DELETE:           return backspaceNode(MainUndoStack.getGlobalStack().addAction);
166                                 case Keyboard.BACKSPACE:        return backspaceNode(MainUndoStack.getGlobalStack().addAction);
167                                 case 189:                                       return backspaceNode(MainUndoStack.getGlobalStack().addAction);
168                                 case 82:                                        repeatTags(firstSelected); return this;
169                         }
170                         var cs:ControllerState = sharedKeyboardEvents(event);
171                         return cs ? cs : this;
172                 }
173                 
174                 protected function keyExitDrawing():ControllerState {
175                         var cs:ControllerState=stopDrawing();
176                         if (selectedWay.length==1) { 
177                                 if (MainUndoStack.getGlobalStack().undoIfAction(BeginWayAction)) { 
178                                         return new NoSelection();
179                                 }
180                                 return deleteWay();
181                         }
182                         return cs;
183                 }
184                 
185                 protected function stopDrawing():ControllerState {
186                         if ( hoverEntity ) {
187                                 controller.map.setHighlightOnNodes(hoverEntity as Way, { hoverway: false });
188                                 hoverEntity = null;
189                         }
190
191                         if ( leaveNodeSelected ) {
192                             return new SelectedWayNode(firstSelected as Way, editEnd ? Way(firstSelected).length-1 : 0);
193                         } else {
194                             return new SelectedWay(firstSelected as Way);
195                         }
196                 }
197
198                 public function createAndAddNode(event:MouseEvent, performAction:Function):Node {
199                     var undo:CompositeUndoableAction = new CompositeUndoableAction("Add node");
200                     
201                         var lat:Number = controller.map.coord2lat(event.localY);
202                         var lon:Number = controller.map.coord2lon(event.localX);
203                         var node:Node = controller.connection.createNode({}, lat, lon, undo.push);
204                         appendNode(node, undo.push);
205                         
206                         performAction(undo);
207                         return node;
208                 }
209                 
210                 protected function appendNode(node:Node, performAction:Function):void {
211                         if ( editEnd )
212                                 Way(firstSelected).appendNode(node, performAction);
213                         else
214                                 Way(firstSelected).insertNode(0, node, performAction);
215                 }
216                 
217                 protected function backspaceNode(performAction:Function):ControllerState {
218                         var node:Node;
219                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Remove node");
220                         var newDraw:int;
221             var state:ControllerState;
222
223                         if (editEnd) {
224                                 node=Way(firstSelected).getLastNode();
225                                 Way(firstSelected).removeNodeByIndex(Way(firstSelected).length-1, undo.push);
226                                 newDraw=Way(firstSelected).length-2;
227                         } else {
228                                 node=Way(firstSelected).getNode(0);
229                                 Way(firstSelected).removeNodeByIndex(0, undo.push);
230                                 newDraw=0;
231                         }
232                         if (node.numParentWays==1 && Way(firstSelected).hasOnceOnly(node)) {
233                                 controller.map.setPurgable([node], true);
234                                 controller.connection.unregisterPOI(node);
235                                 node.remove(undo.push);
236                         }
237
238                         if (newDraw>=0 && newDraw<=Way(firstSelected).length-2) {
239                                 var mouse:Point = new Point(Way(firstSelected).getNode(newDraw).lon, Way(firstSelected).getNode(newDraw).latp);
240                                 elastic.start = mouse;
241                                 state = this;
242                         } else {
243                 Way(firstSelected).remove(undo.push);
244                 state = new NoSelection();
245                         }
246
247             performAction(undo);
248
249             if(!node.isDeleted()) { // i.e. was junction with another way (or is now POI)
250               controller.map.setHighlight(node, {selectedway: false});
251             }
252             return state;
253                 }
254                 
255                 override public function enterState():void {
256                         super.enterState();
257                         
258                         var node:Node = Way(firstSelected).getNode(editEnd ? Way(firstSelected).length-1 : 0);
259                         var start:Point = new Point(node.lon, node.latp);
260                         elastic = new Elastic(controller.map, start, start);
261                         controller.setCursor(controller.pen);
262                         Globals.vars.root.addDebug("**** -> "+this);
263                 }
264                 override public function exitState(newState:ControllerState):void {
265                         super.exitState(newState);
266                         controller.setCursor(null);
267                         elastic.removeSprites();
268                         elastic = null;
269                         Globals.vars.root.addDebug("**** <- "+this);
270                 }
271                 override public function toString():String {
272                         return "DrawWay";
273                 }
274         }
275 }