refactor node stateClasses and stylelist calculation
[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                                 } else if (event.shiftKey && !isNew) {
40                                         // start new way
41                                         var way:Way = controller.connection.createWay({}, [entity],
42                                             MainUndoStack.getGlobalStack().addAction);
43                                         return new DrawWay(way, true, false);
44                                 } else if (event.shiftKey && isNew) {
45                         return new SelectedWayNode(selectedWay,draggingIndex);
46                                 } else {
47                                         // select node
48                                         dragstate=NOT_DRAGGING;
49                         return SelectedWayNode.selectOrEdit(selectedWay, draggingIndex);
50                                 }
51
52                         } else if ( event.type == MouseEvent.MOUSE_MOVE) {
53                                 // dragging
54                                 if (dragstate==NOT_DRAGGING) {
55                                         return this;
56                                 } else if (dragstate==NOT_MOVED && Math.abs(downX - event.localX) < 3 && Math.abs(downY - event.localY) < 3) {
57                                         return this;
58                                 }
59                                 dragstate=DRAGGING;
60                 return dragTo(event);
61
62                         } else {
63                                 // event not handled
64                 return this;
65                         }
66         }
67
68                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
69                         if (event.keyCode==27) {
70                                 draggingNode.setLatLon( originalLat, originalLon, MainUndoStack.getGlobalStack().addAction );
71                 return new SelectedWayNode(selectedWay,draggingIndex);
72                         }
73                         return this;
74                 }
75
76         private function dragTo(event:MouseEvent):ControllerState {
77                         draggingNode.setLatLon( controller.map.coord2lat(event.localY),
78                                     controller.map.coord2lon(event.localX),
79                                     MainUndoStack.getGlobalStack().addAction );
80             return this;
81         }
82         
83                 public function forceDragStart():void {
84                         dragstate=NOT_MOVED;
85                 }
86
87         override public function enterState():void {
88                         controller.map.setHighlightOnNodes(selectedWay, { selectedway: true } );
89                         controller.map.setHighlight(draggingNode, { selected: true } );
90                         Globals.vars.root.addDebug("**** -> "+this);
91         }
92         override public function exitState(newState:ControllerState):void {
93                         controller.map.setHighlightOnNodes(selectedWay, { selectedway: false } );
94                         controller.map.setHighlight(draggingNode, { selected: false } );
95                         Globals.vars.root.addDebug("**** <- "+this);
96         }
97         override public function toString():String {
98             return "DragWayNode";
99         }
100     }
101 }