Unify selection ControllerStates so they can work on either background or editable...
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedWayNode.as
index 77a050a6d272c957ddf2ae42b029d3290f357ed9..86766c2f4f505f983614561feba8a4c97954ebc2 100644 (file)
@@ -25,9 +25,9 @@ package net.systemeD.potlatch2.controller {
                 return;
 
             clearSelection(this);
-            editableLayer.setHighlight(way, { hover: false });
-            editableLayer.setHighlight(node, { selected: true });
-            editableLayer.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 ) {
-               editableLayer.setHighlight(parentWay, { selected: false });
-                               editableLayer.setHighlight(firstSelected, { selected: false });
-                               editableLayer.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 ) {
@@ -97,7 +97,7 @@ package net.systemeD.potlatch2.controller {
                        wayList.splice(wayList.indexOf(parentWay),1);
             // 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],
+                       return new SelectedWay(wayList[0], layer,
                                               new Point(controller.map.lon2coord(Node(firstSelected).lon),
                                                         controller.map.latp2coord(Node(firstSelected).latp)),
                                               wayList.concat(parentWay),
@@ -106,13 +106,13 @@ package net.systemeD.potlatch2.controller {
 
                override public function enterState():void {
             selectNode(parentWay,initIndex);
-                       editableLayer.setPurgable(selection,false);
+                       layer.setPurgable(selection,false);
         }
                override public function exitState(newState:ControllerState):void {
             if (firstSelected.hasTags()) {
               controller.clipboards['node']=firstSelected.getTagsCopy();
             }
-                       editableLayer.setPurgable(selection,true);
+                       layer.setPurgable(selection,true);
             clearSelection(newState);
         }
 
@@ -146,8 +146,8 @@ package net.systemeD.potlatch2.controller {
                            if (parentWay.getLastNode() == n) { return this; }
                        }
 
-                       editableLayer.setHighlightOnNodes(parentWay, { selectedway: false } );
-                       editableLayer.setPurgable([parentWay],true);
+                       layer.setHighlightOnNodes(parentWay, { selectedway: false } );
+                       layer.setPurgable([parentWay],true);
             MainUndoStack.getGlobalStack().addAction(new SplitWayAction(parentWay, ni));
 
                        return new SelectedWay(parentWay);
@@ -162,7 +162,7 @@ package net.systemeD.potlatch2.controller {
                }
                
                public function deleteNode():ControllerState {
-                       editableLayer.setPurgable(selection,true);
+                       layer.setPurgable(selection,true);
                        firstSelected.remove(MainUndoStack.getGlobalStack().addAction);
                        return new SelectedWay(parentWay);
                }
@@ -174,13 +174,15 @@ package net.systemeD.potlatch2.controller {
 
         /** 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 {
                        var p:Point = new Point(controller.map.lon2coord(Node(firstSelected).lon),
                                                controller.map.latp2coord(Node(firstSelected).latp));
             var q:Point = map.localToGlobal(p);
 
             // First, look for POI nodes in 20x20 pixel box around the current node
-            var hitnodes:Array = controller.connection.getObjectsByBbox(
+                       // 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),
@@ -192,21 +194,17 @@ package net.systemeD.potlatch2.controller {
                 }
             }
             
-            var ways:Array=[]; var w:Way;
-            for each (var wayui:WayUI in editableLayer.wayuis) {
-                w=wayui.hitTest(q.x, q.y);
-                if (w && w!=selectedWay) { 
+                       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);
-                        }    
-                    }
-                    ways.push(w); 
-                }
+                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.
@@ -219,9 +217,10 @@ package net.systemeD.potlatch2.controller {
             // 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;
             }
-            // somethingorother.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, msg));
+            controller.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, msg));
             return new SelectedWayNode(n.parentWays[0], Way(n.parentWays[0]).indexOfNode(n));
         }