various click-handling improvements/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
6     public class DragWayNode extends ControllerState {
7         private var selectedWay:Way;
8         private var draggingNode:Node;
9         private var isDraggingStarted:Boolean = false;
10
11         private var downX:Number;
12         private var downY:Number;
13                 private var dragstate:uint=NOT_MOVED;
14                 private const NOT_DRAGGING:uint=0;
15                 private const NOT_MOVED:uint=1;
16                 private const DRAGGING:uint=2;
17         
18         public function DragWayNode(way:Way, node:Node, event:MouseEvent) {
19             selectedWay = way;
20             draggingNode = node;
21             downX = event.localX;
22             downY = event.localY;
23         }
24  
25        override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
26
27             if (event.type==MouseEvent.MOUSE_UP) {
28                                 if (dragstate==DRAGGING) {
29                                         // mouse-up while dragging, so end drag
30                         return endDrag();
31                                 } else if (event.shiftKey) {
32                                         // start new way
33                                         var way:Way = controller.connection.createWay({}, [entity, entity]);
34                                         return new DrawWay(way, true);
35                                 } else {
36                                         // select node
37                                         // *** haven't done node selection code yet!
38                                         dragstate=NOT_DRAGGING;
39                         return new NoSelection();
40                                 }
41
42                         } else if ( event.type == MouseEvent.MOUSE_MOVE) {
43                                 // dragging
44                                 if (dragstate==NOT_DRAGGING) {
45                                         return this;
46                                 } else if (dragstate==NOT_MOVED && Math.abs(downX - event.localX) < 3 && Math.abs(downY - event.localY) < 3) {
47                                         return this;
48                                 }
49                                 dragstate=DRAGGING;
50                 return dragTo(event);
51
52                         } else {
53                                 // event not handled
54                 return this;
55                         }
56         }
57
58         private function endDrag():ControllerState {
59             return previousState;
60         }
61         
62         private function dragTo(event:MouseEvent):ControllerState {
63             draggingNode.lat = controller.map.coord2lat(event.localY);
64             draggingNode.lon = controller.map.coord2lon(event.localX);
65             return this;
66         }
67         
68                 public function forceDragStart():void {
69                         dragstate=NOT_MOVED;
70                 }
71
72         override public function enterState():void {
73             controller.map.setHighlight(selectedWay, "showNodes", true);
74         }
75         override public function exitState():void {
76             controller.map.setHighlight(selectedWay, "showNodes", false);
77         }
78         override public function toString():String {
79             return "DragWayNode";
80         }
81     }
82 }