Show the shortcut key (J) for Merge Ways text in the toolbox.
[potlatch2.git] / net / systemeD / potlatch2 / controller / DragWayNode.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3         import flash.ui.Keyboard;
4     import net.systemeD.potlatch2.EditController;
5     import net.systemeD.halcyon.connection.*;
6         import net.systemeD.halcyon.WayUI;
7
8     public class DragWayNode extends ControllerState {
9         private var draggingNode:Node;
10                 private var draggingIndex:int;
11         private var isDraggingStarted:Boolean = false;
12                 private var isNew:Boolean = false;
13
14         private var downX:Number;
15         private var downY:Number;
16                 private var originalLat:Number;
17                 private var originalLon:Number;
18                 private var dragstate:uint=NOT_MOVED;
19                 private const NOT_DRAGGING:uint=0;
20                 private const NOT_MOVED:uint=1;
21                 private const DRAGGING:uint=2;
22                 
23                 private var parentWay:Way;
24                 private var initEvent:MouseEvent;
25         
26         public function DragWayNode(way:Way, index:int, event:MouseEvent, newNode:Boolean) {
27                         parentWay=way;
28                         draggingIndex=index;
29             downX = event.localX;
30             downY = event.localY;
31                         isNew = newNode;
32                         initEvent=event;
33                         // the rest of the init will be done during enterState, because we need the controller to be initialised
34         }
35
36         private function addNode(selectedWay:Way,event:MouseEvent):int {
37                         var ways:Array = layer.findWaysAtPoint(event.stageX, event.stageY, selectedWay);
38             var lat:Number = controller.map.coord2lat(event.localY);
39             var lon:Number = controller.map.coord2lon(event.localX);
40             var undo:CompositeUndoableAction = new CompositeUndoableAction("Insert node");
41             var node:Node = selectedWay.connection.createNode({}, lat, lon, undo.push);
42             var index:int = selectedWay.insertNodeAtClosestPosition(node, true, undo.push);
43                         for each (var w:Way in ways) { w.insertNodeAtClosestPosition(node, true, undo.push); }
44             MainUndoStack.getGlobalStack().addAction(undo);
45                         return index;
46         }
47
48  
49        override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
50
51             if (event.type==MouseEvent.MOUSE_UP) {
52                                 if (dragstate==DRAGGING) {
53                                         // mouse-up while dragging, so end drag
54                         return new SelectedWayNode(parentWay,draggingIndex);
55                                 } else if (event.shiftKey && !isNew) {
56                                         // start new way
57                                         var way:Way = entity.connection.createWay({}, [entity],
58                                             MainUndoStack.getGlobalStack().addAction);
59                                         return new DrawWay(way, true, false);
60                                 } else if (event.shiftKey && isNew) {
61                         return new SelectedWayNode(parentWay,draggingIndex);
62                                 } else {
63                                         // select node
64                                         dragstate=NOT_DRAGGING;
65                         return SelectedWayNode.selectOrEdit(parentWay, draggingIndex);
66                                 }
67
68                         } else if ( event.type == MouseEvent.MOUSE_MOVE) {
69                                 // dragging
70                                 if (dragstate==NOT_DRAGGING) {
71                                         return this;
72                                 } else if (dragstate==NOT_MOVED && Math.abs(downX - event.localX) < 3 && Math.abs(downY - event.localY) < 3) {
73                                         return this;
74                                 }
75                                 dragstate=DRAGGING;
76                 return dragTo(event);
77
78                         } else {
79                                 // event not handled
80                 return this;
81                         }
82         }
83
84                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
85                         if (event.keyCode==Keyboard.ESCAPE) {
86                                 draggingNode.setLatLon( originalLat, originalLon, MainUndoStack.getGlobalStack().addAction );
87                 return new SelectedWayNode(parentWay,draggingIndex);
88                         }
89                         return this;
90                 }
91
92         private function dragTo(event:MouseEvent):ControllerState {
93                         draggingNode.setLatLon( controller.map.coord2lat(event.localY),
94                                     controller.map.coord2lon(event.localX),
95                                     MainUndoStack.getGlobalStack().addAction );
96             return this;
97         }
98         
99                 public function forceDragStart():void {
100                         dragstate=NOT_MOVED;
101                 }
102                 
103                 override public function get selectedWay():Way {
104                         return parentWay;
105                 }
106
107         override public function enterState():void {
108                         if (isNew && draggingIndex==-1) { draggingIndex=addNode(parentWay,initEvent); }
109             draggingNode = parentWay.getNode(draggingIndex);
110                         originalLat = draggingNode.lat;
111                         originalLon = draggingNode.lon;
112
113                         layer.setHighlightOnNodes(parentWay, { selectedway: true } );
114                         layer.limitWayDrawing(parentWay, draggingIndex);
115                         layer.setHighlight(draggingNode, { selected: true } );
116                         layer.protectWay(parentWay);
117                         layer.limitWayDrawing(parentWay, NaN, draggingIndex);
118         }
119         override public function exitState(newState:ControllerState):void {
120                         layer.unprotectWay(parentWay);
121                         layer.limitWayDrawing(parentWay);
122                         layer.setHighlightOnNodes(parentWay, { selectedway: false } );
123                         layer.setHighlight(draggingNode, { selected: false } );
124         }
125         override public function toString():String {
126             return "DragWayNode";
127         }
128     }
129 }