Potlatch 2 in progress. Haven't figured out the mouse events still...
[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 selectedWay:Way;
9         private var draggingNode:Node;
10         private var isDraggingStarted:Boolean = false;
11         private var downX:Number;
12         private var downY:Number;
13         
14         public function DragWayNode(way:Way, node:Node, mouseDown:MouseEvent) {
15             selectedWay = way;
16             draggingNode = node;
17             downX = mouseDown.localX;
18             downY = mouseDown.localY;
19         }
20  
21         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
22             if ( event.type == MouseEvent.MOUSE_UP ) {
23                                 Globals.vars.root.addDebug("dragwaynode - mouse-up");
24                 return endDrag();
25                         }
26             
27             if ( !isDragging(event) ) {
28                                 Globals.vars.root.addDebug("dragwaynode - not dragging");
29                 return this;
30                         }
31
32             if ( event.type == MouseEvent.MOUSE_MOVE ) {
33                 return dragTo(event);
34                         } else {
35                 return this;
36                         }
37         }
38
39         private function isDragging(event:MouseEvent):Boolean {
40             if ( isDraggingStarted )
41                 return true;
42             
43             isDraggingStarted = Math.abs(downX - event.localX) > 3 ||
44                                 Math.abs(downY - event.localY) > 3;
45             return isDraggingStarted;
46         }
47         
48         private function endDrag():ControllerState {
49             return previousState;
50         }
51         
52         private function dragTo(event:MouseEvent):ControllerState {
53             draggingNode.lat = controller.map.coord2lat(event.localY);
54             draggingNode.lon = controller.map.coord2lon(event.localX);
55             return this;
56         }
57         
58         override public function enterState():void {
59             controller.map.setHighlight(selectedWay, "showNodes", true);
60         }
61         override public function exitState():void {
62             controller.map.setHighlight(selectedWay, "showNodes", false);
63         }
64     }
65 }