slight refactoring of controller states
[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.Globals;
6
7     public class DragWay extends ControllerState {
8         private var isDraggingStarted:Boolean = false;
9                 private var enterTime:Number;
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 DragWay(way:Way, event:MouseEvent) {
19             selectedWay = way;
20             downX = event.localX;
21             downY = event.localY;
22                         enterTime = (new Date()).getTime();
23         }
24  
25        override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
26
27             if (event.type==MouseEvent.MOUSE_UP) {
28                                 if (dragstate==DRAGGING) { moveWay(event.localX, event.localY); }
29                 return new SelectedWay(selectedWay);
30
31                         } else if ( event.type == MouseEvent.MOUSE_MOVE) {
32                                 // dragging
33                                 if (dragstate==NOT_DRAGGING) {
34                                         return this;
35                                 } else if (dragstate==NOT_MOVED && 
36                                                ((Math.abs(downX - event.localX) < 3 && Math.abs(downY - event.localY) < 3) ||
37                                                 (new Date().getTime()-enterTime)<300)) {
38                                         // ** add time check too
39                                         return this;
40                                 }
41                                 dragstate=DRAGGING;
42                 return dragTo(event);
43
44                         } else {
45                                 // event not handled
46                 return this;
47                         }
48         }
49
50         private function dragTo(event:MouseEvent):ControllerState {
51                         selectedWay.dispatchEvent(new WayDraggedEvent(Connection.WAY_DRAGGED, selectedWay, event.localX-downX, event.localY-downY));
52             return this;
53         }
54         
55                 public function forceDragStart():void {
56                         dragstate=NOT_MOVED;
57                 }
58
59         override public function enterState():void {
60             controller.map.setHighlight(selectedWay, { highlight: true } );
61                         Globals.vars.root.addDebug("**** -> "+this);
62         }
63         override public function exitState():void {
64             controller.map.setHighlight(selectedWay, { highlight: false } );
65                         Globals.vars.root.addDebug("**** <- "+this);
66         }
67         override public function toString():String {
68             return "DragWay";
69         }
70
71                 private function moveWay(x:Number, y:Number):void {
72                         var lonDelta:Number = controller.map.coord2lon(downX)-controller.map.coord2lon(x);
73                         var latDelta:Number = controller.map.coord2lat(downY)-controller.map.coord2lat(y);
74                         var moved:Object = {};
75                         selectedWay.suspend();
76                         selectedWay.dispatchEvent(new WayDraggedEvent(Connection.WAY_DRAGGED, selectedWay, 0, 0));
77                         for (var i:uint=0; i<selectedWay.length; i++) {
78                                 var n:Node=selectedWay.getNode(i);
79                                 if (!moved[n.id]) {
80                                         n.setLatLon(n.lat-latDelta, n.lon-lonDelta);
81                                         moved[n.id]=true;
82                                 }
83                         }
84                         selectedWay.resume();
85                 }
86     }
87 }