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