couple of todos, and fix the toolbox
[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 && focus is Way && 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 (entity!=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.DELETE:           return backspaceNode(MainUndoStack.getGlobalStack().addAction);
123                                 case Keyboard.BACKSPACE:        return backspaceNode(MainUndoStack.getGlobalStack().addAction);
124                                 case 82:                                        repeatTags(selectedWay); return this;
125                         }
126                         var cs:ControllerState = sharedKeyboardEvents(event);
127                         return cs ? cs : this;
128                 }
129                 
130                 protected function stopDrawing():ControllerState {
131                         if ( hoverEntity ) {
132                                 controller.map.setHighlight(hoverEntity, { showNodesHover: false });
133                                 hoverEntity = null;
134                         }
135
136                         if ( selectedWay.length<2) {
137                                 controller.map.setHighlight(selectedWay, { showNodes: false });
138                                 selectedWay.remove(MainUndoStack.getGlobalStack().addAction);
139                                 // delete controller.map.ways[selectedWay.id];
140                                 return new NoSelection();
141                         } else if ( leaveNodeSelected ) {
142                             return new SelectedWayNode(selectedWay, editEnd ? selectedWay.length - 1 : 0);
143                         } else {
144                             return new SelectedWay(selectedWay);
145                         }
146                         return this;
147                 }
148
149                 public function createAndAddNode(event:MouseEvent):Node {
150                     var undo:CompositeUndoableAction = new CompositeUndoableAction("Add node");
151                     
152                         var lat:Number = controller.map.coord2lat(event.localY);
153                         var lon:Number = controller.map.coord2lon(event.localX);
154                         var node:Node = controller.connection.createNode({}, lat, lon, undo.push);
155                         appendNode(node, undo.push);
156                         
157                         MainUndoStack.getGlobalStack().addAction(undo);
158                         return node;
159                 }
160                 
161                 protected function appendNode(node:Node, performAction:Function):void {
162                         if ( editEnd )
163                                 selectedWay.appendNode(node, performAction);
164                         else
165                                 selectedWay.insertNode(0, node, performAction);
166                 }
167                 
168                 protected function backspaceNode(performAction:Function):ControllerState {
169                         var node:Node;
170                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Remove node");
171                         var newDraw:int;
172                         if (editEnd) {
173                                 node=selectedWay.getNode(selectedWay.length-1);
174                                 selectedWay.removeNodeByIndex(selectedWay.length-1, undo.push);
175                                 newDraw=selectedWay.length-2;
176                         } else {
177                                 node=selectedWay.getNode(0);
178                                 selectedWay.removeNodeByIndex(0, undo.push);
179                                 newDraw=0;
180                         }
181                         if (node.numParentWays==1) {
182                                 controller.connection.unregisterPOI(node);
183                                 node.remove(undo.push);
184                         }
185                         MainUndoStack.getGlobalStack().addAction(undo);
186
187                         if (newDraw>=0 && newDraw<=selectedWay.length-1) {
188                                 var mouse:Point = new Point(selectedWay.getNode(newDraw).lon, selectedWay.getNode(newDraw).latp);
189                                 elastic.start = mouse;
190                                 return this;
191                         } else {
192                                 return new NoSelection();
193                         }
194                 }
195                 
196                 override public function enterState():void {
197                         super.enterState();
198                         
199                         var node:Node = selectedWay.getNode(editEnd ? selectedWay.length - 1 : 0);
200                         var start:Point = new Point(node.lon, node.latp);
201                         elastic = new Elastic(controller.map, start, start);
202                         controller.setCursor(controller.pen);
203                         Globals.vars.root.addDebug("**** -> "+this);
204                 }
205                 override public function exitState(newState:ControllerState):void {
206                         super.exitState(newState);
207                         controller.setCursor(null);
208                         elastic.removeSprites();
209                         elastic = null;
210                         Globals.vars.root.addDebug("**** <- "+this);
211                 }
212                 override public function toString():String {
213                         return "DrawWay";
214                 }
215         }
216 }