more turn restriction stuff, and checkboxes, and a bunch of little fixes
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedWayNode.as
index f4960d15bca894c33b74864df5db221d5310525b..3e3411e236d0017408d43b7119363744054e5d00 100644 (file)
@@ -3,32 +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 selectedNode:Node;
-        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;
             }
@@ -36,53 +39,45 @@ package net.systemeD.potlatch2.controller {
         
         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
                        if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
-            var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
+            var focus:Entity = getTopLevelFocusEntity(entity);
 
-            if ( event.type == MouseEvent.MOUSE_UP ) {
-                               if ( entity is Node && event.shiftKey ) {
-                                       // start new way
-                    var way:Way = controller.connection.createWay({}, [entity]);
-                    return new DrawWay(way, true, false);
-                               } else if ( entity is Node && focus == selectedWay ) {
-                                       // select node within way
-                                       return selectOrEdit(selectedWay, Node(entity));
-                } else if ( entity is Way ) {
-                                       // select way
-                                       return new SelectedWay(Way(entity));
-                } else if ( focus == null && map.dragstate!=map.DRAGGING ) {
-                    return new NoSelection();
-                               }
-            } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
-                               if ( entity is Way && focus==selectedWay && event.shiftKey) {
-                                       // insert node within way (shift-click)
-                       var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
-                                       d.forceDragStart();
-                                       return d;
-                               } else if ( entity is Node && entity.hasParent(selectedWay) ) {
-                    return new DragWayNode(selectedWay, Node(entity), event, false);
-                               } else if ( focus is Node ) {
-                                       return new DragPOINode(entity as Node,event,false);
-                               }
-            }
-
-            return this;
+            if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey ) {
+                               // start new way
+                var way:Way = controller.connection.createWay({}, [entity],
+                    MainUndoStack.getGlobalStack().addAction);
+                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, 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);
+                               d.forceDragStart();
+                               return d;
+                       }
+                       var cs:ControllerState = sharedMouseEvents(event, entity);
+                       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);
         }
 
@@ -90,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);
         }
@@ -106,24 +102,19 @@ 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));
-                       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, ''));
-                       }
-                       controller.map.paint.wayuis[newWay.id].redraw();        // ** should we do this by firing an event?
+            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();
+                       selectedNode.remove(MainUndoStack.getGlobalStack().addAction);
                        return new SelectedWay(selectedWay);
                }