drag multiple items
authorRichard Fairhurst <richard@systemed.net>
Fri, 31 Dec 2010 13:27:38 +0000 (13:27 +0000)
committerRichard Fairhurst <richard@systemed.net>
Fri, 31 Dec 2010 13:27:38 +0000 (13:27 +0000)
net/systemeD/halcyon/NodeUI.as
net/systemeD/halcyon/WayUI.as
net/systemeD/halcyon/connection/Connection.as
net/systemeD/halcyon/connection/EntityDraggedEvent.as [moved from net/systemeD/halcyon/connection/WayDraggedEvent.as with 54% similarity]
net/systemeD/halcyon/connection/EntityEvent.as
net/systemeD/halcyon/connection/actions/MoveWayAction.as
net/systemeD/potlatch2/controller/ControllerState.as
net/systemeD/potlatch2/controller/DragPOINode.as [deleted file]
net/systemeD/potlatch2/controller/DragSelection.as [moved from net/systemeD/potlatch2/controller/DragWay.as with 57% similarity]

index be5b16d..3a26003 100644 (file)
@@ -38,6 +38,7 @@ package net.systemeD.halcyon {
                        }
                        entity.addEventListener(Connection.NODE_MOVED, nodeMoved);
             entity.addEventListener(Connection.NODE_ALTERED, nodeAltered);
+            entity.addEventListener(Connection.ENTITY_DRAGGED, nodeDragged);
             attachRelationListeners();
                        redraw();
                }
@@ -46,6 +47,7 @@ package net.systemeD.halcyon {
                        removeGenericEventListeners();
                        entity.removeEventListener(Connection.NODE_MOVED, nodeMoved);
             entity.removeEventListener(Connection.NODE_ALTERED, nodeAltered);
+            entity.removeEventListener(Connection.ENTITY_DRAGGED, nodeDragged);
                }
 
                /** Respond to movement event. */
@@ -57,6 +59,10 @@ package net.systemeD.halcyon {
             redraw();
         }
 
+               private function nodeDragged(event:EntityDraggedEvent):void {
+                       updatePosition(event.xDelta,event.yDelta);
+               }
+
                /** Update settings then draw node. */
                override public function doRedraw():Boolean {
                        if (!paint.ready) { return false; }
@@ -185,9 +191,11 @@ package net.systemeD.halcyon {
                        updatePosition();
                }
 
-               private function updatePosition():void {
+               private function updatePosition(xDelta:Number=0,yDelta:Number=0):void {
                        if (!loaded) { return; }
 
+                       var baseX:Number=paint.map.lon2coord(Node(entity).lon);
+                       var baseY:Number=paint.map.latp2coord(Node(entity).latp);
                        for (var i:uint=0; i<sprites.length; i++) {
                                var d:DisplayObject=sprites[i];
                                d.x=0; d.y=0; d.rotation=0;
@@ -195,7 +203,7 @@ package net.systemeD.halcyon {
                                var m:Matrix=new Matrix();
                                m.translate(-d.width/2,-d.height/2);
                                m.rotate(rotation);
-                               m.translate(paint.map.lon2coord(Node(entity).lon),paint.map.latp2coord(Node(entity).latp));
+                               m.translate(baseX+xDelta,baseY+yDelta);
                                d.transform.matrix=m;
                        }
                }
index a7a513f..2145443 100644 (file)
@@ -42,7 +42,7 @@ package net.systemeD.halcyon {
             entity.addEventListener(Connection.WAY_NODE_ADDED, wayNodeAdded);
             entity.addEventListener(Connection.WAY_NODE_REMOVED, wayNodeRemoved);
             entity.addEventListener(Connection.WAY_REORDERED, wayReordered);
-            entity.addEventListener(Connection.WAY_DRAGGED, wayDragged);
+            entity.addEventListener(Connection.ENTITY_DRAGGED, wayDragged);
             attachNodeListeners();
             attachRelationListeners();
             recalculate();
@@ -55,7 +55,7 @@ package net.systemeD.halcyon {
             entity.removeEventListener(Connection.WAY_NODE_ADDED, wayNodeAdded);
             entity.removeEventListener(Connection.WAY_NODE_REMOVED, wayNodeRemoved);
             entity.removeEventListener(Connection.WAY_REORDERED, wayReordered);
-            entity.removeEventListener(Connection.WAY_DRAGGED, wayDragged);
+            entity.removeEventListener(Connection.ENTITY_DRAGGED, wayDragged);
             for (var i:uint = 0; i < Way(entity).length; i++ ) {
                 Way(entity).getNode(i).removeEventListener(Connection.NODE_MOVED, nodeMoved);
             }
@@ -96,7 +96,7 @@ package net.systemeD.halcyon {
             redraw();
                        redrawMultis();
         }
-               private function wayDragged(event:WayDraggedEvent):void {
+               private function wayDragged(event:EntityDraggedEvent):void {
                        offsetSprites(event.xDelta,event.yDelta);
                }
 
index ac06e07..ec8de9f 100644 (file)
@@ -74,7 +74,7 @@ package net.systemeD.halcyon.connection {
         public static var WAY_NODE_ADDED:String = "way_node_added";
         public static var WAY_NODE_REMOVED:String = "way_node_removed";
         public static var WAY_REORDERED:String = "way_reordered";
-        public static var WAY_DRAGGED:String = "way_dragged";
+        public static var ENTITY_DRAGGED:String = "entity_dragged";
                public static var NODE_DELETED:String = "node_deleted";
                public static var WAY_DELETED:String = "way_deleted";
                public static var RELATION_DELETED:String = "relation_deleted";
@@ -2,26 +2,22 @@ package net.systemeD.halcyon.connection {
 
     import flash.events.Event;
 
-    public class WayDraggedEvent extends EntityEvent {
-        private var _way:Way;
+    public class EntityDraggedEvent extends EntityEvent {
                private var _xDelta:Number;
                private var _yDelta:Number;
 
-        public function WayDraggedEvent(type:String, way:Way, xDelta:Number, yDelta:Number) {
-            super(type, way);
-            this._way = way;
+        public function EntityDraggedEvent(type:String, entity:Entity, xDelta:Number, yDelta:Number) {
+            super(type, entity);
             this._xDelta = xDelta;
             this._yDelta = yDelta;
         }
 
-        public function get way():Way { return _way; }
         public function get xDelta():Number { return _xDelta; }
         public function get yDelta():Number { return _yDelta; }
 
         public override function toString():String {
-            return super.toString() + " in "+_way+" by "+_xDelta+","+_yDelta;
+            return super.toString() + " in "+item+" by "+_xDelta+","+_yDelta;
         }
     }
 
 }
-
index 1484b26..0835eaa 100644 (file)
@@ -3,7 +3,7 @@ package net.systemeD.halcyon.connection {
     import flash.events.Event;
 
     public class EntityEvent extends Event {
-        private var item:Entity;
+        protected var item:Entity;
 
         public function EntityEvent(type:String, item:Entity) {
             super(type);
index 2904da6..90342e0 100644 (file)
@@ -6,28 +6,21 @@ package net.systemeD.halcyon.connection.actions {
     public class MoveWayAction extends CompositeUndoableAction {
     
         private var way:Way;
-        private var downX:Number;
-        private var downY:Number;
-        private var x:Number;
-        private var y:Number;
-        private var map:Map;
+        private var lonDelta:Number;
+        private var latDelta:Number;
+               private var moved:Object;
     
-        public function MoveWayAction(way:Way, downX:Number, downY:Number, x:Number, y:Number, map:Map) {
+        public function MoveWayAction(way:Way, latDelta:Number, lonDelta:Number, moved:Object) {
             super("Drag way "+way.id);
             this.way = way;
-            this.downX = downX;
-            this.downY = downY;
-            this.x = x;
-            this.y = y;
-            this.map = map;
+            this.lonDelta = lonDelta;
+            this.latDelta = latDelta;
+                       this.moved = moved;
         }
     
         public override function doAction():uint {
-            var lonDelta:Number = map.coord2lon(downX)-map.coord2lon(x);
-            var latDelta:Number = map.coord2lat(downY)-map.coord2lat(y);
-            var moved:Object = {};
             way.suspend();
-            way.dispatchEvent(new WayDraggedEvent(Connection.WAY_DRAGGED, way, 0, 0));
+            way.dispatchEvent(new EntityDraggedEvent(Connection.ENTITY_DRAGGED, way, 0, 0));
             for (var i:uint=0; i<way.length; i++) {
                 var n:Node=way.getNode(i);
                 if (!moved[n.id]) {
index 93f75a4..060aaec 100644 (file)
@@ -92,13 +92,7 @@ package net.systemeD.potlatch2.controller {
                        }
 
                        if ( event.type == MouseEvent.MOUSE_DOWN ) {
-                               if ( entity is Way ) {
-                                       // click way
-                                       return new DragWay(focus as Way, event);
-                               } else if ( focus is Node ) {
-                                       // select POI node
-                                       return new DragPOINode(entity as Node,event,false);
-                               } else if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
+                               if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
                                        // select node within this way
                        return new DragWayNode(selectedWay,  getNodeIndex(selectedWay,entity as Node),  event, false);
                                } else if ( entity is Node && focus is Way ) {
@@ -107,6 +101,10 @@ package net.systemeD.potlatch2.controller {
                                } else if ( controller.keyDown(Keyboard.SPACE) ) {
                                        // drag the background imagery to compensate for poor alignment
                                        return new DragBackground(event);
+                               } else if (entity && selection.indexOf(entity)>-1) {
+                                       return new DragSelection(selection, event);
+                               } else if (entity) {
+                                       return new DragSelection([entity], event);
                                }
             } else if ( event.type == MouseEvent.CLICK && focus == null && map.dragstate!=map.DRAGGING && this is SelectedMarker) {
                 // this is identical to the below, but needed for unselecting markers on vector background layers.
diff --git a/net/systemeD/potlatch2/controller/DragPOINode.as b/net/systemeD/potlatch2/controller/DragPOINode.as
deleted file mode 100644 (file)
index 9ad233c..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-package net.systemeD.potlatch2.controller {
-       import flash.events.*;
-    import net.systemeD.potlatch2.EditController;
-    import net.systemeD.halcyon.connection.*;
-       import net.systemeD.halcyon.Globals;
-
-    public class DragPOINode extends ControllerState {
-        private var isDraggingStarted:Boolean = false;
-               private var isNew:Boolean = false;
-
-        private var downX:Number;
-        private var downY:Number;
-               private var dragstate:uint=NOT_MOVED;
-               private const NOT_DRAGGING:uint=0;
-               private const NOT_MOVED:uint=1;
-               private const DRAGGING:uint=2;
-        
-        public function DragPOINode(node:Node, event:MouseEvent, newNode:Boolean) {
-            selection = [node];
-            downX = event.localX;
-            downY = event.localY;
-                       isNew = newNode;
-        }
-       override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
-
-            if (event.type==MouseEvent.MOUSE_UP) {
-                       return new SelectedPOINode(firstSelected as Node);
-
-                       } else if ( event.type == MouseEvent.MOUSE_MOVE) {
-                               // dragging
-                               if (dragstate==NOT_DRAGGING) {
-                                       return this;
-                               } else if (dragstate==NOT_MOVED && Math.abs(downX - event.localX) < 3 && Math.abs(downY - event.localY) < 3) {
-                                       return this;
-                               }
-                               dragstate=DRAGGING;
-                return dragTo(event);
-
-                       } else {
-                               // event not handled
-                return this;
-                       }
-        }
-
-        private function dragTo(event:MouseEvent):ControllerState {
-            (firstSelected as Node).lat = controller.map.coord2lat(event.localY);
-            (firstSelected as Node).lon = controller.map.coord2lon(event.localX);
-            return this;
-        }
-        
-               public function forceDragStart():void {
-                       dragstate=NOT_MOVED;
-               }
-
-        override public function enterState():void {
-            controller.map.setHighlight(firstSelected, { selected: true } );
-                       Globals.vars.root.addDebug("**** -> "+this);
-        }
-        override public function exitState(newState:ControllerState):void {
-            controller.map.setHighlight(firstSelected, { selected: false } );
-                       Globals.vars.root.addDebug("**** <- "+this);
-        }
-        override public function toString():String {
-            return "DragPOINode";
-        }
-    }
-}
@@ -7,8 +7,8 @@ package net.systemeD.potlatch2.controller {
     import net.systemeD.halcyon.connection.actions.*;
        import net.systemeD.halcyon.Globals;
 
-    /** The state of moving a way around with the mouse. */
-    public class DragWay extends ControllerState {
+    /** The state of moving a selection around with the mouse. */
+    public class DragSelection extends ControllerState {
         private var isDraggingStarted:Boolean = false;
                private var enterTime:Number;
 
@@ -25,8 +25,8 @@ package net.systemeD.potlatch2.controller {
                private const DRAGGING:uint=2;
         
         /** Start the drag by recording the dragged way, where it started, and when. */
-        public function DragWay(way:Way, event:MouseEvent) {
-            selection = [way];
+        public function DragSelection(sel:Array, event:MouseEvent) {
+            selection = sel.concat();
             downX = event.localX;
             downY = event.localY;
                        enterTime = (new Date()).getTime();
@@ -36,10 +36,22 @@ package net.systemeD.potlatch2.controller {
        override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
             if (event.type==MouseEvent.MOUSE_UP) {
                 if (dragstate==DRAGGING) { 
-                  MainUndoStack.getGlobalStack().addAction(
-                          new MoveWayAction(firstSelected as Way, downX, downY, event.localX, event.localY, controller.map)); 
+                                       var undo:CompositeUndoableAction = new CompositeUndoableAction("Move items");
+                                       var lonDelta:Number = controller.map.coord2lon(downX)-controller.map.coord2lon(event.localX);
+                                       var latDelta:Number = controller.map.coord2lat(downY)-controller.map.coord2lat(event.localY);
+                                       var moved:Object = {};
+                                       for each (var entity:Entity in selection) {
+                                               if (entity is Node) {
+                                                       var node:Node=Node(entity);
+                                                       node.setLatLon(node.lat-latDelta, node.lon-lonDelta, undo.push);
+                                                       moved[node.id]=true;
+                                               } else if (entity is Way) {
+                                                       undo.push(new MoveWayAction(Way(entity), latDelta, lonDelta, moved));
+                                               }
+                                       }
+                                       MainUndoStack.getGlobalStack().addAction(undo);
                 }
-                return new SelectedWay(firstSelected as Way, new Point(event.stageX,event.stageY));
+                               return controller.findStateForSelection(selection);
 
                        } else if ( event.type == MouseEvent.MOUSE_MOVE) {
                                // dragging
@@ -62,14 +74,18 @@ package net.systemeD.potlatch2.controller {
                /** Abort dragging if ESC pressed. */
                override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
                        if (event.keyCode==Keyboard.ESCAPE) {
-                               firstSelected.dispatchEvent(new WayDraggedEvent(Connection.WAY_DRAGGED, firstSelected as Way, 0, 0));
-                               return new SelectedWay(firstSelected as Way);
+                               for each (var entity:Entity in selection) {
+                                       entity.dispatchEvent(new EntityDraggedEvent(Connection.ENTITY_DRAGGED, entity, 0, 0));
+                               }
+                               return controller.findStateForSelection(selection);
                        }
                        return this;
                }
 
         private function dragTo(event:MouseEvent):ControllerState {
-                       firstSelected.dispatchEvent(new WayDraggedEvent(Connection.WAY_DRAGGED, firstSelected as Way, event.localX-downX, event.localY-downY));
+                       for each (var entity:Entity in selection) {
+                               entity.dispatchEvent(new EntityDraggedEvent(Connection.ENTITY_DRAGGED, entity, event.localX-downX, event.localY-downY));
+                       }
             return this;
         }
         
@@ -77,20 +93,24 @@ package net.systemeD.potlatch2.controller {
                        dragstate=NOT_MOVED;
                }
 
-        /** Highlight the dragged way. */
+        /** Highlight the dragged selection. */
         override public function enterState():void {
-            controller.map.setHighlight(firstSelected, { selected: true } );
+                       for each (var entity:Entity in selection) {
+                               controller.map.setHighlight(entity, { selected: true });
+                       }
                        Globals.vars.root.addDebug("**** -> "+this);
         }
         
-        /** Un-highlight the dragged way. */
+        /** Un-highlight the dragged selection. */
         override public function exitState(newState:ControllerState):void {
-            controller.map.setHighlight(firstSelected, { selected: false } );
+                       for each (var entity:Entity in selection) {
+                               controller.map.setHighlight(entity, { selected: false });
+                       }
                        Globals.vars.root.addDebug("**** <- "+this);
         }
-        /** "DragWay" */
+        /** "DragSelection" */
         override public function toString():String {
-            return "DragWay";
+            return "DragSelection";
         }