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