squish a couple of redraw bugs
[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                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
55                         if (event.keyCode==27) {
56                                 selectedWay.dispatchEvent(new WayDraggedEvent(Connection.WAY_DRAGGED, selectedWay, 0, 0));
57                                 return new SelectedWay(selectedWay);
58                         }
59                         return this;
60                 }
61
62         private function dragTo(event:MouseEvent):ControllerState {
63                         selectedWay.dispatchEvent(new WayDraggedEvent(Connection.WAY_DRAGGED, selectedWay, event.localX-downX, event.localY-downY));
64             return this;
65         }
66         
67                 public function forceDragStart():void {
68                         dragstate=NOT_MOVED;
69                 }
70
71         override public function enterState():void {
72             controller.map.setHighlight(selectedWay, { highlight: true } );
73                         Globals.vars.root.addDebug("**** -> "+this);
74         }
75         override public function exitState(newState:ControllerState):void {
76             controller.map.setHighlight(selectedWay, { highlight: false } );
77                         Globals.vars.root.addDebug("**** <- "+this);
78         }
79         override public function toString():String {
80             return "DragWay";
81         }
82
83
84     }
85 }