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