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