Make splitway reversible. TODO - fix relations bug
authorAndy Allan <gravitystorm@gmail.com>
Sun, 2 May 2010 13:31:59 +0000 (13:31 +0000)
committerAndy Allan <gravitystorm@gmail.com>
Sun, 2 May 2010 13:31:59 +0000 (13:31 +0000)
net/systemeD/halcyon/connection/CompositeUndoableAction.as
net/systemeD/halcyon/connection/Way.as
net/systemeD/halcyon/connection/actions/RemoveNodeByIndexAction.as [new file with mode: 0644]
net/systemeD/halcyon/connection/actions/SplitWayAction.as [new file with mode: 0644]
net/systemeD/halcyon/vectorlayers/VectorLayer.as
net/systemeD/potlatch2/controller/SelectedWayNode.as

index 966e159..3cd4e11 100644 (file)
@@ -14,6 +14,10 @@ package net.systemeD.halcyon.connection {
             actions.push(action);
         }
         
+        public function clearActions():void {
+            actions = [];
+        }
+        
         public override function doAction():uint {
             if ( actionsDone )
                 return UndoableAction.FAIL;
index 2df2097..810d03b 100644 (file)
@@ -54,26 +54,20 @@ package net.systemeD.halcyon.connection {
                        performAction(new RemoveNodeFromWayAction(this, node, nodes));
                }
 
-        public function removeNodeByIndex(index:uint,fireEvent:Boolean=true):void {
-            var removed:Array=nodes.splice(index, 1);
-                       if (nodes.indexOf(removed[0])==-1) { removed[0].removeParent(this); }
-                       markDirty();
-                       if (fireEvent) {
-                   dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, removed[0], this, index));
-                       }
+        public function removeNodeByIndex(index:uint, performAction:Function, fireEvent:Boolean=true):void {
+            performAction(new RemoveNodeByIndexAction(this, nodes, index, fireEvent));
         }
 
                public function sliceNodes(start:int,end:int):Array {
                        return nodes.slice(start,end);
                }
 
-               public function deleteNodesFrom(start:int):void {
-                       for (var i:int=start; i<nodes.length; i++) {
-                               nodes[i].removeParent(this);
-                       }
-                       nodes.splice(start);
-                       markDirty();
-               }
+        public function deleteNodesFrom(start:int, performAction:Function):void {
+            for (var i:int=nodes.length-1; i>=start; i--) {
+              performAction(new RemoveNodeByIndexAction(this, nodes, i));
+            }
+            markDirty();
+        }
 
                public function mergeWith(way:Way,topos:int,frompos:int, performAction:Function):void {
                        performAction(new MergeWaysAction(this, way, topos, frompos));
diff --git a/net/systemeD/halcyon/connection/actions/RemoveNodeByIndexAction.as b/net/systemeD/halcyon/connection/actions/RemoveNodeByIndexAction.as
new file mode 100644 (file)
index 0000000..7bea7e3
--- /dev/null
@@ -0,0 +1,41 @@
+package net.systemeD.halcyon.connection.actions {
+
+    import net.systemeD.halcyon.connection.*;
+    
+    public class RemoveNodeByIndexAction extends UndoableEntityAction {
+    
+        private var way:Way;
+        private var nodeList:Array;
+        private var index:uint;
+        private var removed:Array;
+        private var fireEvent:Boolean;
+    
+        public function RemoveNodeByIndexAction(way:Way, nodeList:Array, index:uint, fireEvent:Boolean=true) {
+            super(way, "Remove node " + nodeList[index].id + " from position " + index);
+            this.way = way;
+            this.nodeList = nodeList;
+            this.index = index;
+            this.fireEvent = fireEvent;
+        }
+        
+        public override function doAction():uint {
+            removed = nodeList.splice(index, 1);
+            if (nodeList.indexOf(removed[0])==-1) { removed[0].removeParent(way); }
+            markDirty();
+            if (fireEvent) {
+               entity.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, removed[0], way, index));
+            }
+            return SUCCESS;
+        }
+        
+        public override function undoAction():uint {
+            nodeList.splice(index, 0, removed[0]);
+            removed[0].addParent(way);
+            markClean();
+            if (fireEvent) {
+                entity.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, removed[0], way, index));
+            }
+            return SUCCESS;
+        }
+    }
+}
\ No newline at end of file
diff --git a/net/systemeD/halcyon/connection/actions/SplitWayAction.as b/net/systemeD/halcyon/connection/actions/SplitWayAction.as
new file mode 100644 (file)
index 0000000..0cf03d9
--- /dev/null
@@ -0,0 +1,53 @@
+package net.systemeD.halcyon.connection.actions {
+
+    import net.systemeD.halcyon.connection.*;
+    
+    public class SplitWayAction extends CompositeUndoableAction {
+    
+        private var selectedWay:Way;
+        private var selectedNode:Node;
+        private var newWay:Way;
+    
+        public function SplitWayAction(selectedWay:Way, selectedNode:Node) {
+            super("Split way "+selectedWay.id);
+            this.selectedWay = selectedWay;
+            this.selectedNode = selectedNode;
+        }
+    
+        public override function doAction():uint {
+            if (newWay==null) {
+              newWay = Connection.getConnection().createWay(
+                  selectedWay.getTagsCopy(), 
+                  selectedWay.sliceNodes(selectedWay.indexOfNode(selectedNode),selectedWay.length),
+                  push);
+
+              selectedWay.deleteNodesFrom(selectedWay.indexOfNode(selectedNode)+1, push);
+              
+              // copy relations
+              // TODO make this reversible
+              for each (var r:Relation in selectedWay.parentRelations) {
+                  // ** needs to copy roles as well
+                  r.appendMember(new RelationMember(newWay, ''));
+              }
+            }
+            newWay.suspend();
+            selectedWay.suspend();
+            super.doAction();
+            newWay.resume();
+            selectedWay.resume();
+            return SUCCESS;
+        }
+        
+        public override function undoAction():uint {
+            selectedWay.suspend();
+            newWay.suspend();
+            
+            super.undoAction();
+            
+            newWay.resume();
+            selectedWay.resume();
+            return SUCCESS;
+        }
+    }
+
+}
\ No newline at end of file
index 3c3c2aa..6cd22f3 100644 (file)
@@ -55,7 +55,7 @@ package net.systemeD.halcyon.vectorlayers {
                                // delete this way
                                while (oldWay.length) { 
                                        var id:int=oldWay.getNode(0).id;
-                                       oldWay.removeNodeByIndex(0,false);
+                                       oldWay.removeNodeByIndex(0,MainUndoStack.getGlobalStack().addAction,false);
                                        delete nodes[id];
                                }
                                paint.wayuis[oldWay.id].redraw();
index 872f12d..551db6b 100644 (file)
@@ -3,6 +3,7 @@ package net.systemeD.potlatch2.controller {
        import flash.ui.Keyboard;
     import net.systemeD.potlatch2.EditController;
     import net.systemeD.halcyon.connection.*;
+    import net.systemeD.halcyon.connection.actions.*;
        import net.systemeD.halcyon.Globals;
 
     public class SelectedWayNode extends SelectedWay {
@@ -94,22 +95,7 @@ package net.systemeD.potlatch2.controller {
                        if (selectedWay.getNode(0                   ) == selectedNode) { return this; }
                        if (selectedWay.getNode(selectedWay.length-1) == selectedNode) { return this; }
 
-                       // create new way
-                       var newWay:Way = controller.connection.createWay(
-                               selectedWay.getTagsCopy(), 
-                               selectedWay.sliceNodes(selectedWay.indexOfNode(selectedNode),selectedWay.length),
-                               MainUndoStack.getGlobalStack().addAction);
-                       newWay.suspend();
-                       selectedWay.suspend();
-                       selectedWay.deleteNodesFrom(selectedWay.indexOfNode(selectedNode)+1);
-                       
-                       // copy relations
-                       for each (var r:Relation in selectedWay.parentRelations) {
-                               // ** needs to copy roles as well
-                               r.appendMember(new RelationMember(newWay, ''));
-                       }
-                       newWay.resume();
-                       selectedWay.resume();
+            MainUndoStack.getGlobalStack().addAction(new SplitWayAction(selectedWay, selectedNode));
 
                        return new SelectedWay(selectedWay);
                }