support GPX 1.1 too
[potlatch2.git] / net / systemeD / potlatch2 / controller / DragWay.as
index e51e5bf489835baae31ee970196383db37114f46..dda276c9d5283d64c10e894c26ec911e3a5763aa 100644 (file)
@@ -1,11 +1,12 @@
 package net.systemeD.potlatch2.controller {
        import flash.events.*;
+       import flash.geom.Point;
     import net.systemeD.potlatch2.EditController;
     import net.systemeD.halcyon.connection.*;
+    import net.systemeD.halcyon.connection.actions.*;
        import net.systemeD.halcyon.Globals;
 
     public class DragWay extends ControllerState {
-        private var selectedWay:Way;
         private var isDraggingStarted:Boolean = false;
                private var enterTime:Number;
 
@@ -17,7 +18,7 @@ package net.systemeD.potlatch2.controller {
                private const DRAGGING:uint=2;
         
         public function DragWay(way:Way, event:MouseEvent) {
-            selectedWay = way;
+            selection = [way];
             downX = event.localX;
             downY = event.localY;
                        enterTime = (new Date()).getTime();
@@ -26,8 +27,11 @@ package net.systemeD.potlatch2.controller {
        override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
 
             if (event.type==MouseEvent.MOUSE_UP) {
-                               if (dragstate==DRAGGING) { moveWay(event.localX, event.localY); }
-                       return new SelectedWay(selectedWay);
+                if (dragstate==DRAGGING) { 
+                  MainUndoStack.getGlobalStack().addAction(
+                          new MoveWayAction(firstSelected as Way, downX, downY, event.localX, event.localY, controller.map)); 
+                }
+                return new SelectedWay(firstSelected as Way, new Point(event.stageX,event.stageY));
 
                        } else if ( event.type == MouseEvent.MOUSE_MOVE) {
                                // dragging
@@ -48,8 +52,16 @@ package net.systemeD.potlatch2.controller {
                        }
         }
 
+               override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
+                       if (event.keyCode==27) {
+                               firstSelected.dispatchEvent(new WayDraggedEvent(Connection.WAY_DRAGGED, firstSelected as Way, 0, 0));
+                               return new SelectedWay(firstSelected as Way);
+                       }
+                       return this;
+               }
+
         private function dragTo(event:MouseEvent):ControllerState {
-                       selectedWay.dispatchEvent(new WayDraggedEvent(Connection.WAY_DRAGGED, selectedWay, event.localX-downX, event.localY-downY));
+                       firstSelected.dispatchEvent(new WayDraggedEvent(Connection.WAY_DRAGGED, firstSelected as Way, event.localX-downX, event.localY-downY));
             return this;
         }
         
@@ -58,31 +70,17 @@ package net.systemeD.potlatch2.controller {
                }
 
         override public function enterState():void {
-            controller.map.setHighlight(selectedWay, { highlight: true } );
+            controller.map.setHighlight(firstSelected, { selected: true } );
                        Globals.vars.root.addDebug("**** -> "+this);
         }
-        override public function exitState():void {
-            controller.map.setHighlight(selectedWay, { highlight: false } );
+        override public function exitState(newState:ControllerState):void {
+            controller.map.setHighlight(firstSelected, { selected: false } );
                        Globals.vars.root.addDebug("**** <- "+this);
         }
         override public function toString():String {
             return "DragWay";
         }
 
-               private function moveWay(x:Number, y:Number):void {
-                       var lonDelta:Number = controller.map.coord2lon(downX)-controller.map.coord2lon(x);
-                       var latDelta:Number = controller.map.coord2lat(downY)-controller.map.coord2lat(y);
-                       var moved:Object = {};
-                       selectedWay.suspend();
-                       selectedWay.dispatchEvent(new WayDraggedEvent(Connection.WAY_DRAGGED, selectedWay, 0, 0));
-                       for (var i:uint=0; i<selectedWay.length; i++) {
-                               var n:Node=selectedWay.getNode(i);
-                               if (!moved[n.id]) {
-                                       n.setLatLon(n.lat-latDelta, n.lon-lonDelta);
-                                       moved[n.id]=true;
-                               }
-                       }
-                       selectedWay.resume();
-               }
+
     }
 }