more turn restriction stuff, and checkboxes, and a bunch of little fixes
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedWayNode.as
index 872f12d41b86bdf6c3e95e088a63d78b8e7fec36..3e3411e236d0017408d43b7119363744054e5d00 100644 (file)
@@ -3,31 +3,35 @@ 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 {
-        protected var initNode:Node;
+               protected var selectedIndex:int;
+               protected var initIndex:int;
         
-        public function SelectedWayNode(way:Way,node:Node) {
+        public function SelectedWayNode(way:Way,index:int) {
                        super (way);
-            initNode = node;
+                       initIndex = index;
         }
  
-        protected function selectNode(way:Way,node:Node):void {
+        protected function selectNode(way:Way,index:int):void {
+                       var node:Node=way.getNode(index);
             if ( way == selectedWay && node == selectedNode )
                 return;
 
-            clearSelection();
+            clearSelection(this);
             controller.setSelectedEntity(node);
-            controller.map.setHighlight(way, { showNodes: true, nodeSelected: node.id });
-            selectedWay = way;   initWay  = way;
-            selectedNode = node; initNode = node;
+            controller.map.setHighlight(way, { hover: false, showNodes: true, nodeSelected: node.id });
+            selectedWay = way; initWay = way;
+                       selectedIndex = index; initIndex = index;
+            selectedNode = node;
         }
                 
-        override protected function clearSelection():void {
+        override protected function clearSelection(newState:ControllerState):void {
             if ( selectedNode != null ) {
                controller.map.setHighlight(selectedWay, { selected: false, showNodes: false, nodeSelected: null });
-                controller.setSelectedEntity(null);
+                if (!newState.isSelectionState()) { controller.setSelectedEntity(null); }
                 selectedNode = null;
                                selectedWay = null;
             }
@@ -44,7 +48,7 @@ package net.systemeD.potlatch2.controller {
                 return new DrawWay(way, true, false);
                        } else if ( event.type == MouseEvent.MOUSE_UP && entity is Node && focus == selectedWay ) {
                                // select node within way
-                               return selectOrEdit(selectedWay, Node(entity));
+                               return selectOrEdit(selectedWay, getNodeIndex(selectedWay,Node(entity)));
             } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==selectedWay && event.shiftKey) {
                                // insert node within way (shift-click)
                        var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
@@ -52,25 +56,28 @@ package net.systemeD.potlatch2.controller {
                                return d;
                        }
                        var cs:ControllerState = sharedMouseEvents(event, entity);
-                       if (cs) return cs;
-                       return this;
+                       return cs ? cs : this;
         }
 
                override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
                        switch (event.keyCode) {
-                               case 88:                                        return splitWay();
+                               case 189:                                       return removeNode();                                    // '-'
+                               case 88:                                        return splitWay();                                              // 'X'
+                               case 82:                                        repeatTags(selectedNode); return this;  // 'R'
                                case Keyboard.BACKSPACE:        return deleteNode();
                                case Keyboard.DELETE:           return deleteNode();
                        }
-                       return this;
+                       var cs:ControllerState = sharedKeyboardEvents(event);
+                       return cs ? cs : this;
                }
                
                override public function enterState():void {
-            selectNode(initWay,initNode);
+            selectNode(initWay,initIndex);
                        Globals.vars.root.addDebug("**** -> "+this);
         }
-               override public function exitState():void {
-            clearSelection();
+               override public function exitState(newState:ControllerState):void {
+                       controller.clipboards['node']=selectedNode.getTagsCopy();
+            clearSelection(newState);
                        Globals.vars.root.addDebug("**** <- "+this);
         }
 
@@ -78,13 +85,14 @@ package net.systemeD.potlatch2.controller {
             return "SelectedWayNode";
         }
 
-        public static function selectOrEdit(selectedWay:Way, entity:Node):ControllerState {
+        public static function selectOrEdit(selectedWay:Way, index:int):ControllerState {
                var isFirst:Boolean = false;
                        var isLast:Boolean = false;
-                       isFirst = selectedWay.getNode(0) == entity;
-                       isLast = selectedWay.getNode(selectedWay.length - 1) == entity;
+                       var node:Node = selectedWay.getNode(index);
+                       isFirst = selectedWay.getNode(0) == node;
+                       isLast = selectedWay.getNode(selectedWay.length - 1) == node;
                        if ( isFirst == isLast )    // both == looped, none == central node 
-                           return new SelectedWayNode(selectedWay, entity);
+                           return new SelectedWayNode(selectedWay, index);
                        else
                            return new DrawWay(selectedWay, isLast, true);
         }
@@ -94,26 +102,17 @@ 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);
                }
                
+               public function removeNode():ControllerState {
+                       if (selectedNode.numParentWays==1) { return deleteNode(); }
+                       selectedWay.removeNodeByIndex(selectedIndex, MainUndoStack.getGlobalStack().addAction);
+                       return new SelectedWay(selectedWay);
+               }
+               
                public function deleteNode():ControllerState {
                        selectedNode.remove(MainUndoStack.getGlobalStack().addAction);
                        return new SelectedWay(selectedWay);