refactor EditController to make it stateful, and add addWayNodes functionality with...
[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
6     public class DragWayNode extends ControllerState {
7         private var selectedWay:Way;
8         private var draggingNode:Node;
9         private var isDraggingStarted:Boolean = false;
10         private var downX:Number;
11         private var downY:Number;
12         
13         public function DragWayNode(way:Way, node:Node, mouseDown:MouseEvent) {
14             selectedWay = way;
15             draggingNode = node;
16             downX = mouseDown.localX;
17             downY = mouseDown.localY;
18         }
19  
20         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
21             if ( event.type == MouseEvent.MOUSE_UP )
22                 return endDrag();
23
24             if ( !isDragging(event) )
25                 return this;
26             
27             if ( event.type == MouseEvent.MOUSE_MOVE )
28                 return dragTo(event);
29             else   
30                 return this;
31         }
32
33         private function isDragging(event:MouseEvent):Boolean {
34             if ( isDraggingStarted )
35                 return true;
36             
37             isDraggingStarted = Math.abs(downX - event.localX) > 3 ||
38                                 Math.abs(downY - event.localY) > 3;
39             return isDraggingStarted;
40         }
41         
42         private function endDrag():ControllerState {
43             return previousState;
44         }
45         
46         private function dragTo(event:MouseEvent):ControllerState {
47             draggingNode.lat = controller.map.coord2lat(event.localY);
48             draggingNode.lon = controller.map.coord2lon(event.localX);
49             return this;
50         }
51         
52         override public function enterState():void {
53             controller.map.setHighlight(selectedWay, "showNodes", true);
54         }
55         override public function exitState():void {
56             controller.map.setHighlight(selectedWay, "showNodes", false);
57         }
58     }
59 }