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