Unify selection ControllerStates so they can work on either background or editable...
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedWayNode.as
index 363baf9825f091fbb023c031761735041444d467..86766c2f4f505f983614561feba8a4c97954ebc2 100644 (file)
@@ -3,7 +3,7 @@ package net.systemeD.potlatch2.controller {
        import flash.geom.Point;
        import flash.ui.Keyboard;
        
-       import net.systemeD.halcyon.Globals;
+       import net.systemeD.halcyon.AttentionEvent;
        import net.systemeD.halcyon.WayUI;
        import net.systemeD.halcyon.connection.*;
        import net.systemeD.halcyon.connection.actions.*;
@@ -25,9 +25,9 @@ package net.systemeD.potlatch2.controller {
                 return;
 
             clearSelection(this);
-            controller.map.setHighlight(way, { hover: false });
-            controller.map.setHighlight(node, { selected: true });
-            controller.map.setHighlightOnNodes(way, { selectedway: true });
+            layer.setHighlight(way, { hover: false });
+            layer.setHighlight(node, { selected: true });
+            layer.setHighlightOnNodes(way, { selectedway: true });
             selection = [node]; parentWay = way;
             controller.updateSelectionUI();
                        selectedIndex = index; initIndex = index;
@@ -35,9 +35,9 @@ package net.systemeD.potlatch2.controller {
                 
         protected function clearSelection(newState:ControllerState):void {
             if ( selectCount ) {
-               controller.map.setHighlight(parentWay, { selected: false });
-                               controller.map.setHighlight(firstSelected, { selected: false });
-                               controller.map.setHighlightOnNodes(parentWay, { selectedway: false });
+               layer.setHighlight(parentWay, { selected: false });
+                               layer.setHighlight(firstSelected, { selected: false });
+                               layer.setHighlightOnNodes(parentWay, { selectedway: false });
                                selection = [];
                 if (!newState.isSelectionState()) { controller.updateSelectionUI(); }
             }
@@ -49,7 +49,7 @@ package net.systemeD.potlatch2.controller {
 
             if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey ) {
                                // start new way
-                var way:Way = controller.connection.createWay({}, [entity],
+                var way:Way = entity.connection.createWay({}, [entity],
                     MainUndoStack.getGlobalStack().addAction);
                 return new DrawWay(way, true, false);
                        } else if ( event.type == MouseEvent.MOUSE_UP && entity is Node && focus == parentWay ) {
@@ -76,6 +76,8 @@ package net.systemeD.potlatch2.controller {
                 case 74:                    if (event.shiftKey) { return unjoin() }; return join();// 'J'
                                case Keyboard.BACKSPACE:        return deleteNode();
                                case Keyboard.DELETE:           return deleteNode();
+                               case 188: /* , */           return stepNode(-1);
+                               case 190: /* . */           return stepNode(+1);           
                        }
                        var cs:ControllerState = sharedKeyboardEvents(event);
                        return cs ? cs : this;
@@ -85,28 +87,33 @@ package net.systemeD.potlatch2.controller {
                        return parentWay;
                }
 
+        public function get selectedNode():Node {
+            return parentWay.getNode(selectedIndex);
+        }
+        
                private function cycleWays():ControllerState {
                        var wayList:Array=firstSelected.parentWays;
                        if (wayList.length==1) { return this; }
                        wayList.splice(wayList.indexOf(parentWay),1);
-                       return new SelectedWay(wayList[0],
+            // find index of this node in the newly selected way, to maintain state for keyboard navigation
+            var newindex:int = Way(wayList[0]).indexOfNode(parentWay.getNode(initIndex));
+                       return new SelectedWay(wayList[0], layer,
                                               new Point(controller.map.lon2coord(Node(firstSelected).lon),
                                                         controller.map.latp2coord(Node(firstSelected).latp)),
-                                              wayList.concat(parentWay));
+                                              wayList.concat(parentWay),
+                                              newindex);
                }
 
                override public function enterState():void {
             selectNode(parentWay,initIndex);
-                       controller.map.setPurgable(selection,false);
-                       Globals.vars.root.addDebug("**** -> "+this);
+                       layer.setPurgable(selection,false);
         }
                override public function exitState(newState:ControllerState):void {
             if (firstSelected.hasTags()) {
               controller.clipboards['node']=firstSelected.getTagsCopy();
             }
-                       controller.map.setPurgable(selection,true);
+                       layer.setPurgable(selection,true);
             clearSelection(newState);
-                       Globals.vars.root.addDebug("**** <- "+this);
         }
 
         override public function toString():String {
@@ -125,14 +132,23 @@ package net.systemeD.potlatch2.controller {
                            return new DrawWay(selectedWay, isLast, true);
         }
 
+               /** Splits a way into two separate ways, at the currently selected node. Handles simple loops and P-shapes. Untested for anything funkier. */
                public function splitWay():ControllerState {
+                       var n:Node=firstSelected as Node;
+                       var ni:uint = parentWay.indexOfNode(n);
                        // abort if start or end
-                       if (parentWay.getNode(0)    == firstSelected) { return this; }
-                       if (parentWay.getLastNode() == firstSelected) { return this; }
+                       if (parentWay.isPShape() && !parentWay.hasOnceOnly(n)) {
+                               // If P-shaped, we want to split at the midway point on the stem, not at the end of the loop
+                               ni = parentWay.getPJunctionNodeIndex();
+                               
+                       } else {
+                           if (parentWay.getNode(0)    == n) { return this; }
+                           if (parentWay.getLastNode() == n) { return this; }
+                       }
 
-                       controller.map.setHighlightOnNodes(parentWay, { selectedway: false } );
-                       controller.map.setPurgable([parentWay],true);
-            MainUndoStack.getGlobalStack().addAction(new SplitWayAction(parentWay, firstSelected as Node));
+                       layer.setHighlightOnNodes(parentWay, { selectedway: false } );
+                       layer.setPurgable([parentWay],true);
+            MainUndoStack.getGlobalStack().addAction(new SplitWayAction(parentWay, ni));
 
                        return new SelectedWay(parentWay);
                }
@@ -146,7 +162,7 @@ package net.systemeD.potlatch2.controller {
                }
                
                public function deleteNode():ControllerState {
-                       controller.map.setPurgable(selection,true);
+                       layer.setPurgable(selection,true);
                        firstSelected.remove(MainUndoStack.getGlobalStack().addAction);
                        return new SelectedWay(parentWay);
                }
@@ -156,19 +172,67 @@ package net.systemeD.potlatch2.controller {
             return this;
         }
 
+        /** Attempt to either merge the currently selected node with another very nearby node, or failing that,
+        *   attach it mid-way along a very nearby way. */
+               // FIXME: why are we only merging one node at once? after all, shift-click to insert a node adds into all ways
         public function join():ControllerState {
-            // detect the ways that overlap this node
-            var p:Point = new Point(controller.map.lon2coord(Node(firstSelected).lon),
-                                             controller.map.latp2coord(Node(firstSelected).latp));
+                       var p:Point = new Point(controller.map.lon2coord(Node(firstSelected).lon),
+                                               controller.map.latp2coord(Node(firstSelected).latp));
             var q:Point = map.localToGlobal(p);
-            var ways:Array=[]; var w:Way;
-            for each (var wayui:WayUI in controller.map.paint.wayuis) {
-                w=wayui.hitTest(q.x, q.y);
-                if (w && w!=selectedWay) { ways.push(w); }
+
+            // First, look for POI nodes in 20x20 pixel box around the current node
+                       // FIXME: why aren't we using a hitTest for this?
+            var hitnodes:Array = layer.connection.getObjectsByBbox(
+                map.coord2lon(p.x-10),
+                map.coord2lon(p.x+10),
+                map.coord2lat(p.y-10),
+                map.coord2lat(p.y+10)).poisInside;
+            
+            for each (var n: Node in hitnodes) {
+                if (!n.hasParent(selectedWay)) { 
+                   return doMergeNodes(n);
+                }
+            }
+            
+                       var ways:Array=layer.findWaysAtPoint(q.x, q.y, selectedWay);
+                       for each (var w:Way in ways) {
+                // hit a way, now let's see if we hit a specific node
+                for (var i:uint = 0; i < w.length; i++) {
+                                       n = w.getNode(i);
+                                       var x:Number = map.lon2coord(n.lon);
+                                       var y:Number = map.latp2coord(n.latp);
+                                       if (n != selectedNode && Math.abs(x-p.x) + Math.abs(y-p.y) < 10) {
+                                               return doMergeNodes(n);
+                                       }
+                               }
             }
 
+            // No nodes hit, so join our node onto any overlapping ways.
             Node(firstSelected).join(ways,MainUndoStack.getGlobalStack().addAction);
             return this;
         }
+        
+        private function doMergeNodes(n:Node): ControllerState {
+               n.mergeWith(Node(firstSelected), MainUndoStack.getGlobalStack().addAction);
+            // only merge one node at a time - too confusing otherwise?
+            var msg:String = "Nodes merged."
+            if (MergeNodesAction.lastProblemTags) {
+                               // FIXME: ugh, just ugh.
+                msg += " *Warning* The following tags conflicted and need attention: " + MergeNodesAction.lastProblemTags;
+            }
+            controller.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, msg));
+            return new SelectedWayNode(n.parentWays[0], Way(n.parentWays[0]).indexOfNode(n));
+        }
+        
+        /** Move the selection one node further up or down this way, looping if necessary. */
+        public function stepNode(delta:int):ControllerState {
+            var ni:int = (selectedIndex + delta + parentWay.length) %  parentWay.length
+            controller.map.scrollIfNeeded(parentWay.getNode(ni).lat,parentWay.getNode(ni).lon);
+            return new SelectedWayNode(parentWay, ni);
+        }
+
     }
+    
+    
 }
+