DragWayAction - undoing dragging a way now moves all the nodes together, instead...
[potlatch2.git] / net / systemeD / potlatch2 / controller / DragWay.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.connection.actions.*;
6         import net.systemeD.halcyon.Globals;
7
8     public class DragWay extends ControllerState {
9         private var isDraggingStarted:Boolean = false;
10                 private var enterTime:Number;
11
12         private var downX:Number;
13         private var downY:Number;
14                 private var dragstate:uint=NOT_MOVED;
15                 private const NOT_DRAGGING:uint=0;
16                 private const NOT_MOVED:uint=1;
17                 private const DRAGGING:uint=2;
18         
19         public function DragWay(way:Way, event:MouseEvent) {
20             selectedWay = way;
21             downX = event.localX;
22             downY = event.localY;
23                         enterTime = (new Date()).getTime();
24         }
25  
26        override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
27
28             if (event.type==MouseEvent.MOUSE_UP) {
29                 if (dragstate==DRAGGING) { 
30                   MainUndoStack.getGlobalStack().addAction(
31                           new MoveWayAction(selectedWay, downX, downY, event.localX, event.localY, controller.map)); 
32                 }
33                 return new SelectedWay(selectedWay);
34
35                         } else if ( event.type == MouseEvent.MOUSE_MOVE) {
36                                 // dragging
37                                 if (dragstate==NOT_DRAGGING) {
38                                         return this;
39                                 } else if (dragstate==NOT_MOVED && 
40                                                ((Math.abs(downX - event.localX) < 3 && Math.abs(downY - event.localY) < 3) ||
41                                                 (new Date().getTime()-enterTime)<300)) {
42                                         // ** add time check too
43                                         return this;
44                                 }
45                                 dragstate=DRAGGING;
46                 return dragTo(event);
47
48                         } else {
49                                 // event not handled
50                 return this;
51                         }
52         }
53
54         private function dragTo(event:MouseEvent):ControllerState {
55                         selectedWay.dispatchEvent(new WayDraggedEvent(Connection.WAY_DRAGGED, selectedWay, event.localX-downX, event.localY-downY));
56             return this;
57         }
58         
59                 public function forceDragStart():void {
60                         dragstate=NOT_MOVED;
61                 }
62
63         override public function enterState():void {
64             controller.map.setHighlight(selectedWay, { highlight: true } );
65                         Globals.vars.root.addDebug("**** -> "+this);
66         }
67         override public function exitState():void {
68             controller.map.setHighlight(selectedWay, { highlight: false } );
69                         Globals.vars.root.addDebug("**** <- "+this);
70         }
71         override public function toString():String {
72             return "DragWay";
73         }
74
75
76     }
77 }