DragWayAction - undoing dragging a way now moves all the nodes together, instead...
[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.Globals;
6
7     public class DragWayNode extends ControllerState {
8         private var draggingNode:Node;
9         private var isDraggingStarted:Boolean = false;
10                 private var isNew:Boolean = false;
11
12         private var downX:Number;
13         private var downY:Number;
14                 private var dragstate:uint=NOT_MOVED;
15                 private const NOT_DRAGGING:uint=0;
16                 private const NOT_MOVED:uint=1;
17                 private const DRAGGING:uint=2;
18         
19         public function DragWayNode(way:Way, node:Node, event:MouseEvent, newNode:Boolean) {
20             selectedWay = way;
21             draggingNode = node;
22             downX = event.localX;
23             downY = event.localY;
24                         isNew = newNode;
25         }
26  
27        override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
28
29             if (event.type==MouseEvent.MOUSE_UP) {
30                                 if (dragstate==DRAGGING) {
31                                         // mouse-up while dragging, so end drag
32                         return new SelectedWayNode(selectedWay,draggingNode);
33 //                      return endDrag();
34                                 } else if (event.shiftKey && !isNew) {
35                                         // start new way
36                                         var way:Way = controller.connection.createWay({}, [entity],
37                                             MainUndoStack.getGlobalStack().addAction);
38                                         return new DrawWay(way, true, false);
39                                 } else if (event.shiftKey && isNew) {
40                         return new SelectedWayNode(selectedWay,draggingNode);
41                                 } else {
42                                         // select node
43                                         dragstate=NOT_DRAGGING;
44                         return SelectedWayNode.selectOrEdit(selectedWay, draggingNode);
45                                 }
46
47                         } else if ( event.type == MouseEvent.MOUSE_MOVE) {
48                                 // dragging
49                                 if (dragstate==NOT_DRAGGING) {
50                                         return this;
51                                 } else if (dragstate==NOT_MOVED && Math.abs(downX - event.localX) < 3 && Math.abs(downY - event.localY) < 3) {
52                                         return this;
53                                 }
54                                 dragstate=DRAGGING;
55                 return dragTo(event);
56
57                         } else {
58                                 // event not handled
59                 return this;
60                         }
61         }
62
63         private function endDrag():ControllerState {
64             return previousState;
65         }
66         
67         private function dragTo(event:MouseEvent):ControllerState {
68                         draggingNode.setLatLon( controller.map.coord2lat(event.localY),
69                                     controller.map.coord2lon(event.localX),
70                                     MainUndoStack.getGlobalStack().addAction );
71             return this;
72         }
73         
74                 public function forceDragStart():void {
75                         dragstate=NOT_MOVED;
76                 }
77
78         override public function enterState():void {
79             controller.map.setHighlight(selectedWay, { showNodes: true } );
80                         Globals.vars.root.addDebug("**** -> "+this);
81         }
82         override public function exitState():void {
83             controller.map.setHighlight(selectedWay, { showNodes: false } );
84                         Globals.vars.root.addDebug("**** <- "+this);
85         }
86         override public function toString():String {
87             return "DragWayNode";
88         }
89     }
90 }