f0dca94ca171f642591aa8c8df2cd7e728005ff9
[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.ui.Keyboard;
5         import net.systemeD.potlatch2.EditController;
6         import net.systemeD.halcyon.connection.*;
7         import net.systemeD.halcyon.Elastic;
8         import net.systemeD.halcyon.Globals;
9
10         public class DrawWay extends SelectedWay {
11                 private var elastic:Elastic;
12                 private var editEnd:Boolean;
13                 private var leaveNodeSelected:Boolean;
14                 private var lastClick:Entity=null;
15                 private var lastClickTime:Date;
16                 private var hoverEntity:Entity;                 // keep track of the currently rolled-over object, because
17                                                                                                 // Flash can fire a mouseDown from the map even if you
18                                                                                                 // haven't rolled out of the way
19                 
20                 public function DrawWay(way:Way, editEnd:Boolean, leaveNodeSelected:Boolean) {
21                         super(way);
22                         this.editEnd = editEnd;
23                         this.leaveNodeSelected = leaveNodeSelected;
24                         if (way.length==1 && way.getNode(0).parentWays.length==1) {
25                                 // drawing new way, so keep track of click in case creating a POI
26                                 lastClick=way.getNode(0);
27                                 lastClickTime=new Date();
28                         }
29                 }
30                 
31                 override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
32                         var mouse:Point;
33                         var node:Node;
34                         if (entity == null && hoverEntity) { entity=hoverEntity; }
35                         var focus:Entity = getTopLevelFocusEntity(entity);
36
37                         if ( event.type == MouseEvent.MOUSE_UP ) {
38                                 if ( entity == null ) {
39                                         node = createAndAddNode(event);
40                                         resetElastic(node);
41                                         lastClick=node;
42                                 } else if ( entity is Node ) {
43                                         if (entity==lastClick && (new Date().getTime()-lastClickTime.getTime())<1000) {
44                                                 if (selectedWay.length==1 && selectedWay.getNode(0).parentWays.length==1) {
45                                                         // double-click to create new POI
46                                                         node=selectedWay.getNode(0);
47                                                         stopDrawing();
48                                                         node.setDeletedState(false);    // has just been deleted by stopDrawing
49                                                         controller.connection.registerPOI(node);
50                                                         return new SelectedPOINode(node);
51                                                 } else {
52                                                         // double-click at end of way
53                                                         return stopDrawing();
54                                                 }
55                                         } else {
56                                                 appendNode(entity as Node, MainUndoStack.getGlobalStack().addAction);
57                                                 controller.map.setHighlight(focus, { showNodesHover: false });
58                                                 controller.map.setHighlight(selectedWay, { showNodes: true });
59                                                 resetElastic(entity as Node);
60                                                 lastClick=entity;
61                                                 if (selectedWay.getNode(0)==selectedWay.getNode(selectedWay.length-1)) {
62                                                         return new SelectedWay(selectedWay);
63                                                 }
64                                         }
65                                 } else if ( entity is Way ) {
66                                         node = createAndAddNode(event);
67                                         Way(entity).insertNodeAtClosestPosition(node, true,
68                                             MainUndoStack.getGlobalStack().addAction);
69                                         resetElastic(node);
70                                         lastClick=node;
71                                         controller.map.setHighlight(entity, { showNodesHover: false });
72                                         controller.map.setHighlight(selectedWay, { showNodes: true });
73                                 }
74                                 lastClickTime=new Date();
75                         } else if ( event.type == MouseEvent.MOUSE_MOVE ) {
76                                 mouse = new Point(
77                                                   controller.map.coord2lon(event.localX),
78                                                   controller.map.coord2latp(event.localY));
79                                 elastic.end = mouse;
80                         } else if ( event.type == MouseEvent.ROLL_OVER ) {
81                                 if (focus!=selectedWay) {
82                                         hoverEntity=focus;
83                                         controller.map.setHighlight(focus, { showNodesHover: true });
84                                 }
85                                 if (entity is Node && Way(focus).endsWith(Node(entity))) {
86                                         if (focus==selectedWay) { controller.setCursor(controller.pen_so); }
87                                                            else { controller.setCursor(controller.pen_o); }
88                                 } else if (entity is Node) {
89                                         controller.setCursor(controller.pen_x);
90                                 } else {
91                                         controller.setCursor(controller.pen_plus);
92                                 }
93                         } else if ( event.type == MouseEvent.MOUSE_OUT ) {
94                                 if (focus!=selectedWay) {
95                                         hoverEntity=null;
96                                         controller.map.setHighlight(focus, { showNodesHover: false });
97                                         controller.map.setHighlight(selectedWay, { showNodes: true });
98                                         // ** this call to setHighlight(selectedWay) is necessary in case the hovered way (blue nodes)
99                                         // shares any nodes with the selected way (red nodes): if they do, they'll be wiped out by the
100                                         // first call.
101                                         // Ultimately we should fix this by referring to 'way :selected nodes' instead of 'nodes :selectedway'.
102                                         // But this will do for now.
103                                         // We could do with an optional way of calling WayUI.redraw to only do the nodes, which would be a
104                                         // useful optimisation.
105                                 }
106                                 controller.setCursor(controller.pen);
107                         }
108
109                         return this;
110                 }
111                 
112                 protected function resetElastic(node:Node):void {
113                         var mouse:Point = new Point(node.lon, node.latp);
114                         elastic.start = mouse;
115                         elastic.end = mouse;
116                 }
117
118                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
119                         switch (event.keyCode) {
120                                 case 13:                                        return stopDrawing();
121                                 case 27:                                        return stopDrawing();
122                                 case Keyboard.BACKSPACE:        return backspaceNode(MainUndoStack.getGlobalStack().addAction);
123                                 case 82:                                        repeatTags(selectedWay); return this;
124                         }
125                         var cs:ControllerState = sharedKeyboardEvents(event);
126                         return cs ? cs : this;
127                 }
128                 
129                 protected function stopDrawing():ControllerState {
130                         if ( selectedWay.length<2) {
131                                 controller.map.setHighlight(selectedWay, { showNodes: false });
132                                 selectedWay.remove(MainUndoStack.getGlobalStack().addAction);
133                                 // delete controller.map.ways[selectedWay.id];
134                                 return new NoSelection();
135                         } else if ( leaveNodeSelected ) {
136                             return new SelectedWayNode(selectedWay, editEnd ? selectedWay.length - 1 : 0);
137                         } else {
138                             return new SelectedWay(selectedWay);
139                         }
140                         return this;
141                 }
142
143                 public function createAndAddNode(event:MouseEvent):Node {
144                     var undo:CompositeUndoableAction = new CompositeUndoableAction("Add node");
145                     
146                         var lat:Number = controller.map.coord2lat(event.localY);
147                         var lon:Number = controller.map.coord2lon(event.localX);
148                         var node:Node = controller.connection.createNode({}, lat, lon, undo.push);
149                         appendNode(node, undo.push);
150                         
151                         MainUndoStack.getGlobalStack().addAction(undo);
152                         return node;
153                 }
154                 
155                 protected function appendNode(node:Node, performAction:Function):void {
156                         if ( editEnd )
157                                 selectedWay.appendNode(node, performAction);
158                         else
159                                 selectedWay.insertNode(0, node, performAction);
160                 }
161                 
162                 protected function backspaceNode(performAction:Function):ControllerState {
163                         var node:Node;
164                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Remove node");
165                         var newDraw:int;
166                         if (editEnd) {
167                                 node=selectedWay.getNode(selectedWay.length-1);
168                                 selectedWay.removeNodeByIndex(selectedWay.length-1, undo.push);
169                                 newDraw=selectedWay.length-2;
170                         } else {
171                                 node=selectedWay.getNode(0);
172                                 selectedWay.removeNodeByIndex(0, undo.push);
173                                 newDraw=0;
174                         }
175                         if (node.numParentWays==1) {
176                                 controller.connection.unregisterPOI(node);
177                                 node.remove(undo.push);
178                         }
179                         MainUndoStack.getGlobalStack().addAction(undo);
180
181                         if (newDraw>=0 && newDraw<=selectedWay.length-1) {
182                                 var mouse:Point = new Point(selectedWay.getNode(newDraw).lon, selectedWay.getNode(newDraw).latp);
183                                 elastic.start = mouse;
184                                 return this;
185                         } else {
186                                 return new NoSelection();
187                         }
188                 }
189                 
190                 override public function enterState():void {
191                         super.enterState();
192                         
193                         var node:Node = selectedWay.getNode(editEnd ? selectedWay.length - 1 : 0);
194                         var start:Point = new Point(node.lon, node.latp);
195                         elastic = new Elastic(controller.map, start, start);
196                         controller.setCursor(controller.pen);
197                         Globals.vars.root.addDebug("**** -> "+this);
198                 }
199                 override public function exitState():void {
200                         super.exitState();
201                         controller.setCursor(null);
202                         elastic.removeSprites();
203                         elastic = null;
204                         Globals.vars.root.addDebug("**** <- "+this);
205                 }
206                 override public function toString():String {
207                         return "DrawWay";
208                 }
209         }
210 }