Don't deselect on panning
[potlatch2.git] / net / systemeD / potlatch2 / controller / ControllerState.as
index 6ac2eae..87ff4cb 100644 (file)
@@ -6,11 +6,21 @@ package net.systemeD.potlatch2.controller {
     import net.systemeD.halcyon.connection.*;
     import net.systemeD.potlatch2.collections.Imagery;
     import net.systemeD.potlatch2.EditController;
-       import net.systemeD.halcyon.Globals;
        import net.systemeD.potlatch2.save.SaveManager;
+       import net.systemeD.potlatch2.utils.SnapshotConnection;
+       import flash.ui.Keyboard;
+       import mx.controls.Alert;
+       import mx.events.CloseEvent;
+       
+    /** Represents a particular state of the controller, such as "dragging a way" or "nothing selected". Key methods are 
+    * processKeyboardEvent and processMouseEvent which take some action, and return a new state for the controller. 
+    * 
+    * This abstract class has some behaviour that applies in most states, and lots of 'null' behaviour. 
+    * */
     public class ControllerState {
 
         protected var controller:EditController;
+               public var layer:MapPaint;
         protected var previousState:ControllerState;
 
                protected var _selection:Array=[];
@@ -18,7 +28,8 @@ package net.systemeD.potlatch2.controller {
         public function ControllerState() {}
 
         public function setController(controller:EditController):void {
-            this.controller = controller;
+            this.controller=controller;
+            if (!layer) layer=controller.map.editableLayer;
         }
 
         public function setPreviousState(previousState:ControllerState):void {
@@ -30,101 +41,118 @@ package net.systemeD.potlatch2.controller {
                        return true;
                }
 
+        /** When triggered by a mouse action such as a click, perform an action on the given entity, then move to a new state. */
         public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
             return this;
         }
-
+               
+               /** When triggered by a keypress, perform an action on the given entity, then move to a new state. */
         public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
             return this;
         }
 
+        /** Retrieves the map associated with the current EditController */
                public function get map():Map {
                        return controller.map;
                }
 
+        /** This is called when the EditController sets this ControllerState as the active state.
+        * Override this with whatever is needed, such as adding highlights to entities
+        */
         public function enterState():void {}
+
+        /** This is called by the EditController as the current controllerstate is exiting.
+        * Override this with whatever cleanup is needed, such as removing highlights from entities
+        */
         public function exitState(newState:ControllerState):void {}
 
+               /** Represent the state in text for debugging. */
                public function toString():String {
                        return "(No state)";
                }
-
+               /** Default behaviour for the current state that should be called if state-specific action has been taken care of or ruled out. */
                protected function sharedKeyboardEvents(event:KeyboardEvent):ControllerState {
+                       var editableLayer:MapPaint=controller.map.editableLayer;                                                                // shorthand for this method
                        switch (event.keyCode) {
                                case 66:        setSourceTag(); break;                                                                                                  // B - set source tag for current object
-                               case 67:        controller.connection.closeChangeset(); break;                                                  // C - close changeset
-                               case 68:        controller.map.paint.alpha=1.3-controller.map.paint.alpha; return null; // D - dim
-                               case 83:        SaveManager.saveChanges(); break;                                                                               // S - save
+                               case 67:        editableLayer.connection.closeChangeset(); break;                                               // C - close changeset
+                               case 68:        editableLayer.alpha=1.3-editableLayer.alpha; return null;                               // D - dim
+                               case 83:        SaveManager.saveChanges(editableLayer.connection); break;                               // S - save
                                case 84:        controller.tagViewer.togglePanel(); return null;                                                // T - toggle tags panel
-                               case 90:        MainUndoStack.getGlobalStack().undo(); return null;                                             // Z - undo
-                               case 187:       controller.tagViewer.addNewTag(); return null;                                                  // + - add tag
-                               case 107:       controller.tagViewer.addNewTag(); return null;                                                  // numpad + - add tag
+                               case 90:        if (!event.shiftKey) { MainUndoStack.getGlobalStack().undo(); return null;}// Z - undo
+                                           else { MainUndoStack.getGlobalStack().redo(); return null;  }           // Shift-Z - redo                                           
+                               case Keyboard.ESCAPE:   revertSelection(); break;                                                                       // ESC - revert to server version
+                               case Keyboard.NUMPAD_ADD:                                                                                                                       // + - add tag
+                               case 187:       controller.tagViewer.selectAdvancedPanel();                                                             //   |
+                                                       controller.tagViewer.addNewTag(); return null;                                                  //   |
                        }
                        return null;
                }
 
+               /** Default behaviour for the current state that should be called if state-specific action has been taken care of or ruled out. */
                protected function sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
                        var paint:MapPaint = getMapPaint(DisplayObject(event.target));
             var focus:Entity = getTopLevelFocusEntity(entity);
 
+                       if ( event.type == MouseEvent.MOUSE_UP && focus && map.dragstate!=map.NOT_DRAGGING) {
+                               map.mouseUpHandler();   // in case the end-drag is over an EntityUI
+                       } else if ( event.type == MouseEvent.ROLL_OVER && paint && paint.interactive ) {
+                               paint.setHighlight(focus, { hover: true });
+                       } else if ( event.type == MouseEvent.MOUSE_OUT && paint && paint.interactive ) {
+                               paint.setHighlight(focus, { hover: false });
+                       } else if ( event.type == MouseEvent.MOUSE_WHEEL ) {
+                               if      (event.delta > 0) { map.zoomIn(); }
+                               else if (event.delta < 0) { map.zoomOut(); }
+                       }
+
                        if ( paint && paint.isBackground ) {
-                               if ( event.type == MouseEvent.MOUSE_DOWN && ((event.shiftKey && event.ctrlKey) || event.altKey) ) {
+                               if (event.type == MouseEvent.MOUSE_DOWN && ((event.shiftKey && event.ctrlKey) || event.altKey) ) {
                                        // alt-click to pull data out of vector background layer
-                                       var newEntity:Entity=paint.findSource().pullThrough(entity,controller.connection);
-                                       if (entity is Way) { return new SelectedWay(newEntity as Way); }
-                                       else if (entity is Node) { return new SelectedPOINode(newEntity as Node); }
-                } else if (event.type == MouseEvent.MOUSE_DOWN && entity is Marker) {
-                    return new SelectedMarker(entity as Marker, paint.findSource());
-                               } else if ( event.type == MouseEvent.MOUSE_UP ) {
+                                       var newSelection:Array=[];
+                                       if (selection.indexOf(entity)==-1) { selection=[entity]; }
+                                       for each (var entity:Entity in selection) {
+                                               paint.setHighlight(entity, { hover:false, selected: false });
+                                               if (entity is Way) paint.setHighlightOnNodes(Way(entity), { selectedway: false });
+                                               newSelection.push(paint.pullThrough(entity,controller.map.editableLayer));
+                                       }
+                                       return controller.findStateForSelection(newSelection);
+                               } else if (!paint.interactive) {
+                                       return null;
+                               } else if (event.type == MouseEvent.MOUSE_DOWN && paint.interactive) {
+                                       if      (entity is Way   ) { return new SelectedWay(entity as Way, paint); }
+                                       else if (entity is Node  ) { if (!entity.hasParentWays) return new SelectedPOINode(entity as Node, paint); }
+                                       else if (entity is Marker) { return new SelectedMarker(entity as Marker, paint); }
+                               } else if ( event.type == MouseEvent.MOUSE_UP && !event.ctrlKey) {
                                        return (this is NoSelection) ? null : new NoSelection();
-                               } else { return null; }
-                       }
-
-                       if ( event.type == MouseEvent.MOUSE_DOWN ) {
-                               if ( entity is Way ) {
-                                       // click way
-                                       return new DragWay(focus as Way, event);
-                               } else if ( focus is Node ) {
-                                       // select POI node
-                                       return new DragPOINode(entity as Node,event,false);
-                               } else if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
+                               } else if ( event.type == MouseEvent.CLICK && focus == null && map.dragstate!=map.DRAGGING && !event.ctrlKey) {
+                                       return (this is NoSelection) ? null : new NoSelection();
+                               }
+                                       
+                       } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
+                               if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
                                        // select node within this way
                        return new DragWayNode(selectedWay,  getNodeIndex(selectedWay,entity as Node),  event, false);
                                } else if ( entity is Node && focus is Way ) {
                                        // select way node
                                        return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
-                               } else if ( controller.keyDown(32) ) {
-                                       // drag map
+                               } else if ( controller.keyDown(Keyboard.SPACE) ) {
+                                       // drag the background imagery to compensate for poor alignment
                                        return new DragBackground(event);
+                               } else if (entity && selection.indexOf(entity)>-1) {
+                                       return new DragSelection(selection, event);
+                               } else if (entity) {
+                                       return new DragSelection([entity], event);
+                               } else if (event.ctrlKey && !layer.isBackground) {
+                                       return new SelectArea(event.localX,event.localY,selection);
                                }
-            } else if ( event.type == MouseEvent.CLICK && focus == null && map.dragstate!=map.DRAGGING && this is SelectedMarker) {
-                // this is identical to the below, but needed for unselecting markers on vector background layers.
-                // Deselecting a POI or way on the main layer emits both CLICK and MOUSE_UP, but markers only CLICK
-                // I'll leave it to someone who understands to decide whether they are the same thing and should be
-                // combined with a (CLICK || MOUSE_UP)
-                
-                // "&& this is SelectedMarker" added by Steve Bennett. The CLICK event being processed for SelectedWay state
-                // causes way to get unselected...so restrict the double processing as much as possible.  
-                
+
+            } else if ( event.type==MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING && !event.ctrlKey) {
                 return (this is NoSelection) ? null : new NoSelection();
-                       } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING) {
-                               return (this is NoSelection) ? null : new NoSelection();
-                       } else if ( event.type == MouseEvent.MOUSE_UP && focus && map.dragstate!=map.NOT_DRAGGING) {
-                               map.mouseUpHandler();   // in case the end-drag is over an EntityUI
-                       } else if ( event.type == MouseEvent.ROLL_OVER ) {
-                               controller.map.setHighlight(focus, { hover: true });
-                       } else if ( event.type == MouseEvent.MOUSE_OUT ) {
-                               controller.map.setHighlight(focus, { hover: false });
-            } else if ( event.type == MouseEvent.MOUSE_WHEEL ) {
-                if (event.delta > 0) {
-                  map.zoomIn();
-                } else if (event.delta < 0) {
-                  map.zoomOut();
-                }
             }
                        return null;
                }
 
+               /** Gets the way that the selected node is part of, if that makes sense. If not, return the node, or the way, or nothing. */
                public static function getTopLevelFocusEntity(entity:Entity):Entity {
                        if ( entity is Node ) {
                                for each (var parent:Entity in entity.parentWays) {
@@ -138,6 +166,7 @@ package net.systemeD.potlatch2.controller {
                        }
                }
 
+               /** Find the MapPaint object that this DisplayObject belongs to. */
                protected function getMapPaint(d:DisplayObject):MapPaint {
                        while (d) {
                                if (d is MapPaint) { return MapPaint(d); }
@@ -153,6 +182,7 @@ package net.systemeD.potlatch2.controller {
                        return null;
                }
 
+               /** Create a "repeat tags" action on the current entity, if possible. */
                protected function repeatTags(object:Entity):void {
                        if (!controller.clipboards[object.getType()]) { return; }
                        object.suspend();
@@ -168,14 +198,30 @@ package net.systemeD.potlatch2.controller {
 
                }
 
+               /** Create an action to add "source=*" tag to current entity based on background imagery. This is a convenient shorthand for users. */
                protected function setSourceTag():void {
                        if (selectCount!=1) { return; }
                        if (Imagery.instance().selected && Imagery.instance().selected.sourcetag) {
-                               firstSelected.setTag('source',Imagery.instance().selected.sourcetag, MainUndoStack.getGlobalStack().addAction);
+                               if ("sourcekey" in Imagery.instance().selected)
+                                   firstSelected.setTag(Imagery.instance().selected.sourcekey,Imagery.instance().selected.sourcetag, MainUndoStack.getGlobalStack().addAction);
+                               else
+                                   firstSelected.setTag('source',Imagery.instance().selected.sourcetag, MainUndoStack.getGlobalStack().addAction);
                        }
                        controller.updateSelectionUI();
                }
 
+               /** Revert all selected items to previously saved state, via a dialog box. */
+               protected function revertSelection():void {
+                       if (selectCount==0) return;
+                       Alert.show("Revert selected items to the last saved version, discarding your changes?","Are you sure?",Alert.YES | Alert.CANCEL,null,revertHandler);
+               }
+               protected function revertHandler(event:CloseEvent):void {
+                       if (event.detail==Alert.CANCEL) return;
+                       for each (var item:Entity in _selection) {
+                               item.connection.loadEntity(item);
+                       }
+               }
+
                // Selection getters
 
                public function get selectCount():uint {
@@ -204,6 +250,14 @@ package net.systemeD.potlatch2.controller {
                        return selectedWays;
                }
 
+        public function get selectedNodes():Array {
+            var selectedNodes:Array=[];
+            for each (var item:Entity in _selection) {
+                if (item is Node) { selectedNodes.push(item); }
+            }
+            return selectedNodes;
+        }
+
                public function hasSelectedWays():Boolean {
                        for each (var item:Entity in _selection) {
                                if (item is Way) { return true; }
@@ -225,6 +279,19 @@ package net.systemeD.potlatch2.controller {
                        return false;
                }
 
+        /** Determine whether or not any nodes are selected, and if so whether any of them belong to areas. */
+        public function hasSelectedWayNodesInAreas():Boolean {
+            for each (var item:Entity in _selection) {
+                if (item is Node) {
+                    var parentWays:Array = Node(item).parentWays;
+                    for each (var way:Entity in parentWays) {
+                        if (Way(way).isArea()) { return true; }
+                    }
+                }
+            }
+            return false;
+        }
+
                public function hasAdjoiningWays():Boolean {
                        if (_selection.length<2) { return false; }
                        var endNodes:Object={};