Unify selection ControllerStates so they can work on either background or editable...
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedWay.as
index fd978bebf0d7da8418c2458f2e364db9ab430564..192e2417f710d4fe44a85316c1c6180fa7eff47a 100644 (file)
@@ -1,8 +1,10 @@
 package net.systemeD.potlatch2.controller {
+       import flash.display.*;
        import flash.events.*;
        import flash.geom.Point;
        import flash.ui.Keyboard;
        
+       import net.systemeD.halcyon.MapPaint;
        import net.systemeD.halcyon.connection.*;
        import net.systemeD.potlatch2.tools.Quadrilateralise;
        import net.systemeD.potlatch2.tools.Simplify;
@@ -19,7 +21,8 @@ package net.systemeD.potlatch2.controller {
         * @param way The way that is now selected.
         * @param point The location that was clicked.
         * @param ways An ordered list of ways sharing a node, to make "way cycling" work. */
-        public function SelectedWay(way:Way, point:Point=null, ways:Array=null, index:int=0) {
+        public function SelectedWay(way:Way, layer:MapPaint=null, point:Point=null, ways:Array=null, index:int=0) {
+                       if (layer) this.layer=layer;
             initWay = way;
                        clicked = point;
                        wayList = ways;
@@ -43,20 +46,23 @@ package net.systemeD.potlatch2.controller {
         /** Behaviour includes: start drawing a new way, insert a node within this way, select an additional way */
         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 paint:MapPaint = getMapPaint(DisplayObject(event.target));
             var focus:Entity = getTopLevelFocusEntity(entity);
 
-            if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey ) {
+            if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey && !layer.isBackground ) {
                                // start new way
                                var way:Way = entity.connection.createWay({}, [entity], MainUndoStack.getGlobalStack().addAction);
                                return new DrawWay(way, true, false);
-                       } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==firstSelected && event.shiftKey) {
+                       } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==firstSelected && event.shiftKey && !layer.isBackground ) {
                                // insert node within way (shift-click)
                 var d:DragWayNode=new DragWayNode(firstSelected as Way, -1, event, true);
                                d.forceDragStart();
                                return d;
-                       } else if ( event.type == MouseEvent.MOUSE_DOWN && event.ctrlKey && entity && entity!=firstSelected) {
+                       } else if ( event.type == MouseEvent.MOUSE_DOWN && event.ctrlKey && entity && entity!=firstSelected && paint==layer) {
                                // multiple selection
-                               return new SelectedMultiple([firstSelected,entity]);
+                               return new SelectedMultiple([firstSelected,entity],layer);
+                       } else if ( event.type == MouseEvent.MOUSE_UP && focus==firstSelected ) {
+                               return this;
                        }
                        var cs:ControllerState = sharedMouseEvents(event, entity);
                        return cs ? cs : this;
@@ -65,17 +71,20 @@ package net.systemeD.potlatch2.controller {
                /** Behaviour includes: parallel way, repeat tags, reverse direction, simplify, cycle way selection, delete */
                override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
                        switch (event.keyCode) {
-                               case 80:  /* P */           return new SelectedParallelWay(firstSelected as Way); 
-                               case 81:  /* Q */           Quadrilateralise.quadrilateralise(firstSelected as Way, MainUndoStack.getGlobalStack().addAction); return this;
                                case 82:  /* R */           repeatTags(firstSelected); return this;
-                case 86:  /* V */           Way(firstSelected).reverseNodes(MainUndoStack.getGlobalStack().addAction); return this;
-                case 89:  /* Y */           Simplify.simplify(firstSelected as Way, controller.map, true); return this;
                                case 191: /* / */           return cycleWays();
                                case Keyboard.BACKSPACE:        
                                case Keyboard.DELETE:           if (event.shiftKey) { return deleteWay(); } break;
-                case 188: /* , */           return new SelectedWayNode(initWay, initIndex); // allows navigating from one way to another by keyboard
-                case 190: /* . */           return new SelectedWayNode(initWay, initIndex); //  using <, > and /           
-
+                       }
+                       if (!layer.isBackground) {
+                               switch (event.keyCode) {
+                                       case 80:  /* P */       return new SelectedParallelWay(firstSelected as Way); 
+                                       case 81:  /* Q */       Quadrilateralise.quadrilateralise(firstSelected as Way, MainUndoStack.getGlobalStack().addAction); return this;
+                                       case 86:  /* V */       Way(firstSelected).reverseNodes(MainUndoStack.getGlobalStack().addAction); return this;
+                                       case 89:  /* Y */       Simplify.simplify(firstSelected as Way, controller.map, true); return this;
+                                       case 188: /* , */       return new SelectedWayNode(initWay, initIndex); // allows navigating from one way to another by keyboard
+                                       case 190: /* . */       return new SelectedWayNode(initWay, initIndex); //  using <, > and /           
+                               }
                        }
                        var cs:ControllerState = sharedKeyboardEvents(event);
                        return cs ? cs : this;
@@ -90,7 +99,7 @@ package net.systemeD.potlatch2.controller {
                        wayList=wayList.slice(1).concat(wayList[0]);
                        // Find the new way's index of the currently "selected" node, to facilitate keyboard navigation
                        var newindex:int = Way(wayList[0]).indexOfNode(initWay.getNode(initIndex));
-                       return new SelectedWay(wayList[0], clicked, wayList, newindex);
+                       return new SelectedWay(wayList[0], layer, clicked, wayList, newindex);
                }
 
                /** Perform deletion of currently selected way. */