Reimplement pullThrough in a more undo-friendly fashion - only one undo to reverse...
authorAndy Allan <andy@gravitystorm.co.uk>
Thu, 26 May 2011 16:09:35 +0000 (17:09 +0100)
committerAndy Allan <andy@gravitystorm.co.uk>
Thu, 26 May 2011 16:09:35 +0000 (17:09 +0100)
net/systemeD/halcyon/MapPaint.as
net/systemeD/potlatch2/controller/ControllerState.as

index 0dafd34..123265d 100644 (file)
@@ -5,6 +5,7 @@ package net.systemeD.halcyon {
        import net.systemeD.halcyon.NodeUI;
        import net.systemeD.halcyon.WayUI;
        import net.systemeD.halcyon.connection.*;
+       import net.systemeD.halcyon.connection.actions.CreatePOIAction;
        import net.systemeD.halcyon.styleparser.RuleSet;
 
        /** Manages the drawing of map entities, allocating their sprites etc. */
@@ -349,15 +350,6 @@ package net.systemeD.halcyon {
                        ruleset.loadFromCSS(url);
         }
 
-               // >>>> REFACTOR: remove this
-               public function findSource():VectorLayer {
-//                     var v:VectorLayer;
-//                     for each (v in map.vectorlayers) {
-//                             if (v.paint==this) { return v; }
-//                     }
-                       return null;
-               }
-
                // ==================== Start of code moved from Map.as
 
                // Listeners for Connection events
@@ -437,5 +429,52 @@ package net.systemeD.halcyon {
 
                // ==================== End of code moved from Map.as
 
+        /**
+        * Transfers an entity from this layer into another layer
+        * @param entity The entity from this layer that you want to transfer.
+        * @param target The layer to transfer to
+        *
+        * @return either the newly created entity, or null
+        */
+        public function pullThrough(entity:Entity, target:MapPaint):Entity {
+            // TODO - check the entity actually resides in this layer.
+
+            var action:CompositeUndoableAction = new CompositeUndoableAction("pull through");
+            if (entity is Way) {
+                // copy way through to main layer
+                // ** shouldn't do this if the nodes are already in the main layer
+                //    (or maybe we should just match on lat/long to avoid ways in background having nodes in foreground)
+                var oldWay:Way=Way(entity);
+                var newWay:Way=target.connection.createWay(oldWay.getTagsCopy(), [], action.push);
+                var nodemap:Object={};
+                for (var i:uint=0; i<oldWay.length; i++) {
+                    oldNode = oldWay.getNode(i);
+                    var newNode:Node = nodemap[oldNode.id] ? nodemap[oldNode.id] : target.connection.createNode(
+                        oldNode.getTagsCopy(), oldNode.lat, oldNode.lon,
+                        action.push);
+                    newWay.appendNode(newNode, action.push);
+                    nodemap[oldNode.id]=newNode;
+                }
+                // delete this way
+                oldWay.remove(action.push)
+                MainUndoStack.getGlobalStack().addAction(action);
+                return newWay;
+
+            } else if (entity is Node && !entity.hasParentWays) {
+
+                var oldNode:Node=Node(entity);
+
+                var newPoiAction:CreatePOIAction = new CreatePOIAction(
+                    target.connection, oldNode.getTagsCopy(), oldNode.lat, oldNode.lon);
+                action.push(newPoiAction);
+
+                oldNode.remove(action.push);
+
+                MainUndoStack.getGlobalStack().addAction(action);
+                return newPoiAction.getNode();
+            }
+            return null;
+        }
+
        }
 }
index 1e752f5..db0ef10 100644 (file)
@@ -87,7 +87,7 @@ package net.systemeD.potlatch2.controller {
                        if ( paint && paint.isBackground ) {
                                if ( event.type == MouseEvent.MOUSE_DOWN && ((event.shiftKey && event.ctrlKey) || event.altKey) ) {
                                        // alt-click to pull data out of vector background layer
-                                       var newEntity:Entity=paint.findSource().pullThrough(entity,editableLayer.connection);
+                                       var newEntity:Entity=paint.pullThrough(entity,editableLayer);
                                        if (entity is Way) { return new SelectedWay(newEntity as Way); }
                                        else if (entity is Node) { return new SelectedPOINode(newEntity as Node); }
                 } else if (event.type == MouseEvent.MOUSE_DOWN && entity is Marker) {